didppy.Model

class didppy.Model(maximize=False, float_cost=False)

DyPDL model.

Parameters:
  • maximize (bool, default: false) – Maximize the cost or not.

  • float_cost (bool, default: false) – Use a continuous value to represent the cost or not.

Examples

Create a model.

>>> import didppy as dp
>>> model = dp.Model(maximize=False, float_cost=False)
>>> model.maximize
False
>>> model.float_cost
False

Get and set the target state.

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> state = model.target_state
>>> state[var]
4
>>> state[var] = 5
>>> model.target_state = state
>>> model.target_state[var]
5

Methods

add_base_case(conditions[, cost])

Adds a base case to the model.

add_bool_table(table[, default, name])

Adds a table of bool constants.

add_dual_bound(bound)

Adds a dual bound to the model.

add_element_resource_var(object_type, target)

Adds an element resource variable to the model.

add_element_table(table[, default, name])

Adds a table of element constants.

add_element_var(object_type, target[, name])

Adds an element variable to the model.

add_float_resource_var(target[, ...])

Adds a continuous resource variable to the model.

add_float_table(table[, default, name])

Adds a table of continuous constants.

add_float_var(target[, name])

Adds a continuous variable to the model.

add_int_resource_var(target[, ...])

Adds an integer resource variable to the model.

add_int_table(table[, default, name])

Adds a table of integer constants.

add_int_var(target[, name])

Adds an integer variable to the model.

add_object_type(number[, name])

Adds an object type to the model.

add_set_table(table[, default, name, ...])

Adds a table of set constants.

add_set_var(object_type, target[, name])

Adds a set variable to the model.

add_state_constr(condition)

Adds a state constraint to the model.

add_transition(transition[, forced, backward])

Adds a transition to the model.

check_state_constr(state)

Checks if the state satisfies all the state constraints.

create_set_const(object_type, value)

Creates a set constant given an object type.

dump_to_files(domain_path, problem_path)

Writes the YAML files in the provided paths.

dump_to_str()

Returns the YAML strings representing the model.

eval_base_cost(state)

Evaluates the cost of a base state.

eval_dual_bound(state)

Evaluates the dual bound on the cost of the state.

get_element_resource_var(name)

Gets an element resource variable by a name.

get_element_var(name)

Gets an element variable by a name.

get_float_resource_var(name)

Gets a continuous resource variable by a name.

get_float_var(name)

Gets a continuous variable by a name.

get_int_resource_var(name)

Gets an integer resource variable by a name.

get_int_var(name)

Gets an integer variable by a name.

get_number_of_object(object_type)

Gets the number of objects associated with an object type.

get_object_type(name)

Gets the object type by a name.

get_object_type_of(var)

Gets the object type associated with a variable.

get_preference(var)

Gets the preference of a resource variable.

get_set_var(name)

Gets a set variable by a name.

get_target(var)

Gets the value of a variable in the target state.

get_transitions([forced, backward])

Returns the transitions of the model.

is_base(state)

Checks if the state is a base state.

load_from_files(domain_path, problem_path)

Loads the YAML files in the provided path.

load_from_str(domain_str, problem_str)

Loads a model from the YAML strings.

set_preference(var, less_is_better)

Sets the preference of a resource variable.

set_target(var, target)

Sets the value of a variable in the target state.

validate_forward(transitions, cost[, quiet])

Validates a solution consists of forward transitions.

Attributes

base_cases

Base cases with their cost expressions.

dual_bounds

Dual bounds.

float_cost

If the cost is represented by a continuous value or not.

maximize

Maximize the cost or not.

state_constrs

State constraints.

target_state

Target state.

add_base_case(conditions, cost=None)

Adds a base case to the model.

Parameters:
Raises:
  • RuntimeError – If one of conditions is invalid. E.g., it uses a variable not included in the model or the cost of the transitioned state.

  • PanicException – If an index of a table is out of range. If IntExpr.state_cost() or FloatExpr.state_cost() is used in cost.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.add_base_case([var >= 2, var <= 5])
