HxExpression Class

class hexaly.optimizer.HxExpression

Mathematical modeling expression. Expressions are used to build the mathematical optimization model associated to Hexaly Optimizer. An expression is composed of an operator (which corresponds to its type) and its operands (which are other expressions of the model).

Summary

Attributes

operator

Operator of the expression.

index

Index of the expression.

value

Value of the expression in the best solution found by the optimizer.

name

Name of the expression in the model.

nb_operands

Number of operands of the expression.

operands

Operands of the expression.

external_context

External context of an external function.

Methods

get_operator

Gets the operator of this expression.

get_index

Gets the index of this expression in the model.

is_constant

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

is_decision

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

is_constraint

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

is_objective

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

is_double

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

is_int

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

is_bool

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

is_interval

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

is_array

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

is_collection

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

is_function

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

add_operand

Add the given operand to this expression.

add_operands

Add the given operands to the expression.

get_operand

Gets the operand with the given index.

set_operand

Replaces the operand of the given index.

get_nb_operands

Returns the number of operands of this expression.

set_value

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

get_value

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

get_external_context

Gets the external function context of this expression.

is_violated

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

is_undefined

Returns true if the given expression has an undefined value in the best solution found by the optimizer.

is_named

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

set_name

Sets the name of this expression.

get_name

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

Special methods

__add__

Creates a new HxOperator.SUM expression.

__sub__

Creates a new HxOperator.SUB expression.

__mul__

Creates a new HxOperator.PROD expression.

__div__

Creates a new HxOperator.DIV expression.

__mod__

Creates a new HxOperator.MOD expression.

__pow__

Creates a new HxOperator.POW expression.

__or__

Creates a new HxOperator.OR expression.

__and__

Creates a new HxOperator.AND expression.

__xor__

Creates a new HxOperator.XOR expression.

__invert__

Creates a new HxOperator.NOT expression.

__eq__

Creates a new HxOperator.EQ expression.

__ne__

Creates a new HxOperator.NEQ expression.

__lt__

Creates a new HxOperator.LT expression.

__gt__

Creates a new HxOperator.EQ expression.

__geq__

Creates a new HxOperator.GEQ expression.

__leq__

Creates a new HxOperator.EQ expression.

__getitem__

Creates a new HxOperator.AT expression.

__call__

Creates a new HxOperator.CALL expression.

__str__

Returns useful info about this expression (according to the state of Hexaly Optimizer).

Instance methods

HxExpression.get_operator()

Gets the operator of this expression.

You can also use the shortcut member operator

Returns:

Operator

Return type:

HxOperator

HxExpression.get_index()

Gets the index of this expression in the model.

Returns:

Index in the model of this HxExpression.

Return type:

int

HxExpression.is_constant()

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

Returns:

True if typed as constant.

Return type:

bool

HxExpression.is_decision()

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

Returns:

True if typed as decision.

Return type:

bool

HxExpression.is_constraint()

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

Returns:

True if tagged as constraint

Return type:

bool

HxExpression.is_objective()

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

Returns:

True if tagged as objective.

Return type:

bool

HxExpression.is_double()

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

Returns:

True if the expression is a double.

Return type:

bool

Since:

3.0

HxExpression.is_int()

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

Returns:

True if the expression is a int.

Return type:

bool

Since:

3.0

HxExpression.is_bool()

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

Returns:

True if the expression is a boolean.

Return type:

bool

Since:

3.0

HxExpression.is_interval()

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

Returns:

True if the expression is an interval.

Return type:

bool

Since:

12.0

HxExpression.is_array()

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

Returns:

True if the expression is an array.

Return type:

bool

Since:

3.1

HxExpression.is_collection()

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

Returns:

True if the expression is a collection.

Return type:

bool

Since:

5.5

HxExpression.is_function()

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

Returns:

True if the expression is a function.

Return type:

bool

Since:

6.0

HxExpression.add_operand(operand)

Add the given operand to this expression. The operand can be an HxExpression, a boolean, an integer or a double. Only allowed in state HxState.MODELING.

Parameters:

operand – Operand to add. Can be an HxExpression, a boolean, an integer or a double.

HxExpression.add_operands(operands)
HxExpression.add_operands(*operands)

Add the given operands to the expression. Any object that implements the __iter__ method is accepted. Thus, lists, tuples, sets and their comprehensions counterpart are accepted. It is also possible to use this method with a variadic number of arguments.

Each operand can be an HxExpression, a boolean, an integer or a double. Please note that some of these types can be explicitly forbidden for specific operators. For example, doubles are not allowed with the modulo operator.

Parameters:

