-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
230 lines (207 loc) · 8.32 KB
/
solver.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
from collections import deque
import heapq
import numpy as np
from hungarian import Hungarian
import math
def hungarianDistance(method):
def calc(state, cache):
if 'hungarian' not in cache:
cache['hungarian'] = {}
player = state.getPlayerPosition()
boxes = state.getBoxes()
targets = state.getTargets()
key = (",".join([str(x[0]) + "-" + str(x[1]) for x in boxes]),
",".join([str(x[0]) + "-" + str(x[1]) for x in targets]))
total = 0
if key in cache['hungarian']:
total = cache['hungarian'][key]
else :
distance_list = []
for b in boxes:
distance_list.append([method(b, t) for t in targets])
if len(distance_list) is 0:
return 1
array = np.array(distance_list, dtype='float64')
hungarian = Hungarian(array)
hungarian.calculate()
total = hungarian.get_total_potential()
cache['hungarian'][key] = total
total += sum([method(player, b) for b in boxes] or [0])
return total
return calc
def distance(method):
def calc(state, cache):
# TODO: We could cache a lot of this. In most states
# the position of most boxes don't change.
if 'min_distance' not in cache:
cache['min_distance'] = {}
player = state.getPlayerPosition()
boxes = state.getBoxes()
targets = state.getTargets()
total = 0
key = (",".join([str(x[0]) + "-" + str(x[1]) for x in boxes]),
",".join([str(x[0]) + "-" + str(x[1]) for x in targets]))
if key in cache['min_distance']:
total = cache['min_distance'][key]
else:
for b in boxes:
total += min([method(b, t) for t in targets] or [0])
cache['min_distance'][key] = total
total += sum([method(player, b) for b in boxes] or [0])
return total
return calc
def default(key, cache):
if key is 'Move':
return 1
elif key is 'Push':
return 2
elif key is 'PushOut':
return 10
def cost2(key, cache):
if key is 'Move':
return 2
elif key is 'Push':
return 1
elif key is 'PushOut':
return 2
class solver():
cache = {}
costs = {
"none": lambda key, cache: 1,
"default": default,
"cost2": cost2
}
global distance
heuristic = {
"manhatten": distance(lambda a, b: abs(a[0] - b[0]) + abs(a[1] - b[1])),
"none": lambda x, y: 0,
"hungarian": hungarianDistance(lambda a, b: abs(a[0] - b[0]) + abs(a[1] - b[1])),
"euclidean": distance(lambda x, y: math.sqrt((x[0]-y[0])**2 + (x[1]-y[1])**2)),
"hungarian_euclidean": hungarianDistance(lambda x, y: math.sqrt((x[0]-y[0])**2 + (x[1]-y[1])**2))
}
def refresh(self):
self.cache = {}
def dfs(self, startState, maxDepth=150, cache={}):
stack = deque([(startState, "")])
while len(stack) > 0:
state, actions = stack.pop()
cache[state.toString()] = len(actions)
if state.isSuccess():
return (actions,len(cache))
if state.isFailure():
continue
if len(actions) is maxDepth:
continue
for (action, _) in state.getPossibleActions():
successor = state.successor(action)
# Don't go to an explored state
if successor.toString() in cache and cache[successor.toString()] <= len(actions) + 1:
continue
# # Don't go to a state already marked for visit
# if next((x for (x, _) in stack if x.toString() is successor.toString()), None) is not None:
# continue
stack.append((successor, actions + action))
return ("",0)
def back(self, startState, maxDepth=float('inf'), cache={}):
options = []
stack = deque([(startState, "")])
while len(stack) > 0:
state, actions = stack.pop()
cache[state.toString()] = len(actions)
if state.isSuccess():
options.append(actions);
continue
if state.isFailure():
continue
if len(actions) is maxDepth:
continue
for (action, _) in state.getPossibleActions():
successor = state.successor(action)
# Don't go to an explored state
if successor.toString() in cache and cache[successor.toString()] <= len(actions) + 1:
continue
# # Don't go to a state already marked for visit
# if next((x for (x, _) in stack if x.toString() is successor.toString()), None) is not None:
# continue
stack.append((successor, actions + action))
if len(options) is 0:
return ("",0)
return (min(options, key=lambda x: len(x)), len(cache))
def bfs(self, startState, maxDepth=float('inf'), cache={}):
return self.ucs(startState, cache=cache, cost="none")
def ucs(self, startState, cost="default", maxCost=500, cache={}):
return self.astar(startState, cost=cost, maxCost=maxCost, cache=cache, heuristic="none")
def astar(self, startState, maxCost=1000, cost="default", heuristic="hungarian", cache={}):
h = self.heuristic[heuristic]
costCalc = self.costs[cost]
queue = PriorityQueue()
action_map = {}
startState.h = h(startState, self.cache)
queue.update(startState, startState.h)
action_map[startState.toString()] = ""
while not queue.empty():
state, cost = queue.removeMin()
actions = action_map[state.toString()]
cache[state.toString()] = len(actions)
if state.isSuccess():
return (actions,len(cache))
if state.isFailure():
continue
if cost >= maxCost:
continue
for (action, cost_delta) in state.getPossibleActions():
successor = state.successor(action)
# Don't go to an explored state again
if successor.toString() in cache:
continue
old = action_map[successor.toString()] if successor.toString(
) in action_map else None
if not old or len(old) > len(actions) + 1:
action_map[successor.toString()] = actions + action
successor.h = h(successor, self.cache)
queue.update(successor, cost + costCalc(cost_delta, self.cache) + successor.h - state.h)
return ("",0)
def dfsid(self, startState, maxDepth=500):
i = 1
# while True:
while i<=maxDepth:
val = self.dfs(startState, maxDepth=i, cache={})
if val[0] is not "":
return val
# elif i < maxDepth:
i = i + 1
#Modified for metrics creation
return ("",0)
# def astarid(self):
# pass
def manhattenDistance(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
# Data structure for supporting uniform cost search.
class PriorityQueue:
def __init__(self):
self.DONE = -100000
self.heap = []
self.priorities = {} # Map from state to priority
# Insert |state| into the heap with priority |newPriority| if
# |state| isn't in the heap or |newPriority| is smaller than the existing
# priority.
# Return whether the priority queue was updated.
def update(self, state, newPriority):
oldPriority = self.priorities.get(state)
if oldPriority == None or newPriority < oldPriority:
self.priorities[state] = newPriority
heapq.heappush(self.heap, (newPriority, state))
return True
return False
# Returns (state with minimum priority, priority)
# or (None, None) if the priority queue is empty.
def removeMin(self):
while len(self.heap) > 0:
priority, state = heapq.heappop(self.heap)
if self.priorities[state] == self.DONE:
continue # Outdated priority, skip
self.priorities[state] = self.DONE
return (state, priority)
return (None, None) # Nothing left...
def empty(self):
return len(self.heap) is 0