forked from frantisek901/Spirals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupFormation.nlogo
2172 lines (1958 loc) · 57.7 KB
/
groupFormation.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
;; This is the first model for the high-risk high-gain project with Mike and Ashley
;; Here we'd like to apply HK/Deffuant model in more than 1D and look how agents adapt in >1D opinion space and whether they form groups.
;; MAIN BRANCH: THIS IS OUR THE BEST MODEL SO FAR
;; Created: 2021-10-21 FranCesko
;; Edited: 2021-12-29 FranCesko
;; Encoding: windows-1250
;; NetLogo: 6.2.2
;;
;; IDEA: What about simply employ Spiral of Silence?
;; Just simply -- general parameter on scale (0; 1> and probability of speaking her attitude/opinion,
;; baseline is p==1, everybody speaks always, if p==0.5 so everybody has 0.5 probability to speak her opinion/attitude at given step,
;; if succeeds - speaks in given step, if not - falls silent for the respective step.
;; In HK mechanism, agent computes mean opinion of all speaking agents who are inside 'opinion boundary' (are not further than threshold).
;; In Defuant, agent randomly takes one speaking agent inside the 'opinion boundary' and sets opinon as average of their opinions.
;; DONE!
;;
;; IDEA: Handle P-speaking as Uncertainty -- besides constant value for every agent, create random mode (random uniform for the start),
;; where all agents will have their own value of speaking probability which they will follow.
;; DONE!
;;
;; IDEA: Choose, how many opinions agents update: sometimes 1, 2, 3, 4 ...
;; DONE!
;;
;; IDEA: Give weights to opinions... Taken from media, or from interpersonal communication:
;; - agents pick opinion according the importance, and update importance according number of contacts regarding the opinion
;;
;; IDEA: Compute clusters
;; DONE!
;;
;; WISHLIST:
;; - differentiate between interpersonal communication and social media communication -- two overlapping networks with their own rules
;; - how radicalization is possible? How polarization happens?
;; - differential attraction
;; - repulsion
;; - media exposure will be crucial…we can ask abt opinion consistent content, opinion contrary, and “mainstream/mixed”…
;; how to we conceptualize/model those in ABM? Is this too simplistic (eg, think of the different flavors of conservative media,
;; ranging from CDU type media to extremist hate groups).
;; - how to think about social media influencers (eg Trump before deplatforming)…
;; is it possible to designate “superagents” who influence everyone sharing certain beliefs and see their effects…
;; both reach everyone in a group and their opinions are very highly weighted (or people vary in how much they weight that opinion?
;; Could estimate Twitter effect that way! Perhaps one could even model how movement towards an opinion might influence the superagent
;; to increase communication or change focus…
;; - Employ homophily/heterophily principle at model start.
;; - Control degree of opinion randomness at the start (different mean and SD of opinion for different groups)
;; - Mike was thinking…after we do “superagents”, the Trump/foxnews avatars…one thing that would be neat and represent social reality
;; is to have some kind of attraction to those who share beliefs (including superagents), but that decreases with close proximity…
;; that way we have less ability/willingness to select attitude consistent sources around us (eg can’t escape family and coworkers),
;; but can seek them elsewhere. That might allow us to look at what happens in a more or less diverse local opinion environment, among other things.
;;
;; Parameters:
;; Small-world network (Watts-Strogatz)
;; agents have more than 1 type of attitude
;; opinion on scale <-1;+1>
;; boundary -- defines range as fraction of maximum possible Eucleid distance in n-dimensional space, this maximum depends on number of opinions: sqrt(opinions * 4)
;;
;; TO-DO:
;; 1) constructing file name for recording initial and final state of simulation
;; DONE!
;; 2) implementing recording into the model -- into setup and final steps (delete component detection and just record instead)
;; DONE!
;;
;; 3) Reviewer's comments:
;; The reviewer for your computational model Simulating Components of the Reinforcing Spirals Model and Spiral of Silence v1.0.0 has recommended that changes be made to your model. These are summarized below:
;; Very interesting model! It needs better documentation though, both within the code as comments, and the accompanying narrative documentation. Please consider following the ODD protocol or equivalent to describe your model in sufficient detail so that another could replicate the model based on the documentation.
;; Has Clean Code:
;; The code should be cleaned up and have more comments describing the intent and semantics of the variables.
;; Has Narrative Documentation:
;; The info tab is empty and the supplementary doc does not include sufficient detail to replicate the model. For example documentation please see ODD examples from other peer reviewed models in the library.
;; Is Runnable:
;; Runs well.
;; On behalf of the [email protected], thank you for submitting your computational model(s) to CoMSES Net! Our peer review service is intended to serve the community and we hope that you find the requested changes will improve your model’s accessibility and potential for reuse. If you have any questions or concerns about this process, please feel free to contact us.
;;
;; 4) Adapt recording data for cluster computation -- machine's root independent.
;; DONE!
;;
;; 5) Appropriate recorded data format -- we want it now as:
;; a) dynamical multilayer network, one row is one edge of opinion distance network,
;; b) separate file with agent's traits (P-speaking, Uncertainty etc.)
;; c) as it was before, contextual variables of one whole simulation run are coded in the filenames
;; DONE!
;;
extensions [nw]
breed [ghosts ghost]
turtles-own [Opinion-position P-speaking Speak? Uncertainty Record Last-opinion Pol-bias Initial-opinion]
globals [main-Record components positions]
;; Initialization and setup
to setup
;; Redundant conditions which should be avoided -- if the boundary is drawn as constant, then is completely same whether agents vaguely speak or openly listen,
;; seme case is for probability speaking of 100%, then it's same whether individual probability is drawn as constant or uniform, result is still same: all agents has probability 100%.
if avoid-redundancies? and mode = "vaguely-speak" and boundary-drawn = "constant" [stop]
if avoid-redundancies? and p-speaking-level = 1 and p-speaking-drawn = "uniform" [stop]
;; these two conditions cover 7/16 of all simulations, approx. the half! This code should stop them from running.
;(avoid-redundancies? and mode = "vaguely-speak" and boundary-drawn = "constant") or (avoid-redundancies? and p-speaking-level = 1 and p-speaking-drawn = "uniform")
;; We erase the world and clean patches
ca
ask patches [set pcolor patch-color]
;; We initialize small-world network with random seed
if set-seed? [random-seed RS]
if HK-benchmark? [set n-neis (N-agents - 1) / 2]
nw:generate-watts-strogatz turtles links N-agents n-neis p-random [
fd (max-pxcor - 1)
set size (max-pxcor / 10)
]
;; To avoid some random artificialities due to small-world network generation,
;; we have to set random seed again.
if set-seed? [random-seed RS]
;; Then we migh initialize agents/turtles
ask turtles [
set Opinion-position n-values opinions [precision (1 - random-float 2) 3] ;; We set opinions...
set Last-opinion Opinion-position ;; ...set last opinion as present opinion...
set Initial-opinion Opinion-position ;; ...we record opinion as initial opinion ...
set Record n-values record-length [0] ;; ... we prepare indicator of turtle's stability, at all positions we set 0 as non-stability...
set P-speaking get-speaking ;; ...assigning individual probability of speaking...
set speak? speaking ;; ...checking whether agent speaks...
set Pol-bias get-bias ;; ... setting value of political bias...
set Uncertainty get-uncertainty ;;... setting value of Uncertainty.
getColor ;; Coloring the agents according their opinion.
getPlace ;; Moving agents to the opinion space according their opinions.
]
;; Coloring patches according the number of agents/turtles on them.
ask patches [set pcolor patch-color]
;; Hiding links so to improve simulation speed performance.
ask links [set hidden? TRUE]
;; Setting the indicator of change for the whole simulation, again as non-stable.
set main-Record n-values record-length [0]
reset-ticks
;;;; Finally, we record initial state of simulation
;; If we want we could construct filename to contain all important parameters shaping initial condition, so the name is unique stamp of initial state!
if construct-name? [set file-name-core (word RS "_" N-agents "_" p-random "_" n-neis "_" opinions "_" updating "_" boundary "_" boundary-drawn "_" p-speaking-level "_" p-speaking-drawn "_" mode)]
;; recording itself
if record? [record-state-of-simulation]
end
;; Sub-routine which opens/creates *.csv file and stores there states of all turtles
to record-state-of-simulation
;; setting working directory
;set-current-directory directory
;; seting 'file-name'
let file-name (word "Sims/Nodes01_" file-name-core "_" ticks ".csv")
;;;; File creation and opening: NODES
;; If file exists at the start we delete it to start with clean file
if file-exists? file-name [file-delete file-name]
file-open file-name ;; This opens existing file (at the end) or creates file if doesn't exist (at the begining)
;; Constructing list for the first row with variable names:
let row (list "ID" "Uncertainty" "pSpeaking" "Speaks")
foreach range Opinions [i -> set row lput (word "Opinion" (i + 1)) row]
;; Writing the variable names in the first row at the start
file-print list-to-string (row)
;; For writing states itself we firstly need to create list of sorted turtles 'srt'
let srt sort turtles
;; Then we iterate over the list 'srt':
foreach srt [t -> ask t [ ;; every turtle in the list...
set row (list (word "Nei" who) Uncertainty P-Speaking (ifelse-value (speak?)[1][0])) ;; stores in list ROW its ID, Uncertainty, P-Speaking and whether speaks...
foreach Opinion-position [op -> set row lput (precision(op) 3) row] ;; Opinions ...
file-print list-to-string (row)
file-flush ;; for larger simulations with many agents it will be safer flush the file after each row
]]
;; Finally, we close the file
file-close
file-close
;;;; File creation and opening: LINKS
;; seting 'file-name' for links.
set file-name (word "Sims/Links01_" file-name-core "_" ticks ".csv")
;;;; File creation and opening
;; If file exists at the start we delete it to start with clean file
if file-exists? file-name [file-delete file-name]
file-open file-name ;; This opens existing file (at the end) or creates file if doesn't exist (at the begining)
;; Constructing list for the first row with variable names:
set row (list "ID1" "ID2" "Communication" "Distance")
;; Writing the variable names in the first row at the start
file-print list-to-string (row)
;; We need to prepare counters and other auxilliary varibles for doubled cycle:
let i 0
let j 1
let iMax (count turtles - 2)
let jMax (count turtles - 1)
let mine []
let her []
;; Now double while cycle!
while [i <= iMax] [ ;; Iterating over all turtles except the last one
set j i + 1
while [j <= jMax][ ;; Second cycle iterates over all turtles with index higher than 'i'
set mine ([opinion-position] of turtle i) ;; First opinion for measuring distance...
set her ([opinion-position] of turtle j) ;; Second opinion...
set row (list (word "Nei" i) (word "Nei" j) (ifelse-value (is-link? link i j) [1][0]) opinion-distance2 (mine) (her)) ;; Construction of the 'row'
file-print list-to-string (row) ;; Writing the row...
file-flush ;; for larger simulations with many agents it will be safer flush the file after each row
set j j + 1 ;; Updating counter of second cycle
]
set i i + 1 ;; Updating counter of the first cycle
]
;; Finally, we close the file
file-close
file-close
end
;; reporter function for translating a list into one string of values divided by commas
to-report list-to-string [LtS]
;; Initializing empty string and counter
let str ""
let i 0
;; Now we go through the list item by item and add them into string
while [i < length LtS][
set str (word str item i LtS)
set i i + 1
if (i < length LtS) [set str (word str ", ")]
]
report str
end
;; Sub-routine for assigning value of p-speaking
to-report get-speaking
;; We have to initialize empty temporary variable
let pValue 0
;; Then we draw the value according the chosen method
if p-speaking-drawn = "constant" [set pValue p-speaking-level + random-float 0] ;; NOTE! 'random-float 0' is here for consuming one pseudorandom number to cunsume same number of pseudorandom numbers as "uniform
if p-speaking-drawn = "uniform" [set pValue ifelse-value (p-speaking-level < 0.5)
[precision (random-float (2 * p-speaking-level)) 3]
[precision (1 - (random-float (2 * (1 - p-speaking-level)))) 3]]
if p-speaking-drawn = "function" [set pValue (precision(sqrt (sum (map [ x -> x * x ] opinion-position)) / sqrt opinions) 3) + random-float 0] ;; NOTE! 'random-float 0' is here for consuming one pseudorandom number to cunsume same number of pseudorandom numbers as "uniform
;; Report result back
report pValue
end
;; Sub-routine for assigning value of ideological bias
to-report get-bias
;; initialize 'bias' variable
let bias 0
;; uniformly generating number from interval (0, + bias-margin) and then
;; moving it towards negative pole by (bias-margin * bias-of-bias),
;; bias-of-bias is fraction [0, 1], 0 means full bias towards '+' pole, resulting in interval of bias [0, +bias-margin],
;; 1 means full bias towards '-' pole, interval [-bias-margin, 0], 0.5 means ballanced, resulting in interval [-bias-margin/2, +bias-margin/2]
if bias-drawn = "uniform" [set bias precision ((random-float (bias-margin)) - (bias-margin * bias-of-bias)) 3]
;; report the result back
report bias
end
;; sub-routine for assigning value of uncertainty to the agent
to-report get-uncertainty
;; We have to initialize empty temporary variable
let uValue 0
;; Then we draw the value according the chosen method
if boundary-drawn = "constant" [set uValue boundary + random-float 0] ;; NOTE! 'random-float 0' is here for consuming one pseudorandom number to cunsume same number of pseudorandom numbers as "uniform"
if boundary-drawn = "uniform" [set uValue precision (min-boundary + random-float (2 * boundary - min-boundary)) 3]
if boundary-drawn = "normal" [
set uValue precision (random-normal boundary sigma) 3
while [uValue < min-boundary or uValue > 1] [
set uValue precision (random-normal boundary sigma) 3
]
]
;; reporting value back for assigning
report uValue
end
;; sub-routine for graphical representation -- it takes two opinion dimension and gives the agent on XY coordinates accordingly
to getPlace
;; check whether our cosen dimension is not bigger than maximum of dimensions in the simulation
if X-opinion > opinions [set X-opinion 1]
if Y-opinion > opinions [set Y-opinion 1]
;; then we rotate the agent towards the future place
facexy ((item (X-opinion - 1) opinion-position) * max-pxcor) ((item (Y-opinion - 1) opinion-position) * max-pycor)
;; lastly we move agent on the place given by opinion dimensions chosen for X and Y coordinates
set xcor (item (X-opinion - 1) opinion-position) * max-pxcor
set ycor (item (Y-opinion - 1) opinion-position) * max-pycor
end
;; sub routine for coloring agents according their average opinion across all dimensions --
;; useful for distinguishing agents with same displayed coordinates, but differing in other opinion dimensions,
;; then we see at one place agents with different colors.
to getColor
;; speaking agents are colored from very dark red (average -1) through red (average 0) to very light red (average +1)
ifelse speak? [
set color 15 + 4 * mean(opinion-position)
set size (max-pxcor / 10)
]
;; silent agent are white and of zero size, to just show the speaking one -- later we might parametrize this if we want...
[
set color white
set size 0
]
end
;; Sub routine for dissolving whether agent speaks at the given round/step or not
to-report speaking
;; We just generate random number and compare it with parameter 'p-speaking' -- this code directly produces values TRUE or FALSE
let pValue P-speaking
;; For the case of function we have to update pValue
;if p-speaking-drawn = "function" [set pValue precision(sqrt (sum (map [ x -> x * x ] opinion-position)) / sqrt opinions) 3]
report pValue > random-float 1
end
;; sub-routine for visual purposes -- colors empty patches white, patches with some agents light green, with many agents dark green, with all agents black
to-report patch-color
report 59.9 - (9.8 * (ln(1 + count turtles-here) / ln(N-agents)))
end
;; Main routine
to go
;; Redundant conditions which should be avoided -- if the boundary is drawn as constant, then is completely same whether agents vaguely speak or openly listen,
;; seme case is for probability speaking of 100%, then it's same whether individual probability is drawn as constant or uniform, result is still same: all agents has probability 100%.
if avoid-redundancies? and mode = "vaguely-speak" and boundary-drawn = "constant" [stop]
if avoid-redundancies? and p-speaking-level = 1 and p-speaking-drawn = "uniform" [stop]
;; these two conditions cover 7/16 of all simulations, approx. the half! This code should stop them from running.
;; Check whether we set properly parameter 'updating' --
;; if we want update more dimensions than exists in simulation, then we set 'updating' to max of dimensions, i.e. 'opinions'
if updating > opinions [set updating opinions]
ask turtles [
;; speaking and coloring
if p-speaking-drawn = "function" [set p-speaking (precision(sqrt (sum (map [ x -> x * x ] opinion-position)) / sqrt opinions) 3)]
set speak? speaking
getColor
getPlace
;; storing previous opinion position as 'Last-opinion'
set Last-opinion Opinion-position
;; Mechanism of opinion change
if model = "HK" [
change-opinion-HK
]
;; Note: Now here is only Hegselmann-Krause algorithm, but in the future we might easily employ other algorithms here!
]
;; Patches update color according the number of turtles on it.
ask patches [set pcolor patch-color]
;; We have to check here the change of opinions, resp. how many agents changed,
;; and record it for each agent and also for the whole simulation
;; Turtles update their record of changes:
ask turtles [
;; we take 1 if opinion is same, we take 0 if opinion changes, then
;; we put 1/0 on the start of the list Record, but we omit the last item from Record
set Record fput ifelse-value (Last-opinion = Opinion-position) [1][0] but-last Record
]
;; Then we might update it for the whole:
set main-Record fput precision (mean [mean Record] of turtles) 3 but-last main-Record
tick
;; Finishing condition:
;; 1) We reached state, where no turtle changes for RECORD-LENGTH steps, i.e. average of MAIN-RECORD (list of averages of turtles/agents RECORD) is 1 or
;; 2) We reached number of steps specified in MAX-TICKS
if mean main-Record = 1 or ticks = max-ticks [record-state-of-simulation stop]
if (ticks / record-each-n-steps) = floor(ticks / record-each-n-steps) [record-state-of-simulation]
end
;; sub-routine for updating opinion position of turtle according the Hegselmann-Krause (2002) model
to change-opinion-HK
;; initialization of agent set 'influentials' containing agents whose opinion positions uses updating agent
let influentials nobody
;; 1) updating agent uses only visible link neighbors in small-world network
let visibles other link-neighbors with [color != white]
;; 2) we have different modes for finding influentials:
;; 2.1) in mode "I got them!" agent looks inside his boundary (opinion +/- uncertainty),
;; i.e. agent takes opinions not that much far from her opinion
if mode = "openly-listen" [
;; we compute 'lim-dist' -- it is the numerical distance in given opinion space
let lim-dist (Uncertainty * sqrt(opinions * 4))
;; we set as influentials agents with opinion not further than 'lim-dist'
set influentials visibles with [opinion-distance <= lim-dist]
]
;; 2.1) in mode "They got me!" agent looks inside whose boundaries (opinion +/- uncertainty)
;; she is, i.e. agents takes opinions spoken with such a big uncertainty that it matches her own opinion
if mode = "vaguely-speak" [
;; Note: Here is used the 'Uncetainty' value of called agent, agent who might be used for updating,
;; not 'Uncertainty' of calling agent who updates her opinion.
set influentials visibles with [opinion-distance <= (Uncertainty * sqrt(opinions * 4))]
]
;; 3) we also add the updating agent into 'influentials'
set influentials (turtle-set self influentials)
if social-bias [
;; in case Social-bias is effective, then updating agent will create 'ghost',
;; its younger self with same opinion which agent had at the start of simulation (result of its socialization).
hatch-ghosts 1 [set opinion-position [Initial-opinion] of myself]
;; Then we include this 'ghost' into the agent-set 'influentials'
set influentials (turtle-set ghosts influentials)
]
;; we check whether there is someone else then calling/updating agent in the agent set 'influentials'
if count influentials > 1 [
;; here we draw a list of dimensions which we will update:
;; by 'range opinions' we generate list of integers from '0' to 'opinions - 1',
;; by 'n-of updating' we randomly take 'updating' number of integers from this list
;; by 'shuffle' we randomize order of the resulting list
let op-list shuffle n-of updating range opinions
;; we initialize counter 'step'
let step 0
;; we go through the while-loop 'updating' times:
while [step < updating] [
;; we initialize/set index of updated opinion dimension according the items on the 'op-list',
;; note: since we use while-loop, we go through each item of the 'op-list', step by step, iteration by iteration.
let i (item step op-list)
;; them we update dimension of index 'i' drawn from the 'op-list' in the previous line:
;; 1) we compute average position in given dimension of the calling/updating agent and all other agents from agent set 'influentials'
;; by the command '(mean [item i opinion-position] of influentials)', and
;; 2) we update average value through the pol-bias
;; 3) we set value as new opinion position by command 'set opinion-position replace-item i opinion-position X' where 'X' is the mean opinion (ad 1, see line above)
;; ad 1: averge position computation
let val precision (mean [item i opinion-position] of influentials) 3 ;; NOTE: H-K model really assumes that agent adopts immediatelly the 'consesual' position
;; ad 2: application of bias
if pol-bias < 0 [set val (((1 + val) * (1 + pol-bias)) - 1)]
if pol-bias > 0 [set val (1 - ((1 - val) * (1 - pol-bias)))]
;; ad 3: assigning the value 'val'
set opinion-position replace-item i opinion-position val
;; advancement of counter 'step'
set step step + 1
]
]
;; In case there are some ghosts, kill them!
if (count ghosts) > 0 [ask ghosts [die]]
end
;; sub-routine for computing opinion distance of two comparing agents
to-report opinion-distance
;; we store in temporary variable the opinion of the called and compared agent
let my opinion-position
;; we store in temporary variable the opinion of the calling and comparing agent
let her [opinion-position] of myself
;; we initialize counter of step of comparison -- we will compare as many times as we have dimensions
let step 0
;; we initialize container where we will store squared distance in each dimension
let dist 0
;; while loop going through each dimension, computiong distance in each dimension, squarring it and adding in the container
while [step < opinions] [
;; computiong distance in each dimension, squarring it and adding in the container
set dist dist + (item step my - item step her) ^ 2
;; advancing 'step' counter by 1
set step step + 1
]
;; computing square-root of the container 'dist' -- computing Euclidean distance -- and setting it as 'dist'
set dist sqrt dist
;; reporting Euclidean distance
report dist
end
;; sub-routine for computing opinion distance of two comparing agents
to-report opinion-distance2 [my her]
;; we initialize counter of step of comparison -- we will compare as many times as we have dimensions
let step 0
;; we initialize container where we will store squared distance in each dimension
let dist 0
;; while loop going through each dimension, computiong distance in each dimension, squarring it and adding in the container
while [step < opinions] [
;; computiong distance in each dimension, squarring it and adding in the container
set dist dist + (item step my - item step her) ^ 2
;; advancing 'step' counter by 1
set step step + 1
]
;; computing square-root of the container 'dist' -- computing Euclidean distance -- and setting it as 'dist'
set dist sqrt dist
;; Turning 'dist' into 'weight'
let weight (sqrt(4 * opinions) - dist) / sqrt(4 * opinions)
;; reporting weight of distance
report precision weight 3
end
@#$#@#$#@
GRAPHICS-WINDOW
219
10
667
459
-1
-1
40.0
1
10
1
1
1
0
0
0
1
-5
5
-5
5
1
1
1
ticks
30.0
BUTTON
10
10
73
43
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
73
10
136
43
GO!
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
135
10
198
43
Step
go\n;ask turtles [\n; set speak? TRUE\n; getColor\n;]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
10
42
182
75
N-agents
N-agents
10
1000
101.0
1
1
NIL
HORIZONTAL
SLIDER
10
385
102
418
n-neis
n-neis
1
500
50.0
1
1
NIL
HORIZONTAL
SLIDER
10
417
102
450
p-random
p-random
0
0.5
0.25
0.01
1
NIL
HORIZONTAL
SLIDER
10
75
182
108
opinions
opinions
1
50
8.0
1
1
NIL
HORIZONTAL
BUTTON
10
462
65
495
getPlace
ask turtles [getPlace]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
10
140
102
185
model
model
"HK"
0
SLIDER
10
306
182
339
p-speaking-level
p-speaking-level
0
1
0.5
0.001
1
NIL
HORIZONTAL
SLIDER
10
228
182
261
boundary
boundary
0.01
1
0.3
0.01
1
NIL
HORIZONTAL
INPUTBOX
674
10
746
70
RS
1.0
1
0
Number
SWITCH
746
10
839
43
set-seed?
set-seed?
0
1
-1000
BUTTON
67
462
122
495
getColor
ask turtles [\n set speak? TRUE\n getColor\n]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
120
462
175
495
inspect
inspect turtle 0\nask turtle 0 [print opinion-position]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
175
462
230
495
sizes
show sort remove-duplicates [count turtles-here] of turtles
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
230
462
285
495
HIDE!
\nask turtles [set hidden? TRUE]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
284
462
339
495
SHOW!
\nask turtles [set hidden? FALSE]\nask turtles [set size (max-pxcor / 10)]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
10
261
148
306
boundary-drawn
boundary-drawn
"constant" "uniform" "normal"
1
SLIDER
999
107
1171
140
sigma
sigma
0
1
0.0
0.01
1
NIL
HORIZONTAL
PLOT
967
256
1167
376
Distribution of 'Uncertainty'
NIL
NIL
0.0
1.0
0.0
10.0
true
false
"" ""
PENS
"default" 0.05 1 -16777216 true "" "histogram [Uncertainty] of turtles"
SLIDER
999
173
1171
206
mu
mu
0.01
1
0.0
0.01
1
NIL
HORIZONTAL
BUTTON
339
462
427
495
avg. Unretainty
show mean [Uncertainty] of turtles
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
426
462
488
495
Hide links
ask links [set hidden? TRUE]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
488
462
548
495
Show links
ask links [set hidden? FALSE]
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
931
10
1023
43
X-opinion
X-opinion
1
50
1.0
1
1
NIL
HORIZONTAL
SLIDER
931
42
1023
75
Y-opinion
Y-opinion
1
50
2.0
1
1
NIL
HORIZONTAL
BUTTON
547
462
618
495
avg. Opinion
show mean [mean opinion-position] of turtles
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL