Repository navigation
Expand file tree
/
Copy pathagentset.html
More file actions
931 lines (659 loc) · 47.9 KB
/
Copy pathagentset.html
File metadata and controls
931 lines (659 loc) · 47.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
<!DOCTYPE html>
<html>
<head>
<title>agentset.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>agentset.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>An <strong>AgentSet</strong> is an array, along with a class, agentClass, whose instances
are the items of the array. Instances of the class are created
by the <code>create</code> factory method of an AgentSet.</p>
<p>It is a subclass of <code>Array</code> and is the base class for
<code>Patches</code>, <code>Turtles</code>, and <code>Links</code>. An AgentSet keeps track of all
its created instances. It also provides, much like the <strong>Util</strong>
module, many methods shared by all subclasses of AgentSet.</p>
<p>A model contains three agentsets:</p>
<ul>
<li><code>patches</code>: the model’s “world” grid</li>
<li><code>turtles</code>: the model’s turtles living on the patches</li>
<li><code>links</code>: the network links connecting agent pairs</li>
</ul>
<p>See NetLogo <a href="http://ccl.northwestern.edu/netlogo/docs/">documentation</a>
for explanation of the overall semantics of Agent Based Modeling
used by AgentSets as well as Patches, Turtles, and Links.</p>
<p>Note: subclassing <code>Array</code> can be dangerous and we may have to convert
to a different style. See Trevor Burnham’s <a href="http://goo.gl/Lca8g">comments</a>
but thus far we’ve resolved all related problems.</p>
<p>Because we are an array subset, @[i] == this[i] == agentset[i]</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AgentSet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Array</span></span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<h3 id="static-members">Static members</h3>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p><code>asSet</code> is a static wrapper function converting an array of agents into
an <code>AgentSet</code> .. except for the ID which only impacts the add method.
It is primarily used to turn a comprehension into an AgentSet instance
which then gains access to all the methods below. Ex:</p>
<pre><code>evens = (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <span class="hljs-property">@model</span>.turtles <span class="hljs-keyword">when</span> a.id % <span class="hljs-number">2</span> <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>)
ABM.AgentSet.asSet(evens)
randomEven = evens.oneOf()
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@asSet</span>: <span class="hljs-function"><span class="hljs-params">(a, setType = AgentSet)</span> -></span> <span class="hljs-comment">#(a, setType = ABM.AgentSet)</span>
a.__proto__ = setType.prototype ? setType.constructor.prototype <span class="hljs-comment"># setType.__proto__</span>
a.model=a[<span class="hljs-number">0</span>].model <span class="hljs-keyword">if</span> a[<span class="hljs-number">0</span>]? <span class="hljs-comment"># Used by geometric methods</span>
a</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>In the examples below, we’ll use an array of primitive agent objects
with three fields: id, x, y.</p>
<pre><code>AS = <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">1.</span><span class="hljs-number">.5</span>] <span class="hljs-comment"># long form comprehension</span>
{<span class="hljs-attribute">id</span>:i, <span class="hljs-attribute">x</span>:u.randomInt(<span class="hljs-number">10</span>), <span class="hljs-attribute">y</span>:u.randomInt(<span class="hljs-number">10</span>)}
ABM.AgentSet.asSet AS <span class="hljs-comment"># Convert AS to AgentSet in place</span>
[{<span class="hljs-attribute">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">0</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">1</span>}, {<span class="hljs-attribute">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">8</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">0</span>}, {<span class="hljs-attribute">id</span>:<span class="hljs-number">3</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">6</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">4</span>},
{<span class="hljs-attribute">id</span>:<span class="hljs-number">4</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">3</span>}, {<span class="hljs-attribute">id</span>:<span class="hljs-number">5</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">1</span>}]
</code></pre>
</div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<h3 id="constructor-and-add-remove-agents-">Constructor and add/remove agents.</h3>
</div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Create an empty <code>AgentSet</code> and initialize the <code>ID</code> counter for add().
If mainSet is supplied, the new agentset is a sub-array of mainSet.
This sub-array feature is how breeds are managed, see class <code>Model</code></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@model</span>, <span class="hljs-property">@agentClass</span>, <span class="hljs-property">@name</span>, <span class="hljs-property">@mainSet</span>)</span> -></span>
<span class="hljs-keyword">super</span>(<span class="hljs-number">0</span>) <span class="hljs-comment"># doesn't yield empty array if already instances in the mainSet</span>
u.mixin(@, <span class="hljs-keyword">new</span> Evented())
<span class="hljs-property">@breeds</span> = [] <span class="hljs-keyword">unless</span> <span class="hljs-property">@mainSet</span>?
<span class="hljs-property">@agentClass</span>::breed = @ <span class="hljs-comment"># let the breed know I'm it's agentSet</span>
<span class="hljs-property">@agentClass</span>::model = <span class="hljs-property">@model</span> <span class="hljs-comment"># let the breed know its model</span>
<span class="hljs-property">@ownVariables</span> = [] <span class="hljs-comment"># keep list of user variables</span>
<span class="hljs-property">@ID</span> = <span class="hljs-number">0</span> <span class="hljs-keyword">unless</span> <span class="hljs-property">@mainSet</span>? <span class="hljs-comment"># Do not set ID if I'm a subset</span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Abstract method used by subclasses to create and add their instances.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">create</span>: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Add an agent to the list. Only used by agentset factory methods. Adds
the <code>id</code> property to all agents. Increment <code>ID</code>.
Returns the object for chaining. The set will be sorted by <code>id</code>.</p>
<p>By “agent” we mean an instance of <code>Patch</code>, <code>Turtle</code> and <code>Link</code> and their breeds</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">add</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@mainSet</span>? <span class="hljs-keyword">then</span> <span class="hljs-property">@mainSet</span>.add o <span class="hljs-keyword">else</span> o.id = <span class="hljs-property">@ID</span>++
<span class="hljs-property">@push</span> o; o</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Remove an agent from the agentset, returning the agentset.
Note this does not change ID, thus an
agentset can have gaps in terms of their ids. Assumes set is
sorted by <code>id</code>. If the set is one created by <code>asSet</code>, and the original
array is unsorted, simply call <code>sortById</code> first, see <code>sortById</code> below.</p>
<pre><code>AS.remove(AS[<span class="hljs-number">3</span>]) <span class="hljs-comment"># [{id:0,x:0,y:1}, {id:1,x:8,y:0},</span>
{<span class="hljs-attribute">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">6</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">4</span>}, {<span class="hljs-attribute">id</span>:<span class="hljs-number">4</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">1</span>}]
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">remove</span>: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
u.removeItem <span class="hljs-property">@mainSet</span>, o <span class="hljs-keyword">if</span> <span class="hljs-property">@mainSet</span>?
u.removeItem @, o
@</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Set/get the default value of an agent class</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setDefault</span>: <span class="hljs-function"><span class="hljs-params">(name, value)</span> -></span> <span class="hljs-property">@agentClass</span>::[name] = value
<span class="hljs-attribute">getDefault</span>: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span> <span class="hljs-property">@agentClass</span>::[name]</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Declare variables of an agent class.
Vars = a string of space separated names or an array of name strings
Return agentset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">own</span>: <span class="hljs-function"><span class="hljs-params">(vars)</span> -></span> <span class="hljs-comment"># maybe not set default if val is null?</span>
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> vars.split(<span class="hljs-string">" "</span>)
<span class="hljs-property">@setDefault</span> name, <span class="hljs-literal">null</span>
<span class="hljs-property">@ownVariables</span>.push name
@</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Move an agent from its AgentSet/breed to be in this AgentSet/breed.
REMIND: match NetLogo sematics in terms of own variables.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setBreed</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span> <span class="hljs-comment"># change agent a to be in this breed</span>
u.removeItem a.breed, a, <span class="hljs-string">"id"</span> <span class="hljs-keyword">if</span> a.breed.mainSet?
u.insertItem @, a, <span class="hljs-string">"id"</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@mainSet</span>?
proto = a.__proto__ = <span class="hljs-property">@agentClass</span>.prototype
<span class="hljs-keyword">delete</span> a[k] <span class="hljs-keyword">for</span> own k,v <span class="hljs-keyword">of</span> a <span class="hljs-keyword">when</span> proto[k]?
a</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Return all agents that are not of the given breeds argument.
Breeds is a string of space separated names:
@patches.exclude “roads houses”</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">exclude</span>: <span class="hljs-function"><span class="hljs-params">(breeds)</span> -></span> <span class="hljs-comment"># Not used, remove??</span>
breeds = breeds.split(<span class="hljs-string">" "</span>)
<span class="hljs-property">@asSet</span> (o <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> o.breed.name <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> breeds)</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Remove adjacent duplicates, by reference, in a sorted agentset.
Use <code>sortById</code> first if agentset not sorted.</p>
<pre><code>as = (AS.oneOf() <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">1.</span><span class="hljs-number">.4</span>]) <span class="hljs-comment"># 4 random agents w/ dups</span>
ABM.AgentSet.asSet as <span class="hljs-comment"># [{id:1,x:8,y:0}, {id:0,x:0,y:1},</span>
{<span class="hljs-attribute">id</span>:<span class="hljs-number">0</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">0</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">1</span>}, {<span class="hljs-attribute">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">6</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">4</span>}]
as.sortById().uniq() <span class="hljs-comment"># [{id:0,x:0,y:1}, {id:1,x:8,y:0},</span>
{<span class="hljs-attribute">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">6</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">4</span>}]
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">uniq</span>: <span class="hljs-function">-></span> u.uniq(@)</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>The static <code>ABM.AgentSet.asSet</code> as a method.
Used by agentset methods creating new agentsets.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">asSet</span>: <span class="hljs-function"><span class="hljs-params">(a, setType = AgentSet)</span> -></span> AgentSet.asSet a, setType <span class="hljs-comment"># setType = AgentSet</span></pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Is the given array an agentset of the given type?
It does not use the prototype chain, so</p>
<pre><code>isSet(turtles, <span class="hljs-string">"AgentSet"</span>)
</code></pre><p>is false. Current names: AgentSet, Turtles, Patches, Links.
Default name is “AgentSet”, good test for derived sets using asSet()</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isSet</span>: <span class="hljs-function"><span class="hljs-params">(name = <span class="hljs-string">"AgentSet"</span>)</span> -></span> <span class="hljs-property">@constructor</span>.name <span class="hljs-keyword">is</span> name</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Similar for above but includes breeds of Turtles, Patches, Links too
isBreed(“Turtles”) returns true for an agent that isn’t a breed</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isBreed</span>: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>@isSet(name) or (@agentClass?.name is name)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-property">@agentClass</span>? <span class="hljs-keyword">then</span> (<span class="hljs-property">@agentClass</span>.name <span class="hljs-keyword">is</span> name) <span class="hljs-keyword">else</span> <span class="hljs-property">@isSet</span>(name)</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Similar to above but sorted via <code>id</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">asOrderedSet</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span> <span class="hljs-property">@asSet</span>(a).sortById()</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Return string representative of agentset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toString</span>: <span class="hljs-function">-></span> <span class="hljs-string">"["</span>+(a.toString() <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @).join(<span class="hljs-string">", "</span>)+<span class="hljs-string">"]"</span></pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<h3 id="property-utilities">Property Utilities</h3>
<p>Property access, also useful for debugging<br></p>
</div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Return an array of a property of the agentset</p>
<pre><code> AS.getProp <span class="hljs-string">"x"</span> <span class="hljs-comment"># [0, 8, 6, 1, 1]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">getProp</span>: <span class="hljs-function"><span class="hljs-params">(prop)</span> -></span> u.aProp(@, prop)</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Return an array of agents with the property equal to the given value</p>
<pre><code>AS.getPropWith <span class="hljs-string">"x"</span>, <span class="hljs-number">1</span>
[{<span class="hljs-attribute">id</span>:<span class="hljs-number">4</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">3</span>},{<span class="hljs-attribute">id</span>:<span class="hljs-number">5</span>,<span class="hljs-attribute">x</span>:<span class="hljs-number">1</span>,<span class="hljs-attribute">y</span>:<span class="hljs-number">1</span>}]
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">getPropWith</span>: <span class="hljs-function"><span class="hljs-params">(prop, value)</span> -></span> <span class="hljs-property">@asSet</span> (o <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> o[prop] <span class="hljs-keyword">is</span> value)</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Set the property of the agents to a given value. If value
is an array, its values will be used, indexed by agentSet’s index.
This is generally used via: getProp, modify results, setProp</p>
<pre><code><span class="hljs-comment"># increment x for agents with x=1</span>
AS1 = ABM.AgentSet.asSet AS.getPropWith(<span class="hljs-string">"x"</span>,<span class="hljs-number">1</span>)
AS1.setProp <span class="hljs-string">"x"</span>, <span class="hljs-number">2</span> <span class="hljs-comment"># {id:4,x:2,y:3},{id:5,x:2,y:1}</span>
</code></pre><p>Note this changes the last two objects in the original AS above</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setProp</span>: <span class="hljs-function"><span class="hljs-params">(prop, value)</span> -></span>
<span class="hljs-keyword">if</span> u.isArray value
<span class="hljs-keyword">then</span> o[prop] = value[i] <span class="hljs-keyword">for</span> o,i <span class="hljs-keyword">in</span> @; @
<span class="hljs-keyword">else</span> o[prop] = value <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @; @</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Get the agent with the min/max prop value in the agentset</p>
<pre><code>min = AS.minProp <span class="hljs-string">"y"</span> <span class="hljs-comment"># 0</span>
max = AS.maxProp <span class="hljs-string">"y"</span> <span class="hljs-comment"># 4</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">maxProp</span>: <span class="hljs-function"><span class="hljs-params">(prop)</span> -></span> u.aMax <span class="hljs-property">@getProp</span>(prop)
<span class="hljs-attribute">minProp</span>: <span class="hljs-function"><span class="hljs-params">(prop)</span> -></span> u.aMin <span class="hljs-property">@getProp</span>(prop)
<span class="hljs-attribute">histOfProp</span>: <span class="hljs-function"><span class="hljs-params">(prop, bin=<span class="hljs-number">1</span>)</span> -></span> u.histOf @, bin, prop</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<h3 id="array-utilities-often-from-util">Array Utilities, often from Util</h3>
</div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Randomize the agentset</p>
<pre><code>AS.shuffle(); AS.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [3, 2, 1, 4, 5]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">shuffle</span>: <span class="hljs-function">-></span> u.shuffle @</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Sort the agentset by the agent’s <code>id</code>.</p>
<pre><code>AS.shuffle(); AS.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [3, 2, 1, 4, 5]</span>
AS.sortById(); AS.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [1, 2, 3, 4, 5]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sortById</span>: <span class="hljs-function">-></span> u.sortBy @, <span class="hljs-string">"id"</span></pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Make a copy of an agentset, return as new agentset.<br>
NOTE: does <em>not</em> duplicate the objects, simply creates a new agentset
with references to the same agents. Ex: create a randomized version of AS
but without mangling AS itself:</p>
<pre><code>as = AS.clone().shuffle()
AS.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [1, 2, 3, 4, 5]</span>
as.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [2, 4, 0, 1, 3]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clone</span>: <span class="hljs-function">-></span> <span class="hljs-property">@asSet</span> u.clone @</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Return the last agent in the agentset</p>
<pre><code>AS.last().id <span class="hljs-comment"># l5</span>
l=AS.last(); p=[l.x,l.y] <span class="hljs-comment"># [1,1]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">last</span>: <span class="hljs-function">-></span> u.last @</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Returns true if the agentset has any agents</p>
<pre><code>AS.any() <span class="hljs-comment"># true</span>
AS.getPropWith(<span class="hljs-string">"x"</span>, <span class="hljs-number">99</span>).any() <span class="hljs-comment">#false</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">any</span>: <span class="hljs-function">-></span> u.any @</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>Return an agentset without given agent a</p>
<pre><code>as = AS.clone().other(AS[<span class="hljs-number">0</span>])
as.getProp <span class="hljs-string">"id"</span> <span class="hljs-comment"># [1, 2, 3, 4]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">other</span>: <span class="hljs-function"><span class="hljs-params">(a)</span> -></span></pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>If simple agentset derived by functions returning agentsets, use
remove. Otherwise iterate over myself.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-property">@isSet</span>()
u.removeItem @, a
<span class="hljs-keyword">else</span>
<span class="hljs-property">@asSet</span> (o <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> o <span class="hljs-keyword">isnt</span> a)</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Return random agent in agentset</p>
<pre><code>AS.oneOf() <span class="hljs-comment"># {id:2,x:6,y:4}</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">oneOf</span>: <span class="hljs-function">-></span> u.oneOf @</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Return agentset made of n distinct agents</p>
<pre><code>AS.nOf(<span class="hljs-number">3</span>) <span class="hljs-comment"># [{id:0,x:0,y:1}, {id:4,x:1,y:1}, {id:1,x:8,y:0}]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">nOf</span>: <span class="hljs-function"><span class="hljs-params">(n)</span> -></span> <span class="hljs-property">@asSet</span> u.nOf @, n</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>Return agent when f(o) min/max in agentset. If multiple agents have
min/max value, return the first. Error if agentset empty.
If f is a string, return element with min/max value of that property.
If “valueToo” then return an array of the agent and the value.</p>
<pre><code>AS.minOneOf(<span class="hljs-string">"x"</span>) <span class="hljs-comment"># {id:0,x:0,y:1}</span>
AS.maxOneOf(<span class="hljs-function"><span class="hljs-params">(a)</span>-></span>a.x+a.y, <span class="hljs-literal">true</span>) <span class="hljs-comment"># {id:2,x:6,y:4},10</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">minOneOf</span>: <span class="hljs-function"><span class="hljs-params">(f, valueToo=<span class="hljs-literal">false</span>)</span> -></span> u.minOneOf @, f, valueToo
<span class="hljs-attribute">maxOneOf</span>: <span class="hljs-function"><span class="hljs-params">(f, valueToo=<span class="hljs-literal">false</span>)</span> -></span> u.maxOneOf @, f, valueToo</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<h3 id="drawing">Drawing</h3>
</div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>For agentsets whose agents have a <code>draw</code> method.
Clears the graphics context (transparent), then
calls each agent’s draw(ctx) method.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">draw</span>: <span class="hljs-function"><span class="hljs-params">(ctx)</span> -></span>
u.clearCtx(ctx); o.draw(ctx) <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> o.hidden; <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>Show/Hide all of an agentset or breed. Does not redraw.
To show/hide an individual object, set its prototype: o.hidden = bool</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">show</span>: <span class="hljs-function">-></span> o.hidden = <span class="hljs-literal">false</span> <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @
<span class="hljs-attribute">hide</span>: <span class="hljs-function">-></span> o.hidden = <span class="hljs-literal">true</span> <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @</pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<h3 id="topology">Topology</h3>
</div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>For patches & turtles, which have x,y. See Util doc.
Typically a subclass uses a rect/quadtree array to minimize
the size, then uses asSet(array) to call inRadius or inCone</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRect</span>: <span class="hljs-function"><span class="hljs-params">(o, radius)</span> -></span>
rect = [];
minX = o.x - radius; maxX = o.x + radius
minY = o.y - radius; maxY = o.y + radius
patches = <span class="hljs-property">@model</span>.patches</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<p>Is the o +/- radius entirely inside the patches?</p>
</div>
<div class="content"><div class='highlight'><pre> outside = (minX < patches.minX) <span class="hljs-keyword">or</span> (maxX > patches.maxX) <span class="hljs-keyword">or</span>
(minY < patches.minY) <span class="hljs-keyword">or</span> (maxY > patches.maxY)
checkTorus = patches.isTorus <span class="hljs-keyword">and</span> outside
<span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @
x = a.x; y = a.y <span class="hljs-comment"># agent's x,y</span>
<span class="hljs-keyword">if</span> checkTorus <span class="hljs-comment"># Adjust torus x,y if appropriate</span>
<span class="hljs-keyword">if</span> x<minX <span class="hljs-keyword">then</span> x += patches.numX <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> x>maxX <span class="hljs-keyword">then</span> x -= patches.numX
<span class="hljs-keyword">if</span> y<minY <span class="hljs-keyword">then</span> y += patches.numY <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> y>maxY <span class="hljs-keyword">then</span> y -= patches.numY</pre></div></div>
</li>
<li id="section-43">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-43">¶</a>
</div>
<p>Test x,y inside rect</p>
</div>
<div class="content"><div class='highlight'><pre> rect.push a <span class="hljs-keyword">if</span> (minX <= x <= maxX <span class="hljs-keyword">and</span> minY <= y <= maxY)
<span class="hljs-property">@asSet</span> rect</pre></div></div>
</li>
<li id="section-44">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-44">¶</a>
</div>
<p>Return all agents in agentset within d distance from given object.
By default excludes the given object. Uses linear/torus distance
depending on patches.isTorus, and patches width/height if needed.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRadius</span>: <span class="hljs-function"><span class="hljs-params">(o, radius)</span> -></span>
d2 = radius * radius; x = o.x; y = o.y
<span class="hljs-keyword">if</span> <span class="hljs-property">@model</span>.patches.isTorus
w = <span class="hljs-property">@model</span>.patches.numX; h = <span class="hljs-property">@model</span>.patches.numY
<span class="hljs-property">@asSet</span> (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> \
u.torusSqDistance(x, y, a.x, a.y, w, h) <= d2 )
<span class="hljs-keyword">else</span>
<span class="hljs-property">@asSet</span> (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> \
u.sqDistance(x, y, a.x, a.y) <= d2)</pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-45">¶</a>
</div>
<p>As above, but also limited to the angle <code>angle</code> around
a <code>heading</code> from object <code>o</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inCone</span>: <span class="hljs-function"><span class="hljs-params">(o, radius, angle, heading)</span> -></span>
x = o.x; y = o.y
<span class="hljs-keyword">if</span> <span class="hljs-property">@model</span>.patches.isTorus
w = <span class="hljs-property">@model</span>.patches.numX; h = <span class="hljs-property">@model</span>.patches.numY
<span class="hljs-property">@asSet</span> (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> \
u.inTorusCone(radius, angle, heading, x, y, a.x, a.y, w, h))
<span class="hljs-keyword">else</span>
<span class="hljs-property">@asSet</span> (a <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> \
u.inCone(radius, angle, heading, x, y, a.x, a.y))</pre></div></div>
</li>
<li id="section-46">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-46">¶</a>
</div>
<h3 id="debugging">Debugging</h3>
</div>
</li>
<li id="section-47">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-47">¶</a>
</div>
<p>Useful in console.
Also see <a href="http://goo.gl/1i7bd">CoffeeConsole</a> Chrome extension.</p>
</div>
</li>
<li id="section-48">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-48">¶</a>
</div>
<p>Similar to NetLogo ask & with operators.
Allows functions as strings. Use:</p>
<pre><code>AS.getProp(<span class="hljs-string">"x"</span>) <span class="hljs-comment"># [1, 8, 6, 2, 2]</span>
AS.<span class="hljs-reserved">with</span>(<span class="hljs-string">"o.x<5"</span>).ask(<span class="hljs-string">"o.x=o.x+1"</span>)
AS.getProp(<span class="hljs-string">"x"</span>) <span class="hljs-comment"># [2, 8, 6, 3, 3]</span>
myModel.turtles.<span class="hljs-reserved">with</span>(<span class="hljs-string">"o.id<100"</span>).ask(<span class="hljs-string">"o.color=[255,0,0]"</span>)
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">ask</span>: <span class="hljs-function"><span class="hljs-params">(f)</span> -></span>
eval(<span class="hljs-string">"f=function(o){return "</span>+f+<span class="hljs-string">";}"</span>) <span class="hljs-keyword">if</span> u.isString f
f(o) <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @; @
<span class="hljs-attribute">with</span>: <span class="hljs-function"><span class="hljs-params">(f)</span> -></span>
eval(<span class="hljs-string">"f=function(o){return "</span>+f+<span class="hljs-string">";}"</span>) <span class="hljs-keyword">if</span> u.isString f
<span class="hljs-property">@asSet</span> (o <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> @ <span class="hljs-keyword">when</span> f(o))</pre></div></div>
</li>
<li id="section-49">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>The example agentset AS used in the code fragments was made like this,
slightly more useful than shown above due to the toString method.</p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">XY</span></span>
<span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@x</span>,<span class="hljs-property">@y</span>)</span> -></span>
<span class="hljs-attribute">toString</span>: <span class="hljs-function">-></span> <span class="hljs-string">"{id:<span class="hljs-subst">#{<span class="hljs-property">@id</span>}</span>,x:<span class="hljs-subst">#{<span class="hljs-property">@x</span>}</span>,y:<span class="hljs-subst">#{<span class="hljs-property">@y</span>}</span>}"</span>
<span class="hljs-property">@AS</span> = <span class="hljs-keyword">new</span> ABM.AgentSet <span class="hljs-comment"># @ => global name space</span>
</code></pre><p>The result of</p>
<pre><code>AS.add <span class="hljs-keyword">new</span> XY(u.randomInt(<span class="hljs-number">10</span>), u.randomInt(<span class="hljs-number">10</span>)) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">1.</span><span class="hljs-number">.5</span>]
</code></pre><p>random run, captured so we can reuse.</p>
<pre><code>AS.add <span class="hljs-keyword">new</span> XY(pt...) <span class="hljs-keyword">for</span> pt <span class="hljs-keyword">in</span> [[<span class="hljs-number">0</span>,<span class="hljs-number">1</span>],[<span class="hljs-number">8</span>,<span class="hljs-number">0</span>],[<span class="hljs-number">6</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">1</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">1</span>,<span class="hljs-number">1</span>]]
</code></pre>
</div>
</li>
</ul>
</div>
</body>
</html>