-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuristiche food
372 lines (320 loc) · 11.8 KB
/
euristiche food
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#PASSA TUTTO
-----------------------------------------------------------------
#nodi espansi: 725 (tutti i test vanno :) :) )
import util
foodsList=foodGrid.asList()
distBetweenExtremeFoods = 0
distPositionToClosestFood = None
for food1 in foodsList:
for food2 in foodsList:
if (food1, food2) in problem.heuristicInfo:
if problem.heuristicInfo[(food1, food2)] > distBetweenExtremeFoods:
distBetweenExtremeFoods = problem.heuristicInfo[(food1, food2)]
elif (food2, food1) in problem.heuristicInfo:
if problem.heuristicInfo[(food2, food1)] > distBetweenExtremeFoods:
distBetweenExtremeFoods = problem.heuristicInfo[(food2, food1)]
else:
problem.heuristicInfo[(food1, food2)] = mazeDistance(food1, food2, problem.startingGameState)
if problem.heuristicInfo[(food1, food2)] > distBetweenExtremeFoods:
distBetweenExtremeFoods = problem.heuristicInfo[(food1, food2)]
for food in foodsList:
if (position, food) in problem.heuristicInfo:
if distPositionToClosestFood == None or distPositionToClosestFood > problem.heuristicInfo[(position, food)]:
distPositionToClosestFood = problem.heuristicInfo[(position, food)]
elif (food, position) in problem.heuristicInfo:
if distPositionToClosestFood == None or distPositionToClosestFood > problem.heuristicInfo[(food, position)]:
distPositionToClosestFood = problem.heuristicInfo[(food, position)]
else:
problem.heuristicInfo[(position, food)] = mazeDistance(position, food, problem.startingGameState)
if distPositionToClosestFood == None or distPositionToClosestFood > problem.heuristicInfo[(position, food)]:
distPositionToClosestFood = problem.heuristicInfo[(position, food)]
if distPositionToClosestFood == None:
distPositionToClosestFood = 0
heuristicValue = distBetweenExtremeFoods + distPositionToClosestFood
return heuristicValue
def getNumWalls(walls, ll, ur):
numWalls = 0
for i in range(ll[0], ur[0]):
for j in range(ll[1], ur[1]):
if (walls[i][j]):
numWalls = numWalls + 1
return numWalls
-----------------------------------------------------------------
#nodi espansi: 8109
top, right = problem.walls.height-2, problem.walls.width-2
foodList = foodGrid.asList()
closestDistance = top + right
if len(foodList) > 0:
for food in foodList:
dist = abs(position[0] - food[0]) + abs(position[1] - food[1])
if dist < closestDistance:
closestDistance = dist
else:
closestDistance = 0
furthestDistance = 0
for f1 in foodList:
for f2 in foodList:
dist = abs(f1[0] - f2[0]) + abs(f1[1] - f2[1])
if dist > furthestDistance:
furthestDistance = dist
return closestDistance + furthestDistance #+ len(foodList) - 2
---------------------------------------------------------------------------
#nodi espansi: 11126
foodlist = foodGrid.asList()
if len(foodlist) == 0:
return 0
num = min(map(lambda foodplace: ((position[0] - foodplace[0]) ** 2 + (position[1] - foodplace[1]) ** 2 ) ** 0.5, foodlist))
return num + len(foodlist) - 1
----------------------------------------------------------------------------
#nodi espansi: 10576
closestDistance = 0
for foodCord in foodGrid.asList():
xy1 = foodCord
xy2 = position
distance = abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])
if distance > closestDistance:
closestDistance = distance
return closestDistance
-----------------------------------------------------------------------------
#NON PASSANO
-------------------------------------------------------------------
# used as keys for the dictionary
DISTANCES = 'Distances'
# Helper functions ***
def memoryDistances():
return problem.heuristicInfo[DISTANCES]
def getFoodGrid (state):
return state[1]
def getFoodList(state):
return getFoodGrid(state).asList()
def getFoodDistances():
"""Returns the distance matrix of the original food items"""
return problem.heuristicInfo[DISTANCES]
def mnhttn_dst( pos1, pos2):
x1,y1 = pos1
x2, y2 = pos2
return abs(x1 - x2) + abs(y1 - y2)
def getCurrentPosition(state):
return state[0]
def getFoodCoord(food_index):
return problem.heuristicInfo[food_index]
def getDistance(food_index1, food_index2):
return problem.heuristicInfo[DISTANCES][food_index1][food_index2]
def calculateDistance(coord1, coord2):
"""Distance function"""
# need to write this guy
def stillFood(food_index, food_config):
"""Returns boolean, whether the food_index is present in the given food
config or not"""
# end helper functions ***
def isFirstRun():
"""Tells us whether this is the first time heuristic has been called or
not"""
return not (DISTANCES in problem.heuristicInfo)
def initialize():
"""Set up the distances matrix, store in problem.heuristicInfo"""
points = []
foodList = getFoodList(state)
j = 0
for pos in foodList:
points.append(pos)
problem.heuristicInfo[j] = pos
problem.heuristicInfo[pos] = j
j += 1
distances = [ [ [] for i in foodList] for j in foodList]
for i in range(len(foodList)):
for k in range(len(foodList)):
distances[i][k] = mnhttn_dst(foodList[i], foodList[k])
problem.heuristicInfo[DISTANCES] = distances
def getMinUneatenNeighbour(food_index, food_config, distances):
"""Returns the distance to the nearest uneaten neighbour/current position"""
distances_vector = distances[food_index]
min_distance = -1
for other_index in range(distances[food_index]):
distance = distances[food_index][other_index]
if other_index != food_index and stillFood(other_index, food_config):
if min_distance == -1:
min_distance = distance
elif distance < min_distance:
min_distance = distance
return min_distance
def getFoodConfig(state):
fd_crd_lst = getFoodList(state)
fd_indx_lst = []
for fd_crd in fd_crd_lst:
fd_indx_lst.append(problem.heuristicInfo[fd_crd])
fd_indx_lst.sort()
return tuple(fd_indx_lst)
def getDistances(state, foodConfig):
"""Returns the distances matrix, with the last row, column added for the
current position"""
import copy
distances = copy.deepcopy(getFoodDistances())
size = len(distances)
current_position = getCurrentPosition(state)
final_row = []
for i in range(len(distances)):
food_coord = getFoodCoord(i)
dist = calculateDistance(food_coord, current_position)
distances[i].append(dist)
final_row.append(dist)
distances.append(final_row)
return distances
def getMinPathCost(state, foodConfig):
"""We assume distances is a 2x2 matrix of the distances
Food config tells us whatever foods are still valid"""
distances = getDistances(state, foodConfig)
n = len(distances)
cost = 0
for i in range(n):
if (stillFood(i, foodConfig)):
cost += getMinUneatenNeighbour(i, foodConfig, distances)
return cost
# End helpers
position, foodGrid = state
if isFirstRun():
initialize()
fd_cnfg = getFoodConfig(state)
if len(fd_cnfg) == 0:
return 0
#if not isSeen(fd_cnfg):
#setConfigPaths(fd_cnfg)
min_cost = getMinPathCost(state, fd_cnfg)
return min_cost
-------------------------------------------------------------------
max=-1
maxdot=position
for X in foodGrid.asList(): #find the farthest dot
tmp=abs(position[0]-X[0])+abs(position[1]-X[1])
if(tmp>max):
max=tmp
maxdot=X
dim=position[0]-maxdot[0]
dotnumber=0
if dim>0 :
"""
count the nubmer of dots which's direction is different from farthest
"""
for X in foodGrid.asList():
if (position[0]-X[0])<0 :
dotnumber+=1
elif dim<0:
for X in foodGrid.asList():
if (position[0]-X[0])>0 :
dotnumber+=1
else:
for X in foodGrid.asList():
if (position[0]-X[0])!=0 :
dotnumber+=1
return max+dotnumber
--------------------------------------------------------------------
foodList = list(foodGrid.asList())
gs = problem.startingGameState
if not problem.heuristicInfo.get('foodDistanceGrid', False):
problem.heuristicInfo['foodDistanceGrid'] = {}
foodDistanceGrid = problem.heuristicInfo['foodDistanceGrid']
totalDistance = 0
#if len(foodList) > 0:
return totalDistance
---------------------------------------------------------------------
# NON PASSANO TEST CONSISTENZA
---------------------------------------------------------------------
class FasterPriorityQueue:
"""
Implements a priority queue data structure. This differs from the
PriorityQueue in that it allows multiple copies of the same object,
and doesn't support getPriority or changing priority.
"""
def __init__(self):
self.heap = []
def push(self, item, priority):
pair = (priority,item)
heapq.heappush(self.heap,pair)
def pop(self):
(priority,item) = heapq.heappop(self.heap)
return item
def isEmpty(self):
return len(self.heap) == 0
def heuFOOd
return myFunc7(state)
def buildGraph(foodGrid,PMpos):
V=set()
E=util.FasterPriorityQueue()
listFood=foodGrid.asList()
listFood.append(PMpos)
while len(listFood)!=0:
verTex=listFood.pop()
V.add(verTex)
for restVertex in listFood:
W=manhattanDistance(verTex,restVertex)
pair =(verTex,restVertex)
E.push(pair,W)
return (E,V)
def buildListFood(state):
PMpos=state[0]
foodGrid=state[1]
listFood=foodGrid.asList()
returnList=[]
listDistance=[]
startAt=PMpos
totalpath=0
#if len(listFood)<=5: return (listFood,[],len(listFood))
while len(listFood)>0 :
closestFood=listFood[0]
min=manhattanDistance(startAt,listFood[0])
for food in listFood:
if (manhattanDistance(startAt,food)<=min):
min=manhattanDistance(startAt,food)
closestFood=food
returnList.append(closestFood)
listFood.remove(closestFood)
startAt=closestFood
totalpath+=min
listDistance.append(min)
return (returnList,listDistance,totalpath)
def myFunc1(state):
return state[1].count()
def myFunc6(state):
listTransverse,listMahantan,totalPath=buildListFood(state)
if state[1].count() ==0 : return 0
return totalPath
def myFunc7(state):
PMpos=state[0]
foodGrid=state[1]
G=buildGraph(foodGrid, PMpos)
totalpath,mst_tree=mst(G)
return totalpath
def mst(G):
E=G[0]
V=G[1]
mst_Tree=[]
listofSet=list()
totalpath=0;
for verTex in V:
newSet=set()
newSet.add(verTex)
listofSet.append(newSet)
count =0
while not(E.isEmpty()):
e=E.pop()
count+=1
p1=e[0]
p2=e[1]
for aSet in listofSet:
if (p1 in aSet):
p1Set=aSet
if (p2 in aSet):
p2Set=aSet
if (p1Set==p2Set):
continue
else:
w=util.manhattanDistance(p1,p2)
newSet=set(p1Set.union(p2Set))
listofSet.remove(p1Set)
listofSet.remove(p2Set)
listofSet.append(newSet)
totalpath+=w
mst_Tree.append(e)
if (len(listofSet)==1) : break
return (totalpath,mst_Tree)
-------------------------------------------------------------------------------