/
mainABM.py
218 lines (168 loc) · 6.51 KB
/
mainABM.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
# importing modules
# - mesa modules
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid, Grid
from mesa.datacollection import DataCollector
# matplotlib for plotting when testing
import matplotlib.pyplot as plt
# calculating pseudo-random numbers
import random
# for managing the data-frame produced by the simulation
import pandas as pd
# data handling
import numpy as np
# import dataset
from dataPrep import iqr, mad, pause, speechrate, diagnosis
# import interaction rules
import rulebook
# defining the agent class
class Agent(Agent):
# An agent-cass inheriting the properties of Agent
# define properties needed to specify an object of class Agent()
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
# specify pitch measures for the agent
""" Perhaps we don't need to specify the values that are initiated by the dataframe, we do however have
to specify any variables that we make up that are manipulated by interaction rules. So if an initial
value is manipulated over time we have to specify that in the start a well. """
# specify activity-level
self.status = "Active"
# specify agent-properties
self.unique_interactions = 0
self.interaction_time = 0.001
self.conversation_time = 0
self.change_iqr = 0
self.change_mad = 0
self.change_speechrate = 0
self.change_pause = 0
self.abs_change_iqr = 0
self.abs_change_mad = 0
self.abs_change_speechrate = 0
self.abs_change_pause = 0
self.activity = 0
def move_normal(self):
# examine environment
possible_steps = self.model.grid.get_neighborhood(self.pos, moore=True, include_center=False)
# choose random cells in neighbor grid
new_position = self.random.choice(possible_steps)
# move agent to new cell
self.model.grid.move_agent(self, new_position)
# get cell-contents
cell_info = self.model.grid.get_cell_list_contents([self.pos])
# if the cell contains more than 2 agents already, repeat the movement
while len(cell_info) > 2:
# examine environment
possible_steps = self.model.grid.get_neighborhood(self.pos, moore=True, include_center=False)
# choose random cell in new neighbor grid
new_position = self.random.choice(possible_steps)
# move the agent
self.model.grid.move_agent(self, new_position)
# get new grid info to avoid infinite recursion
cell_info = self.model.grid.get_cell_list_contents([self.pos])
# define skeptical move-function
def move_skeptical(self):
# examine environment
possible_steps = self.model.grid.get_neighborhood(self.pos, moore=True, include_center=False)
# choose random cells in neighbor grid
new_position = self.random.choice(possible_steps)
# move agent to new cell
self.model.grid.move_agent(self, new_position)
# get cell-contents
cell_info = self.model.grid.get_cell_list_contents([self.pos])
# if the cell contains more than 2 agents already, repeat the movement
while not len(cell_info) == 1:
# examine environment
possible_steps = self.model.grid.get_neighborhood(self.pos, moore=True, include_center=False)
# choose random cell in new neighbor grid
new_position = self.random.choice(possible_steps)
# move the agent
self.model.grid.move_agent(self, new_position)
# get new grid info to avoid infinite recursion
cell_info = self.model.grid.get_cell_list_contents([self.pos])
# define step function - what the agent does for each timestep
def step(self):
cell = self.model.grid.get_cell_list_contents([self.pos])
movement_prob = ((0.15 * (1 - self.iqr)) +
(0.4 * (1 - self.mad)) +
(0.15 * self.speechrate) +
(0.15 * (1 - self.pause)))
if len(cell) == 2:
other = self.random.choice(cell)
while other == self:
other = self.random.choice(cell)
rulebook.linguistic_alignment(self, other)
rulebook.interaction_time(self, other)
rulebook.conversation_time(self, other)
rulebook.similarity_check(self, other)
elif len(cell) != 2:
rulebook.explore(self, movement_prob)
if self.status == "Active":
self.activity += 1
if movement_prob < float(random.random()):
self.move_skeptical()
else:
self.move_normal()
# defining the model class
class Model(Model):
# a model-class inheriting the properties of 'Model'
def __init__(self, N, width, height):
self.agents = N
self.grid = MultiGrid(width, height, True)
self.schedule = RandomActivation(self)
self.running = True
self.steps = 0
self.encounters = 0
self.mean_encounters = 0
# creating agents by iterating through n_agents
for i in range(self.agents):
# specify an agent as an object of class 'Agent' with unique_ID 'i'
agent = Agent(i, self)
# specify pitch measures for the agent as type 'float'
agent.iqr = float(iqr.iloc[i])
agent.speechrate = float(speechrate.iloc[i])
agent.mad = float(mad.iloc[i])
agent.pause = float(pause.iloc[i])
agent.diagnosis = float(diagnosis.iloc[i])
agent.symptom_severity = (agent.iqr + agent.mad + (1 - agent.speechrate) + agent.pause) / 4
# add the agent to the model schedule
self.schedule.add(agent)
# adding the agent to a random grid cell
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(agent, (x, y))
# add data-collector to the agent
self.datacollector = DataCollector(
agent_reporters = {"interactions": "unique_interactions",
"interaction_time": "interaction_time",
"conversation_time": "conversation_time",
"change_IQR": "change_iqr",
"change_MAD": "change_mad",
"change_Speechrate": "change_speechrate",
"change_PauseFreq": "change_pause",
"abs_change_IQR": "abs_change_iqr",
"abs_change_MAD": "abs_change_mad",
"abs_change_Speechrate": "abs_change_speechrate",
"abs_change_PauseFreq": "abs_change_pause",
"activity": "activity",
"diagnosis": "diagnosis"},
model_reporters = {"Encounters" : "encounters"})
def step(self):
# advance the model and collect data
self.datacollector.collect(self)
self.schedule.step()
self.encounters = 0
for agent in self.schedule.agents:
self.encounters += agent.unique_interactions
self.encounters = int(self.encounters / 2)
self.steps += 1
self.mean_encounters = round(float(self.encounters / self.steps), 3)
"""
model = Model(50, 16, 16)
for i in range(100):
model.step()
#print("Step: {}/99".format(i))
data = model.datacollector.get_agent_vars_dataframe()
data.to_csv("data.csv")
print("CSV written")
"""