Class BoolExpr
- Namespace
- OptalCP
- Assembly
- OptalCP.dll
Boolean expression, e.g. a comparison or a logical combination of other expressions.
public class BoolExpr : IntExpr
- Inheritance
-
BoolExpr
- Derived
- Inherited Members
Remarks
A class that represents a boolean expression in the model. The expression may depend on one or more variables; therefore, its value may be unknown until a solution is found.
Example:
x and y
and a boolean expression precedes 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:
Use standard comparison operators on expressions: x.End() <= y.Start().
var model = new Model();
var x = model.IntervalVar(length: 10, name: "x");
var y = model.IntervalVar(length: 20, name: "y");
var precedes = x.End() <= y.Start();
var result = model.Solve();
Boolean expressions can be used to create constraints using Model.Enforce. In the example above, we may require that precedes is
true or absent:
model.Enforce(precedes);
Optional boolean expressions
OptalCP is using 3-value logic: a boolean expression can be true, false or absent. Typically, the expression is absent only if one or more underlying variables are absent. The value absent means that the expression doesn't have a meaning because one or more underlying variables are absent (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 BoolExpr.And or BoolExpr.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. Model.NoOverlap.
It is not possible to combine constraints using logical operators like or.
Example:
a and b:
var model = new Model();
var a = model.IntervalVar(length: 10, name: "a", optional: true);
var b = model.IntervalVar(length: 20, name: "b", optional: true);
var precedes = a.End() <= b.Start();
model.Enforce(precedes);
var result = model.Solve();
Adding a boolean expression as a constraint requires that the expression cannot be false in a solution. It could be absent though. Therefore, in our example, there are four kinds of solutions:
- Both
aandbare present, andaends beforebstarts. - Only
ais present, andbis absent. - Only
bis present, andais absent. - Both
aandbare absent.
In case 1, the expression precedes is true. In all the other cases
precedes is absent as at least one of the variables a and b is
absent, and then precedes 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.
Methods
And(BoolExpr)
Returns logical AND of the expression and arg.
public BoolExpr And(BoolExpr rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.And.
And(bool)
Returns logical AND of the expression and arg.
public BoolExpr And(bool rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.And.
Enforce()
Adds this boolean expression as a constraint to the model.
public void Enforce()
Remarks
This method adds the boolean expression as a constraint to the model. It provides a fluent-style alternative to Model.Enforce.
A constraint is satisfied if it is not false. In other words, a constraint is satisfied if it is true or absent.
A boolean expression that is not added as a constraint can have arbitrary value in a solution (true, false, or absent). Once added as a constraint, it can only be true or absent in the solution.
Example:
var model = new Model();
var x = model.IntVar(min: 0, max: 10, name: "x");
var y = model.IntVar(min: 0, max: 10, name: "y");
// Enforce constraint using fluent style
(x + y <= 15).Enforce();
// Equivalent to:
// model.Enforce(x + y <= 15);
var result = model.Solve();
See also:
- Model.Enforce — for the Model-centric style of adding constraints.
- BoolExpr — for more about boolean expressions.
Implies(BoolExpr)
Returns implication between the expression and arg.
public BoolExpr Implies(BoolExpr rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.Implies.
Implies(bool)
Returns implication between the expression and arg.
public BoolExpr Implies(bool rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent, then the resulting expression has also value absent.
Same as Model.Implies.
Not()
Returns negation of the expression.
public BoolExpr Not()
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression has value absent then the resulting expression has also value absent.
Same as Model.Not.
Or(BoolExpr)
Returns logical OR of the expression and arg.
public BoolExpr Or(BoolExpr rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent then the resulting expression has also value absent.
Same as Model.Or.
Or(bool)
Returns logical OR of the expression and arg.
public BoolExpr Or(bool rhs)
Parameters
Returns
- BoolExpr
The resulting Boolean expression
Remarks
If the expression or arg has value absent then the resulting expression has also value absent.
Same as Model.Or.
Operators
operator &(BoolExpr, BoolExpr)
Create a logical AND of boolean expressions using the & operator.
public static BoolExpr operator &(BoolExpr lhs, BoolExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when both operands are true.
Remarks
Returns a BoolExpr representing lhs & rhs. All three overloads are supported:
BoolExpr & BoolExpr, BoolExpr & bool, and bool & BoolExpr.
If either operand is absent, the result is also absent.
Note: use & (bitwise AND), not && (short-circuit AND), as C# does not allow overloading &&.
Operator precedence: In C#, & has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b & c == d parses as a == (b & c) == d
// Correct:
model.Enforce((a == b) & (c == d));
model.Enforce(x & y); // both must be true
model.Enforce(x & true); // equivalent to x
model.Enforce(false & x); // always false
See also:
- BoolExpr.And — for method alternatives that avoid precedence issues.
- Model.And — for method alternatives that avoid precedence issues.
operator &(BoolExpr, bool)
Create a logical AND of boolean expressions using the & operator.
public static BoolExpr operator &(BoolExpr lhs, bool rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when both operands are true.
Remarks
Returns a BoolExpr representing lhs & rhs. All three overloads are supported:
BoolExpr & BoolExpr, BoolExpr & bool, and bool & BoolExpr.
If either operand is absent, the result is also absent.
Note: use & (bitwise AND), not && (short-circuit AND), as C# does not allow overloading &&.
Operator precedence: In C#, & has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b & c == d parses as a == (b & c) == d
// Correct:
model.Enforce((a == b) & (c == d));
model.Enforce(x & y); // both must be true
model.Enforce(x & true); // equivalent to x
model.Enforce(false & x); // always false
See also:
- BoolExpr.And — for method alternatives that avoid precedence issues.
- Model.And — for method alternatives that avoid precedence issues.
operator &(bool, BoolExpr)
Create a logical AND of boolean expressions using the & operator.
public static BoolExpr operator &(bool lhs, BoolExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when both operands are true.
Remarks
Returns a BoolExpr representing lhs & rhs. All three overloads are supported:
BoolExpr & BoolExpr, BoolExpr & bool, and bool & BoolExpr.
If either operand is absent, the result is also absent.
Note: use & (bitwise AND), not && (short-circuit AND), as C# does not allow overloading &&.
Operator precedence: In C#, & has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b & c == d parses as a == (b & c) == d
// Correct:
model.Enforce((a == b) & (c == d));
model.Enforce(x & y); // both must be true
model.Enforce(x & true); // equivalent to x
model.Enforce(false & x); // always false
See also:
- BoolExpr.And — for method alternatives that avoid precedence issues.
- Model.And — for method alternatives that avoid precedence issues.
operator |(BoolExpr, BoolExpr)
Create a logical OR of boolean expressions using the | operator.
public static BoolExpr operator |(BoolExpr lhs, BoolExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when at least one operand is true.
Remarks
Returns a BoolExpr representing lhs | rhs. All three overloads are supported:
BoolExpr | BoolExpr, BoolExpr | bool, and bool | BoolExpr.
If either operand is absent, the result is also absent.
Note: use | (bitwise OR), not || (short-circuit OR), as C# does not allow overloading ||.
Operator precedence: In C#, | has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b | c == d parses as a == (b | c) == d
// Correct:
model.Enforce((a == b) | (c == d));
model.Enforce(x | y); // at least one must be true
model.Enforce(x | false); // equivalent to x
model.Enforce(true | x); // always true
See also:
- BoolExpr.Or — for method alternatives that avoid precedence issues.
- Model.Or — for method alternatives that avoid precedence issues.
operator |(BoolExpr, bool)
Create a logical OR of boolean expressions using the | operator.
public static BoolExpr operator |(BoolExpr lhs, bool rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when at least one operand is true.
Remarks
Returns a BoolExpr representing lhs | rhs. All three overloads are supported:
BoolExpr | BoolExpr, BoolExpr | bool, and bool | BoolExpr.
If either operand is absent, the result is also absent.
Note: use | (bitwise OR), not || (short-circuit OR), as C# does not allow overloading ||.
Operator precedence: In C#, | has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b | c == d parses as a == (b | c) == d
// Correct:
model.Enforce((a == b) | (c == d));
model.Enforce(x | y); // at least one must be true
model.Enforce(x | false); // equivalent to x
model.Enforce(true | x); // always true
See also:
- BoolExpr.Or — for method alternatives that avoid precedence issues.
- Model.Or — for method alternatives that avoid precedence issues.
operator |(bool, BoolExpr)
Create a logical OR of boolean expressions using the | operator.
public static BoolExpr operator |(bool lhs, BoolExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when at least one operand is true.
Remarks
Returns a BoolExpr representing lhs | rhs. All three overloads are supported:
BoolExpr | BoolExpr, BoolExpr | bool, and bool | BoolExpr.
If either operand is absent, the result is also absent.
Note: use | (bitwise OR), not || (short-circuit OR), as C# does not allow overloading ||.
Operator precedence: In C#, | has lower precedence than comparison operators (==, !=, <, etc.). Use parentheses when combining with comparisons:
// Wrong: a == b | c == d parses as a == (b | c) == d
// Correct:
model.Enforce((a == b) | (c == d));
model.Enforce(x | y); // at least one must be true
model.Enforce(x | false); // equivalent to x
model.Enforce(true | x); // always true
See also:
- BoolExpr.Or — for method alternatives that avoid precedence issues.
- Model.Or — for method alternatives that avoid precedence issues.
operator !(BoolExpr)
Negate a boolean expression using the ! operator.
public static BoolExpr operator !(BoolExpr expr)
Parameters
exprBoolExpr
Returns
- BoolExpr
A boolean expression that is true when the operand is false.
Remarks
Returns a BoolExpr representing !expr.
If the operand is absent, the result is also absent.
Operator precedence: In C#, ! has higher precedence than comparison operators. Use parentheses when negating a comparison:
// Wrong: !a == b parses as (!a) == b
// Correct: negate an equality
model.Enforce(!(a == b));
var notX = !x;
model.Enforce(!x | y); // x implies y
See also:
- BoolExpr.Not — for method alternatives.
- Model.Not — for method alternatives.