Class: BoolExpr
A class that represents a boolean expression in the model. The expression may depend on one or more variables and therefore its value may be unknown until a solution is found.
Example
For example, the following code creates two interval variables x
and y
and a boolean expression isBefore
that is true if x
ends before y
starts,
that is, if the end of x
is less than or equal to
the start of y
(see IntExpr.le):
let model = new CP.Model();
let x = model.intervalVar({ length: 10, name: "x" });
let y = model.intervalVar({ length: 20, name: "y" });
let isBefore = x.end().le(y.start());
let result = await CP.solve(model);
Boolean expressions can be used to create constraints using function Model.constraint. In the example above, we may require that isBefore
is
true:
model.constraint(isBefore);
Optional boolean expressions
OptalCP is using 3value logic: a boolean expression can be true, false or absent. Typically the the expression is absent only if one or more underlying variables is absent. The value absent means that the expression doesn't have a meaning because one or more underlying variables are absent (they are not part of the solution).
Difference between constraints and boolean expressions
Boolean expressions can take arbitrary value (true, false, or absent) and can be combined into composed expressions (e.g. using and or or).
Constraints can only be true or absent (in a solution) and cannot be combined into composed expressions.
Some functions create constraints directly, e.g. noOverlap.
Then, it is not necessary to to pass them to function constraint.
It is also not possible to combine constraints into composed expressions
such as or(noOverlap(..), noOverlap(..))
.
Example
Let's consider a similar example to the one above but with optional interval
variables a
and b
:
let model = new CP.Model();
let a = model.intervalVar({ length: 10, name: "a", optional: true });
let b = model.intervalVar({ length: 20, name: "b", optional: true });
let isBefore = a.end().le(b.start());
model.constraint(isBefore);
let result = await CP.solve(model);
The function Model.constraint requires that the constraint cannot be false in a solution. It could be absent though. Therefore, in our example, there are four kinds of solutions:
 Both
a
andb
are present anda
ends beforeb
starts.  Only
a
is present andb
is absent.  Only
b
is present anda
is absent.  Both
a
andb
are absent.
In the case 1 the expression isBefore
is true. In all the other cases
isBefore
is absent as at least one of the variables a
and b
is
absent, and then isBefore
doesn't have a meaning.
Boolean expressions as integer expressions
Class BoolExpr
derives from IntExpr. Therefore boolean expressions can be used
as integer expressions. In this case true is equal to 1, false is
equal to 0, and absent remains absent.
Hierarchy

↳
BoolExpr
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.
Inherited from
and
▸ and(arg
): BoolExpr
Returns logical AND of the expression and arg
.
Parameters
Name  Type 

arg  boolean  BoolExpr 
Returns
Remarks
If the expression or arg
has value absent then the resulting expression has also value absent.
Same as Model.and.
div
▸ div(arg
): IntExpr
Returns integer division of the expression arg
. The division rounds towards zero.
Parameters
Name  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.
Inherited from
eq
▸ eq(arg
): BoolExpr
Creates Boolean expression this
= arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
ge
▸ ge(arg
): BoolExpr
Creates Boolean expression this
≥ arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
getName
▸ getName(): undefined
 string
Returns the name assigned to the node.
Returns
undefined
 string
Inherited from
gt
▸ gt(arg
): BoolExpr
Creates Boolean expression this
> arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
guard
▸ guard(absentValue?
): IntExpr
Creates an expression that replaces value absent by a constant.
Parameters
Name  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.
Inherited from
identity
▸ identity(arg
): void
Constrains the expression to be identical to the argument, including their presence status.
Parameters
Name  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.
Inherited from
implies
▸ implies(arg
): BoolExpr
Returns implication between the expression and arg
.
Parameters
Name  Type 

arg  boolean  BoolExpr 
Returns
Remarks
If the expression or arg
has value absent then the resulting expression has also value absent.
Same as Model.implies.
inRange
▸ inRange(lb
, ub
): BoolExpr
Creates Boolean expression lb
≤ this
≤ ub
.
Parameters
Name  Type 

lb  number 
ub  number 
Returns
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.
Inherited from
le
▸ le(arg
): BoolExpr
Creates Boolean expression this
≤ arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
lt
▸ lt(arg
): BoolExpr
Creates Boolean expression this
< arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
max2
▸ max2(arg
): IntExpr
Creates an integer expression which is the maximum of the expression and arg
.
Parameters
Name  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 nary maximum.
Inherited from
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
min2
▸ min2(arg
): IntExpr
Creates an integer expression which is the minimum of the expression and arg
.
Parameters
Name  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 min for nary minimum.
Inherited from
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
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
Name  Type 

arg  number  IntExpr 
Returns
Inherited from
ne
▸ ne(arg
): BoolExpr
Creates Boolean expression this
≠ arg
.
Parameters
Name  Type 

arg  number  IntExpr 
Returns
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.
Inherited from
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.
Inherited from
not
▸ not(): BoolExpr
Returns negation of the expression.
Returns
Remarks
If the expression has value absent then the resulting expression has also value absent.
Same as Model.not.
or
▸ or(arg
): BoolExpr
Returns logical OR of the expression and arg
.
Parameters
Name  Type 

arg  boolean  BoolExpr 
Returns
Remarks
If the expression or arg
has value absent then the resulting expression has also value absent.
Same as Model.or.
plus
▸ plus(arg
): IntExpr
Returns addition of the expression and the argument.
Parameters
Name  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.
Inherited from
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.
Inherited from
setName
▸ setName(name
): BoolExpr
Assigns a name to the node.
Parameters
Name  Type  Description 

name  string  Named to be assigned. 
Returns
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
▸ 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
Name  Type 

arg  number  IntExpr 