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.
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 with their cost expressions.
Dual bounds.
If the cost is represented by a continuous value or not.
Maximize the cost or not.
State constraints.
Target state.
- add_base_case(conditions, cost=None)
Adds a base case to the model.
- Parameters:
conditions (list of Condition) – Base case.
cost (IntExpr, IntVar, IntResourceVar, FloatExpr, FloatVar, FloatResourceVar, int, float, or None, default: None) – Expression to compute the value of a base state. This expression can use state variables in the base state.
IntExpr.state_cost()
andFloatExpr.state_cost()
are not allowed. If None, the value of the base state is 0. When a state satisfies multiple base cases, the minimum/maximum value is used in minimization/maximization.
- 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()
orFloatExpr.state_cost()
is used incost
.
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 ofElementExpr
,ElementVar
,ElementResourceVar
, andint
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 indict
is given, the table returnsdefault
.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 iftable
islist
ofbool
.BoolTable2D
is returned iftable
islist
oflist
ofbool
.BoolTable3D
is returned iftable
islist
oflist
oflist
ofbool
.BoolTable
is returned ifdict
is given.- Return type:
- Raises:
RuntimeError – If
name
is already used.TypeError – If
table
isdict
anddefault
isNone
.OverflowError – If
table
isdict
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, andbound
isFloatExpr
,FloatVar
,FloatResourceVar
, orfloat
.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:
- Raises:
RuntimeError – If
object_type
is not included in the model. Ifname
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 ofElementExpr
,ElementVar
,ElementResourceVar
, andint
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 indict
is given, the table returnsdefault
.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 iftable
islist
ofint
.ElementTable2D
is returned iftable
islist
oflist
ofint
.ElementTable3D
is returned iftable
islist
oflist
oflist
ofint
.ElementTable
is returned ifdict
is given.- Return type:
ElementTable1D, ElementTable2D, ElementTable3D, or ElementTable
- Raises:
RuntimeError – If
name
is already used.TypeError – If
table
isdict
anddefault
isNone
. Iftable
isdict
and one of its keys contains a negative value.OverflowError – If a value in
table
ordefault
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:
- Raises:
RuntimeError – If
object_type
is not included in the model. Ifname
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:
- 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 ofElementExpr
,ElementVar
,ElementResourceVar
, andint
as indices. The sum, product, maximum, and minimum of values in a table can be taken by usingSetExpr
,SetVar
, andSetConst
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 indict
is given, the table returnsdefault
.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 iftable
islist
ofint
.FloatTable2D
is returned iftable
islist
oflist
ofint
.FloatTable3D
is returned iftable
islist
oflist
oflist
ofint
.FloatTable
is returned ifdict
is given.- Return type:
- Raises:
RuntimeError – If
name
is already used.TypeError – If
table
isdict
anddefault
isNone
.OverflowError – If
table
isdict
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:
- 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:
- 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 ofElementExpr
,ElementVar
,ElementResourceVar
, andint
as indices. The sum, product, maximum, and minimum of values in a table can be taken by usingSetExpr
,SetVar
, andSetConst
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 indict
is given, the table returnsdefault
.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 iftable
islist
ofint
.IntTable2D
is returned iftable
islist
oflist
ofint
.IntTable3D
is returned iftable
islist
oflist
oflist
ofint
.IntTable
is returned ifdict
is given.- Return type:
- Raises:
RuntimeError – If
name
is already used.TypeError – If
table
isdict
anddefault
isNone
.OverflowError – If
table
isdict
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:
- 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:
- 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 ofElementExpr
,ElementVar
,ElementResourceVar
, andint
as indices. The union, intersection, and symmetric difference of sets in a table can be taken by usingSetExpr
,SetVar
, andSetConst
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
ofint
, orset
ofint
.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 indict
is given, the table returnsdefault
.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
ofint
orset
ofint
is used intable
ordefault
. Otherwise, it is ignored.
- Returns:
SetTable1D
is returned iftable
islist
.SetTable2D
is returned iftable
islist
oflist`
.SetTable3D
is returned iftable
islist
oflist
oflist
.SetTable
is returned ifdict
is given.- Return type:
- Raises:
RuntimeError – If
name
is already used.TypeError – If
table
isdict
anddefault
isNone
. iflist
ofint
orset
ofint
is used intable
andobject_type
isNone
.OverflowError – If a value in
table
ordefault
is negative. Iftable
isdict
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:
- Raises:
RuntimeError – If
object_type
is not included in the model. If a value intarget
is greater than or equal to the number of the objects. Ifname
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:
- 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
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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 andFalse
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:
- 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
, andIntResourceVar
,int
is returned. ForSetVar
,set
is returned. ForFloatVar
andFloatResourceVar
,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:
- 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:
- 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:
var (ElementResourceVar, IntResourceVar, or FloatResourceVar) – Resource variable.
less_is_better (bool) – Prefer a smaller value or not.
- 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:
var (ElementVar, ElementResourceVar, SetVar, IntVar, IntResourceVar, FloatVar, or FloatResourceVar) – Variable.
target (int, SetConst, list of int, set of int, or float) – Value in the target state. For
ElementVar
,ElementResourceVar
,IntVar
, andIntResourceVar
, it should beint
. ForSetVar
, it should beSetConst
,list
ofint
, orset
ofint
. ForFloatVar
andFloatResourceVar
, it should befloat
.
- Raises:
TypeError – If the types of
var
andtarget
mismatch.RuntimeError – If the variable is not included in the model. If
var
isSetVar
and a value intarget
is greater than or equal to the number of the associated objects.OverflowError – If
var
isElementVar
orElementResourceVar
andtarget
is negative. Ifvar
isSetVar
and a value intarget
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
- 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