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

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

Functions
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 this 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.
Overloaded operators
operator+ Creates a new O_Sum expression.
operator- Creates a new O_Sub expression.
operator* Creates a new O_Prod expression.
operator% Creates a new O_Mod expression.
operator/ Creates a new O_Div expression.
operator! Creates a new O_Not expression.
operator&& Creates a new O_And expression.
operator|| Creates a new O_Or expression.
operator^ Creates a new O_Xor expression.
operator== Creates a new O_Eq expression.
operator!= Creates a new O_Neq expression.
operator>= Creates a new O_Geq expression.
operator<= Creates a new O_Leq expression.
operator> Creates a new O_Gt expression.
operator< Creates a new O_Lt expression.
operator[] Creates a new O_At expression.
operator+= Sums the given operand with the current expression.
operator*= Multiply the given operand with the current expression.
operator&= Creates a logical AND between the current expression and the given operand.
operator^= Creates a logical XOR between the current expression and the given operand.
operator|= Creates a logical OR between the current expression and the given operand.
operator() Creates a O_Call expression with the given operands as arguments.

Functions

LSOperator localsolver::LSExpression::getOperator()
const

Gets the operator of this expression.

Return:Operator.

int localsolver::LSExpression::getIndex()
const

Gets the index of this expression in the model.

Return:Index in the model.

bool localsolver::LSExpression::isConstant()
const

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

Return:True if typed as constant.

bool localsolver::LSExpression::isDecision()
const

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

Return:True if typed as decision.

bool localsolver::LSExpression::isConstraint()
const

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

Return:True if tagged as constraint.

bool localsolver::LSExpression::isObjective()
const

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

Return:True if tagged as objective.

bool localsolver::LSExpression::isDouble()
const

Returns true if this expression is a double, false otherwise.

Only allowed in states S_Paused or S_Stopped.

Return:True if the expression is a double.
Since:3.0

bool localsolver::LSExpression::isInt()
const

Returns true if this expression is an integer, false otherwise.

Only allowed in states S_Paused or S_Stopped. Note that a boolean is also an integer.

Return:True if the expression is an integer.
Since:3.0

bool localsolver::LSExpression::isBool()
const

Returns true if this expression is a boolean (ie 0 or 1), false otherwise.

Only allowed in states S_Paused or S_Stopped.

Return:True if the expression is a boolean.
Since:3.0

bool localsolver::LSExpression::isArray()
const

Returns true if this expression is an array, false otherwise.

Only allowed in states S_Paused or S_Stopped.

Return:True if the expression is an array.
Since:3.1

bool localsolver::LSExpression::isCollection()
const

Returns true if this expression is a collection (list or set), false otherwise.

Only allowed in states S_Paused or S_Stopped.

Return:True if the expression is a collection.
Since:5.5

bool localsolver::LSExpression::isFunction()
const

Returns true if this expression is a function, false otherwise.

Only allowed in states S_Paused or S_Stopped.

Return:True if the expression is a function.
Since:6.0

void localsolver::LSExpression::addOperand(const LSExpression &expr)

Adds the given operand to this expression.

Only allowed in state S_Modeling.

Parameters:expr - Operand to add.

void localsolver::LSExpression::addOperand(int constant)

Add the given constant operand to this expression.

Only allowed in state S_Modeling.

Parameters:constant - Constant operand to add.

void localsolver::LSExpression::addOperand(lsint constant)

Add the given constant operand to this expression.

Only allowed in state S_Modeling.

Parameters:constant - Constant operand to add.

void localsolver::LSExpression::addOperand(lsdouble constant)

Add the given constant operand to this expression.

Only allowed in state S_Modeling.

Since:3.0
Parameters:constant - Constant operand to add.

template <typename… TN>
void localsolver::LSExpression::addOperands(TN... operands)

Add the given operands to this expression.

Only allowed in state S_Modeling.

The operands can be doubles, integers or previously declared LSExpressions. It is also possible to use this method with iterators. In that case, you have to call this method with 2 arguments exactly that must be iterators of the same type, pointing respectively to the initial and final positions of the operands.

