Skip to main content

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, what 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 then its start, end and length are also absent. 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 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 = 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. Interval variable X represents the task, it is not optional because the task X is mandatory. Interval variable XA represents the task X in the case when performed by worker A and similarly XB represents the task X in the case 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 = 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

ParameterTypeDescription
optionsIntervalVar[]Interval variables to chose 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

IntExpr

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 end of y with a delay at least 10. In addition we constrain length of x to be less or equal than 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


endAtEnd()

endAtEnd(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

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


endAtStart()

endAtStart(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.end().plus(delay).eq(successor.start())).

In other words, end of predecessor plus delay must be equal to start of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


endBeforeEnd()

endBeforeEnd(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.end().plus(delay).le(successor.end())).

In other words, end of predecessor plus delay must be less than or equal to end of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


endBeforeStart()

endBeforeStart(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.end().plus(delay).le(successor.start())).

In other words, end of predecessor plus delay must be less than or equal to start of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


endOr()

endOr(absentValue: number): IntExpr

Creates an integer expression for the end of the interval variable. If the interval is absent then its value is absentValue.

Parameters

ParameterType
absentValuenumber

Returns

IntExpr

Remarks

This function is equivalent to interval.end().guard(absentValue).

See


forbidEnd()

forbidEnd(func: IntStepFunction): void

Constrains the end of the interval variable to be outside of the zero-height segments of the step function.

Parameters

ParameterType
funcIntStepFunction

Returns

void

Remarks

This function is equivalent to:

  model.constraint(func.stepFunctionEval(interval.end()).ne(0));

That is, function value at the end of the interval variable cannot be zero.

See


forbidExtent()

forbidExtent(func: IntStepFunction): void

Forbid the interval variable to overlap with segments of the function where the value is zero.

Parameters

ParameterType
funcIntStepFunction

Returns

void

Remarks

This function prevents the specified interval variable from overlapping with segments of the step function where the value is zero. That is, if [s,e)[s, e) is a segment of the step function where the value is zero then the interval variable either ends before ss (interval.end()s\mathtt{interval.end()} \le s) or starts after ee (einterval.start()e \le \mathtt{interval.start()}.

See


forbidStart()

forbidStart(func: IntStepFunction): void

Constrains the start of the interval variable to be outside of the zero-height segments of the step function.

Parameters

ParameterType
funcIntStepFunction

Returns

void

Remarks

This function is equivalent to:

  model.constraint(func.stepFunctionEval(interval.start()).ne(0));

That is, function value at the start of the interval variable cannot be zero.

See


getEndMax()

getEndMax(): null | number

Returns maximum end assigned to the interval variable during its construction by Model.intervalVar or later by function setEnd or function setEndMax.

If the interval is absent then the function returns null.

Note: This function returns maximum end of the interval in the model (before the solve), not in the solution.

Returns

null | number


getEndMin()

getEndMin(): null | number

Returns minimum end assigned to the interval variable during its construction by Model.intervalVar or later by function setEnd or function setEndMin.

If the interval is absent then the function returns null.

Note: This function returns minimum end of the interval in the model (before the solve), not in the solution.

Returns

null | number


getLengthMax()

getLengthMax(): null | number

Returns maximum length assigned to the interval variable during its construction by Model.intervalVar or later by function setLength or function setLengthMax.

If the interval is absent then the function returns null.

Note: This function returns maximum length of the interval in the model (before the solve), not in the solution.

Returns

null | number


getLengthMin()

getLengthMin(): null | number

Returns minimum length assigned to the interval variable during its construction by Model.intervalVar or later by function setLength or function setLengthMin.

If the interval is absent then the function returns null.

Note: This function returns minimum length of the interval in the model (before the solve), not in the solution.

Returns

null | number


getName()

getName(): undefined | string

Returns the name assigned to the node.

Returns

undefined | string

Inherited from

ModelNode.getName


getStartMax()

getStartMax(): null | number

Returns maximum start value assigned to the interval variable during its construction by Model.intervalVar or later by function setStart or function setStartMax.

If the interval is absent then the function returns null.

Note: This function returns maximum start of the interval in the model (before the solve), not in the solution.

Returns

null | number


getStartMin()

getStartMin(): null | number

Returns minimum start value assigned to the interval variable during its construction by Model.intervalVar or later by function setStart or function setStartMin.

If the interval is absent then the function returns null.

Note: This function returns minimum start of the interval in the model (before the solve), not in the solution.

Returns

null | number


isAbsent()

isAbsent(): boolean

Returns true if the interval variable was created absent (and therefore cannot be present in the solution).

Note: This function checks presence status of the interval in the model (before the solve), not in the solution. In particular, for an optional interval variable this function returns false, even though there could be a solution in which the interval is absent.

Returns

boolean

See


isOptional()

isOptional(): boolean

Returns true if the interval variable was created as optional. Optional interval variable can be absent in the solution, i.e. it can be omitted.

Note: This function checks presence status of the variable in the model (before the solve), not in the solution.

Returns

boolean

See


isPresent()

isPresent(): boolean

Returns true if the interval variable was created present (and therefore cannot be absent in the solution).

Note: This function returns presence status of the interval in the model (before the solve), not in the solution. In particular, for an optional interval variable this function returns false, even though there could be a solution in which the interval is present.

Returns

boolean

See


length()

length(): IntExpr

Creates an integer expression for the length of the interval variable.

Returns

IntExpr

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 end of y with a delay at least 10. In addition we constrain length of x to be less or equal than 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


lengthOr()

lengthOr(absentValue: number): IntExpr

Creates an integer expression for the length of the interval variable. If the interval is absent then its value is absentValue.

Parameters

ParameterType
absentValuenumber

Returns

IntExpr

Remarks

This function is equivalent to interval.length().guard(absentValue).

See


makeAbsent()

makeAbsent(): IntervalVar

Makes the interval variable absent. Absent interval variable cannot be present in the solution, i.e. it will be omitted in the solution (and everything that depends on it).

Returns

IntervalVar

Returns the interval variable itself in order to allow chaining.

See


makeOptional()

makeOptional(): IntervalVar

Makes the interval variable optional. Optional interval variable can be absent in the solution, i.e. it can be omitted.

It is equivalent to setting optional: true in Model.intervalVar.

Returns

IntervalVar

Returns the interval variable itself in order to allow chaining.

See


makePresent()

makePresent(): IntervalVar

Makes the interval variable present. Present interval variable cannot be absent in the solution, i.e. it cannot be omitted.

It is equivalent to setting optional: false (or completely omitting it) in Model.intervalVar.

Returns

IntervalVar

Returns the interval variable itself in order to allow chaining.

See


presence()

presence(): BoolExpr

Creates a Boolean expression which is true if the interval variable is present.

Returns

BoolExpr

Remarks

This function is the same as Model.presenceOf, see its documentation for more details.


pulse()

pulse(height: number | IntExpr): CumulExpr

Creates cumulative function (expression) pulse for the interval variable and specified height.

Parameters

ParameterType
heightnumber | IntExpr

Returns

CumulExpr

Remarks

This function is the same as Model.pulse.


setEnd()

setEnd(e)

setEnd(e: number): IntervalVar

Sets end of the interval variable to the given value.

It overwrites any previous end limits given at variable creation by Model.intervalVar or later by setEnd, setEndMin or setEndMax.

Equivalent to:

intervalVar.setEndMin(e);
intervalVar.setEndMax(e);

Note that the end of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters
ParameterType
enumber
Returns

IntervalVar

See

setEnd(eMin, eMax)

setEnd(eMin: number, eMax: number): IntervalVar

Limits the possible end of the variable to the range eMin to eMax.

It overwrites any previous end limits given at variable creation by Model.intervalVar or later by setEnd, setEndMin or setEndMax.

Equivalent to:

intervalVar.setEndMin(eMin);
intervalVar.setEndMax(eMax);

Note that the end of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters
ParameterType
eMinnumber
eMaxnumber
Returns

IntervalVar

See

setEndMax()

setEndMax(eMax: number): IntervalVar

Sets maximum end of the interval variable to the given value. It overwrites any previous maximum end limit given at variable creation by Model.intervalVar or later by setEnd or setEndMax. The minimum end is not changed by this function.

Note that the end of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters

ParameterType
eMaxnumber

Returns

IntervalVar

See


setEndMin()

setEndMin(eMin: number): IntervalVar

Sets minimum end of the interval variable to the given value.

It overwrites any previous minimum end limit given at variable creation by Model.intervalVar or later by setEnd or setEndMin. The maximum end is not changed by this function.

Note that the end of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters

ParameterType
eMinnumber

Returns

IntervalVar

See


setLength()

setLength(l)

setLength(l: number): IntervalVar

Sets length of the interval variable to the given value. It overwrites any previous length limits given at variable creation by Model.intervalVar or later by setLength, setLengthMin or setLengthMax.

Equivalent to:

intervalVar.setLengthMin(l);
intervalVar.setLengthMax(l);

Note that the length of the interval variable must be in the range 0 to LengthMax.

Parameters
ParameterType
lnumber
Returns

IntervalVar

See

setLength(lMin, lMax)

setLength(lMin: number, lMax: number): IntervalVar

Limits the possible length of the variable to the range lMin to lMax. It overwrites any previous length limits given at variable creation by Model.intervalVar or later by setLength, setLengthMin or setLengthMax.

Equivalent to:

intervalVar.setLengthMin(lMin);
intervalVar.setLengthMax(lMax);

Note that the length of the interval variable must be in the range 0 to LengthMax.

Parameters
ParameterType
lMinnumber
lMaxnumber
Returns

IntervalVar

See

setLengthMax()

setLengthMax(lMax: number): IntervalVar

Sets maximum length of the interval variable to the given value. It overwrites any previous maximum length limit given at variable creation by Model.intervalVar or later by setLength or setLengthMax. The minimum length is not changed by this function.

Note that the length of the interval variable must be in the range to LengthMax.

Parameters

ParameterType
lMaxnumber

Returns

IntervalVar

See


setLengthMin()

setLengthMin(lMin: number): IntervalVar

Sets minimum length of the interval variable to the given value. It overwrites any previous minimum length limit given at variable creation by Model.intervalVar or later by setLength or setLengthMin. The maximum length is not changed by this function.

Note that the length of the interval variable must be in the range 0 to LengthMax.

Parameters

ParameterType
lMinnumber

Returns

IntervalVar

See


setName()

setName(name: string): this

Assigns a name to the node.

Parameters

ParameterTypeDescription
namestringNamed to be assigned.

Returns

this

The node itself so it can be used in chained expression.

Remarks

Assigning a name is optional. However is useful for debugging because variable names appear in the development traces. It is also useful for exporting the model to a file (see problem2json).

Example

let model = new CP.Model();
let x = model.intervalVar({ length: 10 }).setName("x");
// The line above is equivalent to:
// let x = model.intervalVar({ length: 10, name:"x" });
let endOfX = model.endOf(x).setName("endOfX");
let result = await CP.solve(model);

Inherited from

ModelNode.setName


setStart()

setStart(s)

setStart(s: number): IntervalVar

Sets start of the interval variable to the given value.

It overwrites any previous start limits given at variable creation by Model.intervalVar or later by setStart, setStartMin or setStartMax.

Equivalent to:

intervalVar.setStartMin(s);
intervalVar.setStartMax(s);

Note that the start of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters
ParameterType
snumber
Returns

IntervalVar

See

setStart(sMin, sMax)

setStart(sMin: number, sMax: number): IntervalVar

Limits the possible start value of the variable to the range sMin to sMax.

It overwrites any previous start limits given at variable creation by Model.intervalVar or later by setStart, setStartMin or setStartMax.

Equivalent to:

intervalVar.setStartMin(sMin);
intervalVar.setStartMax(sMax);

Note that the start of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters
ParameterType
sMinnumber
sMaxnumber
Returns

IntervalVar

See

setStartMax()

setStartMax(sMax: number): IntervalVar

Sets maximum start of the interval variable to the given value.

It overwrites any previous maximum start limit given at variable creation by Model.intervalVar or later by setStart or setStartMax. The minimum start not changed by this function.

Note that the start of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters

ParameterType
sMaxnumber

Returns

IntervalVar

See


setStartMin()

setStartMin(sMin: number): IntervalVar

Sets minimum start of the interval variable to the given value.

It overwrites any previous minimum start limit given at variable creation by Model.intervalVar or later by setStart or setStartMin. The maximum start is not changed by this function.

Note that the start of the interval variable must be in the range IntervalMin to IntervalMax.

Parameters

ParameterType
sMinnumber

Returns

IntervalVar

See


span()

span(covered: IntervalVar[]): void

Constraints the interval variable to span (cover) a set of other interval variables.

Parameters

ParameterTypeDescription
coveredIntervalVar[]The set of interval variables to cover.

Returns

void

Remarks

This constraint is the same as Model.span.


start()

start(): IntExpr

Creates an integer expression for the start of the interval variable.

Returns

IntExpr

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 end of y with a delay at least 10. In addition we constrain length of x to be less or equal than 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


startAtEnd()

startAtEnd(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.start().plus(delay).eq(successor.end())).

In other words, start 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


startAtStart()

startAtStart(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.start().plus(delay).eq(successor.start())).

In other words, start of predecessor plus delay must be equal to start of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


startBeforeEnd()

startBeforeEnd(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.start().plus(delay).le(successor.end())).

In other words, start of predecessor plus delay must be less than or equal to end of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


startBeforeStart()

startBeforeStart(successor: IntervalVar, delay: number | IntExpr): void

Creates a precedence constraint between two interval variables.

Parameters

ParameterTypeDefault value
successorIntervalVarundefined
delaynumber | IntExpr0

Returns

void

Remarks

Assuming that the current interval is predecessor, the constraint is the same as:

model.constraint(predecessor.start().plus(delay).le(successor.start())).

In other words, start of predecessor plus delay must be less than or equal to start of successor.

When one of the two interval variables is absent then the constraint is satisfied.

See


startOr()

startOr(absentValue: number): IntExpr

Creates an integer expression for the start of the interval variable. If the interval is absent then its value is absentValue.

Parameters

ParameterType
absentValuenumber

Returns

IntExpr

Remarks

This function is equivalent to interval.start().guard(absentValue).

See


stepAtEnd()

stepAtEnd(height: number | IntExpr): CumulExpr

Creates cumulative function (expression) that changes value at end of the interval variable by the given height.

Parameters

ParameterType
heightnumber | IntExpr

Returns

CumulExpr

Remarks

This function is the same as Model.stepAtEnd.


stepAtStart()

stepAtStart(height: number | IntExpr): CumulExpr

Creates cumulative function (expression) that changes value at start of the interval variable by the given height.

Parameters

ParameterType
heightnumber | IntExpr

Returns

CumulExpr

Remarks

This function is the same as Model.stepAtStart.