-
Notifications
You must be signed in to change notification settings - Fork 0
/
croosfield.netlogo
180 lines (157 loc) · 6.39 KB
/
croosfield.netlogo
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
;; sustainable development of Agricultural and pastoral areas
breed [people person] ;; sheep is its own plural: we use "a-sheep" as the singular.
breed [cows cow]
globals [
attendance ;; the current attendance at the bar
history ;; list of past values of attendance
agr-patches ;; agentset of green patches representing the residential area
pas-patches ;; agentset of blue patches representing the bar area
;; crowded-patch ;; patch where we show the "CROWDED" label
]
turtles-own [
strategies ;; list of strategies
best-strategy ;; index of the current best strategy
attend? ;; true if the agent currently plans to attend the bar
prediction ;; current prediction of the bar attendance
value ;; value of each production
]
to setup
clear-all
set-default-shape people "plant"
set-default-shape cows "cow"
;; create the 'homes'
set agr-patches patches with [(pxcor < 0 and pycor < 0) or (pxcor > 0 and pycor > 0 )]
ask agr-patches[ set pcolor green ]
;; create the 'bar'
set pas-patches patches with [pxcor > 0 and pycor < 0 or (pxcor < 0 and pycor > 0 )]
ask pas-patches [ set pcolor blue ]
;; initialize the previous attendance randomly so the agents have a history
;; to work with from the start
set history n-values (memory-size * 2) [random 100]
;; the history is twice the memory, because we need at least a memory worth of history
;; for each point in memory to test how well the strategies would have worked
set attendance first history
;; use one of the patch labels to visually indicate whether or not the
;; bar is "crowded"
;;ask patch (0.75 * max-pxcor) (0.5 * max-pycor) [
;; set crowded-patch self
;; set plabel-color red
;;]
;; create the agents and give them random strategies
;; these are the only strategies these agents will ever have though they
;; can change which of this "bag of strategies" they use every tick
create-people 100 [
set color brown
set size 1.5
set value 5 + 5 * random 2
set strategies n-values number-strategies [random-strategy]
set best-strategy first strategies
move-to-empty-one-of agr-patches
update-strategies
]
create-cows 100 [
set color yellow
set size 1.2
set value 10 + 10 * random 3
set strategies n-values number-strategies [random-strategy]
set best-strategy first strategies
move-to-empty-one-of pas-patches
update-strategies
]
;; start the clock
reset-ticks
end
to go
;; update the global variables
;;ask crowded-patch [ set plabel "" ]
;; each agent predicts attendance at the bar and decides whether or not to go
ask turtles [
set prediction predict-attendance best-strategy sublist history 0 memory-size
set attend? (prediction <= overcrowding-threshold) ;; true or false
]
;; depending on their decision, the agents go to the bar or stay at home
ask people [
ifelse attend?
[ move-to-empty-one-of pas-patches
set attendance attendance + 1 ]
[ ;;move-to-empty-one-of agr-patches
]
set value value - 1
]
ask cows [
ifelse attend?
[ move-to-empty-one-of agr-patches
set attendance attendance + 1 ]
[ ;;move-to-empty-one-of pas-patches
]
set value value - 2
]
;; if the bar is crowded indicate that in the view
set attendance count turtles-on pas-patches
;;if attendance > overcrowding-threshold [
;; ask crowded-patch [ set plabel "CROWDED" ]
;;]
;; update the attendance history
;; remove oldest attendance and prepend latest attendance
set history fput attendance but-last history
;; the agents decide what the new best strategy is
ask turtles [ update-strategies ]
;; advance the clock
tick
end
;; determines which strategy would have predicted the best results had it been used this round.
;; the best strategy is the one that has the sum of smallest differences between the
;; current attendance and the predicted attendance for each of the preceding
;; weeks (going back MEMORY-SIZE weeks)
;; this does not change the strategies at all, but it does (potentially) change the one
;; currently being used and updates the performance of all strategies
to update-strategies
;; initialize best-score to a maximum, which is the lowest possible score
let best-score memory-size * 100 + 1
foreach strategies [ the-strategy ->
let score 0
let week 1
repeat memory-size [
set prediction predict-attendance the-strategy sublist history week (week + memory-size)
set score score + abs (item (week - 1) history - prediction)
set week week + 1
]
if (score <= best-score) [
set best-score score
set best-strategy the-strategy
]
]
end
;; this reports a random strategy. a strategy is just a set of weights from -1.0 to 1.0 which
;; determines how much emphasis is put on each previous time period when making
;; an attendance prediction for the next time period
to-report random-strategy
report n-values (memory-size + 1) [1.0 - random-float 2.0]
end
;; This reports an agent's prediction of the current attendance
;; using a particular strategy and portion of the attendance history.
;; More specifically, the strategy is then described by the formula
;; p(t) = x(t - 1) * a(t - 1) + x(t - 2) * a(t -2) +..
;; ... + x(t - MEMORY-SIZE) * a(t - MEMORY-SIZE) + c * 100,
;; where p(t) is the prediction at time t, x(t) is the attendance of the bar at time t,
;; a(t) is the weight for time t, c is a constant, and MEMORY-SIZE is an external parameter.
to-report predict-attendance [strategy subhistory]
;; the first element of the strategy is the constant, c, in the prediction formula.
;; one can think of it as the agent's prediction of the bar's attendance
;; in the absence of any other data
;; then we multiply each week in the history by its respective weight
report 100 * first strategy + sum (map [ [weight week] -> weight * week ] butfirst strategy subhistory)
end
;; In this model it doesn't really matter exactly which patch
;; a turtle is on, only whether the turtle is in the home area
;; or the bar area. Nonetheless, to make a nice visualization
;; this procedure is used to ensure that we only have one
;; turtle per patch.
to move-to-empty-one-of [locations] ;; turtle procedure
move-to one-of locations
while [any? other turtles-here] [
move-to one-of locations
]
end
; Copyright 2007 Uri Wilensky.
; See Info tab for full copyright and license.