Skip to main content

Class: IntVar

Integer variable represents an unknown (integer) value that solver has to find.

The value of the integer variable can be constrained using mathematical expressions, such as Model.plus, Model.times, Model.le, Model.sum.

OptalCP solver focuses on scheduling problems and concentrates on IntervalVar variables. Therefore, interval variables should be the primary choice for modeling in OptalCP. However, integer variables can be used for other purposes, such as counting or indexing. In particular, integer variables can be helpful for cumulative expressions with variable heights; see Model.pulse, Model.stepAtStart, Model.stepAtEnd, and Model.stepAt.

The integer variable can be optional. In this case, the solver can make the variable absent, which is usually interpreted as the fact that the solver does not use the variable at all. Functions Model.presenceOf and IntExpr.presence can constrain the presence of the variable.

Integer variables can be created using the function Model.intVar.

Example

In the following example we create three interval variables x, y and z. Variables x and y are present, but variable z is optional. Each variable has a different range of possible values.

let model = CP.Model;
let x = model.intervalVar({ name: "x", range: [1, 3] });
let y = model.intervalVar({ name: "y", range: [0, 100] });
let z = model.intervalVar({ name: "z", range: [10, 20], optional: true });

Extends

Methods

abs()

abs(): IntExpr

Creates an integer expression which is absolute value of the expression.

Returns

IntExpr

Remarks

If the expression has value absent, the resulting expression also has value absent.

Same as Model.abs.

Inherited from

IntExpr.abs


div()

div(arg: number | IntExpr): IntExpr

Returns integer division of the expression arg. The division rounds towards zero.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.div.

Inherited from

IntExpr.div


eq()

eq(arg: number | IntExpr): BoolExpr

Creates Boolean expression this = arg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.eq.

Inherited from

IntExpr.eq


ge()

ge(arg: number | IntExpr): BoolExpr

Creates Boolean expression thisarg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.ge.

Inherited from

IntExpr.ge


getMax()

getMax(): null | number

Returns

null | number


getMin()

getMin(): null | number

Returns

null | number


getName()

getName(): undefined | string

Returns the name assigned to the node.

Returns

undefined | string

Inherited from

IntExpr.getName


gt()

gt(arg: number | IntExpr): BoolExpr

Creates Boolean expression this > arg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.gt.

Inherited from

IntExpr.gt


guard()

guard(absentValue: number): IntExpr

Creates an expression that replaces value absent by a constant.

Parameters

ParameterTypeDefault value
absentValuenumber0

Returns

IntExpr

Remarks

The resulting expression is:

  • equal to the expression if the expression is present
  • and equal to absentValue otherwise (i.e. when the expression is absent).

The default value of absentValue is 0.

The resulting expression is never absent.

Same as Model.guard.

Inherited from

IntExpr.guard


identity()

identity(arg: number | IntExpr): void

Constrains the expression to be identical to the argument, including their presence status.

Parameters

ParameterType
argnumber | IntExpr

Returns

void

Remarks

Identity is different than equality. For example, if x is absent, then x.eq(0) is absent, but x.identity(0) is false.

Same as Model.identity.

Inherited from

IntExpr.identity


inRange()

inRange(lb: number, ub: number): BoolExpr

Creates Boolean expression lbthisub.

Parameters

ParameterType
lbnumber
ubnumber

Returns

BoolExpr

Remarks

If the expression has value absent, then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.inRange.

Inherited from

IntExpr.inRange


isAbsent()

isAbsent(): boolean

Returns

boolean


isOptional()

isOptional(): boolean

Returns

boolean


isPresent()

isPresent(): boolean

Returns

boolean


le()

le(arg: number | IntExpr): BoolExpr

Creates Boolean expression thisarg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.le.

Inherited from

IntExpr.le


lt()

lt(arg: number | IntExpr): BoolExpr

Creates Boolean expression this < arg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.lt.

Inherited from

IntExpr.lt


makeAbsent()

makeAbsent(): IntVar

Returns

IntVar


makeOptional()

makeOptional(): IntVar

Returns

IntVar


makePresent()

makePresent(): IntVar

Returns

IntVar


max2()

max2(arg: number | IntExpr): IntExpr

Creates an integer expression which is the maximum of the expression and arg.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.max2. See Model.max for n-ary maximum.

Inherited from

IntExpr.max2


maximize()

maximize(): void

Maximize the expression. I.e., search for a solution that achieves the maximal value of the expression.

Returns

void

Remarks

Equivalent of function Model.maximize.

The opposite of minimize.

Inherited from

IntExpr.maximize


min2()

min2(arg: number | IntExpr): IntExpr

Creates an integer expression which is the minimum of the expression and arg.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.min2. See Model.min for the n-ary minimum.

Inherited from

IntExpr.min2


minimize()

minimize(): void

Minimize the expression. I.e., search for a solution that achieves the minimal value of the expression.

Returns

void

Remarks

Equivalent of function Model.minimize.

Example

In the following model, we search for a solution that minimizes the maximum end of the two intervals x and y:

let model = new CP.Model();
let x = model.intervalVar({ length: 10, name: "x" });
let y = model.intervalVar({ length: 20, name: "y" });
model.max2(x.end(), y.end()).minimize();
let result = await CP.solve(model);

Inherited from

IntExpr.minimize


minus()

minus(arg: number | IntExpr): IntExpr

Returns subtraction of the expression and arg.@remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.minus.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Inherited from

IntExpr.minus


ne()

ne(arg: number | IntExpr): BoolExpr

Creates Boolean expression thisarg.

Parameters

ParameterType
argnumber | IntExpr

Returns

BoolExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Use function Model.constraint to create a constraint from this expression.

Same as Model.ne.

Inherited from

IntExpr.ne


neg()

neg(): IntExpr

Returns negation of the expression.

Returns

IntExpr

Remarks

If the expression has value absent then the resulting expression has also value absent.

Same as Model.neg.

Inherited from

IntExpr.neg


plus()

plus(arg: number | IntExpr): IntExpr

Returns addition of the expression and the argument.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.plus.

Inherited from

IntExpr.plus


presence()

presence(): BoolExpr

Returns an expression which is true if the expression is present and false when it is absent.

Returns

BoolExpr

Remarks

The resulting expression is never absent.

Same as Model.presenceOf.

Inherited from

IntExpr.presence


setMax()

setMax(max: number): IntVar

Parameters

ParameterType
maxnumber

Returns

IntVar


setMin()

setMin(min: number): IntVar

Parameters

ParameterType
minnumber

Returns

IntVar


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, it is helpful for debugging because variable names appear in the development traces. It is also helpful 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

IntExpr.setName


setRange()

setRange(min: number, max: number): IntVar

Parameters

ParameterType
minnumber
maxnumber

Returns

IntVar


times()

times(arg: number | IntExpr): IntExpr

Returns multiplication of the expression and arg.@remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.times.

Parameters

ParameterType
argnumber | IntExpr

Returns

IntExpr

Inherited from

IntExpr.times