213722, 1201 PM How fo Solve Const Salsfatn Problems (CSP) Using AC‘ Agerthm in Python by Cesar Wiiam Atarenge |The
et Open in app
You have 2 free member-only stories left this month.
Sign up for Medium and get an extra one
a Cesar William Alvarenga - Follow we
Jan 22,2021. 4minread *
How to Solve Constraint Satisfaction Problems
(CSPs) Using AC-3 Algorithm in Python
Al Optimization Algorithm
Photo made with Canva.
a Q 4
hitps/imedium.comIswitvhow-to-solve-constrant-satsfaction-problems-csps-with-ac-3-algrihmin-python-7abbe538cre 182yiai22, 12:01 PM How to Solve Constraint Satisfaction Problems (CSPs) Using AC-2 Algorithm in Python | by Cesar Wiliam Alvarenga | The
1,Get all the constraints and turn each one into two arcs. For example:
A> B becomes A > B and B < A.
2. Add all the arcs to a queue.
3. Repeat until the queue is empty:
3.1. Take the first arc (x, y), off the queue (dequeue).
3.2. For every value in the x domain, there must be some value of the y domain.
3.3. Make x are consistent with y. To do so, remove values from x domain for
which there is no possible corresponding value for y domain.
3.4. If the x domain has changed, add all arcs of the form (k, x) to the queue
(enqueue). Here k is another variable different from y that has a relation to x.
Example
Let’s take this example, where we have three variables A, B, and C and the constraints:
A>BandB=
4
hitps/imedium.comiswhvhow-to-solve-constrant-satsfaction-problems-csps-with-a¢-3-algrithm-in-python-7aSbe538c%e 28Solve Constraint Satisfaction Problems (CSPs) Using AC-3 Algorithm in Python | by Cesar Wiliam AWvarenga | The
The first thing to do is to work out all our arcs. We take the constraint A > B and
generate A > B and B < A. With the constraint B = C ill have B = Cand C = B.
Step 2: Create the Queue
3-algrithrmin-pythe2iiaiz2, 12:01 PM How to Solve Constraint Satisfaction Problems (CSPs) Using AC-2 Algorithm in Python | by Cesar Wiliam Alvarenga | The
Step 3: Iterate Over the Queue
First, we will take the first item in the queue (A > B) and remove it, and we are going
through all the values of the A domain. For each value of A, we will compare it with the
values of B. If there are no values in B for the value of A, we remove this value from A.
As the domain of A has changed, we have to look at the right side of all the arcs.
Whether there are variables for A and that arc is not already in the queue. As we can
see, this is not the case here.
4
hitpssImedium.comlswtvhow-to-solve-constrant-satsfaction-problams-csps-with-a¢-3-algrihm-in-python-7aBbe538cte 48zie, 12:01 PM How to Solve Constraint Satisfaction Problems (CSPs) Using AC-2 Algorithm in Python | by Cesar Wiliam Alvarenga | The
On the second iteration, we take the first item in the queue (B < A). After removing all
the inconsistent values in the B domain, we notice that it has been modified. Looking
at all the arcs and checking whether there is the variable B on the right side, we can see
that the arc A > B is notin the queue, so we have to add it again to the queue.
Now, we will keep doing that until the queue is empty. In the end, we will have the
following state:
4
hitpssImedium.comlswtvhow-to-solve-constrant-satsfaction-problams-csps-with-a¢-3-algrihm-in-python-7aBbe538cte
58zie, 12:01 PM
Solution in Python
Here is the implementation of the AC-3 algorithm (with the previous example) in
Python.
1 donain:
2 al,
3 31,
4 3]
5)
6
7
8 (AY, °B'): lambda a, ba > by
° (8°, 1A"): Lambda
10 (18, °C"): Lambda
u (Ce, "BY: Lambda
2 >
2
a4
45 def revise(x, y)
Fr ™
v7 Make variable “x’ arc consistent with variable “y’.
18 To do so, renove values fron “donains[x]" for which there is no
19 possible corresponding value for “y” in “donains{y)”
28
a Retura True if a revision was made to the donain of “x; return
4
hitpssImedium.comlswtvhow-to-solve-constrant-satsfaction-problams-csps-with-a¢-3-algrihm-in-python-7aBbe538cte
How to Solve Constraint Satisfaction Problems (CSPs) Using AC-2 Algorithm in Python | by Cesar Wiliam Alvarenga | Thezing, 12:01 PM
on
2
2
x
a
2
3
i.
%
x
”
8
2
«
a
2
8
“
6
“
eo
e
®
8
s
2
3
se
ss
7
=
%
«
a
8
“#
6
6
°
és
“
def
How to Solve Constraint Satisfaction Problems (CSPs) Using AC-3 Algorithm in Python | by Cs
Wiliam Alvarenga | The
y_donain = donains[y]
# Get all arc (x, y) constraints
all_constraints = [
constraint for constraint in constraints if constraint[@] == x and constraint[1]
for x_value in x domain:
satisfies = False
for y_value in y_donain:
for constraint in all_constraints:
constraint_func = constraints[constraint]
Af constraint_fune(x_value, y_value):
satisfies = True
if not satisfies
x donain.renove(x value)
revised = True
return revised
Update “domains* such that each variable is arc consistent.
# Add all the arcs to a queue.
queue = arcs[:]
# Repeat until the queue is empty
while queue:
4 Take the first are off the queue (dequeue)
(%, y) = queve.pop(o)
Make x arc consistent with y
revised = revise(x, y)
# IF the x domain has changed
if revised:
# Add all arcs of the form (ky x) to the queue (enqueue)
neighbors = (neighbor for neighbor in arcs if neighbor[1] == x]
queue = queue + neighbors
G) Q 4
hitps/imedium.comiswhvhow-to-solve-constrant-satsfaction-problems-csps-with-a¢-3-algrithm-in-python-7aSbe538c%e
7182yiaiz2, 12:01 PM How to Salve Constraint Satisfaction Problems (CSPs) Using AC-3 Algorithm in Python | by Cesar Wiliam AWvarenga | The
hitps:/Imedium.comIswitvhow-to-solve-constrant-satsfaction-problams-csps-with-a¢-3-algrih-in-python-17aBbe538cte
Ine time complexity of AU+S 1s U(ea”), where e 1s the number of constraints and @ 1s
the size of the maximum domain in a problem.
AC-3 is an algorithm with non-optimal worst-case complexity although, it is simple,
efficient in practice, and widely used.
Sign up for Top 10 Stories
By The Startup
Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories —
delivered straight into your inbox, twice a month, Take a look,
Your email
(— Gettnicnewsletter )
N 7
2y signing up, you will reate a Medium account ifyou dont already have one. Review our Privacy Policy for more information
about our privacy practices,
4
38