>>> model.is_base(model.target_state)
True
>>> model.eval_base_cost(model.target_state)
0
>>> model.add_base_case([var >= 3, var <= 4], cost=-var)
>>> model.eval_base_cost(model.target_state)
-4
add_bool_table(table, default=None, name=None)

Adds a table of bool constants. Up to 3-dimensional tables can be added by passing a nested list. For more than 4-dimensional tables, use dict. Values in the table can be accessed in expressions using tuples of ElementExpr, ElementVar, ElementResourceVar, and int as indices.

Parameters:
  • table (list of bool, list of list of bool, list of list of list of bool, or dict[Sequence[int], bool]) – Table of constants.

  • default (bool or None, default: None) – Default value. Used only when dict is given. If a key not included in dict is given, the table returns default.

  • name (str or None, default: None) – Name of the table. If None, __bool_table{dimension}_{id} is used where {dimension} is _1d, _2d, _3d, or empty depending on the input and {id} is the id of the table.

Returns:

BoolTable1D is returned if table is list of bool. BoolTable2D is returned if table is list of list of bool. BoolTable3D is returned if table is list of list of list of bool. BoolTable is returned if dict is given.

Return type:

BoolTable1D, BoolTable2D, BoolTable3D, or BoolTable

Raises:
  • RuntimeError – If name is already used.

  • TypeError – If table is dict and default is None.

  • OverflowError – If table is dict and one of its keys contains a negative value.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> table = model.add_bool_table([[True, False], [False, True]])
>>> table[0, 0].eval(state, model)
True
add_dual_bound(bound)

Adds a dual bound to the model.

Parameters:

bound (IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, or float) – Expression to compute a dual bound.

Raises:
  • RuntimeError – If bound is invalid. E.g., it uses a variable not included in the model or the cost of the transitioned state. If the cost type of model is integer, and bound is FloatExpr, FloatVar, FloatResourceVar, or float.

  • PanicException – If an index of a table is out of range.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.eval_dual_bound(model.target_state)
>>> model.add_dual_bound(var)
>>> model.eval_dual_bound(model.target_state)
4
add_element_resource_var(object_type, target, less_is_better=False, name=None)

Adds an element resource variable to the model.

Intuitively, with less_is_better=True/less_is_better=False, if everything else is the same, a state having a smaller/greater value is better. Formally, if the values of non-resource variables are the same, a state having equal or better resource variable values must lead to an equal or better solution that has equal or fewer transitions than the other.

Parameters:
  • object_type (ObjectType) – Object type associated with the variable.

  • target (int) – Value of the variable in the target state.

  • less_is_better (bool, default: False) – Prefer a smaller value or not.

  • name (str or None, default: None) – Name of the variable. If None, __element_resource_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

ElementResourceVar

Raises:
  • RuntimeError – If object_type is not included in the model. If name is already used.

  • OverflowError – If target is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> var = model.add_element_resource_var(object_type=obj, target=1, less_is_better=True)
>>> model.get_target(var)
1
add_element_table(table, default=None, name=None)

Adds a table of element constants. Up to 3-dimensional tables can be added by passing a nested list. For more than 4-dimensional tables, use dict. Values in the table can be accessed in expressions using tuples of ElementExpr, ElementVar, ElementResourceVar, and int as indices.

Parameters:
  • table (list of int, list of list of int, list of list of list of int, or dict[Sequence[int], int]) – Table of constants.

  • default (int or None, default: None) – Default value. Used only when dict is given. If a key not included in dict is given, the table returns default.

  • name (str or None, default: None) – Name of the table. If None, __element_table{dimension}_{id} is used where {dimension} is _1d, _2d, _3d, or empty depending on the input and {id} is the id of the table.

Returns:

ElementTable1D is returned if table is list of int. ElementTable2D is returned if table is list of list of int. ElementTable3D is returned if table is list of list of list of int. ElementTable is returned if dict is given.

Return type:

ElementTable1D, ElementTable2D, ElementTable3D, or ElementTable

