forked from red42/HTML5_Genetic_Cars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
310 lines (277 loc) · 10.2 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>HTML5 Genetic Algorithm 2D Car Thingy - Chrome recommended</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="clearfix">
<div class="float-left">
<canvas id="mainbox" width="800" height="400"></canvas>
<div class="clearfix">
<div class="float-left">
<div id="graphholder">
<canvas id="graphcanvas" width="400" height="250"></canvas>
<div class="scale" id="s100">250</div>
<div class="scale" id="s75">187</div>
<div class="scale" id="s50">125</div>
<div class="scale" id="s25">62</div>
<div class="scale" id="s0">0</div>
</div>
</div>
<div class="float-left">
<div id="topscoreholder">
<input type="button" value="View top replay" onclick="cw_toggleGhostReplay(this)"/><br/>
<div id="topscores"></div>
</div>
</div>
</div>
<div id="minimapholder">
<!--<img src="reddot.png" id="minimapmarker" />-->
<div id="minimapfog"></div>
<canvas id="minimap" width="800" height="200"></canvas>
<div id="minimapcamera"></div>
</div>
<div id="debug"></div>
</div>
<div class="float-left">
<div id="data">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="X8VB9NDYWQDPE">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit"
alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/pt_BR/i/scr/pixel.gif" width="1" height="1">
</form>
<br/>
<div class="buttons">
<input type="button" value="Save Population" onclick="saveProgress();"/>
<input type="button" value="Restore Saved Population" onclick="restoreProgress();"/>
<input type="button" value="Surprise!" onclick="toggleDisplay()"/>
<input type="button" value="New Population" onclick="cw_resetPopulation()"/>
</div>
<div>
<label>Create new world with seed</label><br>
<input type="text" placeholder="Enter any string" id="newseed"/>
<input type="button" value="Go!" onclick="cw_confirmResetWorld()"/>
</div>
<table>
<tr>
<td>Generation</td><td><div id="generation"></div></td>
</tr>
<tr>
<td>Cars alive</td><td><div id="population"></div></td>
</tr>
<tr>
<td>Distance</td><td><div id="distancemeter"></div></td>
</tr>
<tr>
<td>Height</td><td><div id="heightmeter"></div></td>
</tr>
<tr>
<td>Mutation rate:</td>
<td>
<select id="mutationrate" onchange="cw_setMutation(this.options[this.selectedIndex].value)">
<option value="0">0%</option>
<option value="0.01">1%</option>
<option value="0.02">2%</option>
<option value="0.03">3%</option>
<option value="0.04">4%</option>
<option value="0.05" selected="selected">5%</option>
<option value="0.1">10%</option>
<option value="0.2">20%</option>
<option value="0.3">30%</option>
<option value="0.4">40%</option>
<option value="0.5">50%</option>
<option value="0.75">75%</option>
<option value="1.0">100%</option>
</select>
</td>
</tr>
<tr>
<td>Mutation size:</td>
<td>
<select id="mutationsize" onchange="cw_setMutationRange(this.options[this.selectedIndex].value)">
<option value="0">0%</option>
<option value="0.01">1%</option>
<option value="0.02">2%</option>
<option value="0.03">3%</option>
<option value="0.04">4%</option>
<option value="0.05">5%</option>
<option value="0.1">10%</option>
<option value="0.2">20%</option>
<option value="0.3">30%</option>
<option value="0.4">40%</option>
<option value="0.5">50%</option>
<option value="0.75">75%</option>
<option value="1.0" selected="selected">100%</option>
</select>
</td>
</tr>
<tr>
<td>Floor:</td>
<td>
<select id="floor" onchange="cw_setMutableFloor(this.options[this.selectedIndex].value)">
<option value="0" selected="selected">fixed</option>
<option value="1">mutable</option>
</select>
</td>
</tr>
<tr>
<td>Gravity:</td>
<td>
<select id="gravity" onchange="cw_setGravity(this.options[this.selectedIndex].value)">
<option value="24.8">Jupiter (24.8)</option>
<option value="11.2">Neptune (11.2)</option>
<option value="10.4">Saturn (10.4)</option>
<option value="9.81" selected="selected">Earth (9.81)</option>
<option value="8.9">Venus (8.9)</option>
<option value="8.7">Uranus (8.7)</option>
<option value="3.7">Mars/Mercury (3.7)</option>
<option value="1.6">Moon (1.6)</option>
</select>
</td>
</tr>
<tr>
<td>Elite clones:</td>
<td>
<select id="elitesize" onchange="cw_setEliteSize(this.options[this.selectedIndex].value)">
<option value="0">0</option>
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
</tr>
</table>
<br>
<input type="button" value="Watch Leader" onclick="cw_setCameraTarget(-1)"/>
<div id="health"></div>
<div id="cars"></div>
</div>
</div>
</div>
<div id="explanation">
<h3>But what is it?</h3>
<p>The program uses a simple genetic algorithm to evolve random two-wheeled shapes into cars over generations. Loosely
based on <a href="http://boxcar2d.com/">BoxCar2D</a>, but
written from scratch, only using the same physics engine (<a href="http://box2d.org/">box2d</a>).<br/>
seedrandom.js written by <a href="http://davidbau.com/">David Bau</a>. (thanks!)</p>
<h3>Controls</h3>
<table>
<tr>
<th>Save Population</th>
<td>Saves current population locally.</td>
</tr>
<tr>
<th>Restore Saved Population</th>
<td>Restore a previously saved population.</td>
</tr>
<tr>
<th>Suprise</th>
<td>Toggles drawing, makes the simulation faster.</td>
</tr>
<tr>
<th>New Population</th>
<td>Keeps the generated track and restarts the whole car population.</td>
</tr>
<tr>
<th>Create new world with seed</th>
<td>The same seed always creates the same track, so you can agree on a seed with your friends
and compete. :)
</td>
</tr>
<tr>
<th>Mutation rate</th>
<td>The chance that each gene in each individual will mutate to a random value when a new generation is
born.
</td>
</tr>
<tr>
<th>Mutation size</th>
<td>The range each gene can mutate into. Lower numbers mean the gene will have values closer to the
original.
</td>
</tr>
<tr>
<th>Elite clones</th>
<td>The top n cars that will be copied over to the next generation.</td>
</tr>
<tr>
<th>View top replay</th>
<td>Pause the current simulation and show the top performing car. Click a second time to resume
simulation.
</td>
</tr>
</table>
<h3>Graph</h3>
<table>
<tr>
<th>Red</th>
<td>Top score in each generation</td>
</tr>
<tr>
<th>Green</th>
<td>Average of the top 10 cars in each generation</td>
</tr>
<tr>
<th>Blue</th>
<td>Average of the entire generation</td>
</tr>
</table>
<h3>Genome</h3>
<p>
The genome consists of:
</p>
<ul>
<li>Shape (8 genes, 1 per vertex)</li>
<li>Wheel size (2 genes, 1 per wheel)</li>
<li>Wheel position (2 genes, 1 per wheel)</li>
<li>Wheel density (2 genes, 1 per wheel) darker wheels mean denser wheels</li>
<li>Chassis density (1 gene) darker body means denser chassis</li>
</ul>
<h3>Blurb</h3>
<p>This is not as deterministic as it should be, so your best car may not perform as well as it once did. The terrain
gets more complex with distance.<br/>
I'm not in the mood to deal with checking if all scripts have loaded before running, so refresh the page if things
seem whack.</p>
<h3>GitHub</h3>
<p>The code is now on a <a href="https://github.com/red42/HTML5_Genetic_Cars">GitHub repository</a>. Feel free to
contribute!</p>
<p>Written by <a href="http://rednuht.org">this guy</a>.<br/>
<a href="mailto:[email protected]">Send me food</a>.</p>
</div>
<div name="minimapmarker" class="minimapmarker"></div>
<div name="healthbar" class="healthbar" onclick="cw_setCameraTarget(this.car_index)">
<div name="health" class="health"></div>
<div name="healthtext" class="healthtext"></div>
</div>
<script src="lib/seedrandom.js"></script>
<script src="lib/box2d.js"></script>
<script src="ghost.js"></script>
<script src="path.js"></script>
<script src="cawro.js"></script>
<script src="graphs.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25432661-3']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>