forked from ChalmersGU-AI-course/shrdlite-course-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.ts
185 lines (153 loc) · 5.52 KB
/
AStar.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
175
176
177
178
179
180
181
182
183
184
185
/// <reference path="collections.ts" />
module AStar {
//A StaticGraph contains a state of type S and a list of edges to its neighbours
export interface Node<S> {
getChildren() : Edge<S>[];
getState() : S;
getHeuristic() : number;
}
export class StaticNode<S> implements Node<S> {
constructor(public children : Edge<S>[], public h: Heuristic<S>, public state : S){}
getChildren(){
return this.children;
}
getState(){
return this.state;
}
addEdge(edge : Edge<S>){
this.children.push(edge);
}
getHeuristic() {
return this.h(this.state);
}
}
//A path contains the total cost of the path and a list of
//visited nodes
export class Path<S>{
cost : number;
labels : string[];
constructor(private path : Node<S>[]){this.cost = 0; this.labels = [];}
//Returns a new path containing the graph contained in edge
//appended to the old path
push(e:Edge<S>):Path<S>{
var p = new Path(this.path.concat([e.end]));
p.cost = this.cost + e.cost;
p.labels = this.labels.concat([e.label]);
return p;
}
//Returns the cost of the path
weight():number{
return this.cost;
}
//Retrieves the last object on the path
peek():Node<S> {
return this.path[this.path.length - 1];
}
getPath() : Node<S>[] {
return this.path;
}
getLabelPath() : string[] {
return this.labels;
}
}
export interface Goal<S> {
(s : S):boolean;
}
export interface Heuristic<S> {
(s : S) : number;
}
export interface Edge<S> {
cost : number;
label? : string;
end : Node<S>;
}
//A* search function
export function astarSearch<S>(graph:Node<S>,goal:Goal<S>) : Path<S>{
var frontier = new collections.PriorityQueue<Path<S>>(function(a,b) {
return (b.weight() + b.peek().getHeuristic()) - (a.weight() + a.peek().getHeuristic())
});
frontier.add(new Path<S>([ graph]));
var j : number = 0;
var time : number = new Date().getTime();
while(!frontier.isEmpty()) {
var p = frontier.dequeue();
j++;
if(goal(p.peek().getState())) {
alert("Number of iterations: " + j + "\nTime taken: " + (new Date().getTime() - time));
return p;
} else {
if(new Date().getTime() - time > 4000) { throw new AStar.Error("Request timeout") }
var children = p.peek().getChildren();
for( var i = 0; i < children.length; i++ ) {
frontier.add( p.push(children[i]));
}
}
}
}
export class Error implements Error {
public name = "AStar.Error";
constructor(public message? : string) {}
public toString() {return this.name + ": " + this.message}
}
//Simple test
export function test(){
var heur = function(a){return 0;};
var g1 = new StaticNode<number>([],heur,4);
var g2 = new StaticNode([],heur,3);
var g = new StaticNode<number>([{ cost: 1, end: g1},
{cost: 1, end: g2}], heur, 0);
g1.children.push({cost:1, end: g});
var h = astarSearch<number>(g,function(a : number){return a == 3;})
return h;
}
//Complicated test geolocations
export function geoTest() : string[] {
var heur = function(a : string){ //H(n)
if(a == "gothenburg") {
return Math.sqrt(4*4 + 15*15);
} else if(a == "malmo"){
return Math.sqrt(16*16+15*15);
} else if(a == "varnamo"){
return 32;
} else if(a == "mellerud"){
return 42-16;
} else if(a == "boras") {
return 14;
} else if(a == "jonkoping") {
return 15;
} else if(a == "stockholm") {
return 0;
}
};
var l1 = new StaticNode<string>([], heur,"gothenburg");
var l2 = new StaticNode<string>([], heur,"boras");
var l3 = new StaticNode<string>([], heur,"jonkoping");
var l4 = new StaticNode<string>([], heur,"stockholm");
var l5 = new StaticNode<string>([], heur,"malmo");
var l6 = new StaticNode<string>([], heur,"varnamo");
var l7 = new StaticNode<string>([], heur,"mellerud");
l1.addEdge({cost: 4, end: l2});
l2.addEdge({cost: 8, end: l3});
l4.addEdge({cost: 15, end: l2});
l3.addEdge({cost: 16, end: l4});
l3.addEdge({cost: 23, end: l1});
l2.addEdge({cost: 42, end: l4});
l1.addEdge({cost: 4, end: l5});
l1.addEdge({cost: 8, end: l6});
l3.addEdge({cost: 15, end: l7});
l5.addEdge({cost: 16, end: l2});
l6.addEdge({cost: 23, end: l3});
l7.addEdge({cost: 42, end: l3});
var res = astarSearch<string>(l1,
function(a : string){
return a == "stockholm";
});
var resPath : string[];
resPath = [];
for (var i = 0; i < res.getPath().length; i++) {
var g = res.getPath()[i];
resPath.push(g.getState());
}
return resPath;
}
}