Templates:TN - types of the operands to add. Types allowed: constant types, LSExpression or iterators.
Parameters:operands - operands.

template <typename T0, typename T1>
void localsolver::LSExpression::addOperands(T0 begin, T1 end)

Add the given operands to this expression.

Only allowed in state S_Modeling.

Since:

5.5

Parameters:
  • begin - iterator to the initial position of the operands to add.
  • end - iterator to the final position of the operands to add.

LSExpression localsolver::LSExpression::getOperand(int operandIndex)
const

Gets the operand with the given index.

Return:Operand.
Parameters:operandIndex - Index of the operand.

void localsolver::LSExpression::setOperand(int operandIndex, LSExpression operand)

Replaces the operand of the given index.

Parameters:
  • operandIndex - Index of the operand to change
  • operand - New operand

void localsolver::LSExpression::setOperand(int operandIndex, lsint constant)

Replaces the operand of the given index.

Parameters:
  • operandIndex - Index of the operand to change.
  • constant - New constant operand.

void localsolver::LSExpression::setOperand(int operandIndex, lsdouble constant)

Replaces the operand of the given index.

Since:

3.0

Parameters:
  • operandIndex - Index of the operand to change.
  • constant - New constant operand.

int localsolver::LSExpression::getNbOperands()
const

Gets the number of operands of this expression.

Return:Number of operands.

void localsolver::LSExpression::setValue(lsint value)

Sets the value of this expression in the current solution found by the solver.

Only allowed for decisions. Only allowed in state S_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#setValue(const LSExpression&, lsint) and has the same behavior as setIntValue(lsint).

See:LSSolution::setValue(const LSExpression&, lsint)
See:isInt()
See:isBool()
Parameters:value - Value assigned to this expression.

void localsolver::LSExpression::setValue(lsdouble value)

Sets the value of this expression in the current solution found by the solver.

Only allowed for decisions. Only allowed in state S_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#setValue(const LSExpression&, lsdouble) and has the same behavior as setDoubleValue(lsdouble).

See:LSSolution::setValue(const LSExpression &expr, lsdouble value)
See:isDouble()
Parameters:value - Value assigned to this expression.

void localsolver::LSExpression::setIntValue(lsint value)

Sets the value of this expression in the current solution found by the solver.

Only allowed for decisions. Only allowed in state S_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#setValue(const LSExpression&, lsint).

See:LSSolution::setIntValue(const LSExpression&, lsint)
See:isInt()
See:isBool()
Since:3.0
Parameters:value - Value assigned to this expression.

void localsolver::LSExpression::setDoubleValue(lsdouble value)

Sets the value of this expression in the current solution found by the solver.

Only allowed for decisions. Only allowed in state S_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#setValue(const LSExpression&, lsdouble).

See:LSSolution::setDoubleValue(const LSExpression&, lsdouble)
See:isDouble()
Since:3.0
Parameters:value - Value assigned to this expression.

lsint localsolver::LSExpression::getValue()
const

Gets the value of this expression in the best solution found by the solver.

Only allowed in states S_Paused or S_Stopped. Only allowed if this expression is an integer or a boolean. This method is a shortcut for LSSolution#getValue(const LSExpression&).

Return:Value in the best solution.
See:LSSolution::getValue(const LSExpression&)
See:isInt()
See:isBool()

lsint localsolver::LSExpression::getIntValue()
const

Gets the value of this expression in the best solution found by the solver.

Only allowed in states S_Paused or S_Stopped. Only allowed if this expression is an integer or a boolean. This method is a shortcut for LSSolution#getIntValue(const LSExpression&).

Return:Value in the best solution.
See:LSSolution::getIntValue(const LSExpression&)
See:isInt()
See:isBool()
Since:3.0

lsdouble localsolver::LSExpression::getDoubleValue()
const

Gets the value of this expression in the best solution found by the solver.

