-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASIMOV-FAM.nlogo
More file actions
4014 lines (3624 loc) · 142 KB
/
ASIMOV-FAM.nlogo
File metadata and controls
4014 lines (3624 loc) · 142 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
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
;;=====================================================================================================================================================================================
;;=====================================================================================================================================================================================
;;;
;;; A S I M O V
;;;
;;=====================================================================================================================================================================================
;;=====================================================================================================================================================================================
;;==========================================================================================================================================
;; V A R I A B L E I N I T I A L I Z A T I O N
;;==========================================================================================================================================
extensions [matrix csv]
;;Create animats and assign qualities
globals [data Addiction_Cycle_Phase switch_time stop_time prey-types-list prey-color-list odor-color-list odor-types-list variables-list variable-labels num-odor-types prey-odor-matrix diffc evapc
cluster-x-coordinates cluster-y-coordinates plot-timer wallcount dcount Presenting Present-y-coord pstep pticks Static-Environment Spatial-Mapping-Enabled var-val-list]
breed [arms arm]
breed [Cslugs Cslug]
breed [nociceptors nociceptor] ;ie the pain receptors
breed [prey prey-item] ;includes hermi, flab, fauxflab, drug
breed [pebbles pebble]
breed [color-effects color-effect]
breed [memory-pointers memory-pointer]
breed [wallpts wallpt]
;breed [flabs flab]
;breed [hermis hermi]
;breed [fauxflabs fauxflab]
;breed [drugs drug]
turtles-own [non-edible fixed]
prey-own [prey-type]
Cslugs-own [ Somatic_Map App_State App_State_Switch ExpReward_pos ExpReward_neg iSum Incentive Nutrition Satiation speed forward-movement turn-angle
sns_betaine sns_betaine_left sns_betaine_right sns-pain-left sns-pain-right sns-pain-caud sns-pain spontaneous-pain pain pain-switch
odors_left odors_right sns_odors_left sns_odors_right sns_odors Somatic_Map_Factors Somatic_Map_Sigmoids
num_senses sense_names senses imag_senses imag_senses_accumulators senses_left imag_senses_left senses_right imag_senses_right sense_colors
hermcount flabcount fauxflabcount drugcount drug_reward
R R_hermi R_flab R_drug RewardExperience deg_sensitization IN M M0 W1 W2 W3 dW3 W4 W5
inputs inputs_left inputs_right num_inputs traces memory_traces input_labels input_colors
i j FAMatrix_dim init_list FAMatrix_diff-A FAMatrix_cross-A FAMatrix_strengths FAMatrix_timelags FAMatrix_rewards FAMatrix_vdir FAMatrix_vdist FAMatrix_vec FAMatrix_vtemp FAMatrix_vseq reward_pos reward_neg
Incentives Imag_Incentives Realsense_Rewards Direct_Rewards DirectReward_i Imag_Rewards ImagReward_i
slitherometer head-direction vsum stepsize trace-track vseq_temp vseq_count vseq_sum detour-vector correction-vector HDOut HDWeight HD0 HDtau
HLB HLS HLD sensor_angle lsns_vec rsns_vec left_dist right_dist dist; half-length-body half-length-sensor half-length-diagonal
turn-angle-accumulation trace-changes
]
nociceptors-own [parent painval id hit]
arms-own [parent phase id]
patches-own [odor-list odor_betaine odor_hermi odor_flab odor_drug odor5 odor6 odor7 odor8 odor9 odor10 marine-colors wall]
color-effects-own [large-color-effect]
memory-pointers-own [id parent targ dist dir]
links-own [mdist mdir mstr mord mrew text]
wallpts-own [wall-id]
;-----------------------------------------------------------------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------------------------------------------------------------
;;==========================================================================================================================================
;; S E T U P P R O C E D U R E S
;;==========================================================================================================================================
to Setup
clear-all
reset-ticks
;Set up globals:
;-----------------------
; sets up first phase of Addiction Cycle, to be used when Addiction_Cycle is turned ON
set Pause-At-Tick: 0
set Force-Turn 0
set Addiction_Cycle_Phase "Drug-Free"
set Presenting "" ; for printing out presentation sequence
set File-Name "FAMdata.csv"
set Present-y-coord -6
;set switch_time 15000
set switch_time 5000
set stop_time 60000; was 150000
set prey-types-list ["hermi" "flab" "fauxflab" "drug" "p5" "p6" "p7" "p8" "p9"]
set prey-color-list [85 135 125 45 35 65 75 105 115]
set odor-types-list ["odor_betaine" "odor_hermi" "odor_flab" "odor_drug" "odor5" "odor6" "odor7" "odor8" "odor9" "odor10"]
set odor-color-list [9.9 85 135 45 35 65 75 105 115 125]
;set variables-list ["Odor 2" "Odor 1" "Odor 3" "Odor 5" "Odor 6" "Odor 7" "Odor 8" "Odor 9" "Pain" "Reward" "R-"]
;sns_odors_left
;["Odor 2 Left" "Odor 1 Left" "Odor 3 Left" "Odor 5 Left" "Odor 6 Left" "Odor 7 Left" "Odor 8 Left" "Odor 9" "Pain" "Reward" "R-"]
set num-odor-types length odor-types-list
set prey-odor-matrix [ [0.0 0.5 0 0 0 0 0 0 0 0] ;hermi odor for hermi prey; previously also used betaine odor
[0.0 0 0.5 0 0 0 0 0 0 0] ;flab odor for flab prey; previously also used betaine odor
[0.0 0 0.5 0 0 0 0 0 0 0] ;flab odor for fauxflab prey; previously also used betaine odor
[0 0 0 0.5 0 0 0 0 0 0] ;drug odor for drug items
[0 0 0 0 0.5 0 0 0 0 0] ;odor5 for prey5
[0 0 0 0 0 0.5 0 0 0 0] ;odor6 for prey6
[0 0 0 0 0 0 0.5 0 0 0] ;odor7 for prey7
[0 0 0 0 0 0 0 0.5 0 0] ;odor8 for prey8
[0 0 0 0 0 0 0 0 0.5 0] ;odor9 for prey9
]
setup-ASIMOV-Agent
setup-Environment
ask Cslugs [update-arms set turn-angle-accumulation heading pen-down] ;Track Cslug's path
end
;---------------------------------------------------------------------------------
; Spawns the ASIMOV agent, sets up its variables
;---------------------------------------------------------------------------------
to setup-ASIMOV-Agent
create-Cslugs 1 [
;sets shape, size, color, and position of octo/slug
ifelse member? "Sequence Learning" experiment_mode or member? "Prey Populations" experiment_mode[ ;slug shape for sequence learning and prey populations, octo for all others
set shape "cslug2" ; "slug"
set color 115
set size 16
set heading 0
][
set shape "octobody2" ; "octo"
set color 35
set size 13
set heading 30
;Give octo moving arms for decorative effect
repeat 8[
hatch-arms 1 [
set size 19
set color 35
set heading 0
set parent myself
let idnum ((count arms) mod 8)
let labels ["armo1l" "armo2l" "armo3l" "armo4l" "armo1r" "armo2r" "armo3r" "armo4r"]
set id (item idnum labels)
set shape id
]
]
]
set HLB 3.98
set HLS 3.34
set HLD sqrt((HLB ^ 2) + (HLS ^ 2))
set sensor_angle atan HLS HLB
;creates 7 sensors for pain detection
repeat 7 [
hatch-nociceptors 1 [
set hidden? true
set shape "dot"
set size 3
set parent myself
let idnum ((count nociceptors) mod 7)
let labels ["snsrOL" "snsrOR" "snsrUL" "snsrUR" "snsrBL" "snsrBR" "snsrBM"]
set id (item idnum labels)
if id = "snsrOL" or id = "snsrOR"[set hidden? false]
]
]
;updates pain sensor position
update-nociceptor-position
;sets baseline spontaneous pain activity
set spontaneous-pain 2
;sets initial values for feeding network variables (nutrition, incentive salience, somatic map, and satiation)
set Nutrition 0.01
set Incentive 0
set Somatic_Map 0
set Satiation 0.01
;sets initial habituation/sensitization parameters for Homeostatic Reward Circuit (HRC):
set W1 1
set W2 0.2
set W3 1
set W4 0.1
set W5 0.1
set M0 10 ;Baseline activity for M
;set R_drug 0.5
set drug_reward 10
set HD0 0.5
set HDWeight 1
set HDtau 100
;sets up the Feature Association Matrix
set inputs [0 0 0 0 0 0 0 0 0 0 0] ;first 9 for sensory inputs, last 2 for reward (+,-) inputs
set inputs_left inputs
set inputs_right inputs
set traces [0 0 0 0 0 0 0 0 0 0 0]
set trace-changes traces
;set inputs (list sns_hermi sns_flab sns_drug sns-pain reward_pos reward_neg)
set num_inputs length inputs
set FAMatrix_dim length inputs
set init_list []
repeat FAMatrix_dim [set init_list lput (n-values FAMatrix_dim [0]) init_list]
let init_vseq_list []
let init_vseq_row []
repeat FAMatrix_dim [set init_vseq_row lput ([0 0]) init_vseq_row]
repeat FAMatrix_dim [set init_vseq_list lput (init_vseq_row) init_vseq_list]
set FAMatrix_cross-A matrix:from-row-list init_list
set FAMatrix_diff-A matrix:from-row-list init_list
set FAMatrix_strengths matrix:from-row-list init_list
set FAMatrix_timelags matrix:from-row-list init_list
set FAMatrix_rewards matrix:from-row-list init_list
set FAMatrix_vdir matrix:from-row-list init_list
set FAMatrix_vdist matrix:from-row-list init_list
set FAMatrix_vseq init_vseq_list
set FAMatrix_vec init_vseq_list
set FAMatrix_vtemp init_vseq_list
set stepsize 0
set vsum (list heading stepsize)
set Somatic_Map_Sigmoids n-values ((num-odor-types - 1) + 1) [0]
set Incentives n-values ((num-odor-types - 1) + 1) [0]
set Imag_Incentives n-values ((num-odor-types - 1) + 1) [0]
set Direct_Rewards n-values ((num-odor-types - 1) + 1) [0]
set Imag_Rewards n-values ((num-odor-types - 1) + 1) [0]
set Realsense_Rewards n-values ((num-odor-types - 1) + 1) [0]
set imag_senses n-values ((num-odor-types - 1) + 1) [0]
set imag_senses_accumulators n-values ((num-odor-types - 1) + 1) [0]
set imag_senses_left n-values ((num-odor-types - 1) + 1) [0]
set imag_senses_right n-values ((num-odor-types - 1) + 1) [0]
set sns_odors_left n-values num-odor-types [0]
set sns_odors_right n-values num-odor-types [0]
set sns_odors n-values num-odor-types [0]
;set input_labels ["Hl" "Hr" "Fl" "Fr" "Dl" "Dr" "Pl" "Pr" "R+" "R-"]
set input_labels ["H" "F" "D" "O5" "O6" "O7" "O8" "O9" "P" "R+" "R-"]
;set input_labels ["H" "F" "D" "P" "R+" "R-"]
;set input_colors [83 85 133 135 43 45 13 15 25 22]
set input_colors [85 135 45 35 65 75 105 115 15 25 22]
;set input_colors [85 135 45 15 25 22]
setup-Plot-Legends ;set up legends for plotting
]
end
to setup-Plot-Legends
set-current-plot "Legend"
let k 0
while [k < num_inputs][
create-temporary-plot-pen (item k reverse input_labels)
set-current-plot-pen (item k reverse input_labels)
set-plot-pen-color (item k reverse input_colors)
set k k + 1
]
set sense_names ["betaine" "hermi" "flab" "drug" "odor5" "odor6" "odor7" "odor8" "odor9" "odor10" "pain"]
set sense_colors (lput 15 odor-color-list)
set num_senses num-odor-types + 1
set senses n-values num_senses [0]
set-current-plot "Senses-Legend"
set i 0
while [i < num_senses][
create-temporary-plot-pen (item i sense_names)
set-current-plot-pen (item i sense_names)
set-plot-pen-color (item i sense_colors)
set i i + 1
]
end
;---------------------------------------------------------------------------------
; Set Up Environment, Spawn Prey and Drug
;---------------------------------------------------------------------------------
to setup-Environment
set Static-Environment true
set Presentation-Mode false
set immobilize false
set fix-Matrix false
set fix-Satiation 0.10
set evapc 0.90; evaporation constant for prey odors
set diffc 0.3 ; diffusion constant for prey odors
ifelse member? "Sequence Learning" experiment_mode or member? "Population" experiment_mode[; changes prey locations, environment wrapping, and walls depending on experiment mode
__change-topology true true
set Spatial-Mapping-Enabled false
set evapc 0.95 ;greater diffusion and evaporation constants, for larger area of effect of prey odors (larger overlap for sequences)
set diffc 0.5
;set fix-Satiation 0.15
if experiment_mode = "Prey Populations" [ ;creates randomly moving flabellina, hermissenda and drug prey
set Static-Environment false
create-prey flab-populate [set prey-type "flab" setxy random-xcor random-ycor]
create-prey hermi-populate [set prey-type "hermi" setxy random-xcor random-ycor]
create-prey drug-populate [set prey-type "drug" setxy random-xcor random-ycor]
if Clustering = true [
set cluster-x-coordinates [ 0 0 0 0 ]
set cluster-y-coordinates [ 0 0 0 0 ]
let indices range(length cluster-x-coordinates)
(foreach indices [[k] ->
ifelse k = 0 [set cluster-x-coordinates replace-item k cluster-x-coordinates (random-pxcor) set cluster-y-coordinates replace-item k cluster-y-coordinates (random-pycor)][
let avg-x mean (sublist cluster-x-coordinates 0 (k))
let avg-y mean (sublist cluster-y-coordinates 0 (k))
set cluster-x-coordinates replace-item k cluster-x-coordinates (avg-x + (-1 + 2 * random 2) * (Cluster-Distance + random 20))
set cluster-y-coordinates replace-item k cluster-y-coordinates (avg-y + (-1 + 2 * random 2) * (Cluster-Distance + random 20))
]
])
ask prey [
let ind (position prey-type prey-types-list)
setxy (item ind cluster-x-coordinates + random-float cluster-radius) (item ind cluster-y-coordinates + random-float cluster-radius)
]
]
]
if experiment_mode = "Temporal Sequence Learning" [
ask Cslugs [set heading 0 setxy 0 Present-y-coord]
;set fix-Satiation 0.17
set immobilize true
set Presentation-Mode true
]
if experiment_mode = "Spatial Sequence Learning" [
ask Cslugs [set heading 0 setxy 0 -70]
create-prey 1 [set prey-type "flab" setxy 1 -30 set fixed 1] ;1 -20
create-prey 1 [set prey-type "hermi" setxy 0 2 set fixed 1] ;0 34
create-prey 1 [set prey-type "drug" setxy -1 34 set fixed 1] ;-34 -68
]
][
set Spatial-Mapping-Enabled true
set fix-Satiation 0.01
ask Cslugs [set heading 30 setxy 0 0]
__change-topology false false
ifelse experiment_mode = "3 Source Spatial Mapping" [
create-prey 1 [set prey-type "hermi" setxy 11 40 set fixed 1]
create-prey 1 [set prey-type "flab" setxy -34 -10 set fixed 1] ;-19 -23
create-prey 1 [set prey-type "drug" setxy 47 9 set fixed 1] ;55 -36
][
create-prey 1 [set prey-type "hermi" setxy 11 40 set fixed 1] ;0 34
create-prey 1 [set prey-type "flab" setxy -41 -50 set fixed 1] ;1 -20
create-prey 1 [set prey-type "drug" setxy 45 -7 set fixed 1] ;-34 -68
create-prey 1 [set prey-type "p5" setxy -43 27 set fixed 1]
;create-prey 1 [set prey-type "p6" setxy 45 -46 set fixed 1]
create-prey 1 [set prey-type "p7" setxy 2 -45 set fixed 1]
;create-prey 1 [set prey-type "p8" setxy 51 21 set fixed 1]
]
if experiment_mode = "Obstacle Avoidance Learning" [ ; create wall
ask Cslugs [set heading 150 setxy 7 50]
let wallcoordsx (list 52 22 30 35 42 60 65 14 5)
let wallcoordsy (list 28 7 10 15 21 34 36 3 -3)
; More complex wall below, can be tested with new working version of the detour/correction vector encoding under "reactivate-memory-of-inputs" function
; let wallcoordsx (list 52 22 30 35 42 60 65 14 510 29 38 );(range 24 67 7.17);(list 52 22 33 42 60 65)
; let wallcoordsy (list 28 7 10 15 21 34 36 3 -3 1 -5 -16 );(range 9 38 4.83);(list 28 7 14 21 34 36)
let numpts length(wallcoordsx)
repeat numpts [
create-wallpts 1[setxy (item wallcount wallcoordsx) (item wallcount wallcoordsy) set wall-id count wallpts]
set wallcount wallcount + 1
]
]
set wallcount 0
let wallcoordsxrange (range (-1 * (max-pxcor - 1)) (max-pxcor - 2) 5)
let wallcoordsyrange (range (-1 * (max-pycor - 1)) (max-pycor - 1) 5)
let numpts length(wallcoordsxrange)
repeat numpts [
create-wallpts 1 [setxy (item 1 wallcoordsxrange) (item wallcount wallcoordsyrange) set wall-id count wallpts]
create-wallpts 1 [setxy (last wallcoordsxrange) (item wallcount wallcoordsyrange) set wall-id count wallpts]
create-wallpts 1 [setxy (item wallcount wallcoordsxrange) (item 1 wallcoordsyrange) set wall-id count wallpts]
create-wallpts 1 [setxy (item wallcount wallcoordsxrange) (last wallcoordsyrange) set wall-id count wallpts]
set wallcount wallcount + 1
]
; ask patches [
; set odor-list n-values num-odor-types [0]
; if (abs pxcor > max-pxcor - 5) or (abs pycor > max-pycor - 5) [set odor10 0.5] ; The WALL (of offensive odor)
; ]
; repeat 20 [diffuse odor10 0.1]
]
;create-color-effects 20 [set hidden? true setxy random-xcor random-ycor]
;ask n-of 7 color-effects [set large-color-effect 1]
;create-pebbles 500 [set color 32 set size 3 set shape "pebble" setxy (-70 + 2 * random max-pxcor) (-1 * max-pycor + random 20)]
;create-pebbles 10 [set color 32 set size 3 set shape "pebble" setxy random-xcor random-ycor]
end
;-----------------------------------------------------------------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------------------------------------------------------------
;;==========================================================================================================================================
;; U P D A T E P R O C E D U R E S
;;==========================================================================================================================================
to Go
if Save-Data [write-to-file]
update-prey-and-odors
;=================================================================================
;
; Cslug Actions (update of Cslug variables)
;
;=================================================================================
ask Cslugs [
update-sensors ;updates sensor values and locations
plot-senses
plot-recalled-senses
;------------------------------------------------------;
; Sets values for odor and pain sensation
;------------------------------------------------------;
;; Detecting prey and pain
set sns_odors (map [[x1 x2] -> (x1 + x2) / 2] sns_odors_left sns_odors_right)
set sns-pain ((sns-pain-left + sns-pain-right ) / 2)
;sets an upper limit for pain (via logistic function).
set pain 10 / (1 + e ^ (- 2 * (sns-pain + spontaneous-pain) + 10 ))
;pain-switch will switch the signs of Reward State and Pain in Appetitive State when Pain is greater than Reward State
; (Effectively, this causes pain and reward state to be in reciprocal inhibition.
; In the presence of enough pain, positive rewards will ease the effect of pain,
; while negative rewards will exacerbate it.)
set pain-switch 1 - 2 / (1 + e ^ (- 10 * (sns-pain - 0.2)));
;-----------------------------------------------------------------;
; APPROACH AND AVOIDANCE BEHAVIORS
;=================================================================;
;------------------------------------------------------;
; Sets positive and negative expected reward (unused)
;------------------------------------------------------;
;sets positive expected reward (based on satiation, sns-betaine, sns-hermi, sns-drug, associative value for sns-hermi, and associative value for sns-drug)
;set ExpReward_pos sns_betaine / (1 + (0.05 * Vh * sns_hermi) - 0.006 / Satiation) + 3.0 * Vh * sns_hermi + 8.0 * Vd * sns_drug;
;sets negative reward (based on pain, sns-flab, and associative value for for sns-flab)
;set ExpReward_neg 0.59 * Vf * sns_flab; + pain; R-
;------------------------------------------------------;
; Handles Nutrition and Satiation
;------------------------------------------------------;
handle-consumption-events ; for prey/drug consumption events
; sets decrease in nutrition, satiation (based only on nutrition), and incentive salience (based on positive and negative reward)
set Nutrition Nutrition - 0.0005 * Nutrition ; Nutritional state declines with time
ifelse Fix-var1: [set Satiation fix-satiation] [set Satiation 1 / ((1 + 0.7 * exp(-4 * Nutrition + 2)) ^ (2))]
;set Incentive ExpReward_pos - ExpReward_neg;
;-----------------------------------------------------------;
; FAM Memory, Somatic Map, and Reward Calculations
;-----------------------------------------------------------;
update-RewardExperience false 0.95 ;update procedure for calculating input and output of Homeostatic Reward Circuit; learning demos do not use homeostatic plasticity mechanisms
;decay negative and positive rewards received from prey/drug consumption
set reward_pos 0.7 * reward_pos
set reward_neg 0.7 * reward_neg
set inputs (sentence (sublist sns_odors 1 9) (list sns-pain reward_pos reward_neg)) ;sensory (first 8) and reward (last 2) inputs for FAMatrix and more;
if Spatial-Mapping-Enabled [calculate-path-integration]
calculate-traces ;FAM eligibility traces
calculate-FAMatrix
calculate-Somatic-Map-and-Incentive
reactivate-memory-of-inputs
if experiment_mode = "Obstacle Avoidance Learning" [predefine-memory-vector] ; set up predefined memory vector for testing obstacle learning
plot-FAMatrix
plot-Graphs
if Show-MemoryLabels[
ask links with [text != "" and text != 0] [
let dist1 [distancexy mouse-xcor mouse-ycor] of end1
let dist2 [distancexy mouse-xcor mouse-ycor] of end2
ifelse all-text and link-length > 1 [set label text][ifelse (dist1 + dist2) - link-length < 0.5 [set label text][set label ""]]
]
]
;------------------------------------------------------;
; Calculate Appetitive State and Turning
;------------------------------------------------------;
; sets Appetitve State
;set App_State 0.01 + (1 / (1 + exp(- (0.75 * Incentive - 9 * satiation - 1.8 * Pain - 1.8 * pain-switch * RewardExperience))) + 0.1 * ((App_State_Switch - 1) * 0.5)); + 0.25
set App_State 0.01 + (1 / (1 + exp(- (7.5 * Incentive - 8 * satiation - 0.1 * Pain - 0.1 * pain-switch * RewardExperience))) + 0.1 * ((App_State_Switch - 1) * 0.5)); + 0.25
; The switch for approach-avoidance
set App_State_Switch (((-2 / (1 + exp(-100 * (App_State - 0.245)))) + 1))
;set the turning angle based on appetitive state switch and somatic map
set turn-angle 3 * ((2 * App_State_Switch) / (1 + exp (3 * Somatic_Map)) - App_State_Switch) ; 1st constant (multiplier) was 1
set turn-angle turn-angle + 1 * (item (length(senses_left) - 2) senses_left) ; left turn bias for walls
ifelse force-turn = 0 [rt turn-angle][rt force-turn]
set turn-angle-accumulation turn-angle-accumulation + turn-angle
; if the immobilize switch is ON, then fixes Cslug in place, but it can still turn (for testing purposes)
ifelse immobilize = true[
set speed 0
set forward-movement 0
][
set speed 0.5
set forward-movement speed - (item (length(senses) - 2) senses) / 4 ; stops at walls
]
fd forward-movement
]
;---------------------------------------------------------------------------------------------------
; Updates ticks (units of time), timers, and misc.
;---------------------------------------------------------------------------------------------------
update-arms ;decorative effect
drag; allow user to drag things around
tick
ifelse ticks < 300 [set plot-timer 0][set plot-timer plot-timer + 1] ;for graphing
if ticks = stop_time [stop] ; definite end of an epoch of play
if ticks > 0 and (ticks mod switch_time) = 0 [switch_on]
end
;-----------------------------------------------------------------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------------------------------------------------------------
;;==========================================================================================================================================
;; O T H E R U P D A T E F U N C T I O N S
;;==========================================================================================================================================
;-----------------------------------------------------------------;
; PREY, DRUG, WALL, AND ODOR UPDATE PROCEDURES
;=================================================================;
to update-prey-and-odors
;---------------------------------------------------------------------------------
; Updates Prey and Drug Populations
;---------------------------------------------------------------------------------
ask turtles [set non-edible 0.9 * non-edible]
let flabs prey with [prey-type = "flab"]
let hermis prey with [prey-type = "hermi"]
let drugs prey with [prey-type = "drug"]
let fauxflabs prey with [prey-type = "fauxflab"]
ask prey[
set shape "circle"
set size 1
set color item (position prey-type prey-types-list) prey-color-list
rt -1 + random-float 2 ; random movement of prey
ifelse Immobilize or Static-Environment [ fd 0][fd 0.02]
]
if experiment_mode = "Prey Populations" [
set Presenting ""
create-prey flab-populate - count flabs [set prey-type "flab" setxy random-xcor random-ycor]
if flab-populate < count flabs [ask n-of (count flabs - flab-populate) flabs [die]]
create-prey hermi-populate - count hermis [set prey-type "hermi" setxy random-xcor random-ycor]
if hermi-populate < count hermis [ask n-of (count hermis - hermi-populate) hermis [die]]
; create-prey fauxflab-populate - count fauxflabs [set prey-type "fauxflab" setxy random-xcor random-ycor]
; if fauxflab-populate < count fauxflabs [ask n-of (count fauxflabs - fauxflab-populate) fauxflabs [die]]
if ticks > 0 and (ticks mod switch_time) = 0 and Clustering = true[ ;for Prey clustering updates
let indices range(length cluster-x-coordinates)
(foreach indices [[k] ->
ifelse k = 0 [set cluster-x-coordinates replace-item k cluster-x-coordinates (random-pxcor) set cluster-y-coordinates replace-item k cluster-y-coordinates (random-pycor)][
let avg-x mean (sublist cluster-x-coordinates 0 (k))
let avg-y mean (sublist cluster-y-coordinates 0 (k))
set cluster-x-coordinates replace-item k cluster-x-coordinates (avg-x + (-1 + 2 * random 2) * (Cluster-Distance + random 20))
set cluster-y-coordinates replace-item k cluster-y-coordinates (avg-y + (-1 + 2 * random 2) * (Cluster-Distance + random 20))
]
])
ask prey [
let ind (position prey-type prey-types-list)
setxy (item ind cluster-x-coordinates + random-float cluster-radius) (item ind cluster-y-coordinates + random-float cluster-radius)
]
]
if not Addiction_Cycle[
create-prey drug-populate - count drugs [set prey-type "drug" setxy random-xcor random-ycor]
if drug-populate < count drugs [ask n-of (count drugs - drug-populate) drugs [die]]
]
]
if member? "TS" Presenting [set pticks pticks + 1 present-Temporal-Sequences] ;Present_TemporalSeq1 or Present_TemporalSeq2 [Present_TemporalSequences]
;---------------------------------------------------------------------------------
; Updates Odors
;---------------------------------------------------------------------------------
; Initialize, deposit, diffuse, and evaporate odors
ask prey [
let idnum position prey-type (remove-item 2 prey-types-list)
let pointer-target self
ask other prey [
let pointer-parent self
let idnum-origin position prey-type (remove-item 2 prey-types-list)
if (count memory-pointers with [parent = pointer-parent]) < count other prey[
hatch-memory-pointers 1[
set parent pointer-parent
set targ pointer-target
set id (list idnum-origin idnum)
set label ""
set size 2
;set shape "circle"
set color [color] of pointer-target
let parent-color [color] of parent
create-link-from parent [set color parent-color]
]
]
]
ifelse show-memorylabels [ ;displays learned reward values of prey/landmarks
ifelse idnum != 2 [
set label (word "Ri = " ([precision (item idnum Imag_Rewards) 2] of Cslug 0))
][
set label (word "Rr = " ([precision (item idnum Realsense_Rewards) 2] of Cslug 0))
]
ask memory-pointers [set hidden? false]
ask links [set hidden? false]
][
set label ""
ask memory-pointers [set hidden? true]
ask links [set hidden? true]
]
set odor-list item (position prey-type prey-types-list) prey-odor-matrix
set odor_betaine item 0 odor-list
set odor_hermi item 1 odor-list
set odor_flab item 2 odor-list
set odor_drug item 3 odor-list
set odor5 item 4 odor-list
set odor6 item 5 odor-list
set odor7 item 6 odor-list
set odor8 item 7 odor-list
set odor9 item 8 odor-list
set odor10 item 9 odor-list
]
ask wallpts [set odor10 1]
;; diffuse odors
diffuse odor_betaine diffc
diffuse odor_hermi diffc
diffuse odor_flab diffc
diffuse odor_drug diffc
diffuse odor5 diffc
diffuse odor6 diffc
diffuse odor7 diffc
diffuse odor8 diffc
diffuse odor9 diffc
diffuse odor10 diffc
diffuse marine-colors 0.6
;; evaporate odors
ask patches [
set odor_betaine evapc * odor_betaine
set odor_hermi evapc * odor_hermi
set odor_flab evapc * odor_flab
set odor_drug evapc * odor_drug
set odor5 evapc * odor5
set odor6 evapc * odor6
set odor7 evapc * odor7
set odor8 evapc * odor8
set odor9 evapc * odor9
set odor10 evapc * odor10
set odor-list (list odor_betaine odor_hermi odor_flab odor_drug odor5 odor6 odor7 odor8 odor9 odor10)
set marine-colors 0.999 * marine-colors
recolor-patches
]
end
;---------------------------------------------------------------------------------
; Recolors patches based on odor value
;=================================================================================
to recolor-patches
let color-list (but-first odor-color-list)
let max_odor max (but-first odor-list)
let color_ind position max_odor (but-first odor-list)
;set pcolor scale-color (item color_ind color-list) max_odor 0 0.001
let threshold 1e-7
if color_ind = 8 [set threshold 1e-3]
ifelse max_odor > threshold [set pcolor scale-color (item color_ind color-list) (7 + (log max_odor 10)) 0 13.5][set pcolor scale-color 105 marine-colors 0 10]
;ifelse max_odor > 1e-7 [set pcolor ((floor (item color_ind color-list / 10)) * 10 + 1 + 130 * max_odor)] [set pcolor scale-color 105 marine-colors 0 10]
end
;Marine color background environment (unused)
to floating-marine-colors
ask color-effects [
ifelse large-color-effect = 1 [set marine-colors 10 + random 5 (rt -5 + random-float 10) fd random-float 0.05] ;ask patches in-radius 20 [set marine-colors 10 / (1 + distance myself)
[set marine-colors 1 + random 7 (rt -5 + random-float 10) fd random-float 1.3]
]
;ask n-of (100 + random 100) patches [if ticks mod (1 + random 100) = 0 [ask patches in-radius 2 [set marine-colors marine-colors + 1]]]
end
;---------------------------------------------------------------------------------
; Updates arm movements
;=================================================================================
to update-arms
ask arms [
let x-par [xcor] of parent
let y-par [ycor] of parent
let hd-par [heading] of parent
let sz-par [size] of parent
set heading hd-par
;setxy x-par y-par
let arm-turn (4 + random 2) * sin(40 * ticks + random 2)
let arm-turn2 (4 + random 2) * cos(40 * ticks + random 2)
if id = "armo1r"[setxy (x-par + 0.4 * sz-par * sin (hd-par - 100)) (y-par + 0.4 * sz-par * cos (hd-par - 100)) set heading hd-par + 25 + arm-turn]
if id = "armo2r"[setxy (x-par + 0.8 * sz-par * sin (hd-par + 40)) (y-par + 0.8 * sz-par * cos (hd-par + 40)) set heading hd-par + 100 - arm-turn]
if id = "armo3r"[setxy (x-par + 0.85 * sz-par * sin (hd-par + 10)) (y-par + 0.85 * sz-par * cos (hd-par + 10)) set heading hd-par + 30 + arm-turn2]
if id = "armo4r"[setxy (x-par - 0.8 * sz-par * sin (hd-par + 52)) (y-par - 0.8 * sz-par * cos (hd-par + 52)) set heading hd-par + 230 - arm-turn2]
if id = "armo4l"[setxy (x-par + 0.7 * sz-par * sin (hd-par - 17)) (y-par + 0.7 * sz-par * cos (hd-par - 17)) set heading hd-par - 30 + arm-turn]
if id = "armo3l"[setxy (x-par + 0.75 * sz-par * sin (hd-par - 90)) (y-par + 0.75 * sz-par * cos (hd-par - 90)) set heading hd-par - 160 - arm-turn]
if id = "armo2l"[setxy (x-par + 0.8 * sz-par * sin (hd-par + 110)) (y-par + 0.8 * sz-par * cos (hd-par + 110)) set heading hd-par + 180 + arm-turn2]
if id = "armo1l"[setxy (x-par + 0.8 * sz-par * sin (hd-par + 75)) (y-par + 0.8 * sz-par * cos (hd-par + 75)) set heading hd-par + 60 - arm-turn2]
]
end
;-----------------------------------------------------------------;
; PREY AND DRUG CONSUMPTION
;=================================================================;
to handle-consumption-events
let target other (turtle-set prey with [non-edible < 1]) in-cone (0.4 * size) 45
if any? target [
let drugnum count target with [prey-type = "drug"]
let hermnum count target with [prey-type = "hermi"]
let flabnum count target with [prey-type = "flab"]
let fauxflabnum count target with [prey-type = "fauxflab"]
let pnum count target with [member? "p" prey-type]
set Nutrition Nutrition + 0.1 * (hermnum + flabnum + fauxflabnum + drugnum + pnum)
;set R_hermi R_hermi - hermnum
;set R_flab R_flab - flabnum
set R_drug R_drug + drug_reward * drugnum
set reward_pos reward_pos + drugnum * 8 + hermnum * 2 * 0
set reward_neg reward_neg - flabnum * 5 * 0
set hermcount hermcount + hermnum
set flabcount flabcount + flabnum
set drugcount drugcount + drugnum
ifelse Presentation-Mode [ask target [die]][ask target [ifelse not Static-Environment [setxy random-xcor random-ycor][set non-edible 10]]]
]
end
;---------------------------------------------------------------------------------
; Updates values and positions of sensors
;=================================================================================
to update-sensors
let me self
update-nociceptor-position
;colors pain sensors to show pain value
ask nociceptors [
if id = "snsrOL"[
let pain_indicator [sns-pain-left] of parent
set color scale-color red pain_indicator 0 0.8
if pain_indicator < 0.1 [set color lput 0 extract-rgb color]
]
if id = "snsrOR"[
let pain_indicator [sns-pain-right] of parent
set color scale-color red pain_indicator 0 0.8
if pain_indicator < 0.1 [set color lput 0 extract-rgb color]
]
]
;----------------------------------------------------------
set odors_left [odor-list] of patch-left-and-ahead 40 (0.4 * size)
set sns_odors_left map [x -> ifelse-value (x > 1e-7) [7 + (log x 10)][0]] odors_left
set odors_right [odor-list] of patch-right-and-ahead 40 (0.4 * size)
set sns_odors_right map [x -> ifelse-value (x > 1e-7) [7 + (log x 10)][0]] odors_right
;----------------------------------------------------------
;set sns-pain-left sum [painval] of (nociceptors with [id = one-of["snsrOL" "snsrUL" "snsrBL"] and parent = me])
;set sns-pain-right sum [painval] of (nociceptors with [id = one-of["snsrOR" "snsrUR" "snsrBR"] and parent = me])
;set sns-pain-caud sum [painval] of (nociceptors with [id = one-of["snsrBL" "snsrBR" "snsrBM"] and parent = me])
;sensation of pain
set sns-pain-left 0.9 * sns-pain-left
set sns-pain-right 0.9 * sns-pain-right
end
;---------------------------------------------------------------------------------
; Updates positions of pain sensors
;=================================================================================
to update-nociceptor-position
ask nociceptors[
let x-par [xcor] of parent
let y-par [ycor] of parent
let hd-par [heading] of parent
let sz-par [size] of parent
ifelse id = "snsrOL"[
setxy (x-par + 0.4 * sz-par * sin (hd-par - 40)) (y-par + 0.4 * sz-par * cos (hd-par - 40))
][
ifelse id = "snsrOR"[
setxy (x-par + 0.4 * sz-par * sin (hd-par + 40)) (y-par + 0.4 * sz-par * cos (hd-par + 40))
][
ifelse id = "snsrUL"[
setxy (x-par + 0.3 * sz-par * sin (hd-par - 100)) (y-par + 0.3 * sz-par * cos (hd-par - 100))
][
ifelse id = "snsrUR"[
setxy (x-par + 0.3 * sz-par * sin (hd-par + 100)) (y-par + 0.3 * sz-par * cos (hd-par + 100))
][
ifelse id = "snsrBL"[
setxy (x-par + 0.35 * sz-par * sin (hd-par - 150)) (y-par + 0.35 * sz-par * cos (hd-par - 150))
][
ifelse id = "snsrBR"[
setxy (x-par + 0.35 * sz-par * sin (hd-par + 150)) (y-par + 0.35 * sz-par * cos (hd-par + 150))
][
setxy (x-par + 0.46 * sz-par * sin (hd-par - 180)) (y-par + 0.46 * sz-par * cos (hd-par - 180))
]
]
]
]
]
]
]
end
;---------------------------------------------------------------------------------
; Somatic Map and Incentives Calculations (Function)
;=================================================================================
;---------------------------------------------------------------------------------
to calculate-Somatic-Map-and-Incentive
;let betaine_reward 1
let odor10_reward -2.5
let pain_reward -1.5
set Realsense_Rewards (sentence (sublist Direct_Rewards 0 (num_inputs - 3)) (list odor10_reward pain_reward))
let Somatic_Map_realsenses ( lput sns-pain but-first sns_odors )
let Somatic_Map_senses_left ( lput sns-pain-left but-first sns_odors_left )
let Somatic_Map_senses_right ( lput sns-pain-right but-first sns_odors_right )
set Imag_Incentives (map [[reward sense] -> (reward) * sense] Imag_Rewards imag_senses)
set Incentives (map [[reward sense] -> (reward) * sense] Realsense_Rewards Somatic_Map_realsenses)
;let Somatic_Map_senses (map [[x1 x2] -> (x1 + x2)] Somatic_Map_realsenses imag_senses)
let Somatic_Map_incentives (map [[x1 x2] -> (abs x1 + abs x2) / 10] Incentives Imag_Incentives)
set Somatic_Map_Factors map [x -> 2 * x - sum (Somatic_Map_incentives)] Somatic_Map_incentives ;; Exponent variables for somatic map, which basically determine interaction and "priority" of the sensations
let last_index (length Somatic_Map_Factors - 1)
;set Somatic_Map_Factors replace-item (last_index - 1) Somatic_Map_Factors (item (last_index - 1) Somatic_Map_realsenses)
set Somatic_Map_Factors replace-item last_index Somatic_Map_Factors pain
let indices range (length Somatic_Map_incentives)
(foreach indices Somatic_Map_senses_left Somatic_Map_senses_right Realsense_Rewards imag_senses_left imag_senses_right Imag_Rewards Somatic_Map_Factors [[n sns_left sns_right real_sns_r im_sns_left im_sns_right im_sns_r C] ->
set Somatic_Map_Sigmoids replace-item n Somatic_Map_Sigmoids (((1 + real_sns_r) * (sns_left - sns_right) + 20 * (1 + im_sns_r) * (im_sns_left - im_sns_right)) / (1 + exp (-30 * C))) ])
ifelse Fix-var3: [set Somatic_Map fix-SomaticMap][set Somatic_Map -1 * (sum Somatic_Map_Sigmoids)]
end
;---------------------------------------------------------------------------------
; Homeostatic Reward Circuit Calculations (Function)
;=================================================================================
to update-RewardExperience [homeostatic-change? d-decay]
;sets reward inputs, which decay over time
set R_hermi 0.98 * R_hermi
set R_flab 0.98 * R_flab
set R_drug d-decay * R_drug
;Sets Reward Input to neuron M, with a baseline activity of M0
set R (W1 * R_drug + W2 * IN + W4 * R_hermi + W5 * R_flab + M0)
;Positive feedback loop for reward input
set IN (W2 * R)
;Response of neuron M, based on the dynamic synaptic weight W3 and Reward Input (R)
set M (W3 * R)
;Change in W3 depends on neuron M activity, neuron M baseline (M0), and Reward Input (R)
if homeostatic-change? [
set dW3 ((M0 - M) / R) / 1000
set W3 (W3 + dW3)
]
;sets Reward State value as a logistic function of neuron M activity minus its baseline. Basically indicates how much neuron M activity differs from baseline.
; (Reward State is the output of the Homeostatic Reward Circuit (HRC) to Appetitive State)
ifelse Fix-var2: [set RewardExperience Fix-RewardExp][set RewardExperience -15 + 30 / (1 + e ^ (-(M - M0)))]
end
; H0 = baseline, Wt1 = current value of homeostatic synaptic weight (at time t1), tau = time constant
to-report homeostat [Input H0 Wt1 tau]
if Input = 0 [set Input 0.1]
let Ht1 Wt1 * Input
let Wt2 Wt1 + ((H0 - Ht1) / (Input)) / tau
let Ht2 Wt2 * Input
report (list Wt2 Ht2) ;
end
;-----------------------------------------------------------------------------------------;
;=========================================================================================;
;
; FEATURE ASSOCIATION MATRIX CALCULATION AND PLOTTING FUNCTIONS
;
;=========================================================================================;
to calculate-traces
let old-traces traces
let indices range (length inputs)
(foreach indices [[n] ->
ifelse n < num_inputs - 2 [
let trace item n traces
let others remove-item n inputs
;For temporal sequence learning, trace decays without path integration (slitherometer), for mapping trace is maintained in absence of other inputs; decreases with other inputs
ifelse item n inputs > 1e-7 [set trace 0.7 + 0.3 / (1 + exp(-3 * (item n inputs) + 7))] [set trace (1.94 * (0.5 - speed) * trace) + sign2 slitherometer * (trace - 0.03 * trace * sum others)]
set traces replace-item n traces trace
][set traces replace-item n traces (item n inputs)]
])
set trace-changes (map [[trt1 trt0] -> trt1 - trt0] traces old-traces)
end
to calculate-FAMatrix
; each association is between two inputs (input-i and input-j)
set i 0
set j 0
set Incentive 0
while [i < num_inputs] [
set ImagReward_i 0
set DirectReward_i 0
while [j < num_inputs] [
if i != j[
if not fix-Matrix [
calculate-FAMatrix-element ; calculates strength, order, and memory vector for each association
assign-Reward-element ; assigns expected reward value for each association
; calculates (predicted) vectors for pairwise associations that haven't been experienced yet, condition on max trace introduces slight delay, to ensure reward vals are updated first
if Spatial-Mapping-Enabled and max traces <= 0.9 [assign-Vector-element]
]
calculate-Incentive-Element ; calculates contribution to Incentive for each association
]
set j j + 1
]
if i < num_inputs - 3 [
set Imag_Rewards replace-item i Imag_Rewards ImagReward_i
set Direct_Rewards replace-item i Direct_Rewards DirectReward_i
]
set i i + 1
set j 0
]
set i 0
end
; Calculates strength, order, and memory vector for each association
to calculate-FAMatrix-element
let post-reward-landmark false
let tracei item i traces
let tracej item j traces
let inputi item i inputs
let inputj item j inputs
; if i = num_inputs - 2 [set tracei reward_pos]
; if i = num_inputs - 1 [set tracei reward_neg]
; if j = num_inputs - 2 [set tracej reward_pos]
; if j = num_inputs - 1 [set tracej reward_neg]
let cross-mult 4
let thresh 0.01 ;was 0.1, now lower due to indefinite traces
let cross (abs(tracei) * abs(tracej))
let diff (abs(tracej) - abs(tracei))
let tsum (abs(tracei) + abs(tracej))
let max_trace max (list abs(tracei) abs(tracej))
ifelse i >= (num_inputs - 2) or j >= (num_inputs - 2) [
matrix:set FAMatrix_timelags i j 0
let noreward_decay 0
if i < num_inputs - 2 [set noreward_decay sign2 (inputi - 1) * sign2 (1 - inputj)]
if j < num_inputs - 2 [set noreward_decay sign2 (inputj - 1) * sign2 (1 - inputi)]
matrix:set FAMatrix_cross-A i j (matrix:get FAMatrix_cross-A i j) / (1 + noreward_decay / 50)
if (i = 10 or j = 10) and reward_pos < 3 [ set tsum 0]
if (i = 11 or j = 11) and reward_neg > -3 [ set tsum 0]
set cross-mult 0.3
][
; let k 0
; while [k < num_inputs and not post-reward-landmark ][
; if matrix:get FAMatrix_strengths i j > 0.99 [
; if matrix:get FAMatrix_diff-A i j < -0.001 and matrix:get FAMatrix_timelags j k = 0 and matrix:get FAMatrix_rewards j k > 1 [set post-reward-landmark true print true]
; if matrix:get FAMatrix_diff-A i j > 0.001 and matrix:get FAMatrix_timelags i k = 0 and matrix:get FAMatrix_rewards i k > 1 [set post-reward-landmark true print true]
; ]
; set k k + 1
; ]
; set k 0
;**************
matrix:set FAMatrix_diff-A i j (matrix:get FAMatrix_diff-A i j) + (1 / (1 + exp(3 * matrix:get FAMatrix_rewards i j - 6))) * tsum * (diff - (matrix:get FAMatrix_diff-A i j)) ;accumulator function based on difference of traces (and product of traces)
let ord-factor 2
;if post-reward-landmark [set ord-factor -1 * ord-factor]
matrix:set FAMatrix_timelags i j ord-factor * matrix:get FAMatrix_diff-A i j
;matrix:set FAMatrix_diff-A i j (matrix:get FAMatrix_diff-A i j) + (1 / (1 + exp(3 * matrix:get FAMatrix_rewards i j - 6))) * cross * (diff - (matrix:get FAMatrix_diff-A i j)) ;accumulator function based on difference of traces (and product of traces)
;matrix:set FAMatrix_timelags i j (2 / (1 + exp(-(10 * (matrix:get FAMatrix_diff-A i j)))) - 1)
if Spatial-Mapping-Enabled and cross > 0.15 [ ;>0.4 ;and (max list tracei tracej) > 0.8
let vsign 0
if matrix:get FAMatrix_timelags i j < 0 [set vsign 1]
;if post-reward-landmark [set vsign (vsign + 1) mod 2]
let vdist_prev matrix:get FAMatrix_vdist i j
let vdir_prev matrix:get FAMatrix_vdir i j
;if vdist_prev = 0 and matrix:get FAMatrix_rewards i j > 1e-3 [set vsign vsign + 1]
matrix:set FAMatrix_vdir i j item 0 vsum - vsign * 180
matrix:set FAMatrix_vdist i j item 1 vsum
; ifelse vdist_prev = 0 [
; matrix:set FAMatrix_vdir i j item 0 vsum - vsign * 180
; matrix:set FAMatrix_vdist i j item 1 vsum
; ][
; let v_avg average-vectors (list vdir_prev vdist_prev) (list (item 0 vsum - vsign * 180) (item 1 vsum) )
; matrix:set FAMatrix_vdir i j item 0 v_avg
; matrix:set FAMatrix_vdist i j item 1 v_avg
; ]
]
]
matrix:set FAMatrix_cross-A i j (matrix:get FAMatrix_cross-A i j) + (0.001 * matrix:get FAMatrix_rewards i j + cross-mult * cross - thresh) * (tsum) ;accumulator function based around product of traces (and product threshold and sum)
matrix:set FAMatrix_strengths i j 1 / (1 + exp(-2 * (matrix:get FAMatrix_cross-A i j) + 6))
matrix:set FAMatrix_cross-A i j (matrix:get FAMatrix_cross-A i j) + (matrix:get FAMatrix_strengths i j - matrix:get FAMatrix_cross-A i j) / 10000 ;accumulator function is slowly decayed, to limit growth
end
; Assign expected rewards (but not orders or strengths) through a series of summations based on elements' orders and strengths
to assign-Reward-element
ifelse i > (num_inputs - 3) or j > (num_inputs - 3) [
;let strsign sign2 (matrix:get FAMatrix_strengths i j - 0.1)
let strsign 1.01 / (1 + exp(-20 * (matrix:get FAMatrix_strengths i j) + 8))
if (i = num_inputs - 2) or (j = num_inputs - 2) [matrix:set FAMatrix_rewards i j (10 * strsign)]
if (i = num_inputs - 1) or (j = num_inputs - 1) [matrix:set FAMatrix_rewards i j (-10 * strsign)]
;if (i = num_inputs - 2 and item i inputs > 1e-7 ) or (j = num_inputs - 2 and item j inputs > 1e-7 ) [matrix:set FAMatrix_rewards i j (10 * matrix:get FAMatrix_strengths i j)]
;if (i = num_inputs - 1 and item i inputs > 1e-7 ) or (j = num_inputs - 1 and item j inputs > 1e-7) [matrix:set FAMatrix_rewards i j (-10 * matrix:get FAMatrix_strengths i j)]
][
let k 0
let s-factor 1
let post-reward-landmark false
let cross abs(item i traces) * abs(item j traces) ; 0.01
let diff abs(item j traces) - abs(item i traces) ; 0.001
if cross > 0.01[