Raises:
  • RuntimeError – If name is already used.

  • TypeError – If table is dict and default is None. If table is dict and one of its keys contains a negative value.

  • OverflowError – If a value in table or default is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> table = model.add_element_table([[1, 2], [3, 4]])
>>> table[0, 0].eval(state, model)
1
add_element_var(object_type, target, name=None)

Adds an element variable to the model.

Parameters:
  • object_type (ObjectType) – Object type associated with the variable.

  • target (int) – Value of the variable in the target state.

  • name (str or None, default: None) – Name of the variable. If None, __element_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

ElementVar

Raises:
  • RuntimeError – If object_type is not included in the model. If name is already used.

  • OverflowError – If target is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> var = model.add_element_var(object_type=obj, target=1)
>>> model.get_target(var)
1
add_float_resource_var(target, less_is_better=False, name=None)

Adds a continuous resource variable to the model.

Intuitively, with less_is_better=True/less_is_better=False, if everything else is the same, a state having a smaller/greater value is better. Formally, if the values of non-resource variables are the same, a state having equal or better resource variable values must lead to an equal or better solution that has equal or fewer transitions than the other.

Parameters:
  • target (float or int) – Value of the variable in the target state.

  • less_is_better (bool, default: False) – Prefer a smaller value or not.

  • name (str or None, default: None) – Name of the variable. If None, __float_resource_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

FloatResourceVar

Raises:

RuntimeError – If name is already used.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_float_resource_var(target=1.5, less_is_better=True)
>>> model.get_target(var)
1.5
add_float_table(table, default=None, name=None)

Adds a table of continuous constants. Up to 3-dimensional tables can be added by passing a nested list. For more than 4-dimensional tables, use dict. Values in the table can be accessed in expressions using tuples of ElementExpr, ElementVar, ElementResourceVar, and int as indices. The sum, product, maximum, and minimum of values in a table can be taken by using SetExpr, SetVar, and SetConst in indices.

Parameters:
  • table (list of float or int, list of list of float or int, list of list of list of float or int, or dict[Sequence[int], Union[float|int]]) – Table of constants.

  • default (int or None, default: None) – Default value. Used only when dict is given. If a key not included in dict is given, the table returns default.

  • name (str or None, default: None) – Name of the table. If None, __float_table{dimension}_{id} is used where {dimension} is _1d, _2d, _3d, or empty depending on the input and {id} is the id of the table.

Returns:

FloatTable1D is returned if table is list of int. FloatTable2D is returned if table is list of list of int. FloatTable3D is returned if table is list of list of list of int. FloatTable is returned if dict is given.

Return type:

FloatTable1D, FloatTable2D, FloatTable3D, or FloatTable

Raises:
  • RuntimeError – If name is already used.

  • TypeError – If table is dict and default is None.

  • OverflowError – If table is dict and one of its keys contains a negative value.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> table = model.add_float_table([[1.5, 2.5], [3.5, 4.5]])
>>> table[0, 0].eval(state, model)
1.5
>>> var = model.add_set_var(object_type=obj, target=[0, 1])
>>> state = model.target_state
>>> table[0, var].eval(state, model)
4.0
>>> table.product(0, var).eval(state, model)
3.75
>>> table.max(0, var).eval(state, model)
2.5
>>> table.min(0, var).eval(state, model)
1.5
add_float_var(target, name=None)

Adds a continuous variable to the model.

Parameters:
  • target (float or int) – Value of the variable in the target state.

  • name (str or None, default: None) – Name of the variable. If None, __float_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

FloatVar

Raises:

RuntimeError – If name is already used.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_float_var(target=1.5)
>>> model.get_target(var)
1.5
add_int_resource_var(target, less_is_better=False, name=None)

Adds an integer resource variable to the model.

Intuitively, with less_is_better=True/less_is_better=False, if everything else is the same, a state having a smaller/greater value is better. Formally, if the values of non-resource variables are the same, a state having equal or better resource variable values must lead to an equal or better solution that has equal or fewer transitions than the other.

Parameters:
  • target (int) – Value of the variable in the target state.

  • less_is_better (bool, default: False) – Prefer a smaller value or not.

  • name (str or None, default: None) – Name of the variable. If None, __int_resource_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

IntResourceVar

Raises:

RuntimeError – If name is already used.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_resource_var(target=1, less_is_better=True)
>>> model.get_target(var)
1
add_int_table(table, default=None, name=None)

Adds a table of integer constants. Up to 3-dimensional tables can be added by passing a nested list. For more than 4-dimensional tables, use dict. Values in the table can be accessed in expressions using tuples of ElementExpr, ElementVar, ElementResourceVar, and int as indices. The sum, product, maximum, and minimum of values in a table can be taken by using SetExpr, SetVar, and SetConst in indices.

Parameters:
  • table (list of int, list of list of int, list of list of list of int, or dict[Sequence[int], int]) – Table of constants.

  • default (int or None, default: None) – Default value. Used only when dict is given. If a key not included in dict is given, the table returns default.

  • name (str or None, default: None) – Name of the table. If None, __int_table{dimension}_{id} is used where {dimension} is _1d, _2d, _3d, or empty depending on the input and {id} is the id of the table.

Returns:

IntTable1D is returned if table is list of int. IntTable2D is returned if table is list of list of int. IntTable3D is returned if table is list of list of list of int. IntTable is returned if dict is given.

Return type:

IntTable1D, IntTable2D, IntTable3D, or IntTable

Raises:
  • RuntimeError – If name is already used.

  • TypeError – If table is dict and default is None.

  • OverflowError – If table is dict and one of its keys contains a negative value.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> table = model.add_int_table([[1, 2], [3, 4]])
>>> table[0, 0].eval(state, model)
1
>>> var = model.add_set_var(object_type=obj, target=[0, 1])
>>> state = model.target_state
>>> table[0, var].eval(state, model)
3
>>> table.product(0, var).eval(state, model)
2
>>> table.max(0, var).eval(state, model)
2
>>> table.min(0, var).eval(state, model)
1
add_int_var(target, name=None)

Adds an integer variable to the model.

Parameters:
  • target (int) – Value of the variable in the target state.

  • name (str or None, default: None) – Name of the variable. If None, __int_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

IntVar

Raises:

RuntimeError – If name is already used.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=1)
1
add_object_type(number, name=None)

Adds an object type to the model.

Parameters:
  • number (int) – Number of objects.

  • name (str or None, default: None) – Name of the object type.

Returns:

The object type.

Return type:

ObjectType

Raises:
  • OverflowError – if number is negative.

  • RuntimeError – If name is already used.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> model.get_number_of_object(obj)
4
add_set_table(table, default=None, name=None, object_type=None)

Adds a table of set constants. Up to 3-dimensional tables can be added by passing a nested list. For more than 4-dimensional tables, use dict. Values in the table can be accessed in expressions using tuples of ElementExpr, ElementVar, ElementResourceVar, and int as indices. The union, intersection, and symmetric difference of sets in a table can be taken by using SetExpr, SetVar, and SetConst in indices.

Parameters:
  • table (list of set values, list of list of set values, list of list of list of set values, or dict) – Table of constants. A set value can be SetConst, list of int, or set of int.

  • default (SetConst, list of int, set of int, or None, default: None) – Default value. Used only when dict is given. If a key not included in dict is given, the table returns default.

  • name (str or None, default: None) – Name of the table. If None, __set_table{dimension}_{id} is used where {dimension} is _1d, _2d, _3d, or empty depending on the input and {id} is the id of the table.

  • object_type (ObjectType or None, default: None) – Object type associated with constants. Mandatory if list of int or set of int is used in table or default. Otherwise, it is ignored.

Returns:

