lengthOf

Returns the length of specified interval variable.

Syntax


intExpr lengthOf(intervalVar interval, int absentValue = 0)

Parameters

  • interval: interval variable.
  • absentValue: value to return if the interval variable interval becomes absent.

Description

This function returns an integer expression that is equal to the length (end - start) of the interval variable interval if it is present. If it is absent, then the value of the expression is absentValue (zero by default).

Example

In the following example, task x can be performed in three different modes. The length of the task depends on the mode.


mode = intVar(0..2);
x = intervalVar();
lengthOf(x) = element(mode, [5, 9, 10]);

Note that there is another way to model the same problem using optional interval variables and alternative constraint. It may be more suitable if the mode does not affect only the length:


x = intervalVar();
x0 = intervalVar(optional, length=5);
x1 = intervalVar(optional, length=9);
x2 = intervalVar(optional, length=10);
alternative(x, [x0, x1, x2]);

Example

In the following example there is task a that requires work for 12 time units. The work can be split into up-to three parts a1, a2 and a3. If all the work is done in a1 then a2 and a3 will be absent. Similarly a3 can be absent if all the work is done in a1 and a2. However a2 cannot be absent if a3 is present.


// Master task a:
a = intervalVar();
// Task a can be split into up to 3 parts a1, a2, a3.
// The first part is mandatory, but a2 and a3 are optional.
a1 = intervalVar(length=1..12);
a2 = intervalVar(optional, length=1..12);
a3 = intervalVar(optional, length=1..12);
// The total length of the parts must be 12:
lengthOf(a1) + lengthOf(a2) + lengthOf(a3) == 12;
// The parts are chronologically ordered, the minimum delay between parts is 1:
endBeforeStart(a1, a2, 1);
endBeforeStart(a2, a3, 1);
// If there is the 3rd part then there must be also the 2nd:
presenceOf(a3) => presenceOf(a2);
// Interval var a covers exactly the parts a1, a2, a3.
span(a, [a1, a2, a3]);