-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShortestPathChallenge.py
212 lines (166 loc) · 6.55 KB
/
ShortestPathChallenge.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
from .BaseChallengeGenerator import BaseChallengeGenerator
import random
import coinslib
import heapq
from .Challenge import Challenge
class PriorityQueue:
def __init__(self):
self.elements = []
def empty(self):
return len(self.elements) == 0
def put(self, item, priority):
heapq.heappush(self.elements, (priority, item))
def get(self):
return heapq.heappop(self.elements)[1]
class Grid:
def __init__(self, grid_size):
self.grid_size = grid_size
self.walls = []
self.weights = {}
def in_range(self, pos):
(row, col) = pos
return 0 <= row <= self.grid_size and 0 <= col <= self.grid_size
def walkable(self, pos):
return pos not in self.walls
def neighbors(self, pos):
(row, col) = pos
results = [(row + 1, col), (row - 1, col),
(row, col + 1), (row, col - 1)]
results = filter(self.in_range, results)
results = filter(self.walkable, results)
return results
def cost(self, from_node, to_node):
return self.weights.get(to_node, 1)
def dijkstra_search(grid, start_pos, end_pos):
frontier = PriorityQueue()
frontier.put(start_pos, 0)
came_from = {start_pos: None}
cost_so_far = {start_pos: 0}
while not frontier.empty():
current = frontier.get()
if current == end_pos:
break
for next in grid.neighbors(current):
new_cost = cost_so_far[current] + grid.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
def reconstruct_path(came_from, start_pos, end_pos):
current = end_pos
path = [current]
while current != start_pos:
current = came_from[current]
path.append(current)
path.append(start_pos)
path.reverse()
fixed_path = []
for coord in path:
if len(fixed_path) == 0:
fixed_path.append(coord)
else:
if fixed_path[-1] != coord:
fixed_path.append(coord)
return fixed_path
class ShortestPathChallenge(BaseChallengeGenerator):
def __init__(self, config_file):
BaseChallengeGenerator.__init__(self, 'shortest_path', config_file)
self.debug_output = False
self.read_parameters()
def read_parameters(self):
self.config_file.read_file()
self.parameters["grid_size"] = self.config_file.get_int(
'shortest_path.grid_size', 25)
self.parameters["nb_blockers"] = self.config_file.get_int(
'shortest_path.nb_blockers', 80)
self.debug_output = self.config_file.get_bool(
'shortest_path.debug_output', False)
self.read_nonce_limit()
def save_grid(self, grid, start_pos, end_pos, nonce, path):
fp = open('grid_{0}.txt'.format(nonce), 'w')
for row in range(self.parameters["grid_size"]):
line = ""
for col in range(self.parameters["grid_size"]):
if (row, col) in grid.walls:
line += "x "
elif (row, col) == start_pos:
line += "s "
elif (row, col) == end_pos:
line += "e "
elif (row, col) in path:
line += "p "
else:
line += " "
fp.write(line + "\n")
fp.close()
def generate(self, previous_hash):
self.read_parameters()
solution = None
while True:
nonce = random.randint(self.nonce_min, self.nonce_max)
print(
"Generating {0} problem nonce = {1}".format(
self.problem_name, nonce))
try:
solution = self.generate_solution(previous_hash, nonce)
break
except Exception as e:
print("No solution exists with nonce = {0}".format(nonce))
return solution
def generate_solution(self, previous_hash, nonce):
solution_string = ""
grid = Grid(self.parameters["grid_size"])
seed_hash = self.generate_seed_hash(previous_hash, nonce)
# seed is the last solution hash suffix, else it's 0
prng = coinslib.MT64(coinslib.seed_from_hash(seed_hash))
for i in range(self.parameters["grid_size"]):
# placing extremity walls
grid.walls.append((i, 0))
grid.walls.append((i, self.parameters["grid_size"] - 1))
if i > 0 and i < (self.parameters["grid_size"] - 1):
grid.walls.append((0, i))
grid.walls.append((self.parameters["grid_size"] - 1, i))
start_pos = (
prng.extract_number() %
self.parameters["grid_size"],
prng.extract_number() %
self.parameters["grid_size"])
while start_pos in grid.walls:
start_pos = (
prng.extract_number() %
self.parameters["grid_size"],
prng.extract_number() %
self.parameters["grid_size"])
end_pos = (
prng.extract_number() %
self.parameters["grid_size"],
prng.extract_number() %
self.parameters["grid_size"])
while end_pos in grid.walls or start_pos == end_pos:
end_pos = (
prng.extract_number() %
self.parameters["grid_size"],
prng.extract_number() %
self.parameters["grid_size"])
# placing walls
for i in range(self.parameters["nb_blockers"]):
# wall pos (row, col)
block_pos = (
prng.extract_number() %
self.parameters["grid_size"],
prng.extract_number() %
self.parameters["grid_size"])
if block_pos != start_pos and block_pos != end_pos and block_pos not in grid.walls:
grid.walls.append(block_pos)
path = []
came_from, cost_so_far = dijkstra_search(grid, start_pos, end_pos)
path = reconstruct_path(came_from, start_pos, end_pos)
for coord in path:
solution_string += "{0}{1}".format(coord[0], coord[1])
if self.debug_output:
self.save_grid(grid, start_pos, end_pos, nonce, path)
hash = self.generate_hash(solution_string)
return Challenge(self.problem_name, nonce,
solution_string, hash, self.parameters)