Repository navigation
Expand file tree
/
Copy pathtemplate.html
More file actions
518 lines (352 loc) · 23.9 KB
/
Copy pathtemplate.html
File metadata and controls
518 lines (352 loc) · 23.9 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<!DOCTYPE html>
<html>
<head>
<title>template.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>template.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>This is a general template for creating models.</p>
<p>To build your own model, extend class ABM.Model supplying the
two abstract methods <code>setup</code> and <code>step</code>. <code>@foo</code> signifies
an instance variable or method.
See <a href="http://jashkenas.github.com/coffee-script/#classes">CoffeeScript docs</a>
for explanation of context of a class and its constructor.</p>
<p>We do not provide a constructor of our own.
CoffeeScript automatically calls <code>Model.constructor <args></code>
and <code>setup</code> will be called by <code>Model.constructor</code>. See:</p>
<pre><code>model = <span class="hljs-keyword">new</span> MyModel <span class="hljs-string">"layers"</span>, <span class="hljs-number">13</span>, -<span class="hljs-number">16</span>, <span class="hljs-number">16</span>, -<span class="hljs-number">16</span>, <span class="hljs-number">16</span>
</code></pre><p>below, which passes all its arguments to <code>Model</code></p>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>ABM.Util, ABM.Shapes, ABM.ColorMaps aliases</p>
</div>
<div class="content"><div class='highlight'><pre>u = ABM.Util; Shapes = ABM.Shapes; Maps = ABM.ColorMaps
<span class="hljs-function"><span class="hljs-title">log</span> = <span class="hljs-params">(arg)</span> -></span> <span class="hljs-built_in">console</span>.log arg
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyModel</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ABM</span>.<span class="hljs-title">Model</span></span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p><code>startup</code> initializes resources used by <code>setup</code> and <code>step</code>.
This is called by the constructor which waits for all files
processed by <code>starup</code>. Useful for large files, but here just
for an example, not needed by simple models.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">startup</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># called by constructor</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Add new shapes.</p>
</div>
<div class="content"><div class='highlight'><pre> Shapes.add <span class="hljs-string">"bowtie"</span>, <span class="hljs-literal">true</span>, <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
Shapes.poly c, [[-<span class="hljs-number">.5</span>,-<span class="hljs-number">.5</span>],[<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>],[-<span class="hljs-number">.5</span>,<span class="hljs-number">.5</span>],[<span class="hljs-number">.5</span>,-<span class="hljs-number">.5</span>]]</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>The following two example lines don’t work when opened locally
in Chrome (they work fine from a webserver). Uncomment if you’d
like to add custom images.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-built_in">window</span>.location.protocol <span class="hljs-keyword">is</span> <span class="hljs-string">"file:"</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"Warning: file:// protocol used!"</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"This prevents two user defined image shapes from loading!"</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"Use http:// protocol to enable image shapes."</span>
<span class="hljs-keyword">else</span>
Shapes.add <span class="hljs-string">"cup"</span>, <span class="hljs-literal">true</span>, u.importImage(<span class="hljs-string">"data/coffee.png"</span>)
Shapes.add <span class="hljs-string">"redfish"</span>, <span class="hljs-literal">false</span>, u.importImage(<span class="hljs-string">"data/redfish64t.png"</span>)
Shapes.add <span class="hljs-string">"twitter"</span>, <span class="hljs-literal">false</span>, u.importImage(<span class="hljs-string">"data/twitter.png"</span>)</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Initialize our model via the <code>setup</code> abstract method.
This model simply creates <code>population</code> turtles with
arbitrary shapes with <code>size</code> size and <code>speed</code> velocity.
We also periodically change the patch colors to random gray values.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setup</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># called by Model.constructor</span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>First, we initialize our model instance variables.
Most instance variables are parameters we would like
an external UI to setup for us.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@population</span> = <span class="hljs-number">100</span>
<span class="hljs-property">@size</span> = <span class="hljs-number">2.0</span> <span class="hljs-comment"># size in patch coords</span>
<span class="hljs-property">@speed</span> = <span class="hljs-number">.5</span> <span class="hljs-comment"># move forward this amount in patch coords</span>
<span class="hljs-property">@wiggle</span> = u.degToRad(<span class="hljs-number">30</span>) <span class="hljs-comment"># degrees/radians to wiggle</span>
<span class="hljs-property">@startCircle</span> = <span class="hljs-literal">true</span> <span class="hljs-comment"># initialize turtles randomly or in circle</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Set the default turtle size; save storage over setting size for each turtle</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@turtles</span>.setDefault <span class="hljs-string">"size"</span>, <span class="hljs-property">@size</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Set the turtle to convert shape to bitmap for better performance.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@turtles</span>.setUseSprites()</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Set animation to 30fps, without multiple steps per draw:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@anim</span>.setRate <span class="hljs-number">30</span>, <span class="hljs-literal">false</span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>The patch grid will have been setup for us. Here we initialize
patch variables, either built-in ones or any new patch variables
our model needs. In this case, we set the built-in color to a
random gray value.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> <span class="hljs-property">@patches</span>
p.color = Maps.randomGray(<span class="hljs-number">0</span>,<span class="hljs-number">100</span>)</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Set x,y axes different color</p>
</div>
<div class="content"><div class='highlight'><pre> p.color = <span class="hljs-string">"blue"</span> <span class="hljs-keyword">if</span> p.x <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> p.y <span class="hljs-keyword">is</span> <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Our empty @turtles AgentSet will have been created. Here we
add <code>population</code> Turtles we use in our model.
We set the build-in Turtle variables <code>size</code> and <code>shape</code>
and layout the turtles randomly or in a circle depending
on our modeel’s <code>startCircle</code> variable.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@turtles</span>.create <span class="hljs-property">@population</span>
a.shape = u.oneOf Shapes.names() <span class="hljs-comment"># random shapes</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@startCircle</span>
a.forward <span class="hljs-property">@patches</span>.maxX/<span class="hljs-number">2</span> <span class="hljs-comment"># start in circle</span>
<span class="hljs-keyword">else</span>
a.setXY <span class="hljs-property">@patches</span>.randomPt()... <span class="hljs-comment"># set random location</span></pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Print number of turtles and patches to the console.
Note CoffeeScript
<a href="http://jashkenas.github.com/coffee-script/#strings">string interpolation</a></p>
</div>
<div class="content"><div class='highlight'><pre> log <span class="hljs-string">"total turtles: <span class="hljs-subst">#{<span class="hljs-property">@turtles</span>.length}</span>, total patches: <span class="hljs-subst">#{<span class="hljs-property">@patches</span>.length}</span>"</span></pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Print number of turtles with each shape</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> Shapes.names()
num = <span class="hljs-property">@turtles</span>.getPropWith(<span class="hljs-string">"shape"</span>, s).length
log <span class="hljs-string">"<span class="hljs-subst">#{num}</span> <span class="hljs-subst">#{s}</span>"</span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">"Patch(0,0): "</span>, <span class="hljs-property">@patches</span>.patchXY <span class="hljs-number">0</span>,<span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Update our model via the second abstract method, <code>step</code></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">step</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># called by Model.animate</span></pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>First, update our turtles via <code>updateTurtles</code> below</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@updateTurtles</span>(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@turtles</span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Every 100 steps, update our patches, print stats to
the console, and use the Model refresh flag to redraw
the patches. Otherwise don’t refresh.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-property">@anim</span>.ticks % <span class="hljs-number">100</span> <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
<span class="hljs-property">@updatePatches</span>(p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> <span class="hljs-property">@patches</span>
<span class="hljs-property">@reportInfo</span>()
<span class="hljs-property">@refreshPatches</span> = <span class="hljs-literal">true</span></pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Add use of our first pull request:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@setSpotlight</span> <span class="hljs-property">@turtles</span>.oneOf() <span class="hljs-keyword">if</span> <span class="hljs-property">@anim</span>.ticks <span class="hljs-keyword">is</span> <span class="hljs-number">300</span>
<span class="hljs-property">@setSpotlight</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@anim</span>.ticks <span class="hljs-keyword">is</span> <span class="hljs-number">600</span>
<span class="hljs-keyword">else</span>
<span class="hljs-property">@refreshPatches</span> = <span class="hljs-literal">false</span></pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Stop the animation at 1000. Restart by <code>ABM.model.start()</code> in console.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-property">@anim</span>.ticks <span class="hljs-keyword">is</span> <span class="hljs-number">1000</span>
log <span class="hljs-string">"..stopping, restart by app.start()"</span>
<span class="hljs-property">@stop</span>()</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Three of our own methods to manage turtles & patches
and report model state.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">updateTurtles</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span> <span class="hljs-comment"># a is turtle</span></pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Have our turtle “wiggle” by changing
our heading by +/- <code>wiggle/2</code> radians</p>
</div>
<div class="content"><div class='highlight'><pre> a.rotate u.randomCentered <span class="hljs-property">@wiggle</span></pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Then move forward by our speed.</p>
</div>
<div class="content"><div class='highlight'><pre> a.forward <span class="hljs-property">@speed</span>
<span class="hljs-attribute">updatePatches</span>: <span class="hljs-function"><span class="hljs-params">(p)</span> -></span> <span class="hljs-comment"># p is patch</span></pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Update patch colors to be a random gray.
u.randomGray(p.color) if p.x isnt 0 and p.y isnt 0 # aviod GC, reuse color
Avoid Garbage collection by using a colormap</p>
</div>
<div class="content"><div class='highlight'><pre> p.color = Maps.randomColor() <span class="hljs-keyword">if</span> p.x <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> p.y <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
<span class="hljs-attribute">reportInfo</span>: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Report the average heading, in radians and degrees</p>
</div>
<div class="content"><div class='highlight'><pre> headings = <span class="hljs-property">@turtles</span>.getProp <span class="hljs-string">"heading"</span>
<span class="hljs-function"> <span class="hljs-title">avgHeading</span> = <span class="hljs-params">(headings.reduce (a,b)->a+b)</span> / @<span class="hljs-title">turtles</span>.<span class="hljs-title">length</span>
</span></pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>Note: multiline strings. block strings also available.</p>
</div>
<div class="content"><div class='highlight'><pre> log <span class="hljs-string">"
average heading of turtles:
<span class="hljs-subst">#{avgHeading.toFixed(<span class="hljs-number">2</span>)}</span> radians,
<span class="hljs-subst">#{u.radToDeg(avgHeading).toFixed(<span class="hljs-number">2</span>)}</span> degrees"</span></pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Now that we’ve build our class, we call it with Model’s
constructor arguments:</p>
<pre><code>divName, patchSize, minX, maxX, minY, maxY,
isTorus = <span class="hljs-literal">false</span>, hasNeighbors = <span class="hljs-literal">true</span>
</code></pre><p>Note: Netlogo defaults 13, -16, 16, -16, 16 <br>
for patchSize, minX, maxX, minY, maxY</p>
</div>
<div class="content"><div class='highlight'><pre>model = <span class="hljs-keyword">new</span> MyModel({
<span class="hljs-attribute">div</span>: <span class="hljs-string">"layers"</span>,
<span class="hljs-attribute">size</span>: <span class="hljs-number">13</span>,
<span class="hljs-attribute">minX</span>: -<span class="hljs-number">16</span>,
<span class="hljs-attribute">maxX</span>: <span class="hljs-number">16</span>,
<span class="hljs-attribute">minY</span>: -<span class="hljs-number">16</span>,
<span class="hljs-attribute">maxY</span>: <span class="hljs-number">16</span>,
<span class="hljs-attribute">isTorus</span>: <span class="hljs-literal">true</span>,
<span class="hljs-attribute">hasNeighbors</span>: <span class="hljs-literal">false</span>
})
.debug() <span class="hljs-comment"># Debug: Put Model vars in global name space</span>
.start() <span class="hljs-comment"># Run model immediately after startup initialization</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>