-
Notifications
You must be signed in to change notification settings - Fork 0
/
csp2.py
201 lines (165 loc) · 5.21 KB
/
csp2.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'''Implementation of the Generalized Lookahead Search algorithms'''
import json
import copy
import gui
class csp():
def __init__(self, filename, check):
with open(filename) as data_file:
data = json.load(data_file)
self.domain = {}
for var in data["variables"]:
self.domain[var["name"]] = var["domain"]
self.constraints = {}
for c in data["constraints"]:
a = c["scope"][0]
b = c["scope"][1]
self.constraints[(a,b)] = c["relation"]
self.order = data["ordering"]
self.assign = {}
self.gui = gui.gui(self.domain, self.constraints, self.order, check)
# two variables with given values are consistent
def consistent(self, x1, v1, x2, v2):
if((x1,x2) in self.constraints.keys()):
rel = self.constraints[(x1,x2)]
return ([v1,v2] in rel)
elif((x2,x1) in self.constraints.keys()):
rel = self.constraints[(x2,x1)]
return ([v2,v1] in rel)
else:
return True
# a variable value pair is consistent with an assignment
def consistentAssign(self, x1, v1, assignment):
for var in assignment.keys():
if(not self.consistent(x1, v1, var, assignment[var])):
return False
return True
def consistentFull(self, x1, v1, x2, v2, x3, v3, assignment):
return (
self.consistent(x1, v1, x2, v2) and self.consistent(x1, v1, x3, v3) and self.consistent(x3, v3, x2, v2) and
self.consistentAssign(x1, v1, assignment) and self.consistentAssign(x2, v2, assignment) and
self.consistentAssign(x3, v3, assignment))
def revise(self, x, y, z, val, variables, domains, assignment):
Fl=False
for b in domains[variables[x]]:
flag=0
for c in domains[variables[y]]:
if(self.consistentFull(variables[x], b, variables[y], c, variables[z], val, assignment)):
flag = 1
break
if(not flag):
domains[variables[x]].remove(b)
Fl=True
return Fl, domains
def selectValueAC(self, index, var, dom, assignment):
while len(dom[var[index]]):
flag = 0
temp = copy.deepcopy(dom)
val = dom[var[index]][0]
dom[var[index]].remove(val)
removedValue = True
while(removedValue):
removedValue = False
for j in range(index+1, len(var)):
for k in range(index+1,len(var)):
if(not (j==k)):
valR, dom = self.revise(j,k,index,val,var,dom,assignment)
removedValue = valR
self.gui.update(dom,self.assign,var[index], val)
for j in range(index+1, len(var)):
if(not len(dom[var[j]])): # inconsistent
flag = 1
break
if(flag):
temp[var[index]] = copy.deepcopy(dom[var[index]])
dom = copy.deepcopy(temp)
self.gui.update(dom,self.assign,var[index], val)
else:
return val, dom
return None, dom
def selectValueFullAC(self, index, var, dom, assignment):
while len(dom[var[index]]):
flag = 0
temp = copy.deepcopy(dom)
val = dom[var[index]][0]
dom[var[index]].remove(val)
for j in range(index+1, len(var)):
for k in range(index+1,len(var)):
if(not (j==k)):
valR, dom = self.revise(j,k,index,val,var,dom,assignment)
self.gui.update(dom,self.assign,var[index], val)
for j in range(index+1, len(var)):
if(not len(dom[var[j]])): # inconsistent
flag = 1
break
if(flag):
temp[var[index]] = copy.deepcopy(dom[var[index]])
dom = copy.deepcopy(temp)
self.gui.update(dom,self.assign,var[index], val)
else:
return val, dom
return None, dom
def selectValuePartialAC(self, index, var, dom, assignment):
while len(dom[var[index]]):
flag = 0
temp = copy.deepcopy(dom)
val = dom[var[index]][0]
dom[var[index]].remove(val)
for j in range(index+1, len(var)):
for k in range(j+1,len(var)):
valR, dom = self.revise(j,k,index,val,var,dom,assignment)
self.gui.update(dom,self.assign,var[index], val)
for j in range(index+1, len(var)):
if(not len(dom[var[j]])): # inconsistent
flag = 1
break
if(flag):
temp[var[index]] = copy.deepcopy(dom[var[index]])
dom = copy.deepcopy(temp)
self.gui.update(dom,self.assign,var[index], val)
else:
return val, dom
return None, dom
def selectValueFC(self, index, var, dom, assignment):
while len(dom[var[index]]):
flag = 0
temp = copy.deepcopy(dom)
val = dom[var[index]][0]
dom[var[index]].remove(val)
for k in range(index+1, len(var)):
for b in dom[var[k]]:
if(not self.consistent(var[index], val, var[k], b)):
dom[var[k]].remove(b)
self.gui.update(dom,self.assign,var[index], val)
if(not len(dom[var[k]])): # inconsistent
temp[var[index]] = copy.deepcopy(dom[var[index]])
dom = copy.deepcopy(temp)
self.gui.update(dom,self.assign, var[index], val)
flag = 1
break
if(not flag):
return val, dom
return None, dom
def genaralisedLookAhead(self, func):
dom = copy.deepcopy(self.domain)
var = self.order
domains = []
i = 0
while(i>=0 and i<len(var)):
domains.append(copy.deepcopy(dom))
a, dom1 = func(i, var, dom, self.assign)
if(not a):
# print "Backtrack at "+var[i]
i = i-1
dom = domains.pop()
dom = domains.pop()
dom[var[i]] = dom1[var[i]]
self.assign.pop(var[i])
else:
self.assign[var[i]] = a
dom = copy.deepcopy(dom1)
i = i+1
self.gui.update(dom,self.assign)
if i<0:
return None
else:
return self.assign