-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha-star.html
239 lines (220 loc) · 6.3 KB
/
a-star.html
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
<!DOCTYPE html>
<html>
<head>
<title>A* Algorithm</title>
<script src="js/jquery-v.1.11.1-min.js"></script>
<script src="js/jquery-hoverintent.js"></script>
<script src="js/maps.js"></script>
<script src="js/graph.js"></script>
<script src="js/astar.js"></script>
<script src="js/heuristics.js"></script>
<script type="text/javascript">
window.onload = init;
// Global variables
var world = null;
var maps = null;
var astar = null;
var h = null;
// Initialize everything
function init () {
debugMsg("Initializing everything...");
// Set up world
initWorld();
// Load maps
maps = new Maps ();
// Set up heurtistics
h = new Heuristics ();
// Set up A* Context
initAStar();
// Set up forms
initForms();
}
function initWorld () {
world = new Graph (30, 30);
world.init(0.15);
world.assignTable("graph");
world.constructTable();
}
function initAStar () {
astar = new AStar (world, h.manhattan);
astar.init();
}
function initForms () {
// Wall amount
var wall_amt_input = $("input#wall-amt");
var wall_amt_btn = $("button#wall-amt-btn");
wall_amt_btn.click(function () {
var new_wall_amt = parseFloat(wall_amt_input.val());
world.reinit(world.n, world.m, new_wall_amt);
world.constructTable();
astar.init();
});
// World dimensions
var n_input = $("input#n");
var m_input = $("input#m");
var dimensions_btn = $("button#dimensions-btn");
dimensions_btn.click(function () {
var new_n = parseInt(n_input.val());
var new_m = parseInt(m_input.val());
world.reinit(new_n, new_m, world.wall_amt);
world.constructTable();
astar.init();
});
var map_input = $("select#map");
var map_btn = $("button#map-btn");
if (maps !== null) {
$.each(maps, function (val, text) {
$(map_input).append(new Option(val, val));
});
}
map_btn.click(function () {
var map = maps[map_input.val()];
world.initFromMap(map);
world.constructTable();
astar.init();
});
// Diagonals allowed
var diag_input = $("select#diag-allowed");
var diag_btn = $("button#diag-btn");
diag_btn.click(function () {
var yes_no = parseInt($(diag_input).val()) === 1;
astar.setParameters(astar.nswe_cost, astar.diag_cost, yes_no, astar.color_open, astar.h);
});
// G Costs
var nswe_cost = $("input#nswe");
var diag_cost = $("input#diag");
var gcost_btn = $("button#gcost-btn");
gcost_btn.click(function () {
var new_nswe = parseFloat(nswe_cost.val());
var new_diag = parseFloat(diag_cost.val());
astar.setParameters(new_nswe, new_diag, astar.diag_allowed, astar.color_open, astar.h);
});
// Color open
var color_open_input = $("select#color-open");
var color_open_btn = $("button#color-open-btn");
color_open_btn.click(function () {
var color_open = parseInt($(color_open_input).val()) === 1;
astar.setParameters(astar.nswe_cost, astar.diag_cost, astar.diag_allowed, color_open, astar.h);
});
// Heuristic Functions
var h_input = $("select#h");
var h_btn = $("button#h-btn");
if (h !== null) {
$.each(h, function (val, text) {
$(h_input).append(new Option(val, val));
});
}
h_btn.click(function () {
var new_heuristic = h[h_input.val()];
astar.setParameters(astar.nswe_cost, astar.diag_cost, astar.diag_allowed, astar.color_open, new_heuristic);
});
// Grouped Update Buttons
var world_btn = $("button#world-btn");
var astar_btn = $("button#astar-btn");
world_btn.click(function () {
$(wall_amt_btn).trigger("click");
$(dimensions_btn).trigger("click");
});
astar_btn.click(function () {
$(diag_btn).trigger("click");
$(gcost_btn).trigger("click");
$(h_btn).trigger("click");
});
var update_all_btn = $("button#update-all-btn");
var reset_all_btn = $("button#reset-all-btn");
update_all_btn.click(function () {
$(world_btn).trigger("click");
$(astar_btn).trigger("click");
});
reset_all_btn.click(function () {
initWorld();
initAStar();
});
}
</script>
<style type="text/css">
td {
padding: 10px;
border: 1px solid #000;
}
.param {
margin-bottom: 20px;
}
.param-set {
padding-bottom: 10px;
margin-bottom: 20px;
border-bottom: 1px solid #999999;
}
</style>
</head>
<body>
<header>
<h1>A* Algorithm</h1>
</header>
<div style="float: left;">
<table id="graph" cellspacing="0">
</table>
<div style="margin-top: 20px;">
Path Cost: <span id="cost"></span>
</div>
</div>
<div style="float: left; margin-left: 15px;">
<div class="param-set">
<h2>World Parameters</h2>
<div class="param">
<b>Amount of walls: </b>
<input type="text" size="10" name="wall-amt" id="wall-amt" value="" /><br />
<button id="wall-amt-btn">Update</button>
</div>
<div class="param">
<b>Dimensions: </b>
<input type="text" size="3" name="n" id="n" value="" />
<input type="text" size="3" name="m" id="m" value="" /><br />
<button id="dimensions-btn">Update</button>
</div>
<div class="param">
<b>Load map: </b>
<select id="map">
</select><br />
<button id="map-btn">Select</button>
</div>
<button id="world-btn">Update World Paramters</button>
</div>
<div class="param-set">
<h2>A* Parameters</h2>
<div class="param">
<b>Diagonals Allowed: </b>
<select id="diag-allowed" name="diag-allowed">
<option value="1">Yes</option>
<option value="0">No</option>
</select><br />
<button id="diag-btn">Update</button>
</div>
<div class="param">
<b>G Costs: </b>
nswe: <input type="text" size="3" name="nswe" id="nswe" value="" />
diag: <input type="text" size="3" name="diag" id="diag" value="" /><br />
<button id="gcost-btn">Update</button>
</div>
<div class="param">
<b>Color open: </b>
<select id="color-open" name="color-open">
<option value="0">No</option>
<option value="1">Yes</option>
</select><br />
<button id="color-open-btn">Update</button>
</div>
<div class="param">
<b>Heuristic Function: </b>
<select id="h">
</select><br />
<button id="h-btn">Update</button>
</div>
<button id="astar-btn">Update A* Parameters</button>
</div>
<button id="update-all-btn">Update all parameters</button>
<br />
<button id="reset-all-btn">Reset all parameters</button>
</div>
</body>
</html>