forked from martinResearch/puzzleAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemmingsLearnedRulesSAT.py
338 lines (248 loc) · 13.5 KB
/
lemmingsLearnedRulesSAT.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from __future__ import print_function
import sys
from ortools.constraint_solver import pywrapcp
from lemmings import *
from lemmingsLearnRules import *
from lemmingsSAT import *
import lemmingsMILP
import copy
def decisionTreeToCNF(estimator):
# from http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree-plot-unveil-tree-structure-py
# convert the decision tree into two Conjontive Normal Forms that are negation of each other ?
# CNF[0](input) => result
# CNF[1](input) => not(result)
n_nodes = estimator.tree_.node_count
children_left = estimator.tree_.children_left
children_right = estimator.tree_.children_right
feature = estimator.tree_.feature
threshold = estimator.tree_.threshold
value=estimator.tree_.value
# The tree structure can be traversed to compute various properties such
# as the depth of each node and whether or not it is a leaf.
node_depth = np.zeros(shape=n_nodes, dtype=np.int64)
is_leaves = np.zeros(shape=n_nodes, dtype=bool)
rule=[]
CNF=[[] for i in range(2)]
def addrules(node_id):
# If we have a test node
if (children_left[node_id] != children_right[node_id]):
rule.append((feature[node_id],1))
addrules(children_left[node_id])
rule.pop()
rule.append((feature[node_id],0))
addrules(children_right[node_id])
rule.pop()
else:
if value[node_id][0][0]>value[node_id][0][1]:
CNF[0].append(rule[:])
else:
CNF[1].append(rule[:])
addrules(0)
return CNF
def checkCNF(allCNF,solution):
nbFailed=0
for iclause,NF in enumerate(allCNF):
clauseCheck=0
for e in NF:
if e[1]==1:
clauseCheck=clauseCheck or solution[e[0]]
else:
clauseCheck=clauseCheck or (1-solution[e[0]])
if not(clauseCheck) :
nbFailed+=1
return nbFailed
if __name__ == "__main__":
#game=randomGame(height=10, width=10,seed=15,nbMaxBlocks=4,startLeftCorner=True)
game=readGameFile('level.txt')
nbFrames=game.nbFrames
height=game.height
width=game.width
solutionsSAT=solveGame(game)
solutionSAT=solutionsSAT[0] # get a solution using the SAT solver with hand coded constraints , this will be used to check the validity on the learned constraints
#solutionSAT=lemmingsMILP.solveGame(game)
displayLemmingsAnimation(solutionSAT['lemmingsMap'],solutionSAT['obstaclesMap'],game.targetsMap,gifZoom=30)
nbGamestraining=50
games=generateGames(5,5,10,nbLemmings=1,nbGames=30,startLeftCorner=False)
inputs, outputs,inputNames,outputNames=generateTrainingData(games)
useFeatureAugmentation=False
regs=learnModel(inputs, outputs,regressionType='DecisionTree',useFeatureAugmentation=False)
for idOutput in range(2):
displayDecisionTree(regs[idOutput],inputNames,outputNames[idOutput])
lemmingsMaps=np.zeros((nbFrames,height,width,2))
lemmingsMaps[0]=game.lemmingsMapsInit
simulateTrainedModel(regs,lemmingsMaps,game.obstaclesMap,game.targetsMap,game.nbFrames,useFeatureAugmentation=useFeatureAugmentation,useRounding=True)
displayLemmingsAnimation(lemmingsMaps,game.obstaclesMap,game.targetsMap,'images/learnedSimulationWithRounding.gif')
solver = pywrapcp.Solver("lemmings")
localCNF=[decisionTreeToCNF(regs[0]),decisionTreeToCNF(regs[1])]
# printing the local CNF as strings
for d in range(2): # loop over the directions of the lemming
print ('\n\n')
clauses=[]
for b in range(2): # bloolean value of the ouput
for localNF in localCNF[d][b]:
clauseStrs=[]
for e in localNF:
if e[1]==1:
clauseStrs.append(inputNames[e[0]])
else:
clauseStrs.append('not('+inputNames[e[0]])
if b==1:
clauseStrs.append(outputNames[d])
else:
clauseStrs.append('not('+outputNames[d]+')')
clauses.append('('+' or '.join(clauseStrs)+')')
print (' and \n'.join(clauses))
# checking clause on the training data
nbFailed=0
for inp,out in zip(inputs,outputs):
for d in range(2): # loop over the direction of the lemming
for b in range(2): # bloolean vqlue of the ouput
for localNF in localCNF[d][b]:
NF=[]
for r in localNF:
NF.append((inp[r[0]],r[1]))
NF.append((out[d],b))
clauseEval=0
for e in NF:
if e[1]==1:
clauseEval=clauseEval or e[0]
else:
clauseEval=clauseEval or (1-e[0])
if not(clauseEval):
nbFailed+=1
inputs=[]
outputs=[]
nbVars=0
obstaclesMapIds=(np.arange(height*width)+nbVars).reshape(height,width)
nbVars=nbVars+obstaclesMapIds.size
lemmingsMapsIds=(np.arange(nbFrames*height*width*2)+nbVars).reshape(nbFrames,height,width,2)
nbVars=nbVars+lemmingsMapsIds.size
obstaclePatchesIds=sklearn.feature_extraction.image.extract_patches_2d(obstaclesMapIds, [3,3])
allVars=[]
for idVar in range(nbVars):
allVars.append(solver.BoolVar( 'var%d'%idVar))
allSolutionSATAllVars=[]
for idSol in range(len(solutionsSAT)):
solutionSATAllVars=np.zeros((nbVars))
solutionSATAllVars[lemmingsMapsIds]=solutionSAT['lemmingsMap']
solutionSATAllVars[obstaclesMapIds]=solutionSAT['obstaclesMap']
allSolutionSATAllVars.append(solutionSATAllVars)
iFrame=0
for i in range(height):
for j in range(width):
for d in range(2):
solver.Add(allVars[lemmingsMapsIds[iFrame,i,j,d]] == int(game.lemmingsMapsInit[i,j,d]) )
for solutionSATAllVars in allSolutionSATAllVars:
assert(solutionSATAllVars[lemmingsMapsIds[iFrame,i,j,d]] == int(game.lemmingsMapsInit[i,j,d]))
# the learned predictor cqnnot predict the lemmings map on the boundary as we do not use image wrapping in the patch extraction
# we cheat a bit by forning the lemmings to be zero on the boundraries
for iFrame in range(0,nbFrames):
for i in range(height):
for d in range(2):
solver.Add(allVars[lemmingsMapsIds[iFrame,i,0,d]] == 0 )
solver.Add(allVars[lemmingsMapsIds[iFrame,i,width-1,d]] == 0 )
for solutionSATAllVars in allSolutionSATAllVars:
assert(solutionSATAllVars[lemmingsMapsIds[iFrame,i,0,d]] == 0)
assert(solutionSATAllVars[lemmingsMapsIds[iFrame,i,width-1,d]] == 0)
for j in range(width):
for d in range(2):
solver.Add(allVars[lemmingsMapsIds[iFrame,0,j,d]] == 0 )
solver.Add(allVars[lemmingsMapsIds[iFrame,height-1,j,d]] == 0 )
for solutionSATAllVars in allSolutionSATAllVars:
assert(solutionSATAllVars[lemmingsMapsIds[iFrame,0,j,d]] == 0)
assert(solutionSATAllVars[lemmingsMapsIds[iFrame,height-1,j,d]] == 0)
for i in range(height):
for j in range(width):
solver.Add(allVars[obstaclesMapIds[i,j]] > game.obstaclesMap[i,j]-1)
for solutionSATAllVars in allSolutionSATAllVars:
assert(solutionSATAllVars[obstaclesMapIds[i,j]] > game.obstaclesMap[i,j]-1)
for idFrame in range(1,nbFrames):
lemmingsPatchesIds=[]
for d in range(2):
lemmingsPatchesIds.append(sklearn.feature_extraction.image.extract_patches_2d(lemmingsMapsIds[idFrame-1,:,:,d], [3,3]))
inputs.append(np.stack((obstaclePatchesIds[:,None,:,:],lemmingsPatchesIds[0][:,None,:,:],lemmingsPatchesIds[1][:,None,:,:]), axis=1))
outputs.append(lemmingsMapsIds[idFrame,1:-1,1:-1,:].reshape(-1,2))
inputs=np.vstack(inputs)
inputs=inputs.reshape(inputs.shape[0],-1)
outputs=np.vstack(outputs)
outputs=outputs.reshape(outputs.shape[0],-1)
allCNF=[]
for inp,out in zip(inputs,outputs):
for d in range(2): # loop over the direction of the lemming
for b in range(2): # bloolean vqlue of the ouput
for localNF in localCNF[d][b]:
NF=[]
for r in localNF:
NF.append((inp[r[0]],r[1]))
NF.append((out[d],b))
allCNF.append(NF)
for solutionSATAllVars in allSolutionSATAllVars:
nbFailed=0
for iclause,NF in enumerate(allCNF):
clause=[]
clauseCheck=0
for e in NF:
if e[1]==1:
clause.append( allVars[e[0]])
clauseCheck=clauseCheck + solutionSATAllVars[e[0]]
else:
clause.append(1-allVars[e[0]])
clauseCheck=clauseCheck + (1-solutionSATAllVars[e[0]])
if not(clauseCheck>0) :
nbFailed+=1
solver.Add(solver.Sum(clause)>0) # works also with solver.Add(solver.Max(clause)==1)
if nbFailed>0:
print('the solution provided violates %d constraints'%nbFailed)
nbFailed=checkCNF(allCNF,solutionSATAllVars)
# adding target constraint
solver.Add(solver.Sum([allVars[lemmingsMapsIds[nbFrames-1,i,j,d]]*game.targetsMap[i,j] for i in range(height) for j in range(width) for d in range(2)] )==game.nbLemmings)
for solutionSATAllVars in allSolutionSATAllVars:
assert(np.array([solutionSATAllVars[lemmingsMapsIds[nbFrames-1,i,j,d]]*game.targetsMap[i,j] for i in range(height) for j in range(width) for d in range(2)] ).sum()==game.nbLemmings)
# adding constraint that only 3 bricks should be added
nbMaxBlocks=game.nbMaxBlocks
solver.Add(solver.Sum([allVars[obstaclesMapIds[i,j]] for i in range(height) for j in range(width)] )<(nbMaxBlocks+np.sum(game.obstaclesMap)+1))
for solutionSATAllVars in allSolutionSATAllVars:
assert(np.array([solutionSATAllVars[obstaclesMapIds[i,j]] for i in range(height) for j in range(width)] ).sum()<nbMaxBlocks+np.sum(game.obstaclesMap+1))
db = solver.Phase(allVars, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)
solver.Solve(db)
count = 0
solutions=[]
timestart=time.time()
while solver.NextSolution():
count += 1
print("Solution" , count, '\n')
solution=np.zeros((len(allVars)),dtype=np.bool)
for i in range(len(allVars)):
solution[i]=allVars[i].Value()
nbFailed=checkCNF(allCNF,solution)
assert(nbFailed==0)
lemmingsMapsNp=np.zeros((nbFrames,height,width,2))
obstaclesMapNp=np.zeros((height,width))
targetsMapNp=np.zeros((height,width))
for iFrame in range(nbFrames):
for i in range(height):
for j in range(width):
for d in range(2):
lemmingsMapsNp[iFrame,i, j,d]=allVars[lemmingsMapsIds[iFrame,i, j,d]].Value()
for i in range(height):
for j in range(width):
obstaclesMapNp[i, j]=allVars[obstaclesMapIds[i, j]].Value()
solvedGame=copy.deepcopy(game)
lemmingsMap=solvedGame.simulateMap(obstaclesMapNp)
assert(np.all(lemmingsMap==lemmingsMapsNp))
#checkSolution(obstaclesMapNp,game.targetsMap,lemmingsMapsNp,auxVarsNp)
#assert(np.array([solution[lemmingsMapsIds[nbFrames-1,i,j,d]]*solution[targetsMapIds[i,j]] for i in range(height) for j in range(width)] ).sum()==game.nbLemmings)
print (np.sum(np.sum(lemmingsMapsNp[-1],axis=2)*game.targetsMap))
print ('hash lemmingsMapsNp=%s'%hash(lemmingsMapsNp.tostring()))
print ('hash obstaclesMapNp=%s'%hash(obstaclesMapNp.tostring()))
print ('hash targetsMapNp=%s'%hash(targetsMapNp.tostring()))
print ('hash solution=%s'%hash(solution.tostring()))
solutions.append( {'obstaclesMap':obstaclesMapNp,'lemmingsMap':lemmingsMapsNp})
print ('finding %d solutions took %d seconds'%(len(solutions),time.time()-timestart))
print ('finding all solutions took %d seconds'%(time.time()-timestart))
if count==0:
print('could not find any solution')
assert(count==len(allSolutionSATAllVars))# checking we found as many solution wit the the learnt rules as with the hand coded rules
for count,solution in enumerate(solutions):
gifName='images/SolutionLearnedRulesSAT%d.gif'%count
displayLemmingsAnimation(solution['lemmingsMap'],solution['obstaclesMap'],game.targetsMap,gifName=gifName,gifZoom=30)