didppy.Condition
- class didppy.Condition
Condition.
The negation of a condition can be crated by
~x
. The conjunction of two conditions can be crated byx & y
. The disjunction of two conditions can be crated byx | y
.Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> state = model.target_state >>> condition = var >= 4 >>> condition.eval(state, model) True >>> (~condition).eval(state, model) False >>> (condition & (var <= 5)).eval(state, model) True >>> (condition | (var <= 5)).eval(state, model) True
Methods
eval
(state, model)Evaluates the condition.
if_then_else
(x, y)Returns an 'if-then-else' expression, which returns the first expression if the condition holds and the second one otherwise.
- eval(state, model)
Evaluates the condition.
- Parameters:
- Returns:
Value of the condition.
- Return type:
bool
- Raises:
PanicException – If the condition is not valid.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=0) >>> condition = var >= 0 >>> state = model.target_state >>> condition.eval(state, model) True
- if_then_else(x, y)
Returns an ‘if-then-else’ expression, which returns the first expression if the condition holds and the second one otherwise.
- Parameters:
x (ElementExpr, ElementVar, ElementResourceVar, SetExpr, SetVar, SetConst, IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float) – First expression.
y (ElementExpr, ElementVar, ElementResourceVar, SetExpr, SetVar, SetConst, IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float) – Second expression.
- Returns:
The ‘if-then-else’ expression. The type of the return value is determined according to the types of
x
andy
.- Return type:
ElementExpr, SetExpr, IntExpr, or FloatExpr
- Raises:
TypeError – If the types of
x
andy
mismatch.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> expr = (var >= 0).if_then_else(2, 3) >>> expr.eval(model.target_state, model) 2