# 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: LSModel LSOperator

## Summary¶

 GetModel Returns the model object associated to this expression. GetLocalSolver Returns the LocalSolver object associated to this expression. GetOperator Gets the operator of this expression. GetIndex Gets the index of this expression in the model. IsConstant Returns true if this expression is typed as constant in the model, false otherwise. IsDecision Returns true if this expression is typed as decision in the model, false otherwise. IsConstraint Returns true if this expression is tagged as constraint in the model, false otherwise. IsObjective Returns true if this expression is tagged as objective in the model, false otherwise. IsDouble Returns true if this expression is a double, false otherwise. IsInt Returns true if this expression is an integer, false otherwise. IsBool Returns true if this expression is a boolean (ie 0 or 1), false otherwise. IsArray Returns true if this expression is an array, false otherwise. IsCollection Returns true if this expression is a collection (list or set), false otherwise. IsFunction Returns true if this expression is a function, false otherwise. AddOperand Adds the given operand to this expression. AddOperands Add the given operands to the expression. GetOperand Gets the operand with the given index. SetOperand Replaces the operand of the given index. GetNbOperands Gets the number of operands of this expression. SetValue Sets the value of this expression in the current solution found by the solver. SetIntValue Sets the value of this expression in the current solution found by the solver. SetDoubleValue Sets the value of this expression in the current solution found by the solver. GetValue Gets the value of this expression in the best solution found by the solver. GetIntValue Gets the value of this expression in the best solution found by the solver. GetDoubleValue Gets the value of this expression in the best solution found by the solver. GetCollectionValue Gets the value of this expression in the best solution found by the solver. GetArrayValue Gets the value of this expression in the best solution found by the solver. GetExternalContext Gets the external function context of this expression. GetBlackBoxContext Gets the black-box function context of this expression. IsViolated Returns true if the given expression is violated in the best solution found by the solver. IsUndefined Returns true if the given expression has an undefined value in the best solution found by the solver. SetName Sets the name of this expression. IsNamed Returns true if this expression has a name, and false otherwise. GetName Gets the name of this expression or the empty string if no name has been set. ToString Returns a string representation of this expression. GetInfo Returns useful info about this expression (according to the state of LocalSolver).
 operator + Creates a new LSOperator.Sum expression. operator - Creates a new LSOperator.Sub expression. operator * Creates a new LSOperator.Prod expression. operator / Creates a new LSOperator.Div expression. operator % Creates a new LSOperator.Mod expression. operator ! Creates a new LSOperator.Not expression. operator & Creates a new LSOperator.And expression. operator | Creates a new LSOperator.Or expression. operator ^ Creates a new LSOperator.Xor expression. operator == Creates a new LSOperator.Eq expression. operator != Creates a new LSOperator.Neq expression. operator >= Creates a new LSOperator.Geq expression. operator <= Creates a new LSOperator.Leq expression. operator > Creates a new LSOperator.Gt expression. operator < Creates a new LSOperator.Lt expression. this Creates a new LSOperator.At expression.

## Instance methods¶

LSModel GetModel()

Returns the model object associated to this expression.

Returns: LSModel object LSModel
LocalSolver GetLocalSolver()

Returns the LocalSolver object associated to this expression.

Returns: LocalSolver object LocalSolver
LSOperator GetOperator()

Gets the operator of this expression.

Returns: Operator. LSOperator
int GetIndex()

Gets the index of this expression in the model.

Returns: Index in the model. int
bool IsConstant()

Returns true if this expression is typed as constant in the model, false otherwise.

Returns: True if typed as constant. bool
bool IsDecision()

Returns true if this expression is typed as decision in the model, false otherwise.

Returns: True if typed as decision. bool
bool IsConstraint()

Returns true if this expression is tagged as constraint in the model, false otherwise.

Returns: True if tagged as constraint. bool
bool IsObjective()

Returns true if this expression is tagged as objective in the model, false otherwise.

Returns: True if tagged as objective. bool
bool IsDouble()

Returns true if this expression is a double, false otherwise. Only allowed in states Paused or Stopped.

Since: 3.0 True if the expression is a double. bool
bool IsInt()

Returns true if this expression is an integer, false otherwise. Only allowed in states Paused or Stopped. Note that a boolean is also an integer.

Since: 3.0 True if the expression is an integer. bool
bool IsBool()

Returns true if this expression is a boolean (ie 0 or 1), false otherwise. Only allowed in states Paused or Stopped.

Since: 3.0 True if the expression is a boolean. bool
bool IsArray()

Returns true if this expression is an array, false otherwise. Only allowed in states Paused or Stopped.

Since: 3.1 True if the expression is an array. bool
bool IsCollection()

Returns true if this expression is a collection (list or set), false otherwise. Only allowed in states Paused or Stopped.

Since: 5.5 True if the expression is a collection. bool
bool IsFunction()

Returns true if this expression is a function, false otherwise. Only allowed in states Paused or Stopped.

Since: 6.0 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. Operand. LSExpression
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. 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 as LSExpression.SetIntValue(long).

Arguments: value (long) – Value assigned to this expression. LSSolution.SetIntValue(LSExpression, long) LSExpression.IsInt LSExpression.IsBool
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 as LSExpression.SetDoubleValue(double).

Arguments: value (double) – Value assigned to this expression. LSSolution.SetDoubleValue(LSExpression, double) LSExpression.IsDouble
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 value (long) – Value assigned to this expression. LSSolution.SetIntValue LSExpression.IsInt LSExpression.IsBool
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 value (double) – Value assigned to this expression. LSSolution.SetDoubleValue LSExpression.IsDouble
long GetValue()

Gets the value of this expression in the best solution found by the solver. Only allowed in states Paused or Stopped. 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 as LSExpression.GetIntValue(LSExpression).

Returns: Value in the best solution. long LSSolution.GetValue LSExpression.IsInt LSExpression.IsBool
long GetIntValue()

Gets the value of this expression in the best solution found by the solver. Only allowed in states Paused or Stopped. Only allowed if this expression is an integer or a boolean.

This method is a shortcut for LSSolution.GetIntValue(LSExpression).

Since: 3.0 Value in the best solution. long LSSolution.GetIntValue LSExpression.IsInt LSExpression.IsBool
double GetDoubleValue()

Gets the value of this expression in the best solution found by the solver. Only allowed in states Paused or Stopped. Only allowed if this expression is a double.

This method is a shortcut for LSSolution.GetDoubleValue(LSExpression).

Since: 3.0 Value in the best solution. double LSSolution.GetDoubleValue LSExpression.IsDouble
LSCollection GetCollectionValue()

Gets the value of this expression in the best solution found by the solver. Only allowed in states Paused or Stopped. Only allowed if this expression is a collection (list).

This method is a shortcut for LSSolution.GetCollectionValue(LSExpression).

Since: 5.5 Value in the best solution. LSCollection LSSolution.GetCollectionValue LSExpression.IsCollection
LSArray GetArrayValue()

Gets the value of this expression in the best solution found by the solver. Only allowed in states Paused or Stopped. Only allowed if this expression is an array.

This method is a shortcut for LSSolution.GetArrayValue(LSExpression).

Since: 7.5 Value in the best solution. LSArray LSSolution.GetCollectionValue LSExpression.IsArray
LSExternalContext GetExternalContext()

Gets the external function context of this expression. Only allowed if this expression is an external function.

Since: 9.5 Context of the external function LSExternalContext
LSBlackBoxContext GetBlackBoxContext()

Gets the black-box function context of this expression. Only allowed if this expression is a black-box function.

Since: 9.5 Context of the black-box function LSBlackBoxContext
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 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 or Stopped. This method is a shortcut for LSSolution.IsViolated(LSExpression).

Since: 5.5 True if this expression is violated in the best solution. 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 2 cases:

• It is a a double and its value is NaN (NotANumber).
• It is an integer or boolean with no valid value (arithmetic or out of bounds exception).

Only allowed in states Paused or Stopped. This method is a shortcut for LSSolution.IsUndefined(LSExpression).

Since: 7.0 True if this expression has an undefined value in the best solution. 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. bool
string GetName()

Gets the name of this expression or the empty string if no name has been set.

Returns: Name. 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. string
string GetInfo()

Returns useful info about this expression (according to the state of LocalSolver). Useful for debugging or logging purposes.

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 for model.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. A new LSOperator.Sum expression LSExpression
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 for model.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. A new LSOperator.Sub expression LSExpression
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 for model.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. A new LSOperator.Prod expression LSExpression
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 for model.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. A new LSOperator.Div expression LSExpression
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 for model.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. A new LSOperator.Mod expression LSExpression
LSExpression operator !(LSExpression expr)

Creates a new LSOperator.Not expression. It is a shortcut for model.create_expression(LSOperator.Not, expr).

Returns: A new LSOperator.Not expression LSExpression
LSExpression operator &(LSExpression expr1, LSExpression expr2)

Creates a new LSOperator.And expression. It is a shortcut for model.CreateExpression(LSOperator.And, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.And expression LSExpression
LSExpression operator |(LSExpression expr1, LSExpression expr2)

Creates a new LSOperator.Or expression. It is a shortcut for model.CreateExpression(LSOperator.Or, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Or expression LSExpression
LSExpression operator ^(LSExpression expr1, LSExpression expr2)

Creates a new LSOperator.Xor expression. It is a shortcut for model.CreateExpression(LSOperator.Xor, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.And expression LSExpression
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 for model.CreateExpression(LSOperator.Eq, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Eq expression LSExpression
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 for model.CreateExpression(LSOperator.Neq, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Neq expression LSExpression
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 for model.CreateExpression(LSOperator.Geq, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Geq expression LSExpression
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 for model.CreateExpression(LSOperator.Leq, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Neq expression LSExpression
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 for model.CreateExpression(LSOperator.Gt, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Gt expression LSExpression
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 for model.CreateExpression(LSOperator.Lt, expr1, expr2).

Arguments: expr1 (LSExpression) – First operand. expr2 (LSExpression) – Second operand. A new LSOperator.Lt expression LSExpression
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 for model.CreateExpression(LSOperator.At, index, ...).

Returns: A new LSOperator.At expression LSExpression