forked from yashpatel5400/SexualEqualityABM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SexMinDepressionSimulation.py
364 lines (305 loc) · 15.6 KB
/
SexMinDepressionSimulation.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
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
#####################################################################
# Name: Yash Patel #
# File: SexMinDepressionSimulation.py #
# Description: Contains all the methods pertinent to producing the #
# simulation for modelling the relation between sexual minorities #
# and depression (SMD simulation) #
#####################################################################
import sys
import os
import csv
import random,itertools
from copy import deepcopy
import numpy as np
from NetworkBase import NetworkBase
from ERNetwork import ERNetwork
from ASFNetwork import ASFNetwork
from SWNetwork import SWNetwork
from SMDSensitivity import *
from Hypothetical import *
from Verification import *
import matplotlib.pyplot as plt
from operator import itemgetter
try:
import networkx as nx
except ImportError:
raise ImportError("You must install NetworkX:\
(http://networkx.lanl.gov/) for SE simulation")
class SMDSimulationModel:
#################################################################
# Given the type of network, the simulation time span, and count#
# of agents in the network, a simulation is created and run for #
# testing depression as a function of minority prevalence. Also #
# have control on the impact ratings of each of the parameters: #
# defaults have been provided #
#################################################################
def __init__(self, networkType='ER', timeSpan=10, numAgents=10,
percentMinority=.5, supportDepressionImpact=1.25,
concealDiscriminateImpact=5.0, discriminateConcealImpact=1.0,
discriminateDepressionImpact=3.0, concealDepressionImpact=2.0):
if not self.SMDModel_verifySE(networkType, timeSpan, numAgents):
return None
self.networkType = networkType
self.timeSpan = timeSpan
self.numAgents = numAgents
self.percentMinority = percentMinority
self.supportDepressionImpact = supportDepressionImpact
self.concealDiscriminateImpact = concealDiscriminateImpact
self.discriminateConcealImpact = discriminateConcealImpact
self.discriminateDepressionImpact = discriminateDepressionImpact
self.concealDepressionImpact = concealDepressionImpact
self.SMDModel_setNetwork()
#################################################################
# Based on the specified value of the network type, generates #
# and sets the network accordingly. Sets the initial value of #
# simulation to those specified in the parameters (attitude_0 #
# corresponds to initial value of attitude, etc...) #
#################################################################
def SMDModel_setNetwork(self, attitude_0=None,
support_0=None, discrimination_0=None, conceal_0=None,
depression_0=None, policyScore_0=None):
if self.networkType == 'ER':
self.network = ERNetwork(self.numAgents,
self.percentMinority, self.timeSpan, 0.50, attitude_0,
support_0, discrimination_0, conceal_0,
depression_0, policyScore_0)
elif self.networkType == 'SW':
self.network = SWNetwork(self.numAgents,
self.percentMinority, self.timeSpan, 10, 0.50, attitude_0,
support_0, discrimination_0, conceal_0,
depression_0, policyScore_0)
else:
self.network = ASFNetwork(self.numAgents,
self.percentMinority, self.timeSpan, 3, 4, attitude_0,
support_0, discrimination_0, conceal_0,
depression_0, policyScore_0)
#################################################################
# Given parameters for initializing the simulation, ensures they#
# are legal #
#################################################################
def SMDModel_verifySE(self, networkType, timeSpan, numAgents):
if not Verification_verifyStr(networkType, "Network type"):
return False
if networkType != 'SW' and networkType != 'ASF'\
and networkType != 'ER':
sys.stderr.write("Network type must either SW, ASF, or ER")
return False
if not Verification_verifyInt(timeSpan, "Time span"):
return False
if not Verification_verifyInt(numAgents, "Number of agents"):
return False
return True
#################################################################
# Writes the header of the CSV file to be given as output in the#
# specified file #
#################################################################
def SMDModel_writeSimulationHeader(self, resultsFile):
if resultsFile is not None:
columns = ['time', 'agentID', 'attitude',
'isMinority', 'discrimination', 'support', 'probConceal',
'isConcealed', 'currentDepression', 'isDepressed',
'policy points']
with open(resultsFile, 'w') as f:
writer = csv.writer(f)
writer.writerow(columns)
#################################################################
# Writes the current data/parameters corresponding to each agent#
# in the network at the current time step, given the current #
# time in the simulation and the file to be written to #
#################################################################
def SMDModel_writeSimulationData(self, time, resultsFile):
if resultsFile is not None:
with open(resultsFile, 'a') as f:
writer = csv.writer(f)
Agents = self.network.networkBase.Agents
for agent in Agents:
curAgent = Agents[agent]
row = [time, curAgent.agentID, curAgent.attitude,
curAgent.isMinority, curAgent.discrimination,
curAgent.support, curAgent.probConceal,
curAgent.isConcealed, curAgent.currentDepression,
curAgent.isDepressed, curAgent.network.policyScore]
writer.writerow(row)
#################################################################
# Creates a bar graph comparing two specified values (val1,val2)#
# outputting result into file with fileName. Uses label, title #
# for producing the graph #
#################################################################
def SMDModel_createBarResults(self, val1, val2, fileName, label,
title):
N = len(val1)
ind = np.arange(N) # the x locations for the groups
width = 0.25 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, val1, width, color='r')
rects2 = ax.bar(ind+width, val2, width, color='b')
# add some text for labels, title and axes ticks
ax.set_ylabel("Agent ID")
ax.set_ylabel(label)
ax.set_title(title)
ax.set_xticks(ind+width)
labels = []
for i in range(0, N):
curWord = str(i + 1)
labels.append(curWord)
ax.set_xticklabels(labels)
ax.legend( (rects1[0], rects2[0]), ('Before', 'After') )
plt.savefig("Results\\TimeResults\\{}.png".format(fileName))
plt.close()
#################################################################
# Creates a bar graph comparing two specified values (val1,val2)#
# (for the mean value of depression before/after) #
#################################################################
def SMDModel_createSingleBars(self, xArray, yArray, xLabel, yLabel):
N = len(xArray)
ind = np.arange(N) # the x locations for the groups
width = 0.5 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, yArray, width, color='b')
# add some text for labels, title and axes ticks
ax.set_xlabel(yLabel)
ax.set_ylabel(xLabel)
ax.set_title("{} vs. {}".format(xLabel, yLabel))
ax.set_xticks(ind + width/2)
ax.set_xticklabels(xArray)
plt.savefig("Results\\TimeResults\\{}vs{}.png"\
.format(xLabel, yLabel))
plt.close()
#################################################################
# Runs simulation over the desired timespan and produces/outputs#
# results in CSV file specified along with displaying graphics #
#################################################################
def SMDModel_runSimulation(self, resultsFile):
self.SMDModel_writeSimulationHeader(resultsFile)
# Converts from years to "ticks" (represent 2 week span)
numTicks = self.timeSpan * 26
pos = nx.random_layout(self.network.G)
beforeDepressLevels = []
afterDepressLevels = []
timeLabels = ["Before", "After"]
curNetwork = self.network.networkBase
agents = curNetwork.NetworkBase_getMinorityNodes()
for agent in agents:
if agent.isMinority:
beforeDepressLevels.append(agent.currentDepression)
preAvgDepression = curNetwork.\
NetworkBase_getMinorityDepressionAvg()
for i in range(0, numTicks):
if i % 10 == 0:
self.SMDModel_writeSimulationData(i, resultsFile)
print("Plotting time step {}".format(i))
self.network.networkBase.\
NetworkBase_visualizeNetwork(False, i, pos)
# Updates the agents in the network base and copies those
# to the network
self.network.networkBase.NetworkBase_timeStep(i,
self.supportDepressionImpact, self.concealDiscriminateImpact,
self.discriminateConcealImpact, self.discriminateDepressionImpact,
self.concealDepressionImpact)
self.network.Agents = self.network.networkBase.Agents
for agent in agents:
if agent.isMinority:
afterDepressLevels.append(agent.currentDepression)
postAvgDepression = curNetwork.\
NetworkBase_getMinorityDepressionAvg()
avgDepressLevels = [preAvgDepression, postAvgDepression]
self.SMDModel_createBarResults(beforeDepressLevels,
afterDepressLevels, "depressBar",
"Depression", "Individual Depression Levels Chart")
self.SMDModel_createSingleBars(timeLabels, avgDepressLevels,
"Average_Depression_Level", "Time")
#################################################################
# Runs simulation over the desired timespan without producing #
# visible output: used for sensitivity analysis. Each of the #
# parameters allows manual sets the corresponding attribute (for#
# all of the agents) to the specified value. If none is given, #
# (should be the case if not running sensitivity/hypotheticals) #
# agents follow given default update behavior for the attribute #
#################################################################
def SMDMOdel_runConstSimulation(self, attitude=None,
support=None, discrimination=None, conceal=None,
depression=None, enforcedPolicy=None):
# Converts from years to "ticks" (represent 2 week span)
numTicks = self.timeSpan * 26
for i in range(0, numTicks):
# Updates the agents in the network base and copies those
# to the network
self.network.networkBase.NetworkBase_timeStep(i,
self.supportDepressionImpact, self.concealDiscriminateImpact,
self.discriminateConcealImpact, self.discriminateDepressionImpact,
self.concealDepressionImpact, attitude, support,
discrimination, conceal, depression, enforcedPolicy)
self.network.Agents = self.network.networkBase.Agents
#################################################################
# Runs simulation over the desired timespan without producing #
# visible output: used for sensitivity analysis. Each of the #
# parameters allows manual sets for the initial value of the #
# parameter in the simulation. Simulation then runs as normal. #
# If none is given, agents follow given default update behavior #
# for the attribute #
#################################################################
def SMDModel_runStreamlineSimulation(self, attitude_0=None,
support_0=None, discrimination_0=None, conceal_0=None,
depression_0=None, policyScore_0=None):
# Converts from years to "ticks" (represent 2 week span)
numTicks = self.timeSpan * 26
self.SMDModel_setNetwork(attitude_0, support_0, discrimination_0,
conceal_0, depression_0, policyScore_0)
for i in range(0, numTicks):
# Updates the agents in the network base and copies those
# to the network
self.network.networkBase.NetworkBase_timeStep(i,
self.supportDepressionImpact, self.concealDiscriminateImpact,
self.discriminateConcealImpact, self.discriminateDepressionImpact,
self.concealDepressionImpact)
self.network.Agents = self.network.networkBase.Agents
#####################################################################
# Given the paramters of the simulation (upon being prompted on) #
# command line, runs simulation, outputting a CSV with each time #
# step and a graphical display corresponding to the final iteration #
#####################################################################
def main():
# Used for performing sensitivity analyses
checkSensitivity = True
showOdd = False
showImpact = False
showRegression = False
showSensitivity = True
# Only runs streamlined simulation (no graphical/textual output)
onlyStreamlined = True
# Conducts the hypothetical tests that are of interest in study
performHypothetical = False
# ER, SW, or ASF
networkType = "ER"
timeSpan = 5
numAgents = 250
percentMinority = .10
# The following denote "impact constants" for which we have adopted
# the naming convention of firstSecondImpact to denote the impact of
# first on second
supportDepressionImpact = 4.75
concealDiscriminateImpact = 1.25
discriminateConcealImpact = 1.025
discriminateDepressionImpact = .65
concealDepressionImpact = 1.075
resultsFile = "Results\\TimeResults\\results.csv"
simulationModel = SMDSimulationModel(networkType, timeSpan, numAgents,
percentMinority, supportDepressionImpact, concealDiscriminateImpact,
discriminateConcealImpact, discriminateDepressionImpact,
concealDepressionImpact)
original = deepcopy(simulationModel)
if onlyStreamlined:
simulationModel.SMDModel_runStreamlineSimulation()
else:
simulationModel.SMDModel_runSimulation(resultsFile)
if checkSensitivity:
Sensitivity_sensitivitySimulation(percentMinority,
supportDepressionImpact, concealDiscriminateImpact,
discriminateConcealImpact, discriminateDepressionImpact,
concealDepressionImpact, original, simulationModel,
showOdd, showImpact, showRegression, showSensitivity)
if performHypothetical:
Hypothetical_findEffectiveness(original, simulationModel)
print("Terminating simulation...")
if __name__ == "__main__":
main()