Class: IntExpr
A class representing an integer expression in the model.
Remarks
The expression may depend on the value of a variable (or variables), so the value of the expression is not known until a solution is found. The value must be in the range IntVarMin to IntVarMax.
See IntExpr.plus, IntExpr.minus, IntExpr.times for arithmetic, and IntExpr.le, IntExpr.eq, etc. for comparisons.
Examples
The following code creates two interval variables x and y
and an integer expression makespan that is equal to the maximum of the end
times of x and y (see Model.max2):
let model = new CP.Model();
let x = model.intervalVar({ length: 10, name: "x" });
let y = model.intervalVar({ length: 20, name: "y" });
let makespan = 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 omitted entirely 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.
Except IntExpr.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 an optional interval variable x and
a non-optional interval variable y. We add a constraint that the 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 finish = model.end(x);
let ready = finish.plus(10);
let begin = model.start(y);
let precedes = ready.le(begin);
model.enforce(precedes);
let result = await model.solve();
In this model:
finishis an optional integer expression because it depends on an optional variablex.- The expression
readyis optional for the same reason. - The expression
beginis not optional because it depends only on a non-optional variabley. - Boolean expression
precedesis also optional. Its value could betrue,falseor absent.
The expression precedes is turned into a constraint using
Model.enforce. Therefore, it cannot be false
in the solution. However, it can still be absent. Therefore the constraint
precedes can be satisfied in two ways:
- Both
xandyare present,xis beforey, and the delay between them is at least 10. In this case,precedesistrue. xis absent andyis present. In this case,precedesis absent.
Extends
Extended by
Accessors
name
Get Signature
get name():
undefined|string
The name assigned to this model element.
Remarks
The name is optional and primarily useful for debugging purposes. When set, it helps identify the element in solver logs, error messages, and when inspecting solutions.
Names can be assigned to any model element including variables, expressions, and constraints.
Example
import * as CP from "optalcp";
const model = new CP.Model();
// Name a variable at creation
const task = model.intervalVar({ length: 10, name: "assembly" });
// Or set name later
const x = model.intVar({ min: 0, max: 100 });
x.name = "quantity";
console.log(task.name); // "assembly"
console.log(x.name); // "quantity"
Returns
undefined | string
Set Signature
set name(
value:string):void
Parameters
| Parameter | Type |
|---|---|
value | string |
Returns
void
Inherited from
Constructors
new IntExpr()
new IntExpr():
IntExpr
Returns
Inherited from
Methods
abs()
abs():
IntExpr
Creates an integer expression which is absolute value of the expression.
Returns
The resulting integer expression
Remarks
If the expression has value absent, the resulting expression also has 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 | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.div.
eq()
Create an equality constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self equals rhs.
Remarks
Returns a BoolExpr representing self == other.
If either operand has value absent, the result is also absent.
See
- IntExpr.ne for inequality comparison.
- Model.eq for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.eq(IntExpr)
model.enforce(x.eq(y));
// IntExpr.eq(constant)
model.enforce(x.eq(5));
ge()
Create a greater-than-or-equal constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self is greater than or equal to rhs.
Remarks
Returns a BoolExpr representing self >= other.
If either operand has value absent, the result is also absent.
See
- IntExpr.gt for strict greater-than comparison.
- IntExpr.le for less-than-or-equal comparison.
- Model.ge for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.ge(IntExpr)
model.enforce(x.ge(y));
// IntExpr.ge(constant)
model.enforce(x.ge(5));
gt()
Create a greater-than constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self is greater than rhs.
Remarks
Returns a BoolExpr representing self > other.
If either operand has value absent, the result is also absent.
See
- IntExpr.ge for greater-than-or-equal comparison.
- IntExpr.lt for less-than comparison.
- Model.gt for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.gt(IntExpr)
model.enforce(x.gt(y));
// IntExpr.gt(constant)
model.enforce(x.gt(5));
guard()
guard(
absentValue:number):IntExpr
Creates an expression that replaces value absent by a constant.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
absentValue | number | 0 | The value to use when the expression is absent. |
Returns
The resulting integer expression
Remarks
The resulting expression is:
- equal to the expression if the expression is present
- and equal to
absentValueotherwise (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(
rhs:number|IntExpr):Constraint
Constrains two expressions to be identical, including their presence status.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
Constraint
The constraint object
Remarks
Identity is different than equality. For example, if x is absent, then x == 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 | Description |
|---|---|---|
lb | number | The lower bound of the range. |
ub | number | The upper bound of the range. |
Returns
The resulting Boolean expression
Remarks
If the expression has value absent, then the resulting expression has also value absent.
Use Model.enforce to add this expression as a constraint to the model.
Same as Model.inRange.
le()
Create a less-than-or-equal constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self is less than or equal to rhs.
Remarks
Returns a BoolExpr representing self <= other.
If either operand has value absent, the result is also absent.
See
- IntExpr.lt for strict less-than comparison.
- IntExpr.ge for greater-than-or-equal comparison.
- Model.le for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.le(IntExpr)
model.enforce(x.le(y));
// IntExpr.le(constant)
model.enforce(x.le(5));
lt()
Create a less-than constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self is less than rhs.
Remarks
Returns a BoolExpr representing self < other.
If either operand has value absent, the result is also absent.
See
- IntExpr.le for less-than-or-equal comparison.
- IntExpr.gt for greater-than comparison.
- Model.lt for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.lt(IntExpr)
model.enforce(x.lt(y));
// IntExpr.lt(constant)
model.enforce(x.lt(5));
max2()
Creates an integer expression which is the maximum of the expression and arg.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
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():
Objective
Creates a maximization objective for this expression.
Returns
An Objective that maximizes this expression.
Remarks
Creates an objective to maximize the value of this integer expression. A model can have at most one objective. New objective replaces the old one.
This is a fluent-style alternative to Model.maximize that allows creating objectives directly from expressions.
Example
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intervalVar({start: [0, 100], length: 10, name: "x"});
// Fluent style - maximize the start of x
x.start().maximize();
// Equivalent Model.maximize() style:
model.maximize(x.start());
See
- Model.maximize for Model-centric maximization.
- IntExpr.minimize for minimization.
min2()
Creates an integer expression which is the minimum of the expression and arg.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
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.
minimize()
minimize():
Objective
Creates a minimization objective for this expression.
Returns
An Objective that minimizes this expression.
Remarks
Creates an objective to minimize the value of this integer expression. A model can have at most one objective. New objective replaces the old one.
This is a fluent-style alternative to Model.minimize that allows creating objectives directly from expressions.
Example
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intervalVar({length: 10, name: "x"});
const y = model.intervalVar({length: 20, name: "y"});
// Fluent style - minimize the end of y
y.end().minimize();
// Equivalent Model.minimize() style:
model.minimize(y.end());
See
- Model.minimize for Model-centric minimization.
- IntExpr.maximize for maximization.
minus()
Returns subtraction of the expression and arg.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.minus.
ne()
Create an inequality constraint.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The expression or constant to compare against. |
Returns
A boolean expression that is true when self does not equal rhs.
Remarks
Returns a BoolExpr representing self != other.
If either operand has value absent, the result is also absent.
See
- IntExpr.eq for equality comparison.
- Model.ne for the Model-centric style.
import * as CP from "optalcp";
const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });
// IntExpr.ne(IntExpr)
model.enforce(x.ne(y));
// IntExpr.ne(constant)
model.enforce(x.ne(5));
neg()
neg():
IntExpr
Returns negation of the expression.
Returns
The resulting integer expression
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 | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
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
The resulting Boolean expression
Remarks
The resulting expression is never absent.
Same as Model.presence.
times()
Returns multiplication of the expression and arg.
Parameters
| Parameter | Type | Description |
|---|---|---|
rhs | number | IntExpr | The second integer expression. |
Returns
The resulting integer expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.times.
Properties
| Property | Modifier | Type | Inherited from |
|---|---|---|---|
__floatExprBrand | readonly | "FloatExpr" | FloatExpr.__floatExprBrand |
__intExprBrand | readonly | "IntExpr" | - |