didppy.Transition

class didppy.Transition(name, cost=None, preconditions=None, effects=None)

Transition.

An effect on a variable can be accessed by transition[var], where transition is Transition and var is either of ElementVar, ElementResourceVar, SetVar, IntVar, IntResourceVar, FloatVar, and FloatResourceVar.

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() or FloatExpr.state_cost() can be used to represent the cost of the transitioned state. If None, 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

Cost expression.

name

Name of the transition.

preconditions

Preconditions.

add_effect(var, expr)

Adds an effect to the transition.

Parameters:
Raises:

TypeError – If the types of var and expr 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:
  • state (State) – State to apply the transition to.

  • model (Model) – DyPDL model.

Returns:

State after applying the transition.

Return type:

State

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
cost

Cost expression.

Type:

IntExpr or FloatExpr

eval_cost(cost, state, model)

Evaluates the cost of the transition in the given state.

Parameters:
  • cost (int or float) – Cost of the next state.

  • state (State) – Current state.

  • model (Model) – DyPDL model.

Returns:

Cost of the transition.

Return type:

int or float

Raises:
  • TypeError – If the type of cost mismatches the cost type of model.

  • 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:
  • state (State) – State to check.

  • model (Model) – DyPDL model.

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

preconditions

Preconditions. Note that the preconditions and their order might be changed due to internal optimizations.

Type:

list of Condition