Class: IntervalVar
Interval variable is a task, action, operation, or any other interval with a start and an end. The start and the end of the interval are unknowns that the solver has to find. They could be accessed as integer expressions using IntervalVar.start and IntervalVar.end. or using Model.startOf and Model.endOf. In addition to the start and the end of the interval, the interval variable has a length (equal to end - start) that can be accessed using IntervalVar.length or Model.lengthOf.
The interval variable can be optional. In this case, the solver can decide to make the interval absent, which is usually interpreted as the fact that the interval doesn't exist, the task/action was not executed, or the operation was not performed. When the interval variable is absent, its start, end, and length are also absent. A boolean expression that represents the presence of the interval variable can be accessed using IntervalVar.presence and Model.presenceOf.
Interval variables can be created using the function
Model.intervalVar.
By default, interval variables are present (not optional).
To create an optional interval, specify optional: true
in the
arguments of the function.
@example: present interval variables
In the following example we create three present interval variables x
, y
and z
and we make sure that they don't overlap. Then, we minimize the maximum of
the end times of the three intervals (the makespan):
let model = new CP.Model;
let x = model.intervalVar({ length: 10, name: "x" });
let y = model.intervalVar({ length: 10, name: "y" });
let z = model.intervalVar({ length: 10, name: "z" });
model.noOverlap([x, y, z]);
model.max([x.end(), y.end(), z.end()]).minimize();
let result = await CP.solve(model);
@example: optional interval variables
In the following example, there is a task X that could be performed by two
different workers A and B. The interval variable X
represents the task.
It is not optional because the task X
is mandatory. Interval variable
XA
represents the task X
when performed by worker A and
similarly XB
represents the task X
when performed by worker B.
Both XA
and XB
are optional because it is not known beforehand which
worker will perform the task. The constraint alternative links
X
, XA
and XB
together and ensures that only one of XA
and XB
is present and that
X
and the present interval are equal.
let model = new CP.Model;
let X = model.intervalVar({ length: 10, name: "X" });
let XA = model.intervalVar({ name: "XA", optional: true });
let XB = model.intervalVar({ name: "XB", optional: true });
model.alternative(X, [XA, XB]);
let result = await CP.solve(model);
Variables XA
and XB
can be used elsewhere in the model, e.g. to make sure
that each worker is assigned to at most one task at a time:
// Tasks of worker A don't overlap:
model.noOverlap([... , XA, ...]);
// Tasks of worker B don't overlap:
model.noOverlap([... , XB, ...]);
Extends
Methods
alternative()
alternative(
options
:IntervalVar
[]):void
Creates alternative constraints for the interval variable and provided options
.
Parameters
Parameter | Type | Description |
---|---|---|
options | IntervalVar [] | Interval variables to choose from. |
Returns
void
Remarks
This constraint is the same as Model.alternative.
end()
end():
IntExpr
Creates an integer expression for the end of the interval variable.
Returns
Remarks
If the interval variable is absent, then the resulting expression is also absent.
Example
In the following example, we constraint interval variable y
to start after the end of y
with a delay of at least 10. In addition, we constrain the length of x
to be less or equal to the length of y
.
let model = new CP.Model;
let x = model.intervalVar({ name: "x", ... });
let y = model.intervalVar({ name: "y", ... });
model.constraint(x.end().plus(10).le(y.start()));
model.constraint(x.length().le(y.length()));
When x
or y
is absent then value of both constraints above is absent and therefore they are satisfied.
See
- Model.endOf is equivalent function on Model.
- Function IntervalVar.endOr is a similar function that replaces value absent by a constant.
endAtEnd()
endAtEnd(
successor
:IntervalVar
,delay
:number
|IntExpr
):void
Creates a precedence constraint between two interval variables.
Parameters
Parameter | Type | Default value |
---|---|---|
successor | IntervalVar | undefined |
delay | number | IntExpr | 0 |
Returns
void
Remarks
Assuming that the current interval is predecessor
, the constraint is the same as:
model.constraint(predecessor.end().plus(delay).eq(successor.end())).
In other words, end of predecessor
plus delay
must be equal to end of successor
.
When one of the two interval variables is absent, then the constraint is satisfied.
See
- Model.endAtEnd is equivalent function on Model.
- Model.constraint
- IntervalVar.start, IntervalVar.end
- IntExpr.eq
endAtStart()
endAtStart(
successor
:IntervalVar
,delay
:number
|IntExpr
):void
Creates a precedence constraint between two interval variables.
Parameters
Parameter | Type | Default value |
---|---|---|
successor | IntervalVar | undefined |
delay | number | IntExpr | 0 |
Returns
void