didppy.FloatExpr
- class didppy.FloatExpr(value)
Continuous expression.
If an arithmetic operator (
+
,-
,*
,/
,//
,%
,**
) with anIntExpr
,IntVar
,IntResourceVar
,FloatExpr
,FloatVar
,FloatResourceVar
,int
, orfloat
is applied, a newFloatExpr
is returned. Ifabs()
is applied, a newFloatExpr
is returned.round()
,trunc()
,floor()
, andceil()
return anIntExpr
.If a comparison operator (
<
,<=
,==
,!=
,>
,>=
) with anIntExpr
,IntVar
,IntResourceVar
,FloatExpr
,FloatVar
,FloatResourceVar
,int
, orfloat
is applied, aCondition
is returned.Note that
didppy.max
anddidppy.min
should be used instead ofmax()
andmin()
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.
Returns the cost of the transitioned state, which can be used in a cost expression.
- eval(state, model)
Evaluates the expression.
- Parameters:
- 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:
- 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:
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