operands – Operands to add. The object must be iterable.

Since:

5.5

HxExpression.get_operand(op_index)

Gets the operand with the given index.

You can also use the shortcut member operands

Parameters:

op_index (int) – Index of the operand

Returns:

Operand

Return type:

HxExpression

HxExpression.set_operand(op_index, operand)

Replaces the operand of the given index. The new operand can be an HxExpression, a boolean, an integer or a double.

Parameters:
  • op_index (int) – Index of the operand to change

  • operand – New operand.

HxExpression.get_nb_operands()

Returns the number of operands of this expression.

You can also use the shortcut member nb_operands or the collection member operands

Returns:

Number of operands

Return type:

int

HxExpression.set_value(value)

Sets the value of this expression in the current solution found by the optimizer. Only allowed for decisions. Only allowed in state HxState.STOPPED.

The given value must be compatible with the type of the expression. For example, you cannot set a double value to a boolean expression.

This method is a shortcut for HxSolution.set_value()

You can also use the shortcut member value

Parameters:

value – Value assigned to this expression.

HxExpression.get_value()

Gets the value of this expression in the best solution found by the optimizer. Only allowed in states HxState.PAUSED or HxState.STOPPED.

The type of the returned value depends on the type of the HxExpression. It can be a boolean, an integer, a double, a HxInterval, a HxCollection or a HxArray.

This method is a shortcut for HxSolution.get_value()

You can also use the shortcut member value

Returns:

Value of the HxExpression in the best solution.

Return type:

bool, int, double, HxInterval, HxCollection or HxArray

HxExpression.get_external_context()

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

Returns:

Context of the external function

See:

HxExternalContext

HxExpression.is_violated()

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

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 HxState.PAUSED or HxState.STOPPED.

This method is a shortcut for HxSolution.is_violated()

Returns:

True if this expression is violated in the best solution.

Return type:

bool

HxExpression.is_undefined()

Returns true if the given expression has an undefined value in the best solution found by the optimizer. 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 HxState.PAUSED or HxState.STOPPED.

This method is a shortcut for HxSolution.is_undefined()

Since:

7.0

Returns:

True if this expression has an undefined value in the best solution.

Return type:

bool

HxExpression.is_named()

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

Returns:

True if named

Return type:

bool

HxExpression.set_name(name)

Sets the name of this expression. Only allowed in state HxState.MODELING. The name cannot be empty. Two operators of the model cannot share the same name. Useful for debugging or logging purposes.

You can also use the shortcut member name

Parameters:

name (str) – Name of the HxExpression in the model. The name must be unique.

HxExpression.get_name()

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

You can also use the shortcut member name

Returns:

Name

Return type:

str

Instance attributes

All get/set methods have their attribute counterpart. You can use them as shortcuts to improve the readability or your models and codes.

HxExpression.operator

Operator of the expression. This attribute is read-only. It is a shortcut for get_operator().

HxExpression.index

Index of the expression. This attribute is read-only. It is a shortcut for get_index().

HxExpression.value

Value of the expression in the best solution found by the optimizer. It is a shortcut for get_value() and set_value().

HxExpression.name

Name of the expression in the model. The name must be unique. It is a shortcut for get_name() and set_name().

HxExpression.nb_operands

Number of operands of the expression. This attribute is read-only. It is a shortcut for get_nb_operands().

HxExpression.operands

Operands of the expression. The returned object is iterable, supports the len function and can be indexed with integers. It is a shortcut for get_operand(), get_nb_operands() and set_operand(). Please note, that you still have to use add_operand() to add new operands to the expression.

HxExpression.external_context

External context of an external function. This attribute is readonly and only accessible if the expression is indeed an external function. It is a shortcut for get_external_context().

Special operators and methods

HxExpression overloads many operators and builtin functions to write models very quickly and efficiently. The overloaded operators are:

  • The arithmetic operators: +, -, *, %, /, **

  • The bitwise operators: ~, &, |, ^

  • The relational operators: ==, !=, >=, <=, >, <

  • The compound operators: +=, -=, *=, /=, **=, &=, |=, ^=

  • The special index operator: [] (__getitem__).

Except for the compound operators that can work in-place, calling an overloaded operators creates a completely new HxExpression. For example:

x = model.bool()
y = model.bool()
z = x + y

Creates a new HxExpression z with two operands: x and y.

For that reason, you should avoid creating large nary expressions with x = x + y. It is preferable to use the dedicated compound operators x += y that remove the unecessary intermediate HxExpressions.

HxExpression.__add__(y)
HxExpression.__radd__(y)

