didppy.IntExpr

class didppy.IntExpr(value)

Integer expression.

If an arithmetic operator (+, -, *, //, %) with an IntExpr, IntVar, IntResourceVar, or int is applied, a new IntExpr is returned. For division (/) and power (**), a FloatExpr is returned. If an arithmetic operator with an FloatExpr, FloatVar, FloatResourceVar, or float is applied, a FloatExpr is returned. If abs() is applied, a new IntExpr is returned.

If a comparison operator (<, <=, ==, !=, >, >=) with an IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float is applied, a Condition is returned.

Note that didppy.max() and didppy.min() should be used instead of max() and min() as comparison operators are overloaded.

Parameters:

value (int) – A value from which a constant expression is created.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> expr = dp.IntExpr(3)
>>> expr.eval(state, model)
3
>>> (-expr).eval(state, model)
-3
>>> (expr + 1).eval(state, model)
4
>>> (expr + 1.5).eval(state, model)
4.5
>>> (expr - 1).eval(state, model)
2
>>> (expr * 2).eval(state, model)
6
>>> (expr / 2).eval(state, model)
1.5
>>> (expr // 2).eval(state, model)
1
>>> (expr % 2).eval(state, model)
1
>>> abs(expr).eval(state, model)
3
>>> (expr ** 2).eval(state, model)
9.0
>>> pow(expr, 2).eval(state, model)
9.0
>>> (2 ** expr).eval(state, model)
8.0
>>> pow(2, expr).eval(state, model)
8.0
>>> (expr < 3).eval(state, model)
False
>>> (expr <= 3).eval(state, model)
True
>>> (expr == 3).eval(state, model)
True
>>> (expr != 3).eval(state, model)
False
>>> (expr > 3).eval(state, model)
False
>>> (expr >= 3).eval(state, model)
True

Methods

eval(state, model)

Evaluates the expression.

eval_cost(cost, state, model)

Evaluates the cost expression.

state_cost()

Returns the cost of the transitioned state, which can be used in a cost expression.

eval(state, model)

Evaluates the expression.

Parameters:
  • state (State) – State.

  • model (Model) – DyPDL Model.

Returns:

Value of the expression.

Return type:

int

Raises:

PanicException – If the expression is not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=0)
>>> expr = var + 1
>>> state = model.target_state
>>> expr.eval(state, model)
1
eval_cost(cost, state, model)

Evaluates the cost expression.

Parameters:
  • cost (int) – State cost.

  • state (State) – State.

  • model (Model) – DyPDL Model.

Returns:

Value of the expression.

Return type:

int

Raises:

PanicException – If the expression is not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=0)
>>> expr = var + dp.IntExpr.state_cost()
>>> state = model.target_state
>>> expr.eval_cost(1, state, model)
1
static state_cost()

Returns the cost of the transitioned state, which can be used in a cost expression.

Returns:

The cost of the transitioned state.

Return type:

IntExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> cost = dp.IntExpr.state_cost() + 1
>>> cost.eval_cost(1, state, model)
2