Skip to main content

Constraints

Struct Constraints 

Source
pub struct Constraints { /* private fields */ }
Expand description

Assignment constraints between tasks: which tasks need others, and which exclude each other.

Tasks are grouped into activities. Within an activity, a dependency matrix D says how each pair of tasks relates, as in Table I of Whitten, Choi, Johnson and How (2011). Entry D[q][u], with q and u positions in the activity’s task list, is:

  • 1: task u depends on task q; u may only be assigned if q is;
  • 0: no constraint;
  • -1: q and u are mutually exclusive; at most one of them may be assigned.

Two tasks that depend on each other must be assigned together or not at all. The diagonal is zero. Tasks outside every activity are unconstrained. The paper’s codes above one (“either of these tasks”) are not supported.

§Examples

use task_allocation::TaskId;
use task_allocation::ccbba::Constraints;

// A strike (task 0) needs a sensor (task 1), and the sensor is useless without it.
// Task 2 excludes both.
let mut constraints = Constraints::new(3);
constraints
    .add_activity(
        &[TaskId(0), TaskId(1), TaskId(2)],
        &[&[0, 1, -1], &[1, 0, -1], &[-1, -1, 0]],
    )
    .unwrap();

assert_eq!(constraints.requirements(TaskId(0)).collect::<Vec<_>>(), [TaskId(1)]);
assert!(constraints.excludes(TaskId(2), TaskId(0)));

Implementations§

Source§

impl Constraints

Source

pub fn new(tasks: usize) -> Self

No constraints between tasks 0..tasks.

Source

pub fn add_activity( &mut self, tasks: &[TaskId], dependency: &[&[i8]], ) -> Result<(), ConstraintError>

Adds an activity: tasks, related by dependency as described on Constraints, without timing limits.

§Errors

Returns a ConstraintError when a task is unknown or already in an activity, when the matrix is not square with one row per task, when its diagonal is not zero, or when an entry is not -1, 0 or 1. Nothing is added then.

Source

pub fn add_timed_activity( &mut self, tasks: &[TaskId], dependency: &[&[i8]], timing: &[&[Option<i64>]], ) -> Result<(), ConstraintError>

Adds an activity whose dependent tasks must also start within limits of each other.

timing[q][u] is the most task q may start after task u starts, in the time unit of the agents’ routes; negative values make q start before u; None sets no limit. Together, u imposes on q the window start(u) - timing[u][q] to start(u) + timing[q][u]. This is the matrix T of Whitten, Choi, Johnson and How.

§Errors

As add_activity, plus when the timing matrix is not square with one row per task, has a diagonal other than zero or None, or limits two tasks that do not depend on each other.

§Examples

The paper’s strike activity: the strike (task 0) and the sensing (task 1) start within 20 seconds of each other, and the damage assessment (task 2) starts at least 180 seconds after the strike and the sensing. Task 3, gathering intelligence instead, excludes all three.

use task_allocation::TaskId;
use task_allocation::ccbba::Constraints;

let mut constraints = Constraints::new(4);
constraints
    .add_timed_activity(
        &[TaskId(0), TaskId(1), TaskId(2), TaskId(3)],
        &[&[0, 1, 1, -1], &[1, 0, 1, -1], &[0, 0, 0, -1], &[-1, -1, -1, 0]],
        &[
            &[Some(0), Some(20), Some(-180), None],
            &[Some(20), Some(0), Some(-180), None],
            &[None, None, Some(0), None],
            &[None, None, None, Some(0)],
        ],
    )
    .unwrap();

// A strike starting at 100 lets the sensing start from 80 to 120, and the damage
// assessment from 280 on.
assert_eq!(constraints.window(TaskId(1), TaskId(0), 100), (Some(80), Some(120)));
assert_eq!(constraints.window(TaskId(2), TaskId(0), 100), (Some(280), None));
Source

pub fn set_opening(&mut self, task: TaskId, opening: u64)

Sets the opening of task’s time window, the earliest it may start. A TimedAgent never starts the task earlier. Agents that conflict over two mutually dependent tasks compare how far into their windows they start, and the earlier one gives way.

§Panics

Panics when task is not part of the task set.

Source

pub fn opening(&self, task: TaskId) -> u64

The opening of task’s time window; zero unless set.

§Panics

Panics when task is not part of the task set.

Source

pub fn window( &self, task: TaskId, other: TaskId, start: u64, ) -> (Option<i128>, Option<i128>)

The window other, starting at start, imposes on task: the earliest and the latest start, each None when unbounded. Only a task that depends on other gets a window; otherwise both are None. Bounds are not clamped at zero.

Source

pub fn starts_agree( &self, task: TaskId, start: u64, other: TaskId, other_start: u64, ) -> bool

Whether starts at start for task and other_start for other respect the timing limits between them, in both directions.

Source

pub fn depends_on(&self, task: TaskId, other: TaskId) -> bool

Whether task depends on other.

Source

pub fn tasks(&self) -> usize

The number of tasks the constraints cover.

Source

pub fn requirements(&self, task: TaskId) -> impl Iterator<Item = TaskId> + '_

The tasks task depends on: it may only be assigned if all of them are.

Source

pub fn excludes(&self, a: TaskId, b: TaskId) -> bool

Whether a and b are mutually exclusive.

Source

pub fn exclusions(&self, task: TaskId) -> impl Iterator<Item = TaskId> + '_

The tasks mutually exclusive with task.

Source

pub fn is_mutual(&self, task: TaskId) -> bool

Whether task depends on a task that also depends on it. Agents may bid on such a task optimistically, before its partners have winners.

Source

pub fn is_satisfied_by(&self, assignment: &Assignment) -> bool

Whether assignment meets every constraint: each assigned task’s requirements are assigned too, and no two mutually exclusive tasks are both assigned.

Trait Implementations§

Source§

impl Clone for Constraints

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Constraints

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Constraints

Source§

impl PartialEq for Constraints

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Constraints

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.