Only allowed in states S_Paused or S_Stopped. Only allowed if this expression is a double. This method is a shortcut for LSSolution#getDoubleValue(const LSExpression&).

Return:Value in the best solution.
See:LSSolution::getDoubleValue(const LSExpression&)
See:isDouble()
Since:3.0

LSCollection localsolver::LSExpression::getCollectionValue()
const

Gets the value of this expression in the best solution found by the solver.

Only allowed in states S_Paused or S_Stopped. Only allowed if this expression is a collection (list or set). This method is a shortcut for LSSolution#getCollectionValue(const LSExpression&).

Return:Value in the best solution.
See:LSExpression::isCollection
See:LSSolution::getCollectionValue(const LSExpression&)
Since:5.5

LSArray localsolver::LSExpression::getArrayValue()
const

Gets the value of this expression in the best solution found by the solver.

Only allowed in states S_Paused or S_Stopped. Only allowed if this expression is an array. This method is a shortcut for LSSolution#getArrayValue(const LSExpression&).

Return:Value in the best solution.
See:LSExpression::isArray
See:LSSolution::getArrayValue(const LSExpression&)
Since:7.5

LSExternalContext localsolver::LSExpression::getExternalContext()
const

Gets the external function context of this expression.

Only allowed if this expression is an external function.

Return:Context of the external function.
See:LSExternalContext
Since:9.5

LSBlackBoxContext localsolver::LSExpression::getBlackBoxContext()
const

Gets the black-box function context of this expression.

Only allowed if this expression is a black-box function.

Return:Context of the black-box function.
See:LSBlackBoxContext
Since:9.5

bool localsolver::LSExpression::isViolated()
const

Returns true if the given expression is violated in the best solution found by the solver.

An expression can be violated in 3 cases:

  1. it is a constraint and its value is 0
  2. it is a a double objective and its value is NaN (NotANumber)
  3. it is a constraint with no valid value (arithmetic or out of bounds exception or NaN operands). Note that only constraints and objectives can be violated. Other expression can have undefined value provided that it does not impact a constraint or objective.

Only allowed in states S_Paused or S_Stopped. This method is a shortcut for LSSolution#isViolated().

Return:True if this expression is violated in the best solution.
Since:5.5
See:isUndefined()

bool localsolver::LSExpression::isUndefined()
const

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:

  1. it is a a double and its value is NaN (NotANumber)
  2. it is an integer or boolean with no valid value (arithmetic or out of bounds exception). Only allowed in states S_Paused or S_Stopped. This method is a shortcut for LSSolution#isUndefined(const LSExpression&).

Return:True if this expression has an undefined value in the best solution.
Since:7.0

void localsolver::LSExpression::setName(const std::string &name)

Sets the name of this expression.

Only allowed in state S_Modeling. The name cannot be empty. Two operators of the model cannot share the same name. Useful for debugging or logging purposes.

Parameters:name - Name.

bool localsolver::LSExpression::isNamed()
const

Returns true if this expression has a name, and false otherwise.

Return:True if named.

const std::string localsolver::LSExpression::getName()
const

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

Return:Name.

std::string localsolver::LSExpression::toString()
const

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.

Return:String representation.

template <typename T>
LSExpression localsolver::LSExpression::operator+(T operand)

Creates a new O_Sum expression.

It is a shortcut for model.createExpression(O_Sum, this, operand).

Return:A new O_Sum expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator-(T operand)

Creates a new O_Sub expression.

It is a shortcut for model.createExpression(O_Sub, this, operand).

Return:A new O_Sub expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator*(T operand)

Creates a new O_Prod expression.

It is a shortcut for model.createExpression(O_Prod, this, operand).

Return:A new O_Prod expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator%(T operand)

Creates a new O_Mod expression.

It is a shortcut for model.createExpression(O_Mod, this, operand).

Return:A new O_Mod expression.
Parameters:operand - Operand. Can be an LSExpression or an integer.

template <typename T>
LSExpression localsolver::LSExpression::operator/(T operand)

