-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpath.d
406 lines (347 loc) · 11.1 KB
/
dpath.d
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/rdmd
import core.memory;
import core.time;
import std.c.stdlib: exit;
import std.container;
import std.json;
import std.file;
import std.math;
import std.stdio;
import std.string;
enum DIRECTIONS = [XYZ(1, 0, 0), XYZ(-1, 0, 0),
XYZ(0, 1, 0), XYZ(0, -1, 0)];
enum NOT_PASSABLE = 0;
alias Vertex[XYZ] Graph;
immutable struct XYZ {
immutable long x, y, z;
string toString() {
return format("XYZ(%s, %s, %s)", x, y, z);
}
}
class Vertex {
XYZ xyz;
Connection[] connections;
@property long x() { return xyz.x; }
@property long y() { return xyz.y; }
@property long z() { return xyz.z; }
this(XYZ xyz) {
this.xyz = xyz;
}
override string toString() {
return format("V[%s,%s,%s]", x, y, z);
}
}
struct Connection {
long cost;
Vertex destination;
this(long cost, Vertex destination) {
this.cost = cost;
this.destination = destination;
}
string toString() {
return format("C(%s, %s)", cost, destination);
}
}
struct PathCosts {
long g, h;
Vertex vertex;
Vertex parent;
@property long f() {
return g + h;
}
string toString() {
return format("P%s", vertex);
}
int opCmp(PathCosts other) {
if (this.f == other.f) {
return 0;
} else if (this.f > other.f) {
return 1;
} else {
return -1;
}
}
}
float calculationTime(TickDuration start_time) {
return (TickDuration.currSystemTick() - start_time).msecs / 1000.0;
}
long distanceHeuristic(Vertex v1, Vertex v2) {
return (abs(v1.xyz.x - v2.xyz.x) +
abs(v1.xyz.y - v2.xyz.y) +
abs(v1.xyz.z - v2.xyz.z));
}
Vertex[] findPath(Vertex src, Vertex dst) {
auto start_time = TickDuration.currSystemTick();
Vertex[] path;
PathCosts[Vertex] costs;
bool[Vertex] opened, closed;
long g, h;
if (src == dst) {
return path;
}
h = distanceHeuristic(src, dst);
auto vertex = src;
auto path_vert = PathCosts(0, h, vertex, null);
costs[vertex] = path_vert;
auto queue = new RedBlackTree!(PathCosts, "a.f < b.f", true)(path_vert);
while (!queue.empty()) {
path_vert = queue.front();
queue.removeFront();
vertex = path_vert.vertex;
if (vertex in closed) {
continue;
}
if (vertex == dst) {
break;
}
// move field to closed
opened.remove(vertex);
closed[vertex] = true;
// check every neighbouring fields
foreach (ref connection; vertex.connections) {
auto cost = connection.cost;
auto neighbour = connection.destination;
if (cost == NOT_PASSABLE || neighbour in closed) {
continue;
}
cost += path_vert.g;
if (neighbour in opened) {
auto old_costs = costs[neighbour];
if (cost < old_costs.g) {
// update cost
old_costs.g = cost;
old_costs.parent = vertex;
queue.insert(old_costs);
}
} else {
// add field to opened list
h = distanceHeuristic(neighbour, dst);
auto path_neig = PathCosts(cost, h, neighbour, vertex);
costs[neighbour] = path_neig;
queue.insert(path_neig);
opened[neighbour] = true;
}
}
}
auto path_cost = costs[dst];
while (path_cost.parent) {
path ~= path_cost.parent;
path_cost = costs[path_cost.parent];
}
writefln("astar %.3f %s->%s lenght=%s closed=%s total_cost=%s heur=%s",
calculationTime(start_time), src.xyz, dst.xyz, path.length,
closed.length, costs[dst].g, costs[src].h);
return path;
}
Graph createGraphFromJSON(string json) {
Graph graph;
Vertex getOrCreateVertex(JSONValue json_node) {
Vertex vertex;
auto xyz = XYZ(json_node.array[0].integer,
json_node.array[1].integer,
json_node.array[2].integer);
if (xyz in graph) {
vertex = graph[xyz];
} else {
vertex = new Vertex(xyz);
graph[xyz] = vertex;
}
return vertex;
}
class GraphFromJSONException : Exception {
this(string msg) {
super(msg);
}
}
auto root_node = parseJSON(json);
// validate
if (root_node.type !is JSON_TYPE.OBJECT ||
!("graph" in root_node.object)) {
throw new GraphFromJSONException("Wrong root node formatting");
}
auto graph_node = root_node.object["graph"];
if (graph_node.type !is JSON_TYPE.ARRAY) {
throw new GraphFromJSONException("Wrong graph node formatting");
}
// process graph
foreach (vertex_node; graph_node.array) {
// validate
if (vertex_node.type !is JSON_TYPE.ARRAY ||
vertex_node.array.length != 4 ||
vertex_node.array[0].type !is JSON_TYPE.INTEGER ||
vertex_node.array[1].type !is JSON_TYPE.INTEGER ||
vertex_node.array[2].type !is JSON_TYPE.INTEGER ||
vertex_node.array[3].type !is JSON_TYPE.ARRAY) {
throw new GraphFromJSONException("Wrong vertex formatting");
}
// process
auto vertex = getOrCreateVertex(vertex_node);
foreach (connection_node; vertex_node.array[3].array) {
// validate
if (connection_node.type !is JSON_TYPE.ARRAY) {
throw new GraphFromJSONException("Wrong connection node type");
}
foreach (node; connection_node.array) {
if (node.type !is JSON_TYPE.INTEGER) {
throw new GraphFromJSONException(format(
"Wrong connection node element type %s",
node.type));
}
}
// process
auto destination = getOrCreateVertex(connection_node);
long cost = connection_node.array[3].integer;
vertex.connections ~= Connection(cost, destination);
}
}
return graph;
}
Vertex[] getSampleFromJSON(Graph graph, string json) {
Vertex[] sample = [];
class SampleFromJSONException : Exception {
this(string msg) {
super(msg);
}
}
Vertex getVertex(JSONValue json_node) {
Vertex vertex;
auto xyz = XYZ(json_node.array[0].integer,
json_node.array[1].integer,
json_node.array[2].integer);
if (!(xyz in graph)) {
throw new SampleFromJSONException("Vertex not present in graph");
}
return graph[xyz];
}
auto root_node = parseJSON(json);
// validate
if (root_node.type !is JSON_TYPE.OBJECT ||
!("sample" in root_node.object)) {
throw new SampleFromJSONException("Wrong root node formatting");
}
auto sample_node = root_node.object["sample"];
if (sample_node.type !is JSON_TYPE.ARRAY) {
throw new SampleFromJSONException("Wrong sample node formatting");
}
// process sample
foreach (vertex_node; sample_node.array) {
// validate
if (vertex_node.type !is JSON_TYPE.ARRAY ||
vertex_node.array.length != 3 ||
vertex_node.array[0].type !is JSON_TYPE.INTEGER ||
vertex_node.array[1].type !is JSON_TYPE.INTEGER ||
vertex_node.array[2].type !is JSON_TYPE.INTEGER) {
throw new SampleFromJSONException("Wrong sample formatting");
}
// process
sample ~= getVertex(vertex_node);
}
return sample;
}
float measureFindPath(Vertex[] sample) {
auto start_time = TickDuration.currSystemTick();
foreach (src; sample) {
foreach (dst; sample) {
findPath(src, dst);
}
}
return calculationTime(start_time);
}
/* ---------- UNITTESTS ---------- */
unittest {
Graph createSimpleGraph(uint size_x, uint size_y) {
enum NON_PASSABLE_VERTICES = [
XYZ(8, 0, 0), XYZ(8, 1, 0), XYZ(9, 0, 0)];
auto start_time = TickDuration.currSystemTick();
Graph graph;
// create some vertices
writeln("Create some vertices");
foreach (x; 0..size_x) {
foreach (y; 0..size_y) {
auto xyz = XYZ(x, y, 0);
graph[xyz] = new Vertex(xyz);
}
}
// create connections
writeln("Create connections");
foreach (key, ref vertex; graph) {
foreach (direction; DIRECTIONS) {
auto xyz = XYZ(vertex.x + direction.x,
vertex.y + direction.y, 0);
if (xyz in graph) {
vertex.connections ~= Connection(1, graph[xyz]);
}
}
}
// create some non-passable vertices
writeln("Create some non-passable vertices");
foreach (xyz; NON_PASSABLE_VERTICES) {
foreach (connection; graph[xyz].connections) {
connection.cost = 0;
foreach (other_conn; connection.destination.connections) {
if (other_conn.destination.xyz == xyz) {
other_conn.cost = 0;
}
}
}
}
writefln("Graph was set up in %.3f s", calculationTime(start_time));
return graph;
}
writeln("START UNITTESTS");
enum SizeX = 100;
enum SizeY = 10;
auto sample_graph = createSimpleGraph(SizeX, SizeY);
auto src = sample_graph[XYZ(1, 1, 0)];
auto dst = sample_graph[XYZ(SizeX - 2, SizeY - 2, 0)];
auto sample_path = findPath(src, dst);
writeln("Testing graph from JSON");
enum TEST_JSON_GRAPH = "{
\"sample\": [
[0,0,0],
[1,0,0],
[0,1,0],
[1,1,0]
],
\"graph\": [
[0,0,0, [
[1,0,0, 1],
[0,1,0, 1]
]],
[1,0,0, [
[0,0,0, 1],
[1,1,0, 1]
]],
[0,1,0, [
[0,0,0, 1],
[1,1,0, 1]
]],
[1,1,0, [
[1,0,0, 1],
[0,1,0, 1]
]]
]
}";
auto json_graph = createGraphFromJSON(TEST_JSON_GRAPH);
auto json_sample = getSampleFromJSON(json_graph, TEST_JSON_GRAPH);
auto json_path = findPath(json_graph[XYZ(0, 0, 0)],
json_graph[XYZ(1, 1, 0)]);
auto json_path2 = findPath(json_sample[0],
json_sample[$ - 1]);
writeln("END UNITTESTS");
}
/* ---------- MAIN ---------- */
void main(string[] args) {
if (args.length < 2) {
writefln("Usage: %s <graph.json>", args[0]);
exit(1);
}
auto json = readText(args[1]);
auto graph = createGraphFromJSON(json);
auto sample = getSampleFromJSON(graph, json);
//GC.disable();
foreach (index; 0..10) {
writefln("%s : %.3f", index, measureFindPath(sample));
}
//GC.enable();
}