LSExpression Class¶
- class localsolver.LSExpression¶
Mathematical modeling expression. Expressions are used to build the mathematical optimization model associated to LocalSolver. An expression is composed of an operator (which corresponds to its type) and its operands (which are other expressions of the model).
- See:
- See:
Summary¶
Returns the model object associated to this expression. |
|
Returns the LocalSolver object associated to this expression. |
|
Gets the operator of this expression. |
|
Gets the index of this expression in the model. |
|
Returns true if this expression is typed as constant in the model, false otherwise. |
|
Returns true if this expression is typed as decision in the model, false otherwise. |
|
Returns true if this expression is tagged as constraint in the model, false otherwise. |
|
Returns true if this expression is tagged as objective in the model, false otherwise. |
|
Returns true if this expression is a double, false otherwise. |
|
Returns true if this expression is an integer, false otherwise. |
|
Returns true if this expression is a boolean (ie 0 or 1), false otherwise. |
|
Returns true if this expression is an interval, false otherwise. |
|
Returns true if this expression is an array, false otherwise. |
|
Returns true if this expression is a collection (list or set), false otherwise. |
|
Returns true if this expression is a function, false otherwise. |
|
Adds the given operand to this expression. |
|
Add the given operands to the expression. |
|
Gets the operand with the given index. |
|
Replaces the operand of the given index. |
|
Gets the number of operands of this expression. |
|
Sets the value of this expression in the current solution found by the solver. |
|
Sets the value of this expression in the current solution found by the solver. |
|
Sets the value of this expression in the current solution found by the solver. |
|
Sets the value of this expression in the current solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the value of this expression in the best solution found by the solver. |
|
Gets the external function context of this expression. |
|
Returns true if the given expression is violated in the best solution found by the solver. |
|
Returns true if the given expression has an undefined value in the best solution found by the solver. |
|
Sets the name of this expression. |
|
Returns true if this expression has a name, and false otherwise. |
|
Gets the name of this expression or the empty string if no name has been set. |
|
Returns a string representation of this expression. |
Creates a new LSOperator.Sum expression. |
|
Creates a new LSOperator.Sub expression. |
|
Creates a new LSOperator.Prod expression. |
|
Creates a new LSOperator.Div expression. |
|
Creates a new LSOperator.Mod expression. |
|
Creates a new LSOperator.Not expression. |
|
Creates a new LSOperator.And expression. |
|
Creates a new LSOperator.Or expression. |
|
Creates a new LSOperator.Xor expression. |
|
Creates a new LSOperator.Eq expression. |
|
Creates a new LSOperator.Neq expression. |
|
Creates a new LSOperator.Geq expression. |
|
Creates a new LSOperator.Leq expression. |
|
Creates a new LSOperator.Gt expression. |
|
Creates a new LSOperator.Lt expression. |
|
Creates a new LSOperator.At expression. |
Instance methods¶
- LSModel GetModel()¶
Returns the model object associated to this expression.
- Returns:
LSModel object
- Return type:
- LocalSolver GetLocalSolver()¶
Returns the LocalSolver object associated to this expression.
- Returns:
LocalSolver object
- Return type:
- LSOperator GetOperator()¶
Gets the operator of this expression.
- Returns:
Operator.
- Return type:
- int GetIndex()¶
Gets the index of this expression in the model.
- Returns:
Index in the model.
- Return type:
int
- bool IsConstant()¶
Returns true if this expression is typed as constant in the model, false otherwise.
- Returns:
True if typed as constant.
- Return type:
bool
- bool IsDecision()¶
Returns true if this expression is typed as decision in the model, false otherwise.
- Returns:
True if typed as decision.
- Return type:
bool
- bool IsConstraint()¶
Returns true if this expression is tagged as constraint in the model, false otherwise.
- Returns:
True if tagged as constraint.
- Return type:
bool
- bool IsObjective()¶
Returns true if this expression is tagged as objective in the model, false otherwise.
- Returns:
True if tagged as objective.
- Return type:
bool
- bool IsDouble()¶
Returns true if this expression is a double, false otherwise. Only allowed in states
Paused
orStopped
.- Since:
3.0
- Returns:
True if the expression is a double.
- Return type:
bool
- bool IsInt()¶
Returns true if this expression is an integer, false otherwise. Only allowed in states
Paused
orStopped
. Note that a boolean is also an integer.- Since:
3.0
- Returns:
True if the expression is an integer.
- Return type:
bool
- bool IsBool()¶
Returns true if this expression is a boolean (ie 0 or 1), false otherwise. Only allowed in states
Paused
orStopped
.- Since:
3.0
- Returns:
True if the expression is a boolean.
- Return type:
bool
- bool IsInterval()¶
Returns true if this expression is an interval, false otherwise. Only allowed in states
Paused
orStopped
.- Since:
12.0
- Returns:
True if the expression is an interval.
- Return type:
bool
- bool IsArray()¶
Returns true if this expression is an array, false otherwise. Only allowed in states
Paused
orStopped
.- Since:
3.1
- Returns:
True if the expression is an array.
- Return type:
bool
- bool IsCollection()¶
Returns true if this expression is a collection (list or set), false otherwise. Only allowed in states
Paused
orStopped
.- Since:
5.5
- Returns:
True if the expression is a collection.
- Return type:
bool
- bool IsFunction()¶
Returns true if this expression is a function, false otherwise. Only allowed in states
Paused
orStopped
.- Since:
6.0
- Returns:
True if the expression is a function.
- void AddOperand(LSExpression expr)¶
- void AddOperand(long constant)
- void AddOperand(double constant)
Adds the given operand to this expression. Only allowed in state
Modeling
.- Arguments:
expr – Operand to add.
- void AddOperands(IEnumerable<LSExpression> operands)¶
- void AddOperands(IEnumerable<long> operands)
- void AddOperands(IEnumerable<double> operands)
- void AddOperands(params LSExpression[] operands)
- void AddOperands(params long[] operands)
- void AddOperands(params double[] operands)
Add the given operands to the expression.
- Arguments:
operands – The operands to be added.
- LSExpression GetOperand(int operandIndex)¶
Gets the operand with the given index.
- Arguments:
operandIndex – Index of the operand.
- Returns:
Operand.
- Return type:
- void SetOperand(int operandIndex, LSExpression operand)¶
- void SetOperand(int operandIndex, long constant)
- void SetOperand(int operandIndex, double constant)
Replaces the operand of the given index.
- Arguments:
operandIndex (int) – Index of the operand to change
operand – New operand
- int GetNbOperands()¶
Gets the number of operands of this expression.
- Returns:
Number of operands.
- Return type:
int
- void SetValue(long value)¶
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state
Stopped
. Only allowed if this expression is an integer or a boolean. If the solver was not launched, this value will be used as an initial value for the decision.This method is a shortcut for
LSSolution.SetIntValue(LSExpression, long)
and has the same behavior asLSExpression.SetIntValue(long)
.- Arguments:
value (long) – Value assigned to this expression.
- See:
- See:
- See:
- void SetValue(double value)
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state :field`~LSState.Stopped`. Only allowed if this expression is an integer or a boolean. If the solver was not launched, this value will be used as an initial value for the decision.
This method is a shortcut for
LSSolution.SetDoubleValue(LSExpression, double)
and has the same behavior asLSExpression.SetDoubleValue(double)
.- Arguments:
value (double) – Value assigned to this expression.
- See:
- See:
- void SetValue(LSInterval value)
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state :field`~LSState.Stopped`. Only allowed if this expression is an interval. If the solver was not launched, this value will be used as an initial value for the decision.
This method is a shortcut for
LSSolution.SetIntervalValue(LSExpression, LSInterval)
and has the same behavior asLSExpression.SetIntervalValue(LSInterval)
.- Arguments:
value (LSInterval) – Value assigned to this expression.
- See:
- See:
- void SetIntValue(long value)¶
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state
Stopped
. Only allowed if this expression is an integer or a boolean. If the solver was not launched, this value will be used as an initial value for the decision.This method is a shortcut for
LSSolution.SetIntValue(LSExpression, long)
.- Since:
3.0
- Arguments:
value (long) – Value assigned to this expression.
- See:
- See:
- See:
- void SetDoubleValue(double value)¶
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state
Stopped
. Only allowed if this expression is a double. If the solver was not launched, this value will be used as an initial value for the decision.This method is a shortcut for
LSSolution.SetDoubleValue(LSExpression, double)
.- Since:
3.0
- Arguments:
value (double) – Value assigned to this expression.
- See:
- See:
- void SetIntervalValue(LSInterval value)¶
Sets the value of this expression in the current solution found by the solver. Only allowed for decisions. Only allowed in state
Stopped
. Only allowed if this expression is an interval. If the solver was not launched, this value will be used as an initial value for the decision.This method is a shortcut for
LSSolution.SetIntervalValue(LSExpression, LSInterval)
.- Since:
12.0
- Arguments:
value (LSInterval) – Value assigned to this expression.
- See:
- See:
- long GetValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is an integer or a boolean.This method is a shortcut for
LSSolution.GetIntValue(LSExpression)
and has the same behavior asLSExpression.GetIntValue(LSExpression)
.- Returns:
Value in the best solution.
- Return type:
long
- See:
- See:
- See:
- long GetIntValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is an integer or a boolean.This method is a shortcut for
LSSolution.GetIntValue(LSExpression)
.- Since:
3.0
- Returns:
Value in the best solution.
- Return type:
long
- See:
- See:
- See:
- double GetDoubleValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is a double.This method is a shortcut for
LSSolution.GetDoubleValue(LSExpression)
.- Since:
3.0
- Returns:
Value in the best solution.
- Return type:
double
- See:
- See:
- double GetIntervalValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is an interval.This method is a shortcut for
LSSolution.GetIntervalValue(LSExpression)
.- Since:
12.0
- Returns:
Value in the best solution.
- Return type:
- See:
- See:
- LSCollection GetCollectionValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is a collection (list).This method is a shortcut for
LSSolution.GetCollectionValue(LSExpression)
.- Since:
5.5
- Returns:
Value in the best solution.
- Return type:
- See:
- See:
- LSArray GetArrayValue()¶
Gets the value of this expression in the best solution found by the solver. Only allowed in states
Paused
orStopped
. Only allowed if this expression is an array.This method is a shortcut for
LSSolution.GetArrayValue(LSExpression)
.- Since:
7.5
- Returns:
Value in the best solution.
- Return type:
- See:
- See:
- LSExternalContext GetExternalContext()¶
Gets the external function context of this expression. Only allowed if this expression is an external function.
- Since:
9.5
- Returns:
Context of the external function
- Return type:
- bool IsViolated()¶
Returns true if the given expression is violated in the best solution found by the solver. An expression can be violated in 3 cases:
It is a constraint and its value is 0.
It is a double and its value is NaN (Not A Number).
It is an integer or boolean with no valid value (arithmetic or out of bounds exception).
Only allowed in states
Paused
orStopped
. This method is a shortcut forLSSolution.IsViolated(LSExpression)
.- Since:
5.5
- Returns:
True if this expression is violated in the best solution.
- Return type:
bool
- bool IsUndefined()¶
Returns true if the given expression has an undefined value in the best solution found by the solver. An expression can be undefined in 4 cases:
It is a double and its value is NaN (Not a Number).
It is an integer or boolean with no valid value (arithmetic or out of bounds exception).
It is an interval with at least one undefined bound.
It is the result of any ill-defined operation (at with out of bounds index or operations on undefined values for instance).
Only allowed in states
Paused
orStopped
. This method is a shortcut forLSSolution.IsUndefined(LSExpression)
.- Since:
7.0
- Returns:
True if this expression has an undefined value in the best solution.
- Return type:
bool
- void SetName(string name)¶
Sets the name of this expression. Only allowed in state
Modeling
. The name cannot be empty. Two operators of the model cannot share the same name. Useful for debugging or logging purposes.- Arguments:
name (string) – Name.
- bool IsNamed()¶
Returns true if this expression has a name, and false otherwise.
- Returns:
True if named.
- Return type:
bool
- string GetName()¶
Gets the name of this expression or the empty string if no name has been set.
- Returns:
Name.
- Return type:
string
- string ToString()¶
Returns a string representation of this expression. This representation provides the index of the expression, its type, and its name (if any). Useful for debugging or logging purposes.
- Returns:
String representation.
- Return type:
string
Overloaded operators and indexers¶
- LSExpression operator +(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator +(LSExpression expr1, long expr2)
- LSExpression operator +(LSExpression expr1, double expr2)
- LSExpression operator +(long expr1, LSExpression expr2)
- LSExpression operator +(double expr1, LSExpression expr2)
Creates a new
LSOperator.Sum
expression. It is a shortcut formodel.CreateExpression(LSOperator.Sum, expr1, expr2)
.- Arguments:
expr1 – First operand. Can be an LSExpression, an integer or a double.
expr2 – Second operand. Can be an LSExpression, an integer or a double.
- Returns:
A new
LSOperator.Sum
expression- Return type:
- LSExpression operator -(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator -(LSExpression expr1, long expr2)
- LSExpression operator -(LSExpression expr1, double expr2)
- LSExpression operator -(long expr1, LSExpression expr2)
- LSExpression operator -(double expr1, LSExpression expr2)
Creates a new
LSOperator.Sub
expression. It is a shortcut formodel.CreateExpression(LSOperator.Sub, expr1, expr2)
.- Arguments:
expr1 – First operand. Can be an LSExpression, an integer or a double.
expr2 – Second operand. Can be an LSExpression, an integer or a double.
- Returns:
A new
LSOperator.Sub
expression- Return type:
- LSExpression operator *(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator *(LSExpression expr1, long expr2)
- LSExpression operator *(LSExpression expr1, double expr2)
- LSExpression operator *(long expr1, LSExpression expr2)
- LSExpression operator *(double expr1, LSExpression expr2)
Creates a new
LSOperator.Prod
expression. It is a shortcut formodel.CreateExpression(LSOperator.Prod, expr1, expr2)
.- Arguments:
expr1 – First operand. Can be an LSExpression, an integer or a double.
expr2 – Second operand. Can be an LSExpression, an integer or a double.
- Returns:
A new
LSOperator.Prod
expression- Return type:
- LSExpression operator /(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator /(LSExpression expr1, long expr2)
- LSExpression operator /(LSExpression expr1, double expr2)
- LSExpression operator /(long expr1, LSExpression expr2)
- LSExpression operator /(double expr1, LSExpression expr2)
Creates a new
LSOperator.Div
expression. It is a shortcut formodel.CreateExpression(LSOperator.Div, expr1, expr2)
.- Arguments:
expr1 – First operand. Can be an LSExpression, an integer or a double.
expr2 – Second operand. Can be an LSExpression, an integer or a double.
- Returns:
A new
LSOperator.Div
expression- Return type:
- LSExpression operator %(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator %(LSExpression expr1, long expr2)
- LSExpression operator %(long expr1, LSExpression expr2)
Creates a new
LSOperator.Mod
expression. It is a shortcut formodel.CreateExpression(LSOperator.Mod, expr1, expr2)
.- Arguments:
expr1 – First operand. Can be an LSExpression or an integer.
expr2 – Second operand. Can be an LSExpression or an integer.
- Returns:
A new
LSOperator.Mod
expression- Return type:
- LSExpression operator !(LSExpression expr)¶
Creates a new
LSOperator.Not
expression. It is a shortcut formodel.create_expression(LSOperator.Not, expr)
.- Returns:
A new
LSOperator.Not
expression- Return type:
- LSExpression operator &(LSExpression expr1, LSExpression expr2)¶
Creates a new
LSOperator.And
expression. It is a shortcut formodel.CreateExpression(LSOperator.And, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.And
expression- Return type:
- LSExpression operator |(LSExpression expr1, LSExpression expr2)¶
Creates a new
LSOperator.Or
expression. It is a shortcut formodel.CreateExpression(LSOperator.Or, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Or
expression- Return type:
- LSExpression operator ^(LSExpression expr1, LSExpression expr2)¶
Creates a new
LSOperator.Xor
expression. It is a shortcut formodel.CreateExpression(LSOperator.Xor, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.And
expression- Return type:
- LSExpression operator ==(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator ==(LSExpression expr1, long expr2)
- LSExpression operator ==(LSExpression expr1, double expr2)
- LSExpression operator ==(long expr1, LSExpression expr2)
- LSExpression operator ==(double expr1, LSExpression expr2)
Creates a new
LSOperator.Eq
expression. It is a shortcut formodel.CreateExpression(LSOperator.Eq, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Eq
expression- Return type:
- LSExpression operator !=(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator !=(LSExpression expr1, long expr2)
- LSExpression operator !=(LSExpression expr1, double expr2)
- LSExpression operator !=(long expr1, LSExpression expr2)
- LSExpression operator !=(double expr1, LSExpression expr2)
Creates a new
LSOperator.Neq
expression. It is a shortcut formodel.CreateExpression(LSOperator.Neq, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Neq
expression- Return type:
- LSExpression operator >=(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator >=(LSExpression expr1, long expr2)
- LSExpression operator >=(LSExpression expr1, double expr2)
- LSExpression operator >=(long expr1, LSExpression expr2)
- LSExpression operator >=(double expr1, LSExpression expr2)
Creates a new
LSOperator.Geq
expression. It is a shortcut formodel.CreateExpression(LSOperator.Geq, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Geq
expression- Return type:
- LSExpression operator <=(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator <=(LSExpression expr1, long expr2)
- LSExpression operator <=(LSExpression expr1, double expr2)
- LSExpression operator <=(long expr1, LSExpression expr2)
- LSExpression operator <=(double expr1, LSExpression expr2)
Creates a new
LSOperator.Leq
expression. It is a shortcut formodel.CreateExpression(LSOperator.Leq, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Neq
expression- Return type:
- LSExpression operator >(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator >(LSExpression expr1, long expr2)
- LSExpression operator >(LSExpression expr1, double expr2)
- LSExpression operator >(long expr1, LSExpression expr2)
- LSExpression operator >(double expr1, LSExpression expr2)
Creates a new
LSOperator.Gt
expression. It is a shortcut formodel.CreateExpression(LSOperator.Gt, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Gt
expression- Return type:
- LSExpression operator <(LSExpression expr1, LSExpression expr2)¶
- LSExpression operator <(LSExpression expr1, long expr2)
- LSExpression operator <(LSExpression expr1, double expr2)
- LSExpression operator <(long expr1, LSExpression expr2)
- LSExpression operator <(double expr1, LSExpression expr2)
Creates a new
LSOperator.Lt
expression. It is a shortcut formodel.CreateExpression(LSOperator.Lt, expr1, expr2)
.- Arguments:
expr1 (LSExpression) – First operand.
expr2 (LSExpression) – Second operand.
- Returns:
A new
LSOperator.Lt
expression- Return type:
- LSExpression this(long index)¶
- LSExpression this(LSExpression index)
- LSExpression this(LSExpression index1, LSExpression index2)
- LSExpression this(LSExpression index1, long index2)
- LSExpression this(long index1, LSExpression index2)
- LSExpression this(long index1, long index2)
Creates a new
LSOperator.At
expression. It is a shortcut formodel.CreateExpression(LSOperator.At, index, ...)
.- Returns:
A new
LSOperator.At
expression- Return type: