This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

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

See

LSOperator

Summary

Methods

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.

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.

Overloaded operators

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

Return type

LSModel

LocalSolver GetLocalSolver()

Returns the LocalSolver object associated to this expression.

Returns

LocalSolver object

Return type

LocalSolver

LSOperator GetOperator()

Gets the operator of this expression.

Returns

Operator.

Return type

LSOperator

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 or Stopped.

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 or Stopped. 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 or Stopped.

Since

3.0

Returns

True if the expression is a boolean.

Return type

bool

bool IsArray()

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

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 or Stopped.

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 or Stopped.

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

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.

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 as LSExpression.SetIntValue(long).

Arguments

value (long) – Value assigned to this expression.

See

LSSolution.SetIntValue(LSExpression, long)

See

LSExpression.IsInt

See

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.

See

LSSolution.SetDoubleValue(LSExpression, double)

See

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

Arguments

value (long) – Value assigned to this expression.

See

LSSolution.SetIntValue

See

LSExpression.IsInt

See

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

Arguments

value (double) – Value assigned to this expression.

See

LSSolution.SetDoubleValue

See

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.

Return type

long

See

LSSolution.GetValue

See

LSExpression.IsInt

See

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

Returns

Value in the best solution.

Return type

long

See

LSSolution.GetIntValue

See

LSExpression.IsInt

See

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

Returns

Value in the best solution.

Return type

double

See

LSSolution.GetDoubleValue

See

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

Returns

Value in the best solution.

Return type

LSCollection

See

LSSolution.GetCollectionValue

See

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

Returns

Value in the best solution.

Return type

LSArray

See

LSSolution.GetCollectionValue

See

LSExpression.IsArray

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

LSExternalContext

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

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 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

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 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.

Returns

A new LSOperator.Sum expression

Return type

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.

Returns

A new LSOperator.Sub expression

Return type

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.

Returns

A new LSOperator.Prod expression

Return type

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.

Returns

A new LSOperator.Div expression

Return type

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.

Returns

A new LSOperator.Mod expression

Return type

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

Return type

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
Returns

A new LSOperator.And expression

Return type

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
Returns

A new LSOperator.Or expression

Return type

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
Returns

A new LSOperator.And expression

Return type

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
Returns

A new LSOperator.Eq expression

Return type

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
Returns

A new LSOperator.Neq expression

Return type

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
Returns

A new LSOperator.Geq expression

Return type

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
Returns

A new LSOperator.Neq expression

Return type

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
Returns

A new LSOperator.Gt expression

Return type

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
Returns

A new LSOperator.Lt expression

Return type

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

Return type

LSExpression