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, ...)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 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, less_is_better, ...)Adds an integer 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, object_type)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.
eval_base_cost(state)is_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.
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)
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
conditionsis 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, name)
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, andintas 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
dictis given. If a key not included indictis 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:
BoolTable1Dis returned iftableislistofbool.BoolTable2Dis returned iftableislistoflistofbool.BoolTable3Dis returned iftableislistoflistoflistofbool.BoolTableis returned ifdictis given.- Return type:
- Raises:
RuntimeError – If
nameis already used.TypeError – If
tableisdictanddefaultisNone.OverflowError – If
tableisdictand 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
boundis 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, andboundisFloatExpr,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, name)
Adds an element resource variable to the model.
- 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_typeis not included in the model. Ifnameis already used.OverflowError – If
targetis 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, name)
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, andintas 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
dictis given. If a key not included indictis 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:
ElementTable1Dis returned iftableislistofint.ElementTable2Dis returned iftableislistoflistofint.ElementTable3Dis returned iftableislistoflistoflistofint.ElementTableis returned ifdictis given.- Return type:
ElementTable1D, ElementTable2D, ElementTable3D, or ElementTable
- Raises:
RuntimeError – If
nameis already used.TypeError – If
tableisdictanddefaultisNone. Iftableisdictand one of its keys contains a negative value.OverflowError – If a value in
tableordefaultis 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)
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_typeis not included in the model. Ifnameis already used.OverflowError – If
targetis 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, name)
Adds a continuous variable to the model.
- 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
nameis 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, name)
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, andintas indices. The sum, product, maximum, and minimum of values in a table can be taken by usingSetExpr,SetVar, andSetConstin 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
dictis given. If a key not included indictis 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:
FloatTable1Dis returned iftableislistofint.FloatTable2Dis returned iftableislistoflistofint.FloatTable3Dis returned iftableislistoflistoflistofint.FloatTableis returned ifdictis given.- Return type:
- Raises:
RuntimeError – If
nameis already used.TypeError – If
tableisdictanddefaultisNone.OverflowError – If
tableisdictand 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)
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
nameis 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, name)
Adds an integer variable to the model.
- 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
nameis 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, name)
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, andintas indices. The sum, product, maximum, and minimum of values in a table can be taken by usingSetExpr,SetVar, andSetConstin 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
dictis given. If a key not included indictis 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:
IntTable1Dis returned iftableislistofint.IntTable2Dis returned iftableislistoflistofint.IntTable3Dis returned iftableislistoflistoflistofint.IntTableis returned ifdictis given.- Return type:
- Raises:
RuntimeError – If
nameis already used.TypeError – If
tableisdictanddefaultisNone.OverflowError – If
tableisdictand 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)
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
nameis already used.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> var = model.add_int_var(target=1) 1
- add_object_type(number, name)
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
numberis negative.RuntimeError – If
nameis 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, name, object_type)
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, andintas indices. The union, intersection, and symmetric difference of sets in a table can be taken by usingSetExpr,SetVar, andSetConstin 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,listofint, orsetofint.default (SetConst, list of int, set of int, or None, default: None) – Default value. Used only when
dictis given. If a key not included indictis 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
listofintorsetofintis used intableordefault. Otherwise, it is ignored.
- Returns:
SetTable1Dis returned iftableislist.SetTable2Dis returned iftableislistoflist`.SetTable3Dis returned iftableislistoflistoflist.SetTableis returned ifdictis given.- Return type:
- Raises:
RuntimeError – If
nameis already used.TypeError – If
tableisdictanddefaultisNone. iflistofintorsetofintis used intableandobject_typeisNone.OverflowError – If a value in
tableordefaultis negative. Iftableisdictand 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)
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_typeis not included in the model. If a value intargetis greater than or equal to the number of the objects. Ifnameis already used.TypeError – If a value in
targetis 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, backward)
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
valueis greater than or equal to the number of objects.TypeError – If an element in
valueis 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
- eval_base_cost(state)
is_base(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:
Trueif less is better andFalseotherwise.- 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,intis returned. ForSetVar,setis returned. ForFloatVarandFloatResourceVar,floatis 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, backward)
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
- 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,listofint, orsetofint. ForFloatVarandFloatResourceVar, it should befloat.
- Raises:
TypeError – If the types of
varandtargetmismatch.RuntimeError – If the variable is not included in the model. If
varisSetVarand a value intargetis greater than or equal to the number of the associated objects.OverflowError – If
varisElementVarorElementResourceVarandtargetis negative. IfvarisSetVarand a value intargetis 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)
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
costand 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