# 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, ...]);

## Methods​

### alternative​

alternative(options): void

Creates alternative constraints for the interval variable and provided options.

#### Parameters​

NameTypeDescription
optionsIntervalVar[]Interval variables to chose from.

#### Returns​

void

Remarks

This constraint is the same as 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

• endOf is equivalent function on Model.
• Function endOr is a similar function that replaces value absent by a constant.

### endAtEnd​

endAtEnd(successor, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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): IntExpr

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

#### Parameters​

NameType
absentValuenumber

#### Returns​

IntExpr

Remarks

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

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.

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.

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.

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.

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

### 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.

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.

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

• lengthOf is equivalent function on Model.
• Function lengthOr is a similar function that replaces value absent by a constant.

### lengthOr​

lengthOr(absentValue): IntExpr

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

#### Parameters​

NameType
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): CumulExpr

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

#### Parameters​

NameType
heightnumber | IntExpr

#### Returns​

CumulExpr

Remarks

This function is the same as Model.pulse.

### setEnd​

setEnd(e): 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​

NameType
enumber

#### Returns​

IntervalVar

See

setEnd(eMin, eMax): 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​

NameType
eMinnumber
eMaxnumber

#### Returns​

IntervalVar

See

### setEndMax​

setEndMax(eMax): 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​

NameType
eMaxnumber

#### Returns​

IntervalVar

See

### setEndMin​

setEndMin(eMin): 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​

NameType
eMinnumber

#### Returns​

IntervalVar

See

### setLength​

setLength(l): 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​

NameType
lnumber

#### Returns​

IntervalVar

See

setLength(lMin, lMax): 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​

NameType
lMinnumber
lMaxnumber

#### Returns​

IntervalVar

See

### setLengthMax​

setLengthMax(lMax): 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​

NameType
lMaxnumber

#### Returns​

IntervalVar

See

### setLengthMin​

setLengthMin(lMin): 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​

NameType
lMinnumber

#### Returns​

IntervalVar

See

### setName​

setName(name): IntervalVar

Assigns a name to the node.

#### Parameters​

NameTypeDescription
namestringNamed to be assigned.

#### Returns​

IntervalVar

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 model2json).

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);

### setStart​

setStart(s): 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​

NameType
snumber

#### Returns​

IntervalVar

See

setStart(sMin, sMax): 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​

NameType
sMinnumber
sMaxnumber

#### Returns​

IntervalVar

See

### setStartMax​

setStartMax(sMax): 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​

NameType
sMaxnumber

#### Returns​

IntervalVar

See

### setStartMin​

setStartMin(sMin): 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​

NameType
sMinnumber

#### Returns​

IntervalVar

See

### span​

span(covered): void

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

#### Parameters​

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

#### Returns​

void

Remarks

This constraint is the same as 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

• startOf is equivalent function on Model.
• Function startOr is a similar function that replaces value absent by a constant.

### startAtEnd​

startAtEnd(successor, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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, delay?): void

Creates a precedence constraint between two interval variables.

#### Parameters​

NameTypeDefault 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): IntExpr

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

#### Parameters​

NameType
absentValuenumber

#### Returns​

IntExpr

Remarks

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

See

### stepAtEnd​

stepAtEnd(height): CumulExpr

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

#### Parameters​

NameType
heightnumber | IntExpr

#### Returns​

CumulExpr

Remarks

This function is the same as Model.stepAtEnd.

### stepAtStart​

stepAtStart(height): CumulExpr

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

#### Parameters​

NameType
heightnumber | IntExpr

#### Returns​

CumulExpr

Remarks

This function is the same as Model.stepAtStart.