forked from amaliebanan/BeerModelling
/
Model.py
232 lines (188 loc) · 7.3 KB
/
Model.py
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
from mesa import Agent, Model
from mesa.space import MultiGrid
from mesa.time import SimultaneousActivation,RandomActivation
import Agent as ac
from statistics import mean
import numpy as np
import random
import math
import sys
from mesa.datacollection import DataCollector
'''
The Model-object is responsible for the logical structure of our ABM.
Here, we define our and set-up our visual grid,
our logical algorithm (e.g. should the agents move randomly or simultaneously?)
'''
def pouring_time(self):
sump = 0
for a in self.employees:
sump += a.pouring_time
return sump
def busy_employees(self):
busy = 0
for a in self.employees:
if a.busy == True:
busy+=1
return busy
def busy_employees_at_stalls(self):
stalls = [s for s in self.schedule.agents if isinstance(s,ac.beerstall)]
busy = []
for stall in stalls:
busy_employees = 0
for employee in stall.employees:
if employee.busy == True:
busy_employees+=1
busy.append((stall.id,busy_employees))
return busy
def agents_go_to_concert(self):
all_guests = [a for a in self.schedule.agents if isinstance(a,ac.guest)]
percentages = int((len(all_guests)/100)*90) #get 90% of the guests
counter = 0
while percentages>counter:
agent = self.random.choice(all_guests)
if agent.at_concert == True:
continue
else:
agent.at_concert = True
counter+=1
def end_concert(self):
all_guests = [a for a in self.schedule.agents if isinstance(a,ac.guest)]
for a in all_guests:
a.at_concert = False
class Model(Model):
def __init__(self, N, height, width):
super().__init__()
self.N = N
self.height = height
self.width = width
#Multigrid (The visual grid)
self.grid = MultiGrid(width, height, torus=False) #torus wraps edges
#Schedule (the logical grid)
self.schedule = SimultaneousActivation(self)
#Datacollector to collect our data (pouring time, dispatch time, etc)
self.datacollector = DataCollector(model_reporters={"pouring_time": lambda m: pouring_time(self),
"busy": lambda m: busy_employees(self)})
#Initiate minute, hour and day
self.time_step = 1
self.minute_count = 1
self.hour_count = 1
self.day_count = 1
#The location of the beer stalls
self.stall_positions = [(10,6),(40,6),(15,44),(40,44)]
self.employees = []
self.desk_pos = []
self.busy = []
self.sceneCoords = [(0,i) for i in range(math.floor(height/2)-7,math.floor(height/2)+6)]
setUpGuests(self,N)
setUpScene(self)
setUpStalls(self)
setUpEmployees(self)
setUpFence(self)
def step(self):
self.time_step += 1
if self.time_step%6==0:
self.minute_count += 1
if self.minute_count%60 == 0:
self.hour_count += 1
#Concert is starting
if self.time_step == 90:
agents_go_to_concert(self)
#Concert is ending
elif self.time_step == 630:
end_concert(self)
#With poisson-distribution people leave and join the concert
if self.time_step in [i for i in range(91,630)]:
p_leave = np.random.poisson(3/100)
if p_leave == 1:
guests_at_concert = [a for a in self.schedule.agents if isinstance(a,ac.guest) and a.at_concert == True]
agent = self.random.choice(guests_at_concert)
agent.at_concert = False
p_join = np.random.poisson(3/100)
if p_join == 1:
guests_at_concert = [a for a in self.schedule.agents if isinstance(a,ac.guest) and a.at_concert == False]
agent = self.random.choice(guests_at_concert)
agent.at_concert = True
#Every 6th timestep (every minute) during the concert, one new guest joins
if self.time_step in [i for i in range(100,630,6)]:
max_id = max([a.id for a in self.schedule.agents if isinstance(a,ac.guest)])
newAgent = ac.guest(max_id+1, self)
self.schedule.add(newAgent)
x_,y_ = self.random.choice([(25,0),(25,49),(40,0),(40,49),(49,10),(49,39)])
newAgent.at_concert = True
self.grid.place_agent(newAgent,(x_,y_))
self.busy.append(busy_employees(self))
self.schedule.step()
self.datacollector.collect(self)
busy_employees_at_stalls(self)
mean_busy = mean(self.busy)
def setUpGuests(self,N):
for i in range(0,N):
newAgent = ac.guest(i, self)
self.schedule.add(newAgent)
x_,y = self.grid.find_empty()
x = max(3,x_)
self.grid.place_agent(newAgent,(x,y))
def setUpScene(self):
coords = list.copy(self.sceneCoords)
for i in range(1000,1000+len(coords)):
newAgent = ac.orangeScene(i, self)
self.schedule.add(newAgent)
x,y = coords.pop()
self.grid.place_agent(newAgent,(x,y))
def setUpStalls(self):
pos = list.copy(self.stall_positions)
for i in range(2000,2000+len(pos)*5,5):
newAgent = ac.beerstall(i, self)
self.schedule.add(newAgent)
x,y = pos.pop()
self.grid.place_agent(newAgent,(x,y))
#Here people order and pay for beer
desk_pos = [(x-2,y),(x+2,y),(x,y-2),(x,y+2)]
self.desk_pos = self.desk_pos + desk_pos
#People cannot get past the desk-line (the ones colored pink)
desk_pos_ = [(x-1,y-2),(x-2,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-2),(x+2,y-1),
(x+1,y+2),(x+2,y+2),(x+2,y+1),(x-1,y+2),(x-2,y+2),(x-2,y+1)]
for pos_ in desk_pos_:
newAgent = ac.desk(pos_, self)
self.schedule.add(newAgent)
self.grid.place_agent(newAgent,pos_)
def setUpEmployees(self):
employees = []
for pos in self.stall_positions:
e1 = (pos[0],pos[1]-1)
e2 = (pos[0]-1,pos[1])
e3 = (pos[0]+1,pos[1])
e4 = (pos[0],pos[1]+1)
stall = (pos,e1,e2,e3,e4)
employees.append(stall)
counter = 0
for stalls in employees:
stall = [stall for stall in self.schedule.agents if stall.id == 2000+counter][0]
temp = []
for e in range(1,5):
newAgent = ac.employee(stall.id+e, self)
newAgent.stall = stall
self.schedule.add(newAgent)
x,y = stalls[e]
self.grid.place_agent(newAgent,(x,y))
self.employees.append(newAgent)
temp.append(newAgent)
stall.employees = temp
counter += 5
def setUpFence(self):
#Positions of horizontal and vertical fence
pos_vertical_fence = [(2,i) for i in range(16,33)]
pos_horizontal_fence = [(0,16),(1,16),(0,32),(1,32)]
ids = [i for i in range (3000,3000+len(pos_horizontal_fence)+len(pos_vertical_fence))]
for pos in pos_vertical_fence:
newAgent = ac.fence(ids.pop(), self)
self.schedule.add(newAgent)
newAgent.orientation = 'v'
x,y = pos
self.grid.place_agent(newAgent,(x,y))
for pos in pos_horizontal_fence:
newAgent = ac.fence(ids.pop(), self)
self.schedule.add(newAgent)
newAgent.orientation = 'h'
x,y = pos
self.grid.place_agent(newAgent,(x,y))