Creates a new O_Div expression.

It is a shortcut for model.createExpression(O_Div, this, operand).

Return:A new O_Div expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

LSExpression localsolver::LSExpression::operator!()

Creates a new O_Not expression.

It is a shortcut for model.createExpression(O_Not, this).

Return:A new O_Not expression.

template <typename T>
LSExpression localsolver::LSExpression::operator&&(T operand)

Creates a new O_And expression.

It is a shortcut for model.createExpression(O_And, this, operand).

Return:A new O_And expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename T>
LSExpression localsolver::LSExpression::operator||(T operand)

Creates a new O_Or expression.

It is a shortcut for model.createExpression(O_Or, this, operand).

Return:A new O_Or expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename T>
LSExpression localsolver::LSExpression::operator^(T operand)

Creates a new O_Xor expression.

It is a shortcut for model.createExpression(O_Xor, this, operand).

Return:A new O_Xor expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename T>
LSExpression localsolver::LSExpression::operator==(T operand)

Creates a new O_Eq expression.

It is a shortcut for model.createExpression(O_Eq, this, operand).

Return:A new O_Eq expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator!=(T operand)

Creates a new O_Neq expression.

It is a shortcut for model.createExpression(O_Neq, this, operand).

Return:A new O_Neq expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator>=(T operand)

Creates a new O_Geq expression.

It is a shortcut for model.createExpression(O_Geq, this, operand).

Return:A new O_Geq expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator<=(T operand)

Creates a new O_Leq expression.

It is a shortcut for model.createExpression(O_Leq, this, operand).

Return:A new O_Leq expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator>(T operand)

Creates a new O_Gt expression.

It is a shortcut for model.createExpression(O_Gt, this, operand).

Return:A new O_Gt expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator<(T operand)

Creates a new O_Lt expression.

It is a shortcut for model.createExpression(O_Lt, this, operand).

Return:A new O_Lt expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator[](T operand)

Creates a new O_At expression.

It is a shortcut for model.createExpression(O_At, this, operand).

Return:A new O_At expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator+=(T operand)

Sums the given operand with the current expression.

If the current expression is already of type O_Sum, the given operand is simply pushed on the current list of operands. A new O_Sum expression is created otherwise.

Return:Return the current expression or a new O_Sum expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator*=(T operand)

Multiply the given operand with the current expression.

If the current expression is already of type O_Prod, the given operand is simply pushed on the current list of operands. A new O_Prod expression is created otherwise.

Return:Return the current expression or a new O_Prod expression.
Parameters:operand - Operand. Can be an LSExpression, an integer or a double.

template <typename T>
LSExpression localsolver::LSExpression::operator&=(T operand)

Creates a logical AND between the current expression and the given operand.

If the current expression is already of type O_And, the given operand is simply pushed on the current list of operands. A new O_And expression is created otherwise.

Return:Return the current expression or a new O_And expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename T>
LSExpression localsolver::LSExpression::operator^=(T operand)

Creates a logical XOR between the current expression and the given operand.

If the current expression is already of type O_Xor, the given operand is simply pushed on the current list of operands. A new O_Xor expression is created otherwise.

Return:Return the current expression or a new O_Xor expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename T>
LSExpression localsolver::LSExpression::operator|=(T operand)

Creates a logical OR between the current expression and the given operand.

If the current expression is already of type O_Or, the given operand is simply pushed on the current list of operands. A new O_Or expression is created otherwise.

Return:Return the current expression or a new O_Or expression.
Parameters:operand - Operand. Can be an LSExpression or a boolean.

template <typename… TN>
LSExpression localsolver::LSExpression::operator()(TN... operands)

Creates a O_Call expression with the given operands as arguments.

It is a shortcut for model.createExpression(O_Call, this, operands).

Templates:TN - types of the operands to add. Types allowed: constant types, LSExpression or iterators.
Parameters:operands - operands.