Class IntExpr
- Namespace
- OptalCP
- Assembly
- OptalCP.dll
Integer expression, e.g. an arithmetic combination of variables and constants.
public class IntExpr : ModelElement
- Inheritance
-
IntExpr
- Derived
- Inherited Members
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 Model.IntVarMin to Model.IntVarMax.
Use standard arithmetic operators (+, -, *, /, unary -) and
comparison operators (<, <=, ==, !=, >, >=).
Example:
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):
var model = new Model();
var x = model.IntervalVar(length: 10, name: "x");
var y = model.IntervalVar(length: 20, name: "y");
var 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.
Example:
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:
var model = new Model();
var x = model.IntervalVar(length: 10, name: "x", optional: true);
var y = model.IntervalVar(length: 20, name: "y");
var finish = x.End();
var ready = finish + 10;
var begin = y.Start();
var precedes = ready <= begin;
model.Enforce(precedes);
var result = 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 be true, false or 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,precedesis true. xis absent andyis present. In this case,precedesis absent.
Methods
Abs()
Creates an integer expression which is absolute value of the expression.
public IntExpr Abs()
Returns
- IntExpr
The resulting integer expression
Remarks
If the expression has value absent, the resulting expression also has value absent.
Same as Model.Abs.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Guard(int)
Creates an expression that replaces value absent by a constant.
public IntExpr Guard(int absentValue = 0)
Parameters
absentValueintThe value to use when the expression is absent.
Returns
- IntExpr
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(IntExpr)
Constrains two expressions to be identical, including their presence status.
public Constraint Identity(IntExpr rhs)
Parameters
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.
Identity(int)
Constrains two expressions to be identical, including their presence status.
public Constraint Identity(int rhs)
Parameters
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(int, int)
Creates Boolean expression lb ≤ this ≤ ub.
public BoolExpr InRange(int lb, int ub)
Parameters
Returns
- BoolExpr
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.
Max2(IntExpr)
Creates an integer expression which is the maximum of the expression and arg.
public IntExpr Max2(IntExpr rhs)
Parameters
Returns
- IntExpr
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.
Max2(int)
Creates an integer expression which is the maximum of the expression and arg.
public IntExpr Max2(int rhs)
Parameters
Returns
- IntExpr
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()
Creates a maximization objective for this expression.
public Objective Maximize()
Returns
- Objective
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:
var model = new Model();
var 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 also:
- Model.Maximize — for Model-centric maximization.
- IntExpr.Minimize — for minimization.
Min2(IntExpr)
Creates an integer expression which is the minimum of the expression and arg.
public IntExpr Min2(IntExpr rhs)
Parameters
Returns
- IntExpr
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.
Min2(int)
Creates an integer expression which is the minimum of the expression and arg.
public IntExpr Min2(int rhs)
Parameters
Returns
- IntExpr
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()
Creates a minimization objective for this expression.
public Objective Minimize()
Returns
- Objective
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:
var model = new Model();
var x = model.IntervalVar(length: 10, name: "x");
var 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 also:
- Model.Minimize — for Model-centric minimization.
- IntExpr.Maximize — for maximization.
Presence()
public BoolExpr Presence()
Returns
- BoolExpr
The resulting Boolean expression
Remarks
The resulting expression is never absent.
Same as Model.Presence.
Operators
operator +(IntExpr, IntExpr)
Add two integer expressions using the + operator.
public static IntExpr operator +(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the sum.
Remarks
Returns a new IntExpr representing the sum. All three overloads are supported:
expr + expr, expr + constant, and constant + expr.
If either operand is absent, the result is also absent.
var sum = x + y;
var incremented = x + 1;
var also = 1 + x;
See also:
operator +(IntExpr, int)
Add two integer expressions using the + operator.
public static IntExpr operator +(IntExpr lhs, int rhs)
Parameters
Returns
- IntExpr
A new expression representing the sum.
Remarks
Returns a new IntExpr representing the sum. All three overloads are supported:
expr + expr, expr + constant, and constant + expr.
If either operand is absent, the result is also absent.
var sum = x + y;
var incremented = x + 1;
var also = 1 + x;
See also:
operator +(int, IntExpr)
Add two integer expressions using the + operator.
public static IntExpr operator +(int lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the sum.
Remarks
Returns a new IntExpr representing the sum. All three overloads are supported:
expr + expr, expr + constant, and constant + expr.
If either operand is absent, the result is also absent.
var sum = x + y;
var incremented = x + 1;
var also = 1 + x;
See also:
operator /(IntExpr, IntExpr)
Divide integer expressions using the / operator (integer division).
public static IntExpr operator /(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the integer quotient.
Remarks
Returns a new IntExpr representing integer division. All three overloads are supported:
expr / expr, expr / constant, and constant / expr.
If either operand is absent, the result is also absent.
The result is truncated toward zero, matching C#'s native integer / semantics.
For example, -7 / 2 yields -3, not -4. OptalCP's integer division always rounds
toward zero, and this is consistent across the TypeScript, Python, and C# APIs.
var quotient = x / y;
var halved = x / 2;
var fromConst = 100 / x;
operator /(IntExpr, int)
Divide integer expressions using the / operator (integer division).
public static IntExpr operator /(IntExpr lhs, int rhs)
Parameters
Returns
- IntExpr
A new expression representing the integer quotient.
Remarks
Returns a new IntExpr representing integer division. All three overloads are supported:
expr / expr, expr / constant, and constant / expr.
If either operand is absent, the result is also absent.
The result is truncated toward zero, matching C#'s native integer / semantics.
For example, -7 / 2 yields -3, not -4. OptalCP's integer division always rounds
toward zero, and this is consistent across the TypeScript, Python, and C# APIs.
var quotient = x / y;
var halved = x / 2;
var fromConst = 100 / x;
operator /(int, IntExpr)
Divide integer expressions using the / operator (integer division).
public static IntExpr operator /(int lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the integer quotient.
Remarks
Returns a new IntExpr representing integer division. All three overloads are supported:
expr / expr, expr / constant, and constant / expr.
If either operand is absent, the result is also absent.
The result is truncated toward zero, matching C#'s native integer / semantics.
For example, -7 / 2 yields -3, not -4. OptalCP's integer division always rounds
toward zero, and this is consistent across the TypeScript, Python, and C# APIs.
var quotient = x / y;
var halved = x / 2;
var fromConst = 100 / x;
operator ==(IntExpr, IntExpr)
Create an equality constraint using the == operator.
public static BoolExpr operator ==(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs == rhs.
Remarks
Returns a BoolExpr representing lhs == rhs. All three overloads are supported:
expr == expr, expr == constant, and constant == expr.
If either operand is absent, the result is also absent.
model.Enforce(x == y);
model.Enforce(x == 5);
model.Enforce(5 == x);
See also:
- BoolExpr.Enforce — to post the constraint directly.
operator ==(IntExpr, int)
Create an equality constraint using the == operator.
public static BoolExpr operator ==(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs == rhs.
Remarks
Returns a BoolExpr representing lhs == rhs. All three overloads are supported:
expr == expr, expr == constant, and constant == expr.
If either operand is absent, the result is also absent.
model.Enforce(x == y);
model.Enforce(x == 5);
model.Enforce(5 == x);
See also:
- BoolExpr.Enforce — to post the constraint directly.
operator ==(int, IntExpr)
Create an equality constraint using the == operator.
public static BoolExpr operator ==(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs == rhs.
Remarks
Returns a BoolExpr representing lhs == rhs. All three overloads are supported:
expr == expr, expr == constant, and constant == expr.
If either operand is absent, the result is also absent.
model.Enforce(x == y);
model.Enforce(x == 5);
model.Enforce(5 == x);
See also:
- BoolExpr.Enforce — to post the constraint directly.
operator >(IntExpr, IntExpr)
Create a greater-than constraint using the > operator.
public static BoolExpr operator >(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs > rhs.
Remarks
Returns a BoolExpr representing lhs > rhs. All three overloads are supported:
expr > expr, expr > constant, and constant > expr.
If either operand is absent, the result is also absent.
model.Enforce(x > y);
model.Enforce(x > 0);
model.Enforce(10 > x); // equivalent to x < 10
operator >(IntExpr, int)
Create a greater-than constraint using the > operator.
public static BoolExpr operator >(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs > rhs.
Remarks
Returns a BoolExpr representing lhs > rhs. All three overloads are supported:
expr > expr, expr > constant, and constant > expr.
If either operand is absent, the result is also absent.
model.Enforce(x > y);
model.Enforce(x > 0);
model.Enforce(10 > x); // equivalent to x < 10
operator >(int, IntExpr)
Create a greater-than constraint using the > operator.
public static BoolExpr operator >(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs > rhs.
Remarks
Returns a BoolExpr representing lhs > rhs. All three overloads are supported:
expr > expr, expr > constant, and constant > expr.
If either operand is absent, the result is also absent.
model.Enforce(x > y);
model.Enforce(x > 0);
model.Enforce(10 > x); // equivalent to x < 10
operator >=(IntExpr, IntExpr)
Create a greater-than-or-equal constraint using the >= operator.
public static BoolExpr operator >=(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs >= rhs.
Remarks
Returns a BoolExpr representing lhs >= rhs. All three overloads are supported:
expr >= expr, expr >= constant, and constant >= expr.
If either operand is absent, the result is also absent.
model.Enforce(x >= y);
model.Enforce(x >= 0);
model.Enforce(10 >= x); // equivalent to x <= 10
operator >=(IntExpr, int)
Create a greater-than-or-equal constraint using the >= operator.
public static BoolExpr operator >=(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs >= rhs.
Remarks
Returns a BoolExpr representing lhs >= rhs. All three overloads are supported:
expr >= expr, expr >= constant, and constant >= expr.
If either operand is absent, the result is also absent.
model.Enforce(x >= y);
model.Enforce(x >= 0);
model.Enforce(10 >= x); // equivalent to x <= 10
operator >=(int, IntExpr)
Create a greater-than-or-equal constraint using the >= operator.
public static BoolExpr operator >=(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs >= rhs.
Remarks
Returns a BoolExpr representing lhs >= rhs. All three overloads are supported:
expr >= expr, expr >= constant, and constant >= expr.
If either operand is absent, the result is also absent.
model.Enforce(x >= y);
model.Enforce(x >= 0);
model.Enforce(10 >= x); // equivalent to x <= 10
operator !=(IntExpr, IntExpr)
Create an inequality constraint using the != operator.
public static BoolExpr operator !=(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs != rhs.
Remarks
Returns a BoolExpr representing lhs != rhs. All three overloads are supported:
expr != expr, expr != constant, and constant != expr.
If either operand is absent, the result is also absent.
model.Enforce(x != y);
model.Enforce(x != 0);
operator !=(IntExpr, int)
Create an inequality constraint using the != operator.
public static BoolExpr operator !=(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs != rhs.
Remarks
Returns a BoolExpr representing lhs != rhs. All three overloads are supported:
expr != expr, expr != constant, and constant != expr.
If either operand is absent, the result is also absent.
model.Enforce(x != y);
model.Enforce(x != 0);
operator !=(int, IntExpr)
Create an inequality constraint using the != operator.
public static BoolExpr operator !=(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs != rhs.
Remarks
Returns a BoolExpr representing lhs != rhs. All three overloads are supported:
expr != expr, expr != constant, and constant != expr.
If either operand is absent, the result is also absent.
model.Enforce(x != y);
model.Enforce(x != 0);
operator <(IntExpr, IntExpr)
Create a less-than constraint using the < operator.
public static BoolExpr operator <(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs < rhs.
Remarks
Returns a BoolExpr representing lhs < rhs. All three overloads are supported:
expr < expr, expr < constant, and constant < expr.
If either operand is absent, the result is also absent.
model.Enforce(x < y);
model.Enforce(x < 10);
model.Enforce(0 < x); // equivalent to x > 0
operator <(IntExpr, int)
Create a less-than constraint using the < operator.
public static BoolExpr operator <(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs < rhs.
Remarks
Returns a BoolExpr representing lhs < rhs. All three overloads are supported:
expr < expr, expr < constant, and constant < expr.
If either operand is absent, the result is also absent.
model.Enforce(x < y);
model.Enforce(x < 10);
model.Enforce(0 < x); // equivalent to x > 0
operator <(int, IntExpr)
Create a less-than constraint using the < operator.
public static BoolExpr operator <(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs < rhs.
Remarks
Returns a BoolExpr representing lhs < rhs. All three overloads are supported:
expr < expr, expr < constant, and constant < expr.
If either operand is absent, the result is also absent.
model.Enforce(x < y);
model.Enforce(x < 10);
model.Enforce(0 < x); // equivalent to x > 0
operator <=(IntExpr, IntExpr)
Create a less-than-or-equal constraint using the <= operator.
public static BoolExpr operator <=(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs <= rhs.
Remarks
Returns a BoolExpr representing lhs <= rhs. All three overloads are supported:
expr <= expr, expr <= constant, and constant <= expr.
If either operand is absent, the result is also absent.
model.Enforce(x <= y);
model.Enforce(x <= 10);
model.Enforce(0 <= x); // equivalent to x >= 0
operator <=(IntExpr, int)
Create a less-than-or-equal constraint using the <= operator.
public static BoolExpr operator <=(IntExpr lhs, int rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs <= rhs.
Remarks
Returns a BoolExpr representing lhs <= rhs. All three overloads are supported:
expr <= expr, expr <= constant, and constant <= expr.
If either operand is absent, the result is also absent.
model.Enforce(x <= y);
model.Enforce(x <= 10);
model.Enforce(0 <= x); // equivalent to x >= 0
operator <=(int, IntExpr)
Create a less-than-or-equal constraint using the <= operator.
public static BoolExpr operator <=(int lhs, IntExpr rhs)
Parameters
Returns
- BoolExpr
A boolean expression that is true when lhs <= rhs.
Remarks
Returns a BoolExpr representing lhs <= rhs. All three overloads are supported:
expr <= expr, expr <= constant, and constant <= expr.
If either operand is absent, the result is also absent.
model.Enforce(x <= y);
model.Enforce(x <= 10);
model.Enforce(0 <= x); // equivalent to x >= 0
operator *(IntExpr, IntExpr)
Multiply integer expressions using the * operator.
public static IntExpr operator *(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the product.
Remarks
Returns a new IntExpr representing the product. All three overloads are supported:
expr * expr, expr * constant, and constant * expr.
If either operand is absent, the result is also absent.
var product = x * y;
var scaled = x * 2;
var also = 2 * x;
operator *(IntExpr, int)
Multiply integer expressions using the * operator.
public static IntExpr operator *(IntExpr lhs, int rhs)
Parameters
Returns
- IntExpr
A new expression representing the product.
Remarks
Returns a new IntExpr representing the product. All three overloads are supported:
expr * expr, expr * constant, and constant * expr.
If either operand is absent, the result is also absent.
var product = x * y;
var scaled = x * 2;
var also = 2 * x;
operator *(int, IntExpr)
Multiply integer expressions using the * operator.
public static IntExpr operator *(int lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the product.
Remarks
Returns a new IntExpr representing the product. All three overloads are supported:
expr * expr, expr * constant, and constant * expr.
If either operand is absent, the result is also absent.
var product = x * y;
var scaled = x * 2;
var also = 2 * x;
operator -(IntExpr, IntExpr)
Subtract integer expressions using the - operator.
public static IntExpr operator -(IntExpr lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the difference.
Remarks
Returns a new IntExpr representing the difference. All three overloads are supported:
expr - expr, expr - constant, and constant - expr.
If either operand is absent, the result is also absent.
var diff = x - y;
var decremented = x - 1;
var fromConst = 10 - x;
operator -(IntExpr, int)
Subtract integer expressions using the - operator.
public static IntExpr operator -(IntExpr lhs, int rhs)
Parameters
Returns
- IntExpr
A new expression representing the difference.
Remarks
Returns a new IntExpr representing the difference. All three overloads are supported:
expr - expr, expr - constant, and constant - expr.
If either operand is absent, the result is also absent.
var diff = x - y;
var decremented = x - 1;
var fromConst = 10 - x;
operator -(int, IntExpr)
Subtract integer expressions using the - operator.
public static IntExpr operator -(int lhs, IntExpr rhs)
Parameters
Returns
- IntExpr
A new expression representing the difference.
Remarks
Returns a new IntExpr representing the difference. All three overloads are supported:
expr - expr, expr - constant, and constant - expr.
If either operand is absent, the result is also absent.
var diff = x - y;
var decremented = x - 1;
var fromConst = 10 - x;
operator -(IntExpr)
Negate an integer expression using the unary - operator.
public static IntExpr operator -(IntExpr expr)
Parameters
exprIntExpr
Returns
- IntExpr
A new expression representing the negation.
Remarks
Returns a new IntExpr representing -expr.
If the operand is absent, the result is also absent.
var neg = -x;
model.Enforce(neg >= -5); // equivalent to x <= 5