concepts.dsl.constraint.ConstraintSatisfactionProblem#
- class ConstraintSatisfactionProblem[source]#
Bases:
object
A constraint satisfaction problem.
A constraint satisfaction problem is a set of constraints, and a set of variables. The solution to a constraint satisfaction problem is to find a set of values for the variables that satisfy all the constraints.
Methods
add_constraint
(c[, note])Add a constraint to the constraint satisfaction problem.
add_domain_value
(identifier, value)Add a value to the domain of a variable.
add_equal_constraint
(left[, right, note])Add an equality constraint.
clone
([constraints])Clone the constraint satisfaction problem.
empty
()get_domain
(identifier)Get the domain of a variable.
get_type
(identifier)Get the type of a variable.
ground_assignment_value
(assignments, identifier)ground_assignment_value_partial
(assignments, ...)new_actionable_var
(dtype[, wrap])Create a new actionable variable.
new_var
(dtype[, domain, actionable, name, ...])Create a new variable.
print_assignment_dict
(assignments)with_group
(expression[, note])Add a group constraint.
Attributes
A mapping from variable indices to the variable objects.
A list of tuples (index, index), indicating that the first index should be solved earlier than the second index.
A list of constraints.
- __init__(index2record=None, index_order=None, constraints=None, counter=0, state_timestamp=0)[source]#
Initialize a constraint satisfaction problem.
- Parameters:
index2record (Dict[int, OptimisticValueRecord] | None) – a mapping from variable indices to the variable objects.
index_order (List[Tuple[int, int]] | None) – a list of tuples (index, index), indicating that the first index should be solved earlier than the second index.
constraints (List[Constraint | GroupConstraint] | None) – a list of constraints.
counter (int) – the counter for generating new variable indices.
state_timestamp (int)
- __new__(**kwargs)#
- add_constraint(c, note=None)[source]#
Add a constraint to the constraint satisfaction problem.
- Parameters:
c (Constraint) – the constraint to be added.
note (Any | None) – the note of the constraint.
- add_equal_constraint(left, right=None, note=None)[source]#
Add an equality constraint.
- Parameters:
left (TensorValue | OptimisticValue) – the left hand side of the equality constraint.
right (TensorValue | OptimisticValue | bool | None) – the right hand side of the equality constraint.
note (Any | None) – the note of the constraint.
- clone(constraints=None)[source]#
Clone the constraint satisfaction problem.
- Parameters:
constraints (List[Constraint] | None) – the constraints to be replaced into the cloned constraint satisfaction problem. If None, the constraints of the original constraint satisfaction problem will be used.
- Return type:
- ground_assignment_value(assignments, identifier)[source]#
- Parameters:
assignments (Dict[int, Assignment])
identifier (int)
- Return type:
- ground_assignment_value_partial(assignments, identifier)[source]#
- Parameters:
assignments (Dict[int, Assignment])
identifier (int)
- Return type:
- new_actionable_var(dtype, wrap=False, **kwargs)[source]#
Create a new actionable variable.
- Parameters:
dtype (TensorValueTypeBase | PyObjValueType) – the type of the variable.
wrap (bool) – whether to wrap the variable index into an OptimisticValue.
- Return type:
- new_var(dtype, domain=None, actionable=False, name=None, is_constant=False, wrap=False)[source]#
Create a new variable.
- Parameters:
dtype (TensorValueTypeBase | PyObjValueType) – the type of the variable.
domain (Set[Any] | None) – the domain of the variable. If None, it will be assumed to the full domain of the type.
actionable (bool) – whether the variable is actionable.
name (str | None) – the name of the variable.
is_constant (bool) – whether the variable is constant.
wrap (bool) – whether to wrap the variable index into an OptimisticValue.
- Returns:
The index of the new variable (int) if wrap is False, or the wrapped OptimisticValue if wrap is True.
- Return type:
- print_assignment_dict(assignments)[source]#
- Parameters:
assignments (Dict[int, Assignment])
- with_group(expression, note=None)[source]#
Add a group constraint.
- Parameters:
expression (ValueOutputExpression)
note (Any)
- constraints: List[Constraint | GroupConstraint]#
A list of constraints.
- index2record: Dict[int, OptimisticValueRecord]#
A mapping from variable indices to the variable objects.