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

index2record

A mapping from variable indices to the variable objects.

index_order

A list of tuples (index, index), indicating that the first index should be solved earlier than the second index.

constraints

A list of constraints.

__init__(index2record=None, index_order=None, constraints=None, counter=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.

__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_domain_value(identifier, value)[source]#

Add a value to the domain of a variable.

Parameters:
  • identifier (int)

  • value (Any)

add_equal_constraint(left, right=None, note=None)[source]#

Add an equality constraint.

Parameters:
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:

ConstraintSatisfactionProblem

empty()[source]#
get_domain(identifier)[source]#

Get the domain of a variable.

Parameters:

identifier (int)

Return type:

Set[Any] | None

get_type(identifier)[source]#

Get the type of a variable.

Parameters:

identifier (int)

Return type:

TensorValueTypeBase | PyObjValueType

ground_assignment_value(assignments, identifier)[source]#
Parameters:
Return type:

Any

ground_assignment_value_partial(assignments, identifier)[source]#
Parameters:
Return type:

Any

new_actionable_var(dtype, wrap=False, **kwargs)[source]#

Create a new actionable variable.

Parameters:
Return type:

int | OptimisticValue

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:

int | OptimisticValue

print_assignment_dict(assignments)[source]#
Parameters:

assignments (Dict[int, Assignment])

with_group(expression, note=None)[source]#

Add a group constraint.

Parameters:
constraints: List[Constraint | GroupConstraint]#

A list of constraints.

index2record: Dict[int, OptimisticValueRecord]#

A mapping from variable indices to the variable objects.

index_order: List[Tuple[int, int]]#

A list of tuples (index, index), indicating that the first index should be solved earlier than the second index.