-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
318 lines (243 loc) · 6.22 KB
/
main.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
from __future__ import division
import numpy as np
TOLERANCE = 5e-7
EPSILON = 1e-6
h = 0.1
DIRECTIONS = [[h, 0, 0.0], [-h, 0, 0.0], [0, h, 0.0], [0, -h, 0.0]]
ALPHA = 1
def hash_key(r):
return '%.4f;%.4f' % (r[0], r[1])
def dehash(r):
s = r.split(';')
return float(s[0]), float(s[1])
def compare(u, v, h):
return all(np.abs(u - v) < h)
def save_path(path, U, file_name):
with open(file_name, 'w') as f:
for r in path:
f.write("%.8f %.8f %.8f\n" % (r[0], r[1], U(r)))
def U1(r):
return np.sin(r[0]) + np.sin(r[1])
def U2(r):
return np.sinh(r[0]) + np.sinh(r[1])
def U3(r):
return -10 / np.sqrt(r[0]*r[0]+r[1]*r[1])
def U4(r):
return 0.1 * (r[0]*r[0]+r[1]*r[1]) / 2
def dist(p, q, k):
return np.power(p[0]-q[0],2) + np.power(p[1]-q[1],2) + k * np.power(p[2]-q[2], 2)
def weight_v1(r, dr):
return dr[2]
def weight_v2(r, dr):
return dr[2] - r[2]
def calc_energy(path):
return sum(abs(P(path[k]) - P(path[k - 1])) for k in xrange(1, len(path))) * h
return path
def method_1(r0, rf, U, weight, k):
r = r0
d = dist(r, rf, k)
path = [r]
inf = float('inf')
while not compare(r, rf, h):
next_r, min_U = None, inf
for direc in DIRECTIONS:
dr = r + direc
dr[2] = U(dr)
Udr = weight(r, dr)
if dist(dr, rf, k) <= d and Udr < min_U:
next_r = dr
min_U = Udr
if next_r is None:
raise ValueError("No path was found")
r = next_r
d = dist(r, rf, k)
path.append(r)
return path
def method_2(r0, rf, U, weight, k):
# dictionary of precessors
# each item contains a list of all the points that converg to it
G = { }
# dictionary of distances (energy)
D = { }
D[hash_key(r0)] = 0
def _relax(u, v, w):
if D[v] > D[u] + w:
D[v] = D[u] + w
G[v] = u
def loop(r):
hr = hash_key(r)
d = dist(r, rf, k)
if compare(r, rf, h):
return
for direc in DIRECTIONS:
dr = r + direc
hdr = hash_key(dr)
dr[2] = U(dr)
if dist(dr, rf, k) <= d:
if hdr not in G:
G[hdr] = hr
D[hdr] = D[hr] + weight(r, dr)
loop(dr)
_relax(hr, hdr, weight(r, dr))
def find_path(start, end):
"""
Finds the shortest path between start and end.
Returns the path with the energy spend.
"""
if end not in G:
raise ValueError("No path was found")
path = []
while True:
path.append(dehash(end))
if end in start:
break
end = G[end]
return path[::-1]
loop(r0)
return find_path(hash_key(r0), hash_key(rf))
def bellman_ford(r0, rf, weight, U, k):
def build_graph():
"""
Builds the graph with the vertex that verify the condition
d(a, rf) < d(b, rf)
where a is one of the four neighbours of b
"""
G = { }
def loop(r):
hr = hash_key(r)
if hr in G:
return
else:
G[hr] = { }
d = dist(r, rf, k)
for direc in DIRECTIONS:
dr = r + direc
dr[2] = U(dr)
dist_dr = dist(dr, rf, k)
if dist_dr <= d:
hdr = hash_key(dr)
G[hr][hdr] = weight(r, dr)
loop(dr)
loop(r0)
return G
def algo(graph, start, end):
"""
Implementation of Bellman-Ford's shortest paths algorithm.
It can be used for graphs with negative weight edges and if
there is a neg-cycle it will find it.
Complexity: O(V*E)
given that E ~ O(V^2) this runs in cubic time
"""
def _initialise(graph, source):
inf = float('inf')
D = { } # dictionary of distances
P = { } # dictionary of predecessors
for u in graph:
D[u] = inf
P[u] = None
D[source] = 0
return D, P
def _relax(graph, u, v, D, P):
if D[v] > D[u] + graph[u][v]:
D[v] = D[u] + graph[u][v]
P[v] = u
D, P = _initialise(graph, start)
for _ in xrange(len(graph)):
for u in graph:
for v in graph[u]:
_relax(graph, u, v, D, P)
for u in graph:
for v in graph[u]:
if D[v] > D[u] + graph[u][v]:
raise "Negative weight cycle found"
return (D, P)
def find_path(D, P, start, end):
"""
Finds the shortest path between start and end.
returns the path with the weight.
"""
weight = D[end]
path = []
while True:
path.append(dehash(end))
if end == start:
break
end = P[end]
return path[::-1], weight
start, end = hash_key(r0), hash_key(rf)
G = build_graph()
D, P = algo(G, start, end)
return find_path(D, P, start, end)
def dijkstra(r0, rf, weight, U, k):
def build_graph():
"""
Builds the graph with the vertex that verify the condition
d(a, rf) < d(b, rf)
where a is one of the four neighbours of b
"""
G = { }
def loop(r):
hr = hash_key(r)
if hr in G:
return
else:
G[hr] = { }
d = dist(r, rf, k)
for direc in DIRECTIONS:
dr = r + direc
dr[2] = U(dr)
dist_dr = dist(dr, rf, k)
if dist_dr <= d:
hdr = hash_key(dr)
G[hr][hdr] = abs(weight(r, dr))
loop(dr)
loop(r0)
return G
def algo(graph, start, end=None):
"""
Implementation of Dijkstra shortest paths algorithm. This will find
the shortest path to all elements from start (if no end is specified).
In this case each vertex in the graph has a NON-NEGATIVE weight
associated with it, represented in the graph as a dictionary in which
each element u: graph[u] = {(vertex, weight) for each neighbourg of u}
We here use a priority queue (binary heap) to find the nearest
element to the start one, this is the same as doing a min operation
over all the elements in the adjacency dictionary, and takes O(lg V) time.
Complexity: O(V*lg(V) + E*lg(V))
This can be improved by using a fibonacci heap instead of a binary heap.
"""
D, P = {}, {}
Q = PriorityQueue() # estimated distances
Q[start] = 0
for u in Q:
# finds de nearest element
D[u] = Q[u]
if u == end:
break
for v in graph[u]: # relax distances from u
#relax(graph, u, v, D, P)
uv_length = D[u] + graph[u][v]
if v in D:
if uv_length < D[v]:
raise "Already found a better path to go."
elif v not in Q or uv_length < Q[v]:
Q[v] = uv_length
P[v] = u
return (D, P)
def find_path(D, P, start, end):
"""
Finds the shortest path between start and end.
returns the path with the weight.
"""
weight = D[end]
path = []
while True:
path.append(dehash(end))
if end == start:
break
end = P[end]
return path[::-1], weight
start, end = hash_key(r0), hash_key(rf)
G = build_graph()
D, P = algo(G, start, end)
return find_path(D, P, start, end)