-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucs.js
127 lines (105 loc) · 3.07 KB
/
ucs.js
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
class Dijkstra {
clear() {
var open = this.openSet && this.openSet.nodes || [];
var closed = this.closedSet || [];
for (var i in open) {
open[i].clear();
}
for (var i in closed) {
closed[i].clear();
}
this.openSet = new BinaryHeap((nodeA, nodeB) => { // Comparison function, compare fCost and hCost
var diff = nodeA.gCost - nodeB.gCost;
return diff;
});
this.closedSet = [];
this.path = [];
}
clearNoHeap() {
var open = this.openSet || [];
var closed = this.closedSet || [];
for (var i in open) {
open[i].clear();
}
for (var i in closed) {
closed[i].clear();
}
this.openSet = [];
this.closedSet = [];
this.path = [];
}
compute(start, end) {
this.clear();
this.openSet.push(start);
while (this.openSet.length > 0) {
var current = this.openSet.pop();
this.closedSet.push(current);
if (current == end) {
this.path = this.retracePath(start, end);
return this.path;
}
for (var i in current.neighbors) {
var neighbor = current.neighbors[i];
if (this.closedSet.indexOf(neighbor) > -1) continue;
var newCost = current.gCost + this.heuristic(current, neighbor); // Distance between start and current
if (newCost < neighbor.gCost || !this.openSet.contains(neighbor)) {
neighbor.gCost = newCost;
neighbor.parent = current;
if (!this.openSet.contains(neighbor)) {
this.openSet.push(neighbor);
} else {
this.openSet.sortUp(neighbor);
}
}
}
}
this.path = [];
return this.path; // Target is impossible to reach
}
computeNoHeap(start, end) {
this.clearNoHeap();
this.openSet.push(start);
while (this.openSet.length > 0) {
var currentIndex = 0;
for (var i in this.openSet) {
if (this.openSet[i].fCost < this.openSet[currentIndex].fCost) {
currentIndex = i;
}
}
var current = this.openSet[currentIndex];
this.openSet.splice(currentIndex, 1);
this.closedSet.push(current);
if (current == end) {
this.path = this.retracePath(start, end);
return this.path;
}
for (var i in current.neighbors) {
var neighbor = current.neighbors[i];
if (this.closedSet.indexOf(neighbor) > -1) continue;
var newCost = current.gCost + this.heuristic(current, neighbor); // Distance between start and current
if (newCost < neighbor.gCost || this.openSet.indexOf(neighbor) == -1) {
neighbor.gCost = newCost;
neighbor.parent = current;
if (this.openSet.indexOf(neighbor) == -1) {
this.openSet.push(neighbor);
}
}
}
}
this.path = [];
return this.path; // Target is impossible to reach
}
heuristic(node, end) {
return node.dist(end);
}
retracePath(start, end) {
var current = end;
var path = [];
while (current.parent) {
path.unshift(current);
current = current.parent;
}
path.unshift(start)
return path;
}
}