SetTable1D is returned if table is list. SetTable2D is returned if table is list of list`. SetTable3D is returned if table is list of list of list. SetTable is returned if dict is given.

Return type:

SetTable1D, SetTable2D, SetTable3D, or SetTable

Raises:
  • RuntimeError – If name is already used.

  • TypeError – If table is dict and default is None. if list of int or set of int is used in table and object_type is None.

  • OverflowError – If a value in table or default is negative. If table is dict and one of its keys contains a negative value.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> table = model.add_set_table([[[0, 1], [1, 2]], [[0, 2], [1, 3]]], object_type=obj)
>>> table[0, 0].eval(state, model)
{0, 1}
>>> var = model.add_set_var(object_type=obj, target=[0, 1])
>>> state = model.target_state
>>> table.union(0, var).eval(state, model)
{0, 1, 2}
>>> table.intersection(0, var).eval(state, model)
{1}
>>> table.symmetric_difference(0, var).eval(state, model)
{0, 2}
add_set_var(object_type, target, name=None)

Adds a set variable to the model.

Parameters:
  • object_type (ObjectType) – Object type associated with the variable.

  • target (SetConst, list of int, or set of int) – Value of the variable in the target state.

  • name (str or None, default: None) – Name of the variable. If None, __set_var_{id} is used where {id} is the id of the variable.

Returns:

The variable.

Return type:

SetVar

Raises:
  • RuntimeError – If object_type is not included in the model. If a value in target is greater than or equal to the number of the objects. If name is already used.

  • TypeError – If a value in target is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> var = model.add_set_var(object_type=obj, target=[1, 2, 3])
>>> var.contains(1).eval(model.target_state, model)
True
add_state_constr(condition)

Adds a state constraint to the model.

Parameters:

condition (Condition) – State constraint.

Raises:
  • RuntimeError – If the condition is invalid. E.g., it uses a variable not included in the model or the cost of the transitioned state.

  • PanicException – If an index of a table is out of range.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.add_state_constr(var >= 0)
>>> model.check_state_constr(model.target_state)
True
add_transition(transition, forced=False, backward=False)

Adds a transition to the model.

Parameters:
  • transition (Transition) – Transition.

  • forced (bool, default: False) – If it is a forced transition or not.

  • backward (bool, default: False) – If it is a backward transition or not.

Raises:
  • RuntimeError – If an expression used in the transition is invalid. E.g., it uses a variable not included in the model. If the cost type of the model is integer and a transition with a continuous cost expression is added.

  • PanicException – If an index of a table is out of range.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> t = dp.Transition(name="t", cost=1 + dp.IntExpr.state_cost())
>>> model.add_transition(t)
>>> [t.name for t in model.get_transitions()]
['t']
base_cases

Base cases with their cost expressions.

Type:

list of tuple of list of Conditions and IntExpr or FloatExpr

check_state_constr(state)

Checks if the state satisfies all the state constraints.

Parameters:

state (State) – State to be checked.

Returns:

True if the state satisfies all the state constraints.

Return type:

bool

Raises:

PanicException – If state constraints are invalid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.add_state_constr(var >= 0)
>>> model.check_state_constr(model.target_state)
True
create_set_const(object_type, value)

Creates a set constant given an object type.

Parameters:
  • object_type (ObjectType) – Object type.

  • value (list of int or set of int) – The set of index of objects.

Returns:

The set constant.

Return type:

SetConst

Raises:
  • RuntimeError – If the object type is not included in the model. If an element in value is greater than or equal to the number of objects.

  • TypeError – If an element in value is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> s = model.create_set_const(obj, [0, 1, 2])
>>> var = model.add_element_var(object_type=obj, target=0)
>>> s.contains(var).eval(model.target_state, model)
True
dual_bounds

Dual bounds.

Type:

list of IntExpr or FloatExpr

dump_to_files(domain_path, problem_path)

Writes the YAML files in the provided paths.

Parameters:
  • domain_path (str) – Path for writing the YAML domain file.

  • problem_path (str) – Path for writing the YAML problem file.

Raises:
  • RuntimeError – If the model is not valid.

  • FileNotFoundError – If the file paths are not valid.

dump_to_str()

Returns the YAML strings representing the model.

Returns:

  • domain_str (str) – Single string that can be loaded into the YAML object of the domain file.

  • problem_str (str) – Single string that can be loaded into the YAML object of the problem file.

Raises:

RuntimeError – If the model is not valid.

eval_base_cost(state)

Evaluates the cost of a base state.

Parameters:

state (State) – State to be evaluated.

Returns:

None if the state is a base state.

Return type:

int, float, or None

Raises:

PanicException – If base cases are invalid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.add_base_case([var == 4], cost=2)
>>> model.eval_base_cost(model.target_state)
2
eval_dual_bound(state)

Evaluates the dual bound on the cost of the state.

Parameters:

state (State) – State to be evaluated.

Returns:

The dual bound on the cost of the state. None if no dual bound is defined.

Return type:

int, float, or None

Raises:

PanicException – If dual bounds are invalid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.eval_dual_bound(model.target_state)
>>> model.add_dual_bound(var)
>>> model.eval_dual_bound(model.target_state)
4
float_cost

If the cost is represented by a continuous value or not.

Type:

bool

get_element_resource_var(name)

Gets an element resource variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

ElementResourceVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> model.add_element_resource_var(object_type=obj, target=1, less_is_better=True, name="var")
>>> var = model.get_element_resource_var("var")
>>> model.get_target(var)
1
get_element_var(name)

Gets an element variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

ElementVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> model.add_element_var(object_type=obj, target=1, name="var")
>>> var = model.get_element_var("var")
>>> model.get_target(var)
1
get_float_resource_var(name)

Gets a continuous resource variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

FloatResourceVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_float_resource_var(target=1.5, less_is_better=True, name="var")
>>> var = model.get_float_resource_var("var")
>>> model.get_target(var)
1.5
get_float_var(name)

Gets a continuous variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

FloatVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_float_var(target=1.5, name="var")
>>> var = model.get_float_var("var")
>>> model.get_target(var)
1.5
get_int_resource_var(name)

Gets an integer resource variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

IntResourceVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_int_resource_var(target=1, less_is_better=True, name="var")
>>> var = model.get_int_resource_var("var")
>>> model.get_target(var)
1
get_int_var(name)

Gets an integer variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

IntVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_int_var(target=1, name="var")
>>> var = model.get_int_var("var")
>>> model.get_target(var)
1
get_number_of_object(object_type)

Gets the number of objects associated with an object type.

Parameters:

object_type (ObjectType) – Object type.

Returns:

The number of objects.

Return type:

int

Raises:

RuntimeError – If the object type is not included in the model.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> model.get_number_of_object(obj)
4
get_object_type(name)

Gets the object type by a name.

Parameters:

name (str) – Name of an object type.

Returns:

The object type.

Return type:

ObjectType

Raises:

RuntimeError – If no such object type.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_object_type(number=4, name="obj")
>>> obj = model.get_object_type("obj")
>>> model.get_number_of_object(obj)
4
get_object_type_of(var)

Gets the object type associated with a variable.

Parameters:

var (ElementVar, ElementResourceVar, or SetVar) – Variable.

Returns:

The object type.

Return type:

ObjectType

Raises:

RuntimeError – If the variable is not included in the model.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> var = model.add_element_var(object_type=obj, target=1)
>>> obj = model.get_object_type_of(var)
>>> model.get_number_of_objects(obj)
4
get_preference(var)

Gets the preference of a resource variable.

Parameters:

var (ElementResourceVar, IntResourceVar, or FloatResourceVar) – Resource variable.

Returns:

True if less is better and False otherwise.

Return type:

bool

Raises:

RuntimeError – If the variable is not included in the model.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_resource_var(target=1, less_is_better=True)
>>> model.get_preference(var)
True
get_set_var(name)

Gets a set variable by a name.

Parameters:

name (str) – Name of a variable.

Returns:

The variable.

Return type:

SetVar

Raises:

RuntimeError – If no such variable.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> model.add_set_var(object_type=obj, target=[1, 2, 3], name="var")
>>> var = model.get_set_var("var")
>>> var.contains(1).eval(model.target_state, model)
True
get_target(var)

Gets the value of a variable in the target state.

Parameters:

var (ElementVar, ElementResourceVar, SetVar, IntVar, IntResourceVar, FloatVar, or FloatResourceVar) – Variable.

Returns:

The value in the target state. For ElementVar, ElementResourceVar, IntVar, and IntResourceVar, int is returned. For SetVar, set is returned. For FloatVar and FloatResourceVar, float is returned.

Return type:

int, set, or float

Raises:

RuntimeError – If the variable is not included in the model.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=1)
>>> model.get_target(var)
1
get_transitions(forced=False, backward=False)

Returns the transitions of the model.

Parameters:
  • forced (bool, default: False) – Get forced transitions or not.

  • backward (bool, default: False) – Get backward transitions or not.

Returns:

Transitions.

Return type:

list of Transition

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> model.add_transition(dp.Transition(name="t1"))
>>> model.add_transition(dp.Transition(name="t2"))
>>> model.add_transition(dp.Transition(name="ft1"), forced=True)
>>> model.add_transition(dp.Transition(name="ft2"), forced=True)
>>> [t.name for t in model.get_transitions()]
['t1', 't2']
>>> [t.name for t in model.get_transitions(forced=True)]
['ft1', 'ft2']
is_base(state)

Checks if the state is a base state.

Parameters:

state (State) – State to be checked.

Returns:

True if the state is a base state.

Return type:

bool

Raises:

PanicException – If base cases are invalid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=4)
>>> model.add_base_case([var == 4])
>>> model.is_base(model.target_state)
True
static load_from_files(domain_path, problem_path)

Loads the YAML files in the provided path.

Parameters:
  • domain_path (str) – Path for the given YAML domain file.

  • problem_path (str) – Path for the given YAML problem file.

Returns:

Model represented by the YAML strings.

Return type:

Model

Raises:
  • FileNotFoundError – If the files do not exist.

  • RuntimeError – If the strings are not formatted in YAML or invalid.

  • PanicException – If the model is invalid.

static load_from_str(domain_str, problem_str)

Loads a model from the YAML strings.

Parameters:
  • domain_str (str) – Single string that can be loaded into the YAML object of the domain file.

  • problem_str (str) – Single string that can be loaded into the YAML object of the problem file.

Returns:

Model represented by the YAML strings.

Return type:

Model

Raises:
  • RuntimeError – If the strings are not formatted in YAML or invalid.

  • PanicException – If the model is invalid.

maximize

Maximize the cost or not.

Type:

bool

set_preference(var, less_is_better)

Sets the preference of a resource variable.

Parameters:
Raises:

RuntimeError – If the variable is not included in the model.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_resource_var(target=1, less_is_better=True)
>>> model.set_preference(var, False)
>>> model.get_preference(var)
False
set_target(var, target)

Sets the value of a variable in the target state.

Parameters:
Raises:
  • TypeError – If the types of var and target mismatch.

  • RuntimeError – If the variable is not included in the model. If var is SetVar and a value in target is greater than or equal to the number of the associated objects.

  • OverflowError – If var is ElementVar or ElementResourceVar and target is negative. If var is SetVar and a value in target is negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=1)
>>> model.set_target(var, 2)
>>> model.get_target(var)
2
state_constrs

State constraints.

Type:

list of Condition

target_state

Target state.

Type:

State

validate_forward(transitions, cost, quiet=False)

Validates a solution consists of forward transitions.

Parameters:
  • transitions (list of Transition) – Transitions in the solution.

  • cost (int or float) – Cost of the solution.

  • quiet (bool, default: False) – Suppress output messages.

Returns:

True if the solution is valid.

Return type:

bool

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

  • PanicException – If expressions in the transitions are not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> var = model.add_int_var(target=1)
>>> model.add_base_case([var == 0])
>>> t = dp.Transition(
...     name="t",
...     effects=[(var, var - 1)],
...     cost=dp.IntExpr.state_cost() + 1,
... )
>>> model.add_transition(t)
>>> model.validate_forward([t], 1)
True