-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase-model.nlogo
1857 lines (1568 loc) · 58.1 KB
/
base-model.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
globals [
hypothesis-value ;actual Boolean value of the instantiated hypothesis (world model)
evidence-list ;List to store the actual values of the nodes designated as evidence (world model)
optimal-posterior ;the optimal belief p(HYP| evidence list) about the hypothesis given access to all evidence nodes' actual values
Bayes-net ;causal structure of the world model and of the agents' faithful subjective representations thereof
hypothesis-node ;specification/ selection of the hypothesis nodes
evidence-nodes ;specification/ selection of the evidence nodes
arguments ;list used to count the uses of each argument
number-of-evidence ;number of pieces of evidence
evidence-probabilities-list ;marginal probabilities of each piece of evidence given the actual value of the hypothesis
]
turtles-own [
agent-evidence-list ;stores which parcels/pieces of evidence this agent has seen
agent-belief ;current degree of belief in the hypothesis
current-evidence ;list index of the currently handled piece of evidence
last-heard ;the number of the last-heard piece of evidence
draws ;How many more draws before this agent reaches max-draws?
initial-draws-list ;used to store which pieces of evidence this agent pre-drew last time
heard ;list of indexes of the pieces of evidence this agent has already encountered
update-total ;list of evidence-strengths for this agent
initial-belief ;this agent's prior about HYP from before they drew or heard any evidence
recency-list ;list of evidence this agent has encountered, from least to most recent (no duplicated)
]
extensions [r Nw]
;******************************************************************
;MAIN MODEL FLOW **************************************************
;******************************************************************
to setup
my-clear-all
reset-ticks
carefully [
loadDAG ;all routines to do with loading or modifying DAGs can be found at the bottom of this code file
][
print "EXCEPTION: Please ensure you are providing a valid R-file in the chosen location."
print error-message
stop
]
if reset-world-? [
carefully [
reset-evidence
][
print "EXCEPTION: please ensure that your custom settings for the Bayesian network are sound."
print error-message
stop
]
set reset-agents-initial-evidence-? true ;a new world necessitates new initial draws
]
compute-optimal-posterior
if reset-social-network-? [
clear-turtles
carefully [
if network = "wheel" [create-turtles number-of-agents create-network-wheel]
][
print "EXCEPTION: please ensure that your wheel network contains at least three agents."
stop
]
if network = "complete" [create-turtles number-of-agents create-network-complete]
if network = "small-world" [
carefully [
create-network-small-world
][
print "EXCEPTION: To create a small-world network, please make sure that [k] is less than half of [number-of-agents]."
print error-message
stop
]
]
if network = "null" [create-turtles number-of-agents create-network-null]
set reset-agents-initial-evidence-? true ;a new network structure necessitates freshly initialized agents
]
initializeAgents
setupPlots
end
to go
if check-stoping-conditions [stop] ;Optionally, the simulation stops when all agents know all the evidence or when max-ticks is reached.
if show-me-? [
show (word "--------------------------------------------")
show (word "NEW TICK: " ticks)
show (word "--------------------------------------------")
show (word "Step 1: Inquiry")
show (word "--------------------------------------------")
]
ask turtles [
if shouldInquire? [
collectEvidence "random"
]
]
if show-me-? [
show (word "--------------------------------------------")
show (word "Step 2: COMMUNICATION")
show (word "--------------------------------------------")
]
ask turtles [
if shouldShare? [
if share = "impact" [impactShare]
if share = "random" [randomShare]
if share = "recent" [simpleRecentShare]
]
]
plotArguments
tick
end
;******************************************************************
;INQUIRY AND REASONING SUBROUTINES ********************************
;******************************************************************
to compute-posterior
;This is called whenever an agent receives a new piece of evidence (parcel). The procedure computes the posterior belief in the hypothesis, given ALL currently recorded pieces of evidence.
let b0 agent-belief
let b1 0
;***********************************************************************************************************************************
;the following block creates a string that is later used to query the R instance
let heard-sorted sort heard ;recall that heard just stores indexes, so sort applies
let rQuery (word "cpquery("Bayes-net", event = ("Hypothesis-node" == 'yes'), evidence = (")
let x 0 ;iterates through all eNodes
let y 0 ;keeps track of the additions to the rQuery string
while [x < number-of-evidence] [
if member? x heard-sorted [
set rQuery (word rQuery ""item x evidence-nodes" == '" item x agent-evidence-list"'")
set y y + 1
if y < length heard-sorted [set rQuery (word rQuery " & ")]
]
set x x + 1
]
set rQuery (word rQuery "))")
if show-me-? [print (word "The rQuery for compute-optimal-posterior is " rQuery)]
;***********************************************************************************************************************************
;Here again, we repeat the query to counteract approximation errors.
if approximation = "repeater" [
repeat repeater [
let m r:get rQuery
set b1 (b1 + m)
]
set b1 (b1 / repeater)
]
if approximation = "seed"[
r:eval (word "set.seed("seed") ")
set b1 r:get rQuery
]
if show-me-? [show (word "my posterior: " b1)]
;This computes the "relative" update: it records how the agent's belief has changed through this particular piece of evidence.
let update-r b1 - b0
;set update-relative replace-item last-heard update-relative update-r
compute-singular
if show-me-? [show (word "update-total: " update-total)]
set agent-belief b1
change-colors
end
to compute-singular ;It computes how much the last received piece of evidence would have changed the agent's belief had this been the first piece of evidence they heard (relative to the original prior).
;In this way, the agents record the isolated diagnosticity of the evidence.
;This is called whenever a new posterior is calculated.
if last-heard != "none"
[
;***********************************************************************************************************************************
;the following block creates a string that is later used to query the R instance
let rQuery (word "cpquery("Bayes-net", event = ("Hypothesis-node" == 'yes'), evidence = (")
set rQuery (word rQuery ""item last-heard evidence-nodes" == '" item last-heard evidence-list "' ))")
;***********************************************************************************************************************************
let r 0
if approximation = "repeater"[
repeat repeater [
let v r:get rQuery
set r (r + v)
]
set r (r / repeater)
]
if approximation = "seed"[
r:eval (word "set.seed("seed") ")
set r r:get rQuery
]
if show-me-? [
show (word "singular posterior: " rQuery ": " r)]
set update-total replace-item last-heard update-total (r - initial-belief)
]
end
to change-colors
;changes the agents' colors, given their belief in the hypothesis node.
if agent-belief = initial-belief [set color grey]
if agent-belief > initial-belief [set color green]
if agent-belief < initial-belief [set color red]
end
to collectEvidence [number] ;number is either the index of the piece of evidence to be collected, or the string "random"
let i number
if number = "random" [
;select the index of an hitherto unknown piece of evidence;
let nIndices n-values length agent-evidence-list [ ? -> ifelse-value (item ? agent-evidence-list = "-") [?][false]]
set nIndices filter [x -> x != false] nIndices
set i one-of nIndices
]
;Computation and recording of new evidence
set heard lput i heard
set recency-list lput i recency-list
set last-heard i
if item i agent-evidence-list = "-"[
set agent-evidence-list replace-item i agent-evidence-list item i evidence-list
]
if show-me-? [show (word "I drew item " i "; my list: " agent-evidence-list)]
if show-me-? and share = "recent" [show (word "recency-list: " recency-list)]
compute-posterior
set draws draws - 1
end
to-report shouldInquire? ;Each round, the agents may collect a new piece of evidence, but only given three conditions hold:
ifelse draws > 0 [ ;Condition 1: They must still have "draws" (see interface).
let p random-float 1
ifelse p < curiosity [ ; Condition 2: They must be curious enough
ifelse length heard < number-of-evidence [ ;Condition 3: They must not know all the evidence already
report true
][
if show-me-? [show (word "I already know all pieces of evidence." )]
report false
]
][
if show-me-? [show (word "I am not curious this round.")]
report false
]
][
if show-me-? [show (word "I have no more draws left.")]
report false
]
end
to compute-optimal-posterior
let rQuery (word "cpquery("Bayes-net", event = ("Hypothesis-node" == 'yes'), evidence = (")
let x 0
while [x < number-of-evidence] [
set rQuery (word rQuery "" item x evidence-nodes " == '" item x evidence-list "' ")
set x x + 1
if x < number-of-evidence [set rQuery (word rQuery " & ")]
]
set rQuery (word rQuery "))")
if show-me-? [print (word "The rQuery for compute-optimal-posterior is " rQuery)]
if approximation = "repeater"[
repeat repeater [
let m r:get rQuery
set optimal-posterior (optimal-posterior + m)
]
set optimal-posterior (optimal-posterior / repeater)
]
if approximation = "seed"[
r:eval (word "set.seed("seed") ")
set optimal-posterior r:get rQuery
]
if show-me-? [show (word "The optimal posterior is " optimal-posterior)]
end
;******************************************************************
;COMMUNICATION SUBROUTINES ****************************************
;******************************************************************
to-report shouldShare?
;Three conditions need to obtain for agents to share evidence:
;1. They have already heard of a piece of evidence
;2. They need to be chatty enough
;3. They need to pass the conviction threshold
ifelse length heard > 0 [
let x random-float 1
ifelse x < chattiness [
let lowerBound (initial-belief - initial-belief * conviction-threshold)
let upperBound (initial-belief + (1 - initial-belief) * conviction-threshold)
ifelse (agent-belief < lowerBound OR agent-belief > upperBound)[
report true
][
if show-me-? [show (word "My belief does not pass the threshold.")]
report false
]
][
if show-me-? [show (word "I am not chatty right now.")]
report false
]
][
if show-me-? [show (word "I don't know anything, so I cannot share evidence.")]
report false
]
end
to receiveShare [sharedPiece] ;sharedPiece is the list index the shared piece of evidence
;*****************************************************************************************************
;upon receiving a piece of evidence via communication, agents put it to the top of their simple-memory
if member? sharedPiece recency-list [
set recency-list remove sharedPiece recency-list
]
set recency-list lput sharedPiece recency-list
if show-me-? and share = "recent" [show (word "recency-list: " recency-list)]
;*****************************************************************************************************
;Link-neighbors only accept the piece of evidence if they have not yet seen the parcel
ifelse item sharedPiece [agent-evidence-list] of self != "-" [
if show-me-? [show (word "I've heard " sharedPiece " before, thanks " [who] of myself". My list: " agent-evidence-list)]
][
set agent-evidence-list replace-item sharedPiece agent-evidence-list (item sharedPiece evidence-list)
set heard lput sharedPiece heard
set last-heard sharedPiece
compute-posterior
if plotting-type = "received as novel" [ask myself [countArguments]]
if show-me-? [show (word [who] of myself " told me " sharedPiece ", now I entertain:" agent-evidence-list " and believe " agent-belief"." )]
]
if plotting-type = "sent to" [ask myself [countArguments]]
end
to randomshare
;This is the simplest of the sharing procedures: the agent chooses a random piece of evidence they have heard and shares it with their neighbours.
let sharedPiece one-of heard
if show-me-? [show (word "I want to communicate " sharedPiece)]
set current-evidence sharedPiece
if plotting-type = "uttered" [countArguments]
ask link-neighbors [receiveShare sharedPiece]
end
to impactshare
;Strategic sharing procedures: the agent chooses the piece of evidence that most convinced them of their current position. They measure this by consulting ther "update-total" string.
let sharedPiece "-"
;*************************************************************************************************************************************
;agents select the strongest positive piece of evidence if believing in HYP more than they started out, strongest negative piece of evidence otherwise
ifelse agent-belief > initial-belief [
let n max update-total
set sharedPiece position n update-total
][
let n min update-total
set sharedPiece position n update-total
]
;*************************************************************************************************************************************
if show-me-? [ show (word "I communicate " sharedPiece) ]
set current-evidence sharedPiece
if plotting-type = "uttered" [countArguments]
ask link-neighbors [receiveShare sharedPiece]
end
to simplerecentshare
ifelse length recency-list >= 1 [
let sharedPiece "-"
let n random-float 1
;*************************************************************************************************************************************
;agents have a 90% chance of sharing the last piece of evidence they encountered and–iff they have recently encountered more than once
;piece–a 10% chance of sharing the second to last
ifelse n < 0.9 OR length recency-list = 1 [ ;HERE IS THE PROBABILITY OF PICKING THE MOST RECENT EVIDENCE
set sharedPiece last recency-list
][
let simple-memory-derivative (remove last recency-list recency-list)
set sharedPiece one-of simple-memory-derivative
]
;*************************************************************************************************************************************
if show-me-? [show (word "I want to communicate " sharedPiece)]
set current-evidence sharedPiece
if plotting-type = "uttered" [countArguments]
if show-me-? [show (word "recency-list: " recency-list)]
ask link-neighbors [receiveShare sharedPiece]
][
if show-me-? [show (word "I don't remember anything from last round.")]
]
end
;******************************************************************
;NETWORK CREATION SUBROUTINES *************************************
;******************************************************************
to create-network-small-world
nw:generate-watts-strogatz turtles links number-of-agents k rewiring-probability
layout-circle sort turtles 10
end
to create-network-complete
ask turtles [ create-links-with other turtles ]
layout-circle sort turtles 10
end
to create-network-null
layout-circle sort turtles 10
end
to create-network-cycle
[turtle-list]
let previous-turtle 0
foreach turtle-list [ [cur-turtle] ->
ask cur-turtle [
ifelse previous-turtle != 0 [
create-link-with previous-turtle
set previous-turtle self
][
create-link-with last turtle-list
set previous-turtle self
]
]
]
layout-circle sort turtles 10
end
to create-network-wheel
let turtle-list sort turtles
create-network-cycle but-first turtle-list
ask first turtle-list [
setxy 0 0
create-links-with other turtles
]
end
;******************************************************************
;MISCELLANEOUS SUBROUTINES ****************************************
;******************************************************************
to initializeAgents
ask turtles [
;******************************************************************************************************
;This is the agent's initial belief in the hypothesis, determined by the CPT.
let rQuery (word "cpquery("Bayes-net", event = ("Hypothesis-node" == 'yes'), evidence = TRUE)")
let r 0
if approximation = "repeater"[
repeat repeater [let v r:get rQuery
set r (r + v)]
set r (r / repeater)]
if approximation = "seed"[
r:eval (word "set.seed("seed") ")
let v r:get rQuery
set r v ]
set agent-belief r
set initial-belief r
;******************************************************************************************************
set agent-evidence-list []
set heard [] ;Stores which pieces of evidence the agent has heard, in order of reception.
set recency-list []
set last-heard "none" ;Stores the piece of evidence the agent has last heard.
set draws maximum-draws ;Agents may only "draw" from the evidence a limited amount of times. If maximum-draws = number-of-evidence, then full information is assured
set update-total [] ;Stores the size of the "total" update: P(HYP|E_{i+1}) - P(HYP); where E_{i+1} is what the agent has just learned.
repeat number-of-evidence [
set agent-evidence-list lput "-" agent-evidence-list
set update-total lput "-" update-total
]
;******************************************************************************************************
;Pre-draws
;[UI toggle] Either each agent performs random initial draws, or they re-draw their previous initial draws
ifelse reset-agents-initial-evidence-? [
repeat initial-draws [collectEvidence "random"]
set initial-draws-list agent-evidence-list
][
set agent-evidence-list initial-draws-list
let x 0
while [x < number-of-evidence] [
if item x agent-evidence-list != "-" [collectEvidence x]
set x x + 1
]
]
;******************************************************************************************************
change-colors
if show-me-? [show (word "my list " agent-evidence-list)]
]
end
to reset-evidence
let c random-float 1
ifelse c < hypothesis-probability [set hypothesis-value "yes"][set hypothesis-value "no"]
set evidence-probabilities-list []
repeat number-of-evidence [
set evidence-probabilities-list lput "-" evidence-probabilities-list
]
set evidence-list []
;*********************************************************************************************************************************************
;For each piece of evidence, the rquery (using either seed or repeater) determines the conditional probability given the actual value of HYP
;Then, we use that probability to determine the actual value of each piece of evidence
let x 0
while [x < number-of-evidence] [
let r 0
if approximation = "repeater" [
repeat repeater[
set r r + (r:get (word "cpquery(" Bayes-net ", event = ("item x evidence-nodes" == 'yes'), evidence = (" Hypothesis-node " == '" hypothesis-value "'))"))
]
set r r / repeater
]
if approximation = "seed" [
r:eval (word "set.seed("seed") ")
set r (r:get (word "cpquery(" Bayes-net ", event = ("item x evidence-nodes" == 'yes'), evidence = (" Hypothesis-node " == '" hypothesis-value "'))"))
]
let n random-float 1
ifelse (n < r) [set evidence-list lput "yes" evidence-list][set evidence-list lput "no" evidence-list ]
set evidence-probabilities-list (replace-item x evidence-probabilities-list (precision r 2))
set x x + 1
]
;*********************************************************************************************************************************************
if show-me-?[
show (word "Probabilities that evidence nodes are true, given truth/falsity of hypothesis: " evidence-probabilities-list)
show (word "Evidence list " evidence-list)
]
set arguments [] ;resets arguments counter
repeat number-of-evidence [
set arguments lput 0 arguments
]
if maximum-draws > number-of-evidence [
set maximum-draws number-of-evidence
print "Note: automatically adjusted [maximum-draws]."
]
if initial-draws > maximum-draws [
set initial-draws maximum-draws
print "Note: automatically adjusted [initial-draws]."
]
end
to my-clear-all
show (word "------------------------")
show "**NEW RUN**"
show (word "------------------------")
; manually clears the globals we want to reset, leaves retain-me alone.
clear-ticks
set optimal-posterior 0
clear-patches
clear-drawing
clear-all-plots
clear-output
end
to countArguments
if current-evidence != "null" [set arguments replace-item current-evidence arguments (item current-evidence arguments + 1)] ;counts current evidence as an argument used
end
to setupPlots
let x 0
while [x < number-of-evidence] [
create-temporary-plot-pen item x evidence-nodes
ifelse x < 13 [set-plot-pen-color 5 + (x * 10)][set-plot-pen-color 0]
set x x + 1
]
update-plots
end
to plotArguments
let x 0
while [x < number-of-evidence] [
set-current-plot-pen item x evidence-nodes
plot item x arguments
set x x + 1
]
set arguments [] ;resets arguments counter
repeat number-of-evidence [
set arguments lput 0 arguments
]
end
to-report check-stoping-conditions
let stop? false
if stop-at-full-information-? [
if not any? turtles with [length heard < number-of-evidence ][
show "*** State of full information reached ***"
set stop? true
]
]
if stop-at-max-ticks-? [
if ticks >= max-ticks [
show "*** Upper tick-limit reached ***"
set stop? true
]
]
report stop?
end
;******************************************************************
;DAG-RELATED SUBROUTINES ******************************************
;******************************************************************
to loadDAG
r:eval "library (bnlearn)" ;the R library that lets us use Bayes' nets
if causal-structure = "big net"[LoadBigNet]
if causal-structure = "small net"[LoadSmallNet]
if causal-structure = "asia" [loadAsiaNet]
if causal-structure = "alarm" [loadAlarmNet]
if causal-structure = "Vole" [loadVoleNET]
if causal-structure = "WetGrass" [loadWetGrassNet]
if causal-structure = "SallyClark" [loadSallyClarkNet]
if causal-structure = "custom" [
;######Upload your custom net here. Either use a whole file via using the UI and this line: <r:eval (word "source('" pathToCustomDAG "')")>
;OR copy paste the R script's lines here. Needed: 1. a dag, b. a probability distribution/data fitted onto the dag ("bn.fit")
;Make sure each line is embedded like so: r:eval "R-SCRIPT-LINE". Make sure the syntax is correct (e.g., R is sensitive to small differences such as punctuation).
;#####################COPY-PASTE BEGIN
r:eval (word "source('" path-to-custom-DAG "')")
;#####################COPY-PASTE END
useCustomEvidenceAndHypothesisNodes
]
set Bayes-net "bn"
set number-of-evidence length evidence-nodes
end
to useCustomEvidenceAndHypothesisNodes
set evidence-nodes []
let tempString evidence-nodes-custom-DAG
while [position "\n" tempString != FALSE] [
let nextNode (substring tempString 0 position "\n" tempString)
set evidence-nodes lput nextNode evidence-nodes
repeat (position "\n" tempString + 1) [set tempString (but-first tempString)]
]
set evidence-nodes lput tempString evidence-nodes
set hypothesis-node hypothesis-node-custom-DAG
end
to loadAsiaNet
r:eval "data(asia)"
r:eval "dag <- model2network('[A][S][T|A][L|S][B|S][D|B:E][E|T:L][X|E]')"
r:eval "bn <- bn.fit(dag, asia)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "L"
set evidence-nodes ["A" "S" "T" "B" "D" "X"]
][useCustomEvidenceAndHypothesisNodes]
end
to loadVoleNET
r:eval "dag1 <- model2network('[M1][M2][M3|M1:M2][Vole_present][H0|M3:Vole_present][H1|H0][H5][E6|H5][A3|H5:H1][E7|H5:A3:H1][A1][E1|Vole_present:A1][A2|H0][E2|A2:Vole_present][H3][E5|H3][H2|H0][H4|H3][E4|H4][E3|H2:H4][Auxilliary|H2:H4][Constraint|Auxilliary]')"
r:eval "cptM1 <- array(c(0.5,0.5), dim = 2, dimnames = list(M1 = c('no', 'yes')))"
r:eval "cptM2 <- array(c(0.5,0.5), dim = 2, dimnames = list(M2 = c('no', 'yes')))"
r:eval "cptVole_present <- array(c(0.5,0.5), dim = 2, dimnames = list(Vole_present = c('no', 'yes')))"
r:eval "cptA1 <- array(c(0.5,0.5), dim = 2, dimnames = list(A1 = c('no', 'yes')))"
r:eval "cptH3 <- array(c(0.5,0.5), dim = 2, dimnames = list(H3 = c('no', 'yes')))"
r:eval "cptH5 <- array(c(0.5,0.5), dim = 2, dimnames = list(H5 = c('no', 'yes')))"
r:eval "cptH1 <- array(c(1,0,0.5,0.5), dim = c(2,2), dimnames = list(H1 = c('no', 'yes'), H0 =c('no', 'yes')))"
r:eval "cptH2 <- array(c(0.9,0.1,0.3,0.7), dim = c(2,2), dimnames = list(H2 = c('no', 'yes'), H0 =c('no', 'yes')))"
r:eval "cptH2 <- array(c(0.9,0.1,0.3,0.7), dim = c(2,2), dimnames = list(H2 = c('no', 'yes'), H0 =c('no', 'yes')))"
r:eval "cptE5 <- array(c(0.99,0.01,0.1,0.9), dim = c(2,2), dimnames = list(E5 = c('no', 'yes'), H3 =c('no', 'yes')))"
r:eval "cptH4 <- array(c(1.0,0.0,0.1,0.9), dim = c(2,2), dimnames = list(H4 = c('no', 'yes'), H3 =c('no', 'yes')))"
r:eval "cptE4 <- array(c(0.99,0.01,0,1), dim = c(2,2), dimnames = list(E4 = c('no', 'yes'), H4 =c('no', 'yes')))"
r:eval "cptConstraint <- array(c(0.283,0.717,0.45,0.55,1,0), dim = c(2,3), dimnames = list(Constraint = c('no', 'yes'), Auxilliary = c('H2','H4','impossible')))"
r:eval "cptA2 <- array(c(0,1,0.9,0.1), dim = c(2,2), dimnames = list(A2 = c('no', 'yes'), H0 =c('no', 'yes')))"
r:eval "cptE6 <- array(c(0.99,0.01,0.1,0.9), dim = c(2,2), dimnames = list(E6 = c('no', 'yes'), H5 =c('no', 'yes')))"
r:eval "cptM3 <- array(c(0.9,0.1,0.3,0.7,0.4,0.6,0.2,0.8), dim = c(2,2,2), dimnames = list(M3 = c('no','yes'),M2 = c('no', 'yes'),M1 = c('no', 'yes')))"
r:eval "cptH0 <- array(c(1.0,0.0,1.0,0.0,0.5,0.5,0.3,0.7), dim = c(2,2,2), dimnames = list(H0 = c('no','yes'),M3 = c('no', 'yes'),Vole_present = c('no', 'yes')))"
r:eval "cptE3 <- array(c(1,0,0.99,0.01,0,1,0,1), dim = c(2,2,2), dimnames = list(E3 = c('no','yes'), H4 = c('no', 'yes'), H2= c('no', 'yes')))"
r:eval "cptAuxilliary <-array(c(0,0,1,0,1,0,1,0,0,0,0,1), dim = c(3,2,2), dimnames = list(Auxilliary = c('H2','H4','impossible'), H4 = c('no', 'yes'), H2= c('no', 'yes')))"
r:eval "cptE1 <- array(c(0.5,0.5,0.5,0.5,1,0,0,1), dim = c(2,2,2), dimnames = list(E1 = c('no','yes'), Vole_present= c('no', 'yes'), A1= c('no', 'yes')))"
r:eval "cptE2 <- array(c(0,1,0.4,0.6,0,1,1,0), dim = c(2,2,2), dimnames = list(E2 = c('no','yes'), Vole_present= c('no', 'yes'), A2= c('no', 'yes')))"
r:eval "cptA3 <- array(c(0.9,0.1,0.001,0.999,1,0,0.3,0.7), dim = c(2,2,2), dimnames = list(A3 = c('no','yes'), H1= c('no', 'yes'), H5= c('no', 'yes')))"
r:eval "cptE7 <- array(c(1,0,1,0,0.2,0.8,1,0,1,0,0.4,0.6,0.01,0.99,0,1),dim = c(2,2,2,2), dimnames = list(E7 = c('no','yes'), A3= c('no', 'yes'), H5= c('no', 'yes'),H1= c('no', 'yes')))"
r:eval "cpt1 <- list( H0 = cptH0, M1 = cptM1, M2 = cptM2, M3 = cptM3, Vole_present = cptVole_present, E1 = cptE1, A1 = cptA1, E2 = cptE2, A2 = cptA2, H1 = cptH1, H5 = cptH5, E6 = cptE6, E7 = cptE7, A3 = cptA3, H2 = cptH2, H3 = cptH3, H4 = cptH4, E3 = cptE3, E4 = cptE4, E5 = cptE5, Auxilliary = cptAuxilliary, Constraint = cptConstraint)"
r:eval "bn <- custom.fit(dag1, cpt1)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "H0"
set evidence-nodes ["E1" "E2" "E3" "E4" "E5" "E6" "E7"]
][
useCustomEvidenceAndHypothesisNodes
]
end
to loadAlarmNet
r:eval "data(alarm)"
r:eval "dag <- model2network('[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF][LVF][STKV|HYP:LVF][ERLO][HRBP|ERLO:HR][HREK|ERCA:HR][ERCA][HRSA|ERCA:HR][ANES][APL][TPR|APL][ECO2|ACO2:VLNG][KINK][MINV|INT:VLNG][FIO2][PVS|FIO2:VALV][SAO2|PVS:SHNT][PAP|PMB][PMB][SHNT|INT:PMB][INT][PRSS|INT:KINK:VTUB][DISC][MVS][VMCH|MVS][VTUB|DISC:VMCH][VLNG|INT:KINK:VTUB][VALV|INT:VLNG][ACO2|VALV][CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]')"
r:eval "bn <- bn.fit(dag, alarm)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "CVP"
set evidence-nodes ["HIST" "CO"]
][
useCustomEvidenceAndHypothesisNodes
]
end
to loadWetGrassNet
r:eval "library (bnlearn)"
r:eval "dag1 <- model2network('[Rain][Sprinkler][Watson|Rain][Holmes|Rain:Sprinkler]')"
r:eval "cptRain <- array(c(0.2,0.8), dim = 2, dimnames = list(Rain = c('yes', 'no')))"
r:eval "cptSprinkler <- array(c(0.1,0.9), dim = 2, dimnames = list(Sprinkler = c('yes', 'no')))"
r:eval "cptWatson <- array(c(1.0,0.0,0.2,0.8), dim = c(2,2), dimnames = list(Watson = c('yes', 'no'),Rain = c('yes', 'no')))"
r:eval "cptHolmes <- array(c(1.0, 0.0, 0.9, 0.1,1.0,0.0,0.0,1.0), dim = c(2,2,2), dimnames = list(Holmes = c('yes', 'no'), Rain = c('yes', 'no'), Sprinkler = c('yes', 'no')) )"
r:eval "cpt1 <- list(Rain = cptRain,Sprinkler = cptSprinkler, Watson = cptWatson, Holmes =cptHolmes)"
r:eval "bn <- custom.fit(dag1, cpt1)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "Sprinkler"
set evidence-nodes ["Holmes" "Watson"]
][
useCustomEvidenceAndHypothesisNodes
]
end
to loadSallyClarkNet
;NOTE: I changed SIDS to NO and MURDER to YES.
r:eval "dag1 <- model2network('[ChildACause][ChildBCause|ChildACause][Guilty|Findings][Findings|ChildACause:ChildBCause][ChildABruising|ChildACause][ChildADisease|ChildACause][ChildBBruising|ChildBCause][ChildBDisease|ChildBCause]')"
r:eval "cptChildACause <- array(c(0.921659,0.07834101), dim = 2, dimnames = list(ChildACause = c('no', 'yes')))"
r:eval "cptChildBCause <- array(c(0.9993604, 6.3959067E-4,1.4622862E-4, 0.9998538), dim = c(2, 2), dimnames = list(ChildBCause = c('no', 'yes'),ChildACause = c('no', 'yes')))"
r:eval "cptChildABruising <- array(c(0.99,0.01,0.95,0.05), dim = c(2, 2), dimnames = list(ChildABruising = c('no', 'yes'), ChildACause = c('no', 'yes')))"
r:eval "cptChildBBruising <- array(c(0.99,0.01,0.95,0.05), dim = c(2, 2), dimnames = list(ChildBBruising = c('no', 'yes'), ChildBCause = c('no', 'yes')))"
r:eval "cptChildADisease <- array(c(0.95,0.05,0.999,0.001), dim = c(2, 2), dimnames = list(ChildADisease = c('no', 'yes'),ChildACause = c('no', 'yes')))"
r:eval "cptChildBDisease <- array(c(0.95,0.05,0.999,0.001), dim = c(2, 2), dimnames = list(ChildBDisease = c('no', 'yes'),ChildBCause = c('no', 'yes')))"
r:eval "cptGuilty <- array(c(0.0,1.0,0.0,1.0,1.0,0.0), dim = c(2, 3), dimnames = list(Guilty = c('no', 'yes'), Findings = c('Both Murdered', 'Either Murdered', 'Neither Murdered')))"
r:eval "cptFindings <- array(c(0.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,0.0), dim = c(3,2,2), dimnames = list(Findings = c('Both Murdered', 'Either Murdered', 'Neither Murdered'), ChildBCause = c('no', 'yes'), ChildACause = c('no', 'yes')))"
r:eval "cpt1 <- list(ChildABruising = cptChildABruising, ChildADisease = cptChildADisease, ChildBBruising = cptChildBBruising, ChildBDisease = cptChildBDisease, ChildACause = cptChildACause, ChildBCause = cptChildBCause, Findings = cptFindings, Guilty = cptGuilty)"
r:eval "bn <- custom.fit(dag1, cpt1)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "Guilty"
set evidence-nodes ["ChildADisease" "ChildBDisease" "ChildABruising" "ChildBBruising"]
][
useCustomEvidenceAndHypothesisNodes
]
end
to loadBigNet
r:eval "dag1 <- model2network('[A][B|A][C|A][D|A][one|B][two|B][three|B][four|C][five|C][six|C][seven|D][eight|D][nine|D]')"
r:eval "cptA <- array(c(0.5,0.5), dim = 2, dimnames = list(A = c('yes', 'no')))"
r:eval "cptB <- array(c(0.9, 0.1, 0.1, 0.9), dim = c(2, 2), dimnames = list(B = c('yes', 'no'), A = c('yes', 'no')))"
r:eval "cptC <- array(c(0.5, 0.5, 0.5, 0.5), dim = c(2, 2), dimnames = list(C = c('yes', 'no'), A = c('yes', 'no')))"
r:eval "cptD <- array(c(0.1, 0.9, 0.9, 0.1), dim = c(2, 2), dimnames = list(D = c('yes', 'no'), A = c('yes', 'no')))"
r:eval "cptone <- array(c(0.9, 0.1, 0.1, 0.9), dim = c(2, 2), dimnames = list(one = c('yes', 'no'), B = c('yes', 'no')))"
r:eval "cpttwo <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(two = c('yes', 'no'), B = c('yes', 'no')))"
r:eval "cptthree <- array(c(0.7, 0.3, 0.3, 0.7), dim = c(2, 2), dimnames = list(three = c('yes', 'no'), B = c('yes', 'no')))"
r:eval "cptfour <- array(c(0.9, 0.1, 0.1, 0.9), dim = c(2, 2), dimnames = list(four = c('yes', 'no'), C = c('yes', 'no')))"
r:eval "cptfive <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(five = c('yes', 'no'), C = c('yes', 'no')))"
r:eval "cptsix <- array(c(0.7, 0.3, 0.3, 0.7), dim = c(2, 2), dimnames = list(six = c('yes', 'no'), C = c('yes', 'no')))"
r:eval "cptseven <- array(c(0.9, 0.1, 0.1, 0.9), dim = c(2, 2), dimnames = list(seven = c('yes', 'no'), D = c('yes', 'no')))"
r:eval "cpteight <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(eight = c('yes', 'no'), D = c('yes', 'no')))"
r:eval "cptnine <- array(c(0.7, 0.3, 0.3, 0.7), dim = c(2, 2), dimnames = list(nine = c('yes', 'no'), D = c('yes', 'no')))"
r:eval "cpt1 <- list(A = cptA, B = cptB, C = cptC, D = cptD, one = cptone, two = cpttwo, three = cptthree, four = cptfour, five = cptfive, six = cptsix, seven = cptseven, eight = cpteight, nine = cptnine)"
r:eval "bn <- custom.fit(dag1, cpt1)"
ifelse custom-evidence-and-hypothesis-? = false [
set hypothesis-node "A"
set evidence-nodes [ "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"]
][
useCustomEvidenceAndHypothesisNodes
]
end
to loadSmallNet
r:eval "dag <- model2network('[V][CS|V][VT|V][M|CS][I|CS][WHO|VT][RS|VT]')"
r:eval "cptV <- array(c(0.5,0.5), dim = 2, dimnames = list(A = c('yes', 'no')))"
r:eval "cptCS <- array(c(0.9, 0.1, 0.1, 0.9), dim = c(2, 2), dimnames = list(CS = c('yes', 'no'), V = c('yes', 'no')))"
r:eval "cptVT <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(VT = c('yes', 'no'), V = c('yes', 'no')))"
r:eval "cptI <- array(c(0.7, 0.3, 0.3, 0.7), dim = c(2, 2), dimnames = list(I = c('yes', 'no'), CS = c('yes', 'no')))"
r:eval "cptM <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(M = c('yes', 'no'), CS = c('yes', 'no')))"
r:eval "cptWHO <- array(c(0.8, 0.2, 0.2, 0.8), dim = c(2, 2), dimnames = list(WHO = c('yes', 'no'), VT = c('yes', 'no')))"
r:eval "cptRS <- array(c(0.2, 0.8, 0.8, 0.2), dim = c(2, 2), dimnames = list(RS = c('yes', 'no'), VT = c('yes', 'no')))"
r:eval "cpt <- list(V = cptV, VT = cptVT, CS = cptCS, I = cptI, M = cptM, WHO = cptWHO, RS = cptRS)"
r:eval "bn <- custom.fit(dag, cpt)"
ifelse custom-evidence-and-hypothesis-? = false [
set evidence-nodes ["I" "M" "WHO" "RS"]
set hypothesis-node "V"
][
useCustomEvidenceAndHypothesisNodes
]
end
@#$#@#$#@
GRAPHICS-WINDOW
9
10
367
369
-1
-1
10.61
1
10
1
1
1
0
0
0
1
-16
16
-16
16
0
0
1
ticks
30.0
SLIDER
885
84
1040
117
number-of-agents
number-of-agents
1
1000
10.0
1
1
NIL
HORIZONTAL
BUTTON
380
171
457
205
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SWITCH
607
126
760
159
show-me-?
show-me-?
1
1
-1000
BUTTON
531
172
594
205
NIL
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
462
172
525
205
NIL
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
CHOOSER
885
37
1040
82
network
network
"wheel" "complete" "small-world" "null"
0
PLOT
848
377
1353
630
Histogram of beliefs
Degree of belief
Number of people
0.0
1.01
0.0
10.0
true
false
"" ""
PENS
"default" 0.01 1 -16777216 true "" "histogram [agent-belief] of turtles"
SLIDER
1056