didppy.Transition
- class didppy.Transition(name, cost=None, preconditions=None, effects=None)
Transition.
An effect on a variable can be accessed by
transition[var]
, wheretransition
isTransition
andvar
is either ofElementVar
,ElementResourceVar
,SetVar
,IntVar
,IntResourceVar
,FloatVar
, andFloatResourceVar
.- Parameters:
name (str) – Name of the transition.
cost (IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, float, or None, default: None) – Cost expression.
IntExpr.state_cost()
orFloatExpr.state_cost()
can be used to represent the cost of the transitioned state. IfNone
,IntExpr.state_cost()
is used.preconditions (list of Condition or None, default: None) – Preconditions, which must be satisfied by a state to be applicable.
effects (list of tuple of a variable and an expression or None, default: None) – Effects, a sequence of tuple of a variable and an expression. Instead of an expression, a variable or an immediate value can be used.
- Raises:
RuntimeError – If multiple effects are defined for the same variable.
TypeError – If the types of a variable and an expression in
effects
mismatch.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition( ... name="t", ... cost=dp.IntExpr.state_cost() + 1, ... preconditions=[var >= 1], ... effects=[(var, var - 1)], ... ) >>> state = model.target_state >>> t.cost.eval_cost(0, state, model) 1 >>> t.cost = dp.IntExpr.state_cost() + 2 >>> t.cost.eval_cost(0, state, model) 2 >>> preconditions = t.preconditions >>> preconditions[0].eval(state, model) True >>> t[var].eval(state, model) 3 >>> t[var] = var + 1 >>> t[var].eval(state, model) 5
Methods
add_effect
(var, expr)Adds an effect to the transition.
add_precondition
(condition)Adds a precondition to the transition.
apply
(state, model)Applies the transition to the given state.
eval_cost
(cost, state, model)Evaluates the cost of the transition in the given state.
is_applicable
(state, model)Checks if the transition is applicable in the given state.
Attributes
Cost expression.
Name of the transition.
Preconditions.
- add_effect(var, expr)
Adds an effect to the transition.
- Parameters:
var (ElementVar, ElementResourceVar, SetVar, IntVar, IntResourceVar, FloatVar, or FloatResourceVar) – Variable to update.
expr (ElementExpr, ElementVar, ElementResourceVar, SetExpr, SetVar, SetConst, IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float) – Expression to update the variable.
- Raises:
TypeError – If the types of
var
andexpr
mismatch.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition(name="t") >>> t.add_effect(var, var + 1) >>> t[var].eval(model.target_state, model) 5
- add_precondition(condition)
Adds a precondition to the transition.
- Parameters:
condition (Condition) – Precondition.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition(name="t") >>> t.add_precondition(var >= 1) >>> t.preconditions[0].eval(model.target_state, model) True
- apply(state, model)
Applies the transition to the given state.
- Parameters:
- Returns:
State after applying the transition.
- Return type:
- Raises:
PanicException – If preconditions are invalid.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition(name="t", effects=[(var, var + 1)]) >>> state = t.apply(model.target_state, model) >>> state[var] 5
- eval_cost(cost, state, model)
Evaluates the cost of the transition in the given state.
- Parameters:
- Returns:
Cost of the transition.
- Return type:
int or float
- Raises:
TypeError – If the type of
cost
mismatches the cost type ofmodel
.PanicException – If the cost expression is invalid.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition(name="t", cost=dp.IntExpr.state_cost() + 1) >>> t.eval_cost(1, model.target_state, model) 2
- is_applicable(state, model)
Checks if the transition is applicable in the given state.
- Parameters:
- Returns:
True if the transition is applicable in the given state.
- Return type:
bool
- Raises:
PanicException – If preconditions are invalid.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=4) >>> t = dp.Transition(name="t", preconditions=[var >= 0]) >>> t.is_applicable(model.target_state, model) True
- name
Name of the transition.
- Type:
str