This commit is contained in:
2025-09-23 15:04:02 +02:00
parent 4e65d200d3
commit 41edee1bc9

View File

@@ -56,8 +56,47 @@ class CSP:
bool bool
False if a domain becomes empty, otherwise True False if a domain becomes empty, otherwise True
""" """
# YOUR CODE HERE (and remove the assertion below) queue = Queue()
assert False, "Not implemented" for edge in self.binary_constraints.keys():
queue.put(edge)
while not queue.empty():
(xi, xj) = queue.get()
if self._revise(xi, xj):
if len(self.domains[xi]) == 0:
return False
for neighboring_edge in [
(a, b)
for (a, b) in self.binary_constraints.keys()
if a != xj and b == xi
]:
queue.put(neighboring_edge)
return True
def _revise(self, xi, xj) -> bool:
"""Internal of ac_3
Makes an arc consistent by shrinking xi's domain.
Parameters
----------
xi, xj
Nodes of the arc
Returns
-------
bool
True if a change was made to the domain of xi, False otherwise
"""
revised = False
for x in set(self.domains[xi]):
if not any(
[(x, y) in self.binary_constraints[(xi, xj)] for y in self.domains[xj]]
):
self.domains[xi].remove(x)
revised = True
return revised
def _consistent(self, var, value, assignment) -> bool: def _consistent(self, var, value, assignment) -> bool:
for v in assignment: for v in assignment: