-
Notifications
You must be signed in to change notification settings - Fork 10
/
DijkstraInstance.ts
174 lines (144 loc) Β· 5.57 KB
/
DijkstraInstance.ts
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
import { injectable } from "inversify";
import TinyQueue from "tinyqueue";
import { DistanceM, DurationMs } from "../../interfaces/units";
import PathfindingGraph from "../graph";
import { IShortestPathInstance } from "../pathfinder";
const POSITION = 0;
const DISTANCE = 1;
const DURATION = 2;
const COST = 3;
/*
interface IState {
position: number;
distance: DistanceM;
duration: DurationMs;
cost: number;
}duration
*/
@injectable()
export class DijkstraInstance implements IShortestPathInstance {
private graph: PathfindingGraph;
private useWeightedCost: boolean;
private costs: Float32Array;
private previousNodes: Float32Array;
constructor(graph: PathfindingGraph) {
this.useWeightedCost = true;
this.graph = graph;
this.costs = new Float32Array();
this.previousNodes = new Float32Array();
}
public setUseWeightedCost(useWeightedCost: boolean) {
this.useWeightedCost = useWeightedCost;
}
public setBreakPoint(on: string, callback: (on: string) => Promise<void>): void {
this.graph.setBreakPoint(on, callback);
}
public removeBreakPoint(on: string): void {
this.graph.removeBreakPoint(on);
}
public async queryPath(from: string, to: string, maxDistance = Infinity) {
let queue: TinyQueue<Float32Array>;
if (this.useWeightedCost) {
queue = new TinyQueue([], (a, b) => a[COST] - b[COST]);
} else {
queue = new TinyQueue([], (a, b) => a[DURATION] - b[DURATION]);
}
this.costs = this.costs.fill(Infinity);
this.previousNodes = this.previousNodes.fill(undefined);
const fromIndex = this.graph.getNodeIndex(from);
this.setCost(fromIndex, 0);
const state = new Float32Array(4);
state[COST] = 0;
state[POSITION] = fromIndex;
state[DISTANCE] = 0;
state[DURATION] = 0;
queue.push(state);
const toIndex = this.graph.getNodeIndex(to);
while (queue.length) {
const [position, distance, duration, cost] = queue.pop();
if (distance > maxDistance) {
// avoid hopeless searches
break;
}
if (position === toIndex) {
// it is done, break the loop and start reconstructing the path
break;
}
if (cost > this.getCost(position)) {
// we have already found a better way
continue;
}
if (this.graph.getBreakPoint(position)) {
await this.graph.getBreakPoint(position)(this.graph.getLabel(position));
}
for (const edge of this.graph.getAdjacencyList()[position]) {
const next = new Float32Array(4);
next[COST] = cost + edge.cost;
next[POSITION] = edge.node;
next[DISTANCE] = distance + edge.distance;
next[DURATION] = duration + edge.duration;
/*const next = {
distance: distance + edge.distance,
duration: duration + edge.duration,
cost: Math.fround(cost + edge.cost),
position: edge.node,
};*/
if (next[COST] < this.getCost(next[POSITION])) {
this.setCost(next[POSITION], next[COST]);
this.previousNodes[next[POSITION]] = position;
queue.push(next);
}
}
}
let currentPosition = toIndex;
const steps = [];
// reconstruct the path
while (!isNaN(this.previousNodes[currentPosition])) {
let nextEdge;
let nextPositionCost = Infinity;
for (const edge of this.graph.getReverseAdjacencyList()[currentPosition]) {
if (this.getCost(edge.node) < nextPositionCost) {
nextPositionCost = this.getCost(edge.node);
nextEdge = edge;
}
}
if (!nextEdge) {
break;
}
steps.push({
from: this.graph.getLabel(nextEdge.node),
to: this.graph.getLabel(currentPosition),
distance: nextEdge.distance,
duration: nextEdge.duration,
});
currentPosition = nextEdge.node;
}
return steps.reverse();
}
private setCost(position: number, cost: number) {
if (position >= this.costs.length) {
const newCosts = new Float32Array(this.graph.getAdjacencyList().length)
.fill(Infinity, this.costs.length);
newCosts.set(this.costs);
this.costs = newCosts;
const newPrevious = new Float32Array(this.graph.getAdjacencyList().length)
.fill(undefined, this.previousNodes.length);
newPrevious.set(this.previousNodes);
this.previousNodes = newPrevious;
}
this.costs[position] = cost;
}
private getCost(position: number): number {
if (position >= this.costs.length) {
const newCosts = new Float32Array(this.graph.getAdjacencyList().length)
.fill(Infinity, this.costs.length);
newCosts.set(this.costs);
this.costs = newCosts;
const newPrevious = new Float32Array(this.graph.getAdjacencyList().length)
.fill(undefined, this.previousNodes.length);
newPrevious.set(this.previousNodes);
this.previousNodes = newPrevious;
}
return this.costs[position];
}
}