didppy.FloatExpr

class didppy.FloatExpr(value)

Continuous expression.

If an arithmetic operator (+, -, *, /, //, %, **) with an IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float is applied, a new FloatExpr is returned. If abs() is applied, a new FloatExpr is returned. round(), trunc(), floor(), and ceil() return an IntExpr.

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 (float) – A value from which a constant expression is created.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> expr = dp.FloatExpr(3.5)
>>> expr.eval(state, model)
3.5
>>> (-expr).eval(state, model)
-3.5
>>> (expr + 1.5).eval(state, model)
5.0
>>> (expr + 1).eval(state, model)
4.5
>>> (expr - 1.5).eval(state, model)
2.0
>>> (expr * 2.5).eval(state, model)
8.75
>>> (expr / 2.5).eval(state, model)
1.4
>>> (expr // 2.5).eval(state, model)
1.0
>>> (expr % 2.5).eval(state, model)
1.0
>>> abs(expr).eval(state, model)
3.5
>>> (expr ** 2.0).eval(state, model)
12.25
>>> pow(expr, 2.0).eval(state, model)
12.25
>>> (1.0 ** expr).eval(state, model)
1.0
>>> pow(1.0, expr).eval(state, model)
1.0
>>> round(expr).eval(state, model)
4
>>> import math
>>> math.trunc(expr).eval(state, model)
3
>>> math.floor(expr).eval(state, model)
3
>>> math.ceil(expr).eval(state, model)
4
>>> (expr < 3.0).eval(state, model)
False
>>> (expr > 3.0).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:

float

Raises:

PanicException – If the expression is not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_float_var(target=1.5)
>>> expr = var + 1.5
>>> state = model.target_state
>>> expr.eval(state, model)
3.0
eval_cost(cost, state, model)

Evaluates the cost expression.

Parameters:
  • cost (float) – State cost.

  • state (State) – State.

  • model (Model) – DyPDL Model.

Returns:

Value of the expression.

Return type:

float

Raises:

PanicException – If the expression is not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_float_var(target=1.5)
>>> expr = var + dp.FloatExpr.state_cost()
>>> state = model.target_state
>>> expr.eval_cost(1.5, state, model)
3.0
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:

FloatExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> cost = dp.FloatExpr.state_cost() + 1.5
>>> cost.eval_cost(1.5, state, model)
3.0