-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC3.py
78 lines (70 loc) · 2.46 KB
/
AC3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Travaille TP2 Projet CSP
# Gastli Oussama
# Hanana Nour
# GL4
from CSP import CSP
from copy import deepcopy
class AC3(CSP):
def reviser(self, i, j,ok):
deleted = False
if ok:
assignValueI,assignValueJ=self.assigned[i],self.assigned[j]
index1 = 0
while index1 < len(self.domains[i]):
go = False
val1 = self.domains[i][index1]
for index2, val2 in enumerate(self.domains[j]):
self.variables[i] = val1
self.variables[j] = val2
self.assigned[i] = self.assigned[j] = 1
if self.checkAllConstraints():
go = True
# cleaning:
self.assigned[i],self.assigned[j]=assignValueI,assignValueJ
if go:
break
if not go:
self.domains[i].pop(index1)
index1 -= 1
deleted = True
if len(self.domains[i])==0:
# if one domain gets empty no need to continue the processing because it means that this can't contain a solution
ok=False
break
index1 += 1
return deleted
def AC1(self):
go = True
while go:
go = False
for i in range(len(self.variables)):
for j in range(len(self.variables)):
if i != j:
self.reviser(i, j)
def rec_reviser(self, i, j):
ok=True
if self.assigned[i]*self.assigned[j]==0:
change = self.reviser(i, j,ok)
if not ok:
return ok
if change:
for k in self.constraints_graph[i]:
self.rec_reviser(i, k)
return ok
def initArcConsistency3(self):
go=True
# called only the first time to get an initial consistincy constraints graph
for i in range(len(self.constraints_graph)):
for j in self.constraints_graph[i]:
go=self.rec_reviser(i, j)
if not go:
break
return go
def arcConsistency3(self,index):
go=True
for i in self.constraints_graph[index]:
go1=self.rec_reviser(index,i)
go2=self.rec_reviser(i,index)
if not go1 or not go2:
break
return go