Skip to main content

Class: IntExpr

A class that represents an integer expression in the model. The expression may depend on a value of a variable (or variables) and so the value of the expression is not known until a solution is found. The value must be in the range IntVarMin to IntVarMax.

Example

The following code creates two interval variables x and y and an integer expression maxEnd that is equal to the maximum of the end times of x and y (see max2):

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

Optional integer expressions

Underlying variables of an integer expression may be optional, i.e. they may or may not be present in a solution (for example, an optional task can be completely omitted from the solution). In this case the value of the integer expression is absent. The value absent means that the variable has no meaning, it does not exist in the solution.

With an exception of guard expression, any value of an integer expression that depends on an absent variable is also absent. As we don't know the value of the expression before the solution is found, we call such expression optional.

Example

In the following model, there is optional interval variable x and non-optional interval variable y. We add a constraint that end of x plus 10 must be less or equal to the start of y:

let model = new CP.Model();
let x = model.intervalVar({ length: 10, name: "x", optional: true });
let y = model.intervalVar({ length: 20, name: "y" });
let endX = model.endOf(x);
let afterX = endX.plus(10);
let startY = model.startOf(y);
let isBefore = afterX.le(startY);
model.constraint(isBefore);
let result = await CP.solve(model);

In this model:

  • endX is an optional integer expression because it depends on an optional variable x.
  • The expression afterX is optional for the same reason.
  • The expression startY is not optional because it depends only on a non-optional variable y.
  • Boolean expression isBefore is also optional. It's value could be true, false or absent.

The expression isBefore is turned into a constraint using Model.constraint. Therefore it cannot be false in the solution. However it can still be absent. Therefore the constraint isBefore can be satisfied in two ways:

  1. Both x and y are present, x is before y and the delay between them is at least 10. In this case isBefore is true.
  2. x is absent and y is present. In this case isBefore is absent.

Hierarchy

Methods

abs

abs(): IntExpr

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

Returns

IntExpr

Remarks

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

Same as Model.abs.

Overrides

FloatExpr.abs


div

div(arg): IntExpr

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

Parameters

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


eq

eq(arg): BoolExpr

Creates Boolean expression this = arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.eq.


ge

ge(arg): BoolExpr

Creates Boolean expression this arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.ge.


getName

getName(): undefined | string

Returns the name assigned to the node.

Returns

undefined | string

Inherited from

FloatExpr.getName


gt

gt(arg): BoolExpr

Creates Boolean expression this > arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.gt.


guard

guard(absentValue?): IntExpr

Creates an expression that replaces value absent by a constant.

Parameters

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


identity

identity(arg): void

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

Parameters

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


inRange

inRange(lb, ub): BoolExpr

Creates Boolean expression lb this ub.

Parameters

NameType
lbnumber
ubnumber

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.inRange.


le

le(arg): BoolExpr

Creates Boolean expression this arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.le.


lt

lt(arg): BoolExpr

Creates Boolean expression this < arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.lt.


max2

max2(arg): IntExpr

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

Parameters

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


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.


min2

min2(arg): IntExpr

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

Parameters

NameType
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 min for n-ary minimum.


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

minus

minus(arg): 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

NameType
argnumber | IntExpr

Returns

IntExpr


ne

ne(arg): BoolExpr

Creates Boolean expression this arg.

Parameters

NameType
argnumber | IntExpr

Returns

BoolExpr

Remarks

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

Use function constraint to create a constraint from this expression.

Same as Model.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.

Overrides

FloatExpr.neg


plus

plus(arg): IntExpr

Returns addition of the expression and the argument.

Parameters

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


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.


setName

setName(name): IntExpr

Assigns a name to the node.

Parameters

NameTypeDescription
namestringNamed to be assigned.

Returns

IntExpr

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

Inherited from

FloatExpr.setName


times

times(arg): 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

NameType
argnumber | IntExpr

Returns

IntExpr