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

Summary

Methods

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.

isConstant

Returns true if this expression is typed as constant in the model, 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.

isDouble

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

isCollection

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

isArray

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

isFunction

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

isRange

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

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.

isNamed

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

Overloaded operators

pos

Returns the same expression without change.

neg

Returns a new expression that is the opposite of the current expression.

add

Returns a new expression that is the sum of the current expression and the argument other.

sub

Returns a new expression that is the substraction of the current expression and the argument other.

mul

Returns a new expression that is the product of the current expression and the argument other.

div

Returns a new expression that is the division of the current expression and the argument other.

mod

Returns a new expression that is the remainder of the integer division of the current expression and the argument other.

not

Returns a new expression that is the negative of the current expression.

and

Returns a new expression that is the logical and of the current expression and the argument other.

or

Returns a new expression that is the logical or of the current expression and the argument other.

lt

Returns a new strictly lower expression between the current expression and the argument other.

leq

Returns a new lower or equal expression between the current expression and the argument other.

gt

Returns a new strictly greater expression between the current expression and the argument other.

geq

Returns a new greater or equal expression between the current expression and the argument other.

eq

Returns a new equal expression between the current expression and the argument other.

neq

Returns a new not equal expression between the current expression and the argument other.

range

Returns a new range expression between the current expression and the argument other.

ternary

Returns a new if-then-else expression (also called the ternary operator).

linkable

This operator is applied when the <- sign is used on an LSExpression.

assignable

This operator is applied when the = sign is used on an LSExpression.

modify

This operator is called when an expression is used with one of the three special modifiers : MINIMIZE, MAXIMIZE or CONSTRAINT.

Fields

LSExpression.operator

Operator of this expression returned as a string.

Return type

string

LSExpression.index

Index of this expression in the model.

Return type

int

LSExpression.operands

This field returns a readonly map-like structure containing all the operands of this expression. The returned structure has the following features:

  • A count field to get the number of operands, e.g. operands.count().

  • An overloaded index [] operator to get a specific operand, e.g. operands[0]. The index must be between 0 and operands.count() - 1. Accessing a value outside these bounds throws an exception.

  • An overloaded iterator operator to iterate over the operands with a for statement.

Return type

map-like structure containing objects of type LSExpression.

LSExpression.nbOperands

Number of operands of this expression.

Return type

int

LSExpression.name

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

This attribute is read-write but it can only be changed in state MODELING.

Return type

String

LSExpression.value

Value of this expression in the best solution found by the solver. This method is a shortcut for LSSolution.getValue() and LSSolution.setValue().

The type of the returned value depends on the type of the expression. It can be a number if the expression has an integer or double value, an array if the expression is an array, or a collection if the expression is a list or a set decision.

This attribute is read-write but it can only be changed in state STOPPED and queried in states PAUSED or STOPPED.

Return type

a number, an array or a collection depending on the type of the expression.

LSExpression.context

External function context of this expression. Only set and allowed if this expression is an external function.

Return type

LSExternalContext

Methods

LSExpression.isDecision()

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

Return type

bool

LSExpression.isConstraint()

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

Return type

bool

LSExpression.isObjective()

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

Return type

bool

LSExpression.isConstant()

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

Return type

bool

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

Return type

bool

LSExpression.isBool()

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

Return type

bool

LSExpression.isDouble()

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

Return type

bool

LSExpression.isCollection()

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

Return type

bool

LSExpression.isArray()

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

Return type

bool

LSExpression.isFunction()

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

Return type

bool

LSExpression.isRange()

Returns true if this expression is a range, false otherwise. Only allowed in states PAUSED or STOPPED.

Return type

bool

LSExpression.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 or STOPPED. This method is a shortcut for LSSolution.isViolated().

Return type

bool

LSExpression.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 or STOPPED. This method is a shortcut for LSSolution.isUndefined().

Return type

bool

LSExpression.isNamed()

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

Return type

bool

Overloaded operators

LSExpression.pos()

Returns the same expression without change.

Return type

LSExpression

LSExpression.neg()

Returns a new expression that is the opposite of the current expression.

