-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_opinion_v01_crazy_links.nlogo
1406 lines (1218 loc) · 43.8 KB
/
public_opinion_v01_crazy_links.nlogo
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
extensions [ palette csv nw matrix profiler ]
; Bad news: The NW extension is not particularly useful for us. "At the moment, nw:save-matrix does not support link weights.
; Every link is represented as a "1.00" in the connection matrix. This will change in a future version of the extension."
; TODO:
; 4. Links should really be named with ties, e.g. not family but family-ties, to be clearer about what's up
; 5. Have a chooser for network display since a circle makes groups hard to visualize
; DONE. 1.1 Make bounds on the opinions so they can't go beyond 0 or 100.
; DONE. 1.2 Make bounds on the weights so they can't go beyond 0 or 1.
; DONE. 2. Make opinion chooser that allows uniform or normal distribution (say, dev at 10).
; DONE. 3. Create better network.
; make a toggle switch setting for a) the basic every family 4, every workplace 20, and every friend group 10;
; OR b) more distributional methods as best I can implement them in a first pass.
globals [
num-agent
num-interactions
family-ties-m
coworker-ties-m
friend-ties-m
interactions-family-m
interactions-coworkers-m
interactions-friends-m
]
turtles-own [
opinion
tolerance
family_id
workplace_id
friend_group_id
num-family-ties
num-coworker-ties
num-friend-ties
my-interactions
my-group ;; a number representing the group this turtle is a member of, or -1 if this
my-fam-group ;; turtle is not in a group.
my-work-group
my-friend-group
my-interactors
]
links-own [ weight ]
directed-link-breed [family family-member] ; because we are using weights, these need to be directed
directed-link-breed [coworkers coworker]
directed-link-breed [friends friend]
;undirected-link-breed [family family-member] ; because we are using weights, these need to be directed
;undirected-link-breed [coworkers coworker]
;undirected-link-breed [friends friend]
; SETUP ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; OVERVIEW. This text is repeated again below, a snippet at a time, to match it to the code.
;## setup (setup is the standard name for the setup routine)
;1. Get global variables or setup variables from interface.
; - Default tolerance value (e.g. 20).
; - Default opinion split (e.g. 50%).
; - Default family size; default coworkers size; default friends size.
;2. Generate agents.
; - Number of agents from interface control.
; - Each agent has an opinion. Value from -50 to 50, say.
; - Each agent has a tolerance for other opinions. Allowed distance of another agents' opinion, e.g. 20. This can be used for deciding on whether to strengthen or weaken a tie.
; - Each agent has family ties, coworker ties, and friend ties. This may be done a number of ways: agentsets, links, different link breeds, hypergraph etc. Not sure of the best way. This could also be done outside of agent generation, in a network setup routine.
;3. Generate initial network
; - However makes sense
;4. Report initial network of ties (ties matrix) for output. This would be an adjacency matrix where each tie has a weight from 0 to 1.
to setup
; Clearing all world
clear-all
; Seting random seed for value RS, if we want!
if random-seed? [random-seed RS]
;1. Get global variables or setup variables from interface.
; - Default tolerance value (e.g. 20).
; - Default opinion split (e.g. 50%).
; - Default family size; default coworkers size; default friends size.
set family-ties-m matrix:make-constant number-of-agents number-of-agents 0
set coworker-ties-m matrix:make-constant number-of-agents number-of-agents 0
set friend-ties-m matrix:make-constant number-of-agents number-of-agents 0
;2. Generate agents.
; - Number of agents from interface control.
create-turtles number-of-agents [
; - Each agent has an opinion. Value from -50 to 50, say.
; set opinion -50
; set opinion opinion + random 101
if opinion-distribution = "normal" [
; [mid dev mmin mmax] [50 10 0 100]
set opinion random-normal-in-bounds 50 15 0 100
]
if opinion-distribution = "uniform" [
set opinion random 101
]
set color palette:scale-gradient [[ 255 0 0 ] [ 255 255 255 ] [0 0 255]] opinion 0 100
; - Each agent has a tolerance for other opinions. Allowed distance of another agents' opinion, e.g. 20. This can be used for deciding on whether to strengthen or weaken a tie.
set tolerance agent-tolerance
]
; put them in a circle with radius value
layout-circle turtles 12
;3. Generate initial network
; First we'll try using NetLogo's links, which are actually agents. This may end up a mess, but in THEORY it should make things easier.
; generate-the-network ; Stan version
generate_clustered_networks ; Elle version. using sub-procedure to generate somewhat clustered networks (scroll down)
ask family [ set color yellow ]
ask coworkers [ set color green ]
ask friends [ set color blue ]
; ask links [ set weight 1 ] ; at present, weights are set to .5 when links are created ^
; for testing
;ask family [ hide-link ]
;ask coworkers [ hide-link ]
;ask friends [ hide-link ]
;4. Report initial network of ties (ties matrix) for output. This would be an adjacency matrix where each tie has a weight from 0 to 1.
;4.1. matrices are initialised for storage
set family-ties-m matrix:make-constant number-of-agents number-of-agents 0 ; empty num-agent * num-agent adjacency matrix to store family ties
set coworker-ties-m matrix:make-constant number-of-agents number-of-agents 0
set friend-ties-m matrix:make-constant number-of-agents number-of-agents 0
;4.2. matrices are filled with information from the links
if another-adj-matrices? [
rewrite-adj-matrices
]
; NETWORK VISUALIZATION
; make links a bit transparent. Taken from Uri Wilensky's copyright-waived transparency model
ask links [
;; since turtle colors might be either numbers (NetLogo colors) or lists
;; (RGB or RGBA colors) make sure to handle both cases when changing the
;; transparency
ifelse is-list? color
;; list might either have 3 or 4 member since RGB and RGBA colors
;; are allowed, so you can't just replace or add an item at the
;; end of the list. So, we take the first 3 elements of the list
;; and add the alpha to the end
[ set color lput transparency sublist color 0 3 ]
;; to get the RGB equivalent of a NetLogo color we
;; use EXTRACT-RGB and then add alpha to the end
[ set color lput transparency extract-rgb color ]
]
set num-interactions 2 ; This controls the number of interactions of every agent in each tick
reset-ticks
end
to generate_clustered_networks ; this is a dumb first pass at generating clustered networks, not for use in final sims.
; families
let n_families round (number-of-agents / 4) ; ~ 4 agents per family
ask turtles [
set family_id item (who mod n_families) range n_families ; assign each agent to a family
]
ask turtles [
create-family-to other turtles with [family_id = [family_id] of myself] [ set weight .5 ] ; create ties to all family members
]
; workplaces
let n_workplaces 1 + random (number-of-agents / 2) ; randomly generate number of workplaces
ask turtles [
set workplace_id item (who mod n_workplaces) range n_workplaces ; assign each agent to a work place
]
ask turtles [
let fellow_workers [who] of other turtles with [workplace_id = [workplace_id] of myself] ; get list of potential workmates
foreach fellow_workers [
i ->
if random-float 1.01 < .8 [ ; 80% chance of having tie to each agent in same work place
create-coworker-to turtle i [ set weight .5 ]
]
]
]
; friends
let n_friend_groups round (number-of-agents / 8) ; start with assumption that friendship groups have ~ 8 people in them...
ask turtles [
set friend_group_id item (who mod n_friend_groups) range n_friend_groups
]
ask turtles [
let main_gang [who] of other turtles with [friend_group_id = [ friend_group_id] of myself]
foreach main_gang [
i ->
ifelse random-float 1.01 < .8 [ ; .8 prob that ties is made to each member of main gang
create-friend-to turtle i [ set weight .5 ]
] [
create-friend-to one-of turtles with [friend_group_id != [friend_group_id] of myself]
[ set weight .5 ] ; otherwise made randomly to another agent outside of main gang
]
]
]
end
to rewrite-adj-matrices
;show family-ties-m
;show friend-ties-m
;show coworker-ties-m
; We need to erase matrix every tick...
set family-ties-m matrix:make-constant number-of-agents number-of-agents 0 ; empty num-agent * num-agent adjacency matrix to store family ties
set coworker-ties-m matrix:make-constant number-of-agents number-of-agents 0
set friend-ties-m matrix:make-constant number-of-agents number-of-agents 0
nw:set-context turtles family
ask turtles [
; Creates list of 'who' of targets of out-links
let fam-neis sort [who] of out-family-member-neighbors
let i who
; writing weights into the matrices
foreach fam-neis [j -> matrix:set family-ties-m i j [weight] of family-member i j]
;show fam-neis
]
nw:set-context turtles friends
ask turtles [
; Creates list of 'who' of targets of out-links
let frn-neis sort [who] of out-friend-neighbors
let i who
; writing weights into the matrices
foreach frn-neis [j -> matrix:set friend-ties-m i j [weight] of friend i j]
;show frn-neis
]
nw:set-context turtles coworkers
ask turtles [
; Creates list of 'who' of targets of out-links
let job-neis sort [who] of out-coworker-neighbors
let i who
; writing weights into the matrices
foreach job-neis [j -> matrix:set coworker-ties-m i j [weight] of coworker i j]
;show job-neis
]
;show family-ties-m
;show friend-ties-m
;show coworker-ties-m
end
; SCHEDULE ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;## go (go is the standard name for the main action loop routine)
;One way to do this, as a place to start.
;1. Each agent chooses (default random, build in moral circle / parochial prefs here) some agents in its network to interact with, writes those choices to a current-tick all-agents-interactions adjacency matrix (interaction matrix, for short). (Btw a tick is Netlogo term for one loop through the go routine.)
;2. The interaction matrix is reported in some way to get written as an output in behaviorspace (we'll explain behaviorspace later). We can also calculate some network stat here to plot in the interface if we care to.
;3. Iterate through the interaction matrix so that each agent
; - "interacts" with the agents in its row, using its own opinion and tolerance to upweigh or downweigh the tie with each agent. (devil is in the mechanic here, we have options)
;4. Output new ties matrix for writing to table/file, polarization stats.
;5. Check if ties matrix has changed, if not maybe we can stop.
;6. New tick (loop to beginning of go to do it all over again)
to go
;1. Each agent chooses (default random, build in moral circle / parochial prefs here) some agents in its network to interact with, writes those choices to a current-tick all-agents-interactions adjacency matrix (interaction matrix, for short). (Btw a tick is Netlogo term for one loop through the go routine.)
; Instead of using an interaction adj matrix, for now, we'll just use the links. It's simpler. The link sets are equiv to edge lists which are equiv to an adj matrix.
ask turtles [
; I'm just using the num-interactions variable from the setup right now, but turtles could have differing interaction numbers in the future
ifelse count my-links > num-interactions [
set my-interactions n-of num-interactions my-links ; 2 interactions per tick, selected from all possible networks
] [
set my-interactions my-links ; avoid run time error if agents don't have enough partners
]
if verbose? [
print [ [(word breed " " who)] of other-end ] of my-interactions
print [ (word other-end) ] of my-interactions
]
]
;2. The interaction matrix is reported in some way to get written as an output in behaviorspace (we'll explain behaviorspace later). We can also calculate some network stat here to plot in the interface if we care to.
; Currently we won't bother with recording this matrix, since the end-of-tick network matrices are more useful.
;3. Iterate through the interaction matrix so that each agent
; - "interacts" with the agents in its row, using its own opinion and tolerance to upweigh or downweigh the tie with each agent. (devil is in the mechanic here, we have options)
; We'll just be using the agent iterating through the links for now, since the link set is equiv to an adj matrix.
ask turtles [
; we make a set of turtles who are at the end of the randomly chosen links
set my-interactors turtle-set [other-end] of my-interactions
; we call the function that houses the decision rules
apply-decision-rule-to-interactions
]
;4. Output new ties matrix for writing to table/file, polarization stats.
if another-adj-matrices? [
rewrite-adj-matrices
]
;5. Check if ties matrices have changed, if not maybe we can stop.
; This is not necessary if we're only running the model for a limited number of ticks in behavior space.
; VISUALIZATION UPDATING
; redraw links for thickness according to weight
ask links [ set thickness weight ]
; pointless turning so turtles do something visual each tick
ask turtles [ rt 20 ]
; layout using other than the circle
; layout-spring turtles family 0.2 10 1
;6. New tick (loop to beginning of go to do it all over again)
tick
end
to assign-by-size [ group-size ]
;; all turtles are initially ungrouped
ask turtles [ set my-group -1 ]
let unassigned turtles
;; start with group 0 and loop to build each group
let current 0
while [any? unassigned]
[
;; place a randomly chosen set of group-size turtles into the current
;; group. or, if there are less than group-size turtles left, place the
;; rest of the turtles in the current group.
ask n-of (min (list group-size (count unassigned))) unassigned
[ set my-group current ]
;; consider the next group.
set current current + 1
;; remove grouped turtles from the pool of turtles to assign
set unassigned unassigned with [my-group = -1]
]
end
to apply-decision-rule-to-interactions
; First, they need to set the lower and upper bounds on their opinion using the tolerance.
; Later they will test if lower < others-opinion <= upper than the others-opinion is within their tolerance
let lower opinion - tolerance
let upper opinion + tolerance
; FYI: (and this is given as "simple" by the documentation)
; "self" and "myself" are very different.
; "self" means "me".
; "myself" means "the turtle, patch or link who asked me to do what I'm doing right now."
if decision_rule = "trial_run" [
; THE FIRST DECISION RULE! Just some garbage to show how it's done. This decision rule makes no sense.
ask my-interactions [
; Check if within tolerance range, do something depending on that.
; The link is "smart" enough to know the other-end is NOT the turtle asking it, it's the other!
ifelse lower < ([opinion] of other-end) and ([opinion] of other-end) <= upper
[ ; within tolerance
set weight weight + .1 ; upweigh link
keep-weight-in-bounds
ask myself [ ; here myself is the turtle
set opinion (opinion + [weight] of myself) ; here myself is the link. GENIUS!
keep-opinion-in-bounds
]
]
[ ; outside tolerance
set weight weight - .1 ; downweigh link
keep-weight-in-bounds
ask myself [
set opinion (opinion - [weight] of myself)
keep-opinion-in-bounds
]
]
]
]
if decision_rule = "weighdiff_sigweight" [
; first pass at decision rule discussed by group on 25th of Jan, by Elle
let i [who] of self
; print word " I am" i
; print word "my tolerance zone is" lower
; print word " to " upper
ask my-interactions [ ; haha, someone make this method less shit (please)
ask turtle i [
let j [who] of other-end
; print word "my partner is" j
let weight_ij [weight] of myself
; print word "weight of link: " weight_ij
; print word "their opinon is " [opinion] of other-end
if lower < ([opinion] of other-end) and ([opinion] of other-end) <= upper [ ; i updates their opinion if j's opinion falls within their tolerance envelope
; print "in bounds!"
; print word "my original opinion was: " opinion
set opinion (opinion + (weight_ij * ( [opinion] of other-end - opinion)))
; print word "and after updating, it is: " opinion
]
let diff abs ( [opinion] of other-end - opinion )
ifelse diff < 2 * tolerance [ ; as long as i thinks j's opinion is not too extreme (i.e., it's < 2* their tolerance level)
ask myself [
set weight sigmoid (diff / 100)
]
] [
ask myself [ ; when i thinks j is extreme,
;set weight 0 ; they cut the i -> j tie by setting the weight = 0
die ; AND asking the link to die
]
; next step is to get agent i to make another tie here (I need to sleep, so not very well now)
ask turtle i [
; Let's do it just super-brutal-random for now -- we might polish it when the dust settles down...
; Idea is:
; 1. Randomly select another agent
; 2. Randomly select which type of link create
; 3. carefully try to establish the link of chosen type to chosen agent
; 4. In case the link exists, the new link is not created
let her one-of other turtles ; Because of ifelse cycle we need to store the agent
let rnn random 3 ; For the same reason we store the random number
(ifelse
rnn = 0 [carefully [create-family-member-to her] [print "Link has already existed! Nothing is created"]
]
rnn = 1 [carefully [create-friend-to her] [print "Link has already existed! Nothing is created"]
]
[
carefully [create-coworker-to her] [print "Link has already existed! Nothing is created"]
])
]
]
]
]
]
end
to keep-opinion-in-bounds
if opinion < 0 [ set opinion 0 ]
if opinion > 100 [set opinion 100 ]
end
to keep-weight-in-bounds
if weight < 0 [ set weight 0 ]
if weight > 1 [ set weight 1 ]
end
to-report sigmoid [ x ] ; sigmoid function
report (1 / (1 + exp (8 * ( x - .5))) ) ; smaller values of x (i.e., differences between agents' opinions) produce higher values (i.e., of new weights) ;
end
; from https://stackoverflow.com/questions/20230685/netlogo-how-to-make-sure-a-variable-stays-in-a-defined-range
to-report random-normal-in-bounds [mid dev mmin mmax]
let result random-normal mid dev
if result < mmin or result > mmax
[ report random-normal-in-bounds mid dev mmin mmax ]
report result
end
;observer> clear-plot set-plot-pen-interval 0.01 set-plot-x-range -0.1 1.1
;observer> histogram n-values 1000000 [ random-normal-in-bounds 0.5 0.2 0 1 ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Unused procedure for updating networks
; if make-adj-matrices? [
; create-adj-matrices ; see function to create the adj matrices below
; ]
;
; if make-adj-matrices? [
; create-adj-matrices ; see function to create the adj matrices below
; ]
;to create-adj-matrices
; let list_turtles range number-of-agents ; create an ordered list of turtles to loop through
; foreach list_turtles [
; i ->
; foreach list_turtles [
; j ->
; if i != j [ ; avoid error when turtles try to evaluate self
; nw:set-context turtles family
; ask turtle i [
; if out-family-member-neighbor? turtle j = true [ ; if i and j are family
; let tie_strength [weight] of (family-member i j) ; get the weight of the tie
; matrix:set family-ties-m i j tie_strength ; update relevant cell in family-ties-m with weight
; ]
; ]
; nw:set-context turtles coworkers
; ask turtle i [
; if out-coworker-neighbor? turtle j = true [ ; if i and j are coworkers
; let tie_strength [weight] of (coworker i j) ; ... ... ...
; matrix:set coworker-ties-m i j tie_strength
; ]
; ]
; nw:set-context turtles friends
; ask turtle i [
; if out-friend-neighbor? turtle j = true [ ; finally, if i and j are friends
; let tie_strength [weight] of (friend i j)
; matrix:set friend-ties-m i j tie_strength
; ]
; ]
; ]
; ]
; ]
; ; uncomment to confirm this ^ works
; ;print matrix:pretty-print-text family-ties-m
; ;print matrix:pretty-print-text coworker-ties-m
; ;print matrix:pretty-print-text friend-ties-m
; ;print family-ties-m
;
;end
;; Unused procedure for network generation
;to generate-the-network
; ; We'll use Uri Wilkensky's public domain Grouping Turtles Example code, modified, to help us.
;
; if network-groups-sizes = "fam4 work20 friend10" [
; let fam-size 4
; let work-size 20
; let friend-size 10
;
; assign-by-size fam-size
; ask turtles [
; set my-fam-group my-group
; create-family-with other turtles with [ my-fam-group = [my-fam-group] of self ]
; ask my-links [ if random 100 < 5 [ die ] ] ; 5% chance of being estranged.
; if random 100 < 10 [
; if 0 < count ((other turtles) with [ my-fam-group != [my-fam-group] of myself ] ) [
; create-family-member-with one-of ((other turtles) with [ my-fam-group != [my-fam-group] of myself ] )
; ]
; ]
; ]
;
; assign-by-size work-size
; ask turtles [
; set my-work-group my-group
; create-coworkers-with other turtles with [ my-work-group = [my-work-group] of self ]
; ask my-links [ if random 100 < 10 [ die ] ] ; 10% chance of being estranged or not knowing each other.
; if random 100 < 10 [
; if 0 < count ((other turtles) with [ my-work-group != [my-work-group] of myself ] ) [
; create-coworkers-with one-of ((other turtles) with [ my-work-group != [my-work-group] of myself ] )
; ]
; ]
; ]
;
; assign-by-size friend-size
; ask turtles [
; set my-friend-group my-group
; create-friends-with other turtles with [ my-friend-group = [my-friend-group] of self ]
; ask my-links [ if random 100 < 5 [ die ] ] ; 5% chance of being estranged or not knowing a friend of friends.
; if random 100 < 10 [
; if 0 < count ((other turtles) with [ my-friend-group != [my-friend-group] of myself ] ) [
; create-friend-with one-of ((other turtles) with [ my-friend-group != [my-friend-group] of myself ] )
; ]
; ]
; ]
;
; ]
;
; if network-groups-sizes = "size drawn from dists" [
; print "Sorry, the 'size drawn from dists' option has not been finished yet!"
;
; ]
;
; if network-groups-sizes = "random" [
; ; n-of size agentset
; ask turtles [
; create-family-with n-of num-family-ties other turtles
; create-coworkers-with n-of num-coworker-ties other turtles
; create-friends-with n-of num-friend-ties other turtles
; ]
; ]
;
;end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Elle's suggested CODE DUMP !!!
; - Each agent has family ties, coworker ties, and friend ties. This may be done a number of ways: agentsets, links, different link breeds, hypergraph etc. Not sure of the best way. This could also be done outside of agent generation, in a network setup routine.
; ELLE - this is now achieved by the generate_clustered_networks procedure, but with much less variability
; set num-family-ties 1 + random 5
; set num-coworker-ties 1 + random 10
; set num-friend-ties 1 + random 10
; ask turtles [
; ; n-of size agentset
; create-family-with n-of num-family-ties other turtles
; create-coworkers-with n-of num-coworker-ties other turtles
; create-friends-with n-of num-friend-ties other turtles
; ]
; ;4. Report initial network of ties (ties matrix) for output. This would be an adjacency matrix where each tie has a weight from 0 to 1.
; ; HOWEVER, right now I'm just trying to get an edge list, which can be made into an adj matrix.
;
; ; TODO: This is frustrating as hell.
;
; ; taken from https://stackoverflow.com/a/44568348/10405322 and modified
; ; each of these work separately, but I need them together; the link to report its breed AND the turtles at both ends
; ;print (word [breed] of links)
;
; ;print ("Try to get the link to report breed AND color:")
; ;print [(word breed color)] of links
; ; of can handle multiple variables or more complicated expressions. But trying to nest things breaks down.
;
; ; Ok now try to get the link to report its breed and its ends
; ; print (word [ [who] of both-ends breed ] of links)
; ; nope, doesn't work!
; ; now try
; ;print("now try this")
;
; print [(word [who] of both-ends " " breed)] of links
;
; print [ map [ t -> [ (word breed " " who) ] of t ] (list end1 end2) ] of links
;
; ; Then that can be modified to write the edge list to a csv file.
; ; This works, believe it or not, but it doesn't have the link breed:
; ; csv:to-file "test.csv" [ [ (word breed " " who) ] of both-ends ] of links
;
; ; This DOES NOT work.
; ; csv:to-file "test.csv" [(word [who] of both-ends " " breed)] of links
;
; ; TODO: create slider for this num-interactions
; show (list [(word "turtle:" who " " opinion)] of turtles )
;; !!!FrK: Now the opinion is generated from 0 to 99,
;; !!!FrK: if we want -50 to +50 we need code 'set opinion -50 + random 101', since 'random 101' generates integers from 0 to 100.
;; !!!FrK: But because of color palette we need to stay on 0--100, se then 'set opinion random 101'.
;; !!!FrK: I know how my comments my look like, in Czech we say these people 'hnidopich' which means 'flea hunter' :-),
;; !!!FrK: BUT we easily might produce an artifact by this slight asymetry, in very ballanced scenario might big clusters happen
;; !!!FrK: near the 0 since the symetrically oposite value taken for granted 100 would be missing.
; Stan: Ah yes, good catch on the range not including the ending integer!
; The opinion was set to 0 to 100 to be lazy for the first draft and show some coloring options.
; We can really use whatever, and modifying the color and visualization code to match shouldn't be a problem.
;; !!!FrK: Be aware that this code might lead with probability 0.002 to situation that some agent will have no links,
;; !!!FrK: now it is not the serious problem since we test model with 20 turtles/agents, but on larger experiment runtime error will happen for sure.
;; !!!FrK: Problem is that code 'random 5' generates integers from 0 to 4, 'random 10' from 0 to 9.
;; !!!FrK: Solution might be code '1 + random 5' which generates random integers from 1 to 5, or
;; !!!FrK: to check if every turle/agent has at least one link and in case it has no links,
;; !!!FrK: then randomly choose which type of link we creates for this agent and then create this link.
;; !!!FrK: We maight also decide that in case of 'family' we will go for '1 + random 5',
;; !!!FrK: since we assume that every agent has to have at least one family relative,
;; !!!FrK: and the rest of the code we let as it is, since we let agents have no job and no friends.
;; !!!FrK: Last solution that comes to my mind is to parametrize the minima:
;; !!!FrK: we create sliders 'min-family' 'min-coworker' 'min-frind' and the code we change to 'min-family + random 5' etc.,
;; !!!FrK: but this probably also would lead to demand parametrize maxima,
;; !!!FrK: so we will end up with something like 'min-family + random (max-family - min-family + 1)' and things would get more and more complicated...
;; !!!FrK: And we would like to keep it simple, right? Yes, I'm not the right person for this sentence :-)
; Stan: I am not sure having some very small probability of agents having no links is a problem?
; There are some people in the world who are very socially isolated, even without family connection, but it is rare.
; If a later version of the model has people meet new people once in a while (which it should, I think) it will be a feature not a bug.
;; !!!FrK: Right! We have to be carefully of division by zero with these lone agents, and for the future devolopement it's a promising feature, when the rules allow create new links.
;; !!!FrK: We have also carefully pick out interactions from existing links, since this code produces error: 'n-of 5 range 3',
;; !!!FrK: which means that if we ask for more interactions in 'go' than agent has links, then we receive error.
;; !!!FrK: No we ask constantly for 2 interactions, so turtles with 0 or 1 link only are real problem now.
;; !!!FrK: But you wisely changed code that each agent has at least 3 links and we ask just for 2, so we are safe now and
;; !!!FrK: in the future we find a way how to get around this problem/bug/feature.
@#$#@#$#@
GRAPHICS-WINDOW
210
10
647
448
-1
-1
13.0
1
10
1
1
1
0
0
0
1
-16
16
-16
16
0
0
1
ticks
30.0
BUTTON
21
28
76
61
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
21
70
193
103
number-of-agents
number-of-agents
20
100
100.0
1
1
NIL
HORIZONTAL
BUTTON
137
28
192
61
go
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
21
105
193
138
agent-tolerance
agent-tolerance
0
100
24.0
1
1
NIL
HORIZONTAL
SLIDER
657
38
830
71
transparency
transparency
0
255
90.0
1
1
NIL
HORIZONTAL
TEXTBOX
660
19
797
37
Link transparency
11
0.0
1
SWITCH
658
79
766
112
verbose?
verbose?
1
1
-1000
PLOT
657
184
857
334
Mean opinion
NIL
NIL
0.0
10.0
0.0
10.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot mean [opinion] of turtles"
BUTTON
79
28
134
61
go
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
21
144
159
189
opinion-distribution
opinion-distribution
"uniform" "normal"
0
CHOOSER
10
401
180
446
network-groups-sizes
network-groups-sizes
"fam4 work20 friend10" "size drawn from dists" "random"
1
CHOOSER
921
28
1127
73
decision_rule
decision_rule
"trial_run" "weighdiff_sigweight"
1
TEXTBOX
923
10
1073
28
Decision rule in play
11
0.0
1
PLOT
918
183
1118
333
Histogram of opinions
NIL
NIL
0.0
110.0
0.0
10.0
true
false
"" ""
PENS
"default" 10.0 1 -16777216 true "" "histogram [opinion] of turtles"
INPUTBOX
1177
29
1230
89
RS
10.0
1
0
Number
SWITCH
1237
29
1371
62
random-seed?
random-seed?
0
1
-1000
TEXTBOX
1178
13
1406
41
Controlling the value of random seed
11
0.0
1
PLOT
1134
108
1334
258
Weight of liinks
NIL
NIL
0.0
10.0
0.0
1.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot mean [weight] of links"
SWITCH
853
124
1031
157
another-adj-matrices?
another-adj-matrices?
0
1
-1000
BUTTON
1370
189
1436
222
profile
profiler:start ;; start profiling\nsetup ;; run something you want to measure\nprofiler:stop ;; stop profiling\nprint profiler:report ;; view the results\nprofiler:reset ;; clear the data
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
PLOT
1127
268
1421
418
Numbers of links
NIL
NIL
0.0
10.0
0.0
10.0
true
true
"" ""