Creates a new HxOperator.SUM expression. It is a shortcut for model.create_expression(HxOperator.SUM, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.SUM expression

Return type:

HxExpression

HxExpression.__sub__(y)
HxExpression.__rsub__(y)

Creates a new HxOperator.SUB expression. It is a shortcut for model.create_expression(HxOperator.SUB, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.SUB expression

Return type:

HxExpression

HxExpression.__mul__(y)
HxExpression.__rmul__(y)

Creates a new HxOperator.PROD expression. It is a shortcut for model.create_expression(HxOperator.PROD, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.PROD expression

Return type:

HxExpression

HxExpression.__div__(y)
HxExpression.__rdiv__(y)
HxExpression.__truediv__(y)
HxExpression.__rtruediv__(y)

Creates a new HxOperator.DIV expression. It is a shortcut for model.create_expression(HxOperator.DIV, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.DIV expression

Return type:

HxExpression

HxExpression.__mod__(y)
HxExpression.__rmod__(y)

Creates a new HxOperator.MOD expression. It is a shortcut for model.create_expression(HxOperator.MOD, self, y).

Parameters:

y – Second operand. Can be an HxExpression with integer value, a boolean or an integer.

Returns:

A new HxOperator.MOD expression

Return type:

HxExpression

HxExpression.__pow__(y)
HxExpression.__rpow__(y)

Creates a new HxOperator.POW expression. It is a shortcut for model.create_expression(HxOperator.POW, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.POW expression

Return type:

HxExpression

HxExpression.__or__(y)
HxExpression.__ror__(y)

Creates a new HxOperator.OR expression. It is a shortcut for model.create_expression(HxOperator.OR, self, y).

Parameters:

y – Second operand. Can be an HxExpression with boolean value, a boolean, an integer or a double.

Returns:

A new HxOperator.OR expression

Return type:

HxExpression

HxExpression.__and__(y)
HxExpression.__rand__(y)

Creates a new HxOperator.AND expression. It is a shortcut for model.create_expression(HxOperator.AND, self, y).

Parameters:

y – Second operand. Can be an HxExpression with boolean value, a boolean, an integer or a double.

Returns:

A new HxOperator.AND expression

Return type:

HxExpression

HxExpression.__xor__(y)
HxExpression.__rxor__(y)

Creates a new HxOperator.XOR expression. It is a shortcut for model.create_expression(HxOperator.XOR, self, y).

Parameters:

y – Second operand. Can be an HxExpression with boolean value, a boolean, an integer or a double.

Returns:

A new HxOperator.XOR expression

Return type:

HxExpression

HxExpression.__invert__()

Creates a new HxOperator.NOT expression. It is a shortcut for model.create_expression(HxOperator.NOT, self).

Returns:

A new HxOperator.NOT expression

Return type:

HxExpression

HxExpression.__eq__(y)

Creates a new HxOperator.EQ expression. It is a shortcut for model.create_expression(HxOperator.EQ, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.EQ expression

Return type:

HxExpression

HxExpression.__ne__(y)

Creates a new HxOperator.NEQ expression. It is a shortcut for model.create_expression(HxOperator.NEQ, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.NEQ expression

Return type:

HxExpression

HxExpression.__lt__(y)

Creates a new HxOperator.LT expression. It is a shortcut for model.create_expression(HxOperator.LT, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.LT expression

Return type:

HxExpression

HxExpression.__gt__(y)

Creates a new HxOperator.EQ expression. It is a shortcut for model.create_expression(HxOperator.GT, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.GT expression

Return type:

HxExpression

HxExpression.__geq__(y)

Creates a new HxOperator.GEQ expression. It is a shortcut for model.create_expression(HxOperator.GEQ, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.GEQ expression

Return type:

HxExpression

HxExpression.__leq__(y)

Creates a new HxOperator.EQ expression. It is a shortcut for model.create_expression(HxOperator.LEQ, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.LEQ expression

Return type:

HxExpression

HxExpression.__getitem__(y)

Creates a new HxOperator.AT expression. It is a shortcut for model.create_expression(HxOperator.AT, self, y).

Parameters:

y – Second operand. Can be an HxExpression, a boolean, an integer or a double.

Returns:

A new HxOperator.LEQ expression

Return type:

HxExpression

HxExpression.__call__(*args)

Creates a new HxOperator.CALL expression. It is a shortcut for model.create_expression(HxOperator.CALL, self, args).

Parameters:

args – Operands. The object must be iterable.

Returns:

A new HxOperator.CALL expression

Return type:

HxExpression

HxExpression.__str__()

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

Returns:

Info about this search during the solving process.

Return type:

str