Return type

LSExpression

LSExpression.add(other)
LSExpression.radd(other)

Returns a new expression that is the sum of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the addition (left or right).

Note that in some cases, this operator can return one of the two operands rather than creating a new expression (sum with constant 0).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.sub(other)
LSExpression.rsub(other)

Returns a new expression that is the substraction of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the substraction (left or right).

Note that in some cases, this operator can return the left operand instead of creating a new expression if the right operand is the constant 0.

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.mul(other)
LSExpression.rmul(other)

Returns a new expression that is the product of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the product (left or right).

Note that in some cases, this operator can return one of the two operands rather than creating a new expression (product with constant 1) or returns the lsexpression representing the constant 0 (product with constant 0).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.div(other)
LSExpression.rdiv(other)

Returns a new expression that is the division of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the division (left or right).

Note that in some cases, this operator can return the left operand instead of creating a new expression if the right operand is the constant 1.

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.mod(other)
LSExpression.rmod(other)

Returns a new expression that is the remainder of the integer division of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the modulo (left or right).

Note that in some cases, this operator can return the lsexpression representing the constant 0 if the right operand is the constant 1.

Parameters

other – Can be a bool, an integer or another lsexpression

Return type

LSExpression

LSExpression.not(other)

Returns a new expression that is the negative of the current expression. NOT(a) = 1 - a.

Return type

LSExpression

LSExpression.and(other)
LSExpression.rand(other)

Returns a new expression that is the logical and of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the product (left or right).

Note that in some cases, this operator can return one of the two operands rather than creating a new expression (logical and with constant 1 or true) or returns the lsexpression representing the constant 0 (logical and with constant 0 or false).

Parameters

other – Can be a bool or another lsexpression

Return type

LSExpression

LSExpression.or(other)
LSExpression.ror(other)

Returns a new expression that is the logical or of the current expression and the argument other. This operator exists in two versions depending on the position of the current expression in the product (left or right).

Note that in some cases, this operator can return one of the two operands rather than creating a new expression (logical or with constant 0 or false) or returns the lsexpression representing the constant 1 (logical or with constant 1 or true).

Parameters

other – Can be a bool or another lsexpression

Return type

LSExpression

LSExpression.lt(other)
lsexpressionr.rlt(other)

Returns a new strictly lower expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.leq(other)
LSExpression.rleq(other)

Returns a new lower or equal expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.gt(other)
lsexpressionr.rgt(other)

Returns a new strictly greater expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.geq(other)
LSExpression.rgeq(other)

Returns a new greater or equal expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.eq(other)
lsexpressionr.req(other)

Returns a new equal expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.neq(other)
LSExpression.rneq(other)

Returns a new not equal expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.range(other)
LSExpression.rrange(other)

Returns a new range expression between the current expression and the argument other. This operator exists in two versions depdending on the position of the current expression (left or right).

Parameters

other – Can be a bool, an integer, a double or another lsexpression

Return type

LSExpression

LSExpression.ternary(trueValue, falseValue)

Returns a new if-then-else expression (also called the ternary operator). The current expression is used as the condition and the arguments as true and false values respectively.

Parameters
  • trueValue – Can be a bool, an integer, a double or an lsexpression

  • falseValue – Can be a bool, an integer, a double or an lsexpression

Return type

LSExpression

LSExpression.index(index)

Returns a new at expression. The current expression is used as the array and the argument as the index.

Parameters

index – Can be a bool, an integer or another lspexression

Return type

LSExpression

LSExpression.linkable()

This operator is applied when the <- sign is used on an LSExpression. It only returns the current lsexpression, without changing anything

LSExpression.assignable()

This operator is applied when the = sign is used on an LSExpression. The implemented behavior is to throw an exception. lsexpressions can only be manipulated with the <- operator.

LSExpression.modify({MINIMIZE, MAXIMIZE, CONSTRAINT})

This operator is called when an expression is used with one of the three special modifiers : MINIMIZE, MAXIMIZE or CONSTRAINT. It adds the expression to the list of objectives or constraints of the current model.