python-constraint2 2.5.0__cp314-cp314-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- constraint/__init__.py +38 -0
- constraint/constraints.c +56770 -0
- constraint/constraints.py +1572 -0
- constraint/domain.c +9866 -0
- constraint/domain.py +102 -0
- constraint/parser.c +24324 -0
- constraint/parser.py +446 -0
- constraint/problem.c +18512 -0
- constraint/problem.py +305 -0
- constraint/solvers.c +30180 -0
- constraint/solvers.py +788 -0
- python_constraint2-2.5.0.dist-info/METADATA +252 -0
- python_constraint2-2.5.0.dist-info/RECORD +15 -0
- python_constraint2-2.5.0.dist-info/WHEEL +4 -0
- python_constraint2-2.5.0.dist-info/licenses/LICENSE +23 -0
constraint/domain.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Module containing the code for the Variable and Domain classes."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def check_if_compiled() -> bool:
|
|
5
|
+
"""Check if this code has been compiled with Cython.
|
|
6
|
+
|
|
7
|
+
Returns:
|
|
8
|
+
bool: whether the code has been compiled.
|
|
9
|
+
"""
|
|
10
|
+
try:
|
|
11
|
+
from cython import compiled
|
|
12
|
+
|
|
13
|
+
return compiled
|
|
14
|
+
except ImportError or ModuleNotFoundError:
|
|
15
|
+
return False
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ----------------------------------------------------------------------
|
|
19
|
+
# Variables
|
|
20
|
+
# ----------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Variable:
|
|
24
|
+
"""Helper class for variable definition.
|
|
25
|
+
|
|
26
|
+
Using this class is optional, since any hashable object,
|
|
27
|
+
including plain strings and integers, may be used as variables.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, name):
|
|
31
|
+
"""Initialization method.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
name (string): Generic variable name for problem-specific
|
|
35
|
+
purposes
|
|
36
|
+
"""
|
|
37
|
+
self.name = name
|
|
38
|
+
|
|
39
|
+
def __repr__(self):
|
|
40
|
+
"""Represents itself with the name attribute."""
|
|
41
|
+
return self.name
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
Unassigned = Variable("Unassigned") #: Helper object instance representing unassigned values
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ----------------------------------------------------------------------
|
|
48
|
+
# Domains
|
|
49
|
+
# ----------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Domain(list):
|
|
53
|
+
"""Class used to control possible values for variables.
|
|
54
|
+
|
|
55
|
+
When list or tuples are used as domains, they are automatically
|
|
56
|
+
converted to an instance of that class.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
def __init__(self, set):
|
|
60
|
+
"""Initialization method.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
set: Set of values, comparable by equality, that the given variables may assume.
|
|
64
|
+
"""
|
|
65
|
+
list.__init__(self, set)
|
|
66
|
+
self._hidden = []
|
|
67
|
+
self._states = []
|
|
68
|
+
|
|
69
|
+
def resetState(self):
|
|
70
|
+
"""Reset to the original domain state, including all possible values."""
|
|
71
|
+
self.extend(self._hidden)
|
|
72
|
+
del self._hidden[:]
|
|
73
|
+
del self._states[:]
|
|
74
|
+
|
|
75
|
+
def pushState(self):
|
|
76
|
+
"""Save current domain state.
|
|
77
|
+
|
|
78
|
+
Variables hidden after that call are restored when that state is popped from the stack.
|
|
79
|
+
"""
|
|
80
|
+
self._states.append(len(self))
|
|
81
|
+
|
|
82
|
+
def popState(self):
|
|
83
|
+
"""Restore domain state from the top of the stack.
|
|
84
|
+
|
|
85
|
+
Variables hidden since the last popped state are then available again.
|
|
86
|
+
"""
|
|
87
|
+
diff = self._states.pop() - len(self)
|
|
88
|
+
if diff:
|
|
89
|
+
self.extend(self._hidden[-diff:])
|
|
90
|
+
del self._hidden[-diff:]
|
|
91
|
+
|
|
92
|
+
def hideValue(self, value):
|
|
93
|
+
"""Hide the given value from the domain.
|
|
94
|
+
|
|
95
|
+
After that call the given value won't be seen as a possible value on that domain anymore.
|
|
96
|
+
The hidden value will be restored when the previous saved state is popped.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
value: Object currently available in the domain
|
|
100
|
+
"""
|
|
101
|
+
list.remove(self, value)
|
|
102
|
+
self._hidden.append(value)
|