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.
Examples
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.
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 variablex
.- The expression
afterX
is optional for the same reason. - The expression
startY
is not optional because it depends only on a non-optional variabley
. - 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:
- Both
x
andy
are present,x
is beforey
and the delay between them is at least 10. In this caseisBefore
is true. x
is absent andy
is present. In this caseisBefore
is absent.
Extends
Extended by
Methods
abs()
abs():
IntExpr
Creates an integer expression which is absolute value of the expression.
Returns
Remarks
If the expression has value absent then the resulting expression has also value absent.
Same as Model.abs.
Overrides
FloatExpr.abs
div()
Returns integer division of the expression arg
. The division rounds towards zero.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
Remarks
If the expression or arg
has value absent then the resulting expression has also value absent.
Same as Model.div.
eq()
Creates Boolean expression this
= arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
ge()
Creates Boolean expression this
≥ arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
getName()
getName():
undefined
|string
Returns the name assigned to the node.
Returns
undefined
| string
Inherited from
gt()
Creates Boolean expression this
> arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
guard()
guard(
absentValue
:number
):IntExpr
Creates an expression that replaces value absent by a constant.
Parameters
Parameter | Type | Default value |
---|---|---|
absentValue | number | 0 |
Returns
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
:number
|IntExpr
):void
Constrains the expression to be identical to the argument, including their presence status.
Parameters
Parameter | Type |
---|---|
arg | number | 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
:number
,ub
:number
):BoolExpr
Creates Boolean expression lb
≤ this
≤ ub
.
Parameters
Parameter | Type |
---|---|
lb | number |
ub | number |
Returns
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.
le()
Creates Boolean expression this
≤ arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
lt()
Creates Boolean expression this
< arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
max2()
Creates an integer expression which is the maximum of the expression and arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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()
Creates an integer expression which is the minimum of the expression and arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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 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()
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
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
ne()
Creates Boolean expression this
≠ arg
.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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.
neg()
neg():
IntExpr
Returns negation of the expression.
Returns
Remarks
If the expression has value absent then the resulting expression has also value absent.
Same as Model.neg.
Overrides
FloatExpr.neg
plus()
Returns addition of the expression and the argument.
Parameters
Parameter | Type |
---|---|
arg | number | IntExpr |
Returns
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
Remarks
The resulting expression is never absent.
Same as Model.presenceOf.
setName()
setName(
name
:string
):this
Assigns a name to the node.
Parameters
Parameter | Type | Description |
---|---|---|
name | string | Named 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
times()
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
Parameter | Type |
---|---|
arg | number | IntExpr |