-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
292 lines (237 loc) · 10.4 KB
/
server.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
# !/usr/bin/python
"""
This document runs the server and helps visualize the agents.
"""
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule, TextElement
# from model import *
# from agents import *
from excel_export_summary import *
from excel_export_summary_2014 import *
from excel_export_household import *
from excel_export_household_2014 import *
from SimpleContinuousModule import SimpleCanvas
import matplotlib.pyplot as plt
def agent_draw(agent):
draw = {"r": 3, # radius in pixels, for circles
"w": 0.01, # width in % of drawing window, for rectangles
"h": 0.01, # height in % of drawing window, for rectangles
"Filled": "true"}
# Household blue, individual green, land parcel yellow, PES policy unassigned
# Will add legend later
# admin_village is not actually important to the model; it is a placeholder attribute to identify households
# so they can be drawn
# see model.py, line 179
try:
if agent.admin_village == 1:
draw["Shape"] = "rect"
draw["Color"] = "blue"
draw["Layer"] = 0
except:
pass
# black if non-GTGP land parcel, yellow if GTGP
try:
if agent.gtgp_enrolled == 0:
draw["Shape"] = "circle"
draw["Color"] = "black"
draw["Layer"] = 1
if agent.gtgp_enrolled == 1:
draw["Shape"] = "circle"
draw["Color"] = "gold"
draw["Layer"] = 2
except:
pass
return draw
agent_canvas = SimpleCanvas(agent_draw, 700, 700) # create simulation window
# create line graph
chart = ChartModule([{"Label": 'Average Number of Migrants',
"Color": "Black"}], canvas_height = 250, canvas_width = 700,
data_collector_name = 'datacollector')
chart2 = ChartModule([{"Label": 'Total # of Marriages in the Reserve',
"Color": "Black"}], canvas_height = 250, canvas_width = 700,
data_collector_name = 'datacollector2')
model = ABM(100, 10, 10)
erase_summary()
erase_household()
erase_household_2014()
erase_summary_2014()
initialize_household()
initialize_household_2014()
for i in range(81): # sets up model to run for 80 steps
model.step()
save_summary(i, show_cumulative_mig(model), show_instant_mig(model), show_instant_mig_per_hh(model),
show_cumulative_re_mig(model), show_instant_re_mig(model), \
show_instant_re_mig_per_hh(model), show_marriages(model), show_births(model), len(death_list),
show_num_labor(model), show_hh_size(model), show_income(model),
show_pop(model), show_gtgp_per_hh(model), show_non_gtgp_per_hh(model))
save_summary_2014(i, show_cumulative_mig_2014(model), show_instant_mig_2014(model),
show_instant_mig_per_hh_2014(model), show_cumulative_re_mig_2014(model),
show_instant_re_mig_2014(model), show_instant_re_mig_per_hh_2014(model),
show_marriages_2014(model), show_births_2014(model), len(death_list_2014),
show_num_labor_2014(model), show_hh_size_2014(model), show_income_2014(model),
show_pop_2014(model), show_gtgp_per_hh_2014(model), show_non_gtgp_per_hh_2014(model))
# 1 at the end of the variable name means that it's per year instead of accumulative
cumulative_out_mig_2016 = model.datacollector.get_model_vars_dataframe() # see model.py
instant_out_mig_2016 = model.datacollector2.get_model_vars_dataframe() # see model.py
cumulative_re_mig_2016 = model.datacollector3.get_model_vars_dataframe()
instant_re_mig_2016 = model.datacollector4.get_model_vars_dataframe()
marriage_2016 = model.datacollector5.get_model_vars_dataframe()
birth_2016 = model.datacollector6.get_model_vars_dataframe()
death_2016 = model.datacollector7.get_model_vars_dataframe()
pop_2016 = model.datacollector8.get_model_vars_dataframe()
gtgp_2016 = model.datacollector9.get_model_vars_dataframe()
non_gtgp_2016 = model.datacollector10.get_model_vars_dataframe()
hh_size_2016 = model.datacollector11.get_model_vars_dataframe()
num_labor_2016 = model.datacollector12.get_model_vars_dataframe()
income_2016 = model.datacollector13.get_model_vars_dataframe()
cumulative_mig_2014 = model.datacollector14.get_model_vars_dataframe()
instant_mig_2014 = model.datacollector15.get_model_vars_dataframe()
cumulative_re_mig_2014 = model.datacollector16.get_model_vars_dataframe()
instant_re_mig_2014 = model.datacollector17.get_model_vars_dataframe()
marriage_2014 = model.datacollector18.get_model_vars_dataframe()
birth_2014 = model.datacollector19.get_model_vars_dataframe()
death_2014 = model.datacollector20.get_model_vars_dataframe()
pop_2014 = model.datacollector21.get_model_vars_dataframe()
gtgp_2014 = model.datacollector22.get_model_vars_dataframe()
non_gtgp_2014 = model.datacollector23.get_model_vars_dataframe()
hh_size_2014 = model.datacollector24.get_model_vars_dataframe()
num_labor_2014 = model.datacollector25.get_model_vars_dataframe()
income_2014 = model.datacollector26.get_model_vars_dataframe()
instant_out_mig_2016.plot()
plt.title('Instant # of Out-Migrants in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Migrants')
cumulative_re_mig_2016.plot()
plt.title('Cumulative # of Re-migrants in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Re-migrants')
instant_re_mig_2016.plot()
plt.title('Instant # of Re-migrants in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Re-migrants')
# household_out_mig_2016.plot()
# plt.title('Average Number of Out-Migrants Per Household (2016 data)')
# plt.xlabel('Years (Steps)')
# plt.ylabel('# of Migrants')
# household_re_mig_2016.plot()
# plt.title('Average Number of Re-Migrants Per Household (2016 data)')
# plt.xlabel('Years (Steps)')
# plt.ylabel('# of Re-migrants')
marriage_2016.plot()
plt.title('Total # of Marriages in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Marriages')
birth_2016.plot()
plt.title('Total # of Births in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Births')
death_2016.plot()
plt.title('Total # of Deaths in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Deaths')
pop_2016.plot()
plt.title('Total Population in the Reserve (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Population')
gtgp_2016.plot()
plt.title('Average # of GTGP Parcels Per Household (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('GTGP Parcels')
non_gtgp_2016.plot()
plt.title('Average # of Non-GTGP Parcels Per Household (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Non-GTGP Parcels')
hh_size_2016.plot()
plt.title('Average Household Size (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of People in Household')
num_labor_2016.plot()
plt.title('Average # of Laborers in Household (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Laborers in Household')
income_2016.plot()
plt.title('Average Yearly Household Income (2016 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Income (Yuan)')
# 2014
cumulative_mig_2014.plot()
plt.title('Cumulative # of Out-Migrants in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Migrants')
instant_mig_2014.plot()
plt.title('Instant # of Out-Migrants in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Migrants')
cumulative_re_mig_2014.plot()
plt.title('Cumulative # of Re-migrants in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Re-migrants')
instant_re_mig_2014.plot()
plt.title('Instant # of Re-Migrants in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Migrants')
# household_mig_2014.plot()
# plt.title('Average Number of Out-Migrants Per Household (2014 data)')
# plt.xlabel('Years (Steps)')
# plt.ylabel('# of Migrants')
# household_re_mig_2014.plot()
# plt.title('Average Number of Re-Migrants Per Household (2014 data)')
# plt.xlabel('Years (Steps)')
# plt.ylabel('# of Re-migrants')
marriage_2014.plot()
plt.title('Total # of Marriages in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Marriages')
birth_2014.plot()
plt.title('Total # of Births in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Births')
death_2014.plot()
plt.title('Total # of Deaths in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Deaths')
pop_2014.plot()
plt.title('Total Population in the Reserve (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Population')
gtgp_2014.plot()
plt.title('Average # of GTGP Parcels Per Household (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('GTGP Parcels')
non_gtgp_2014.plot()
plt.title('Average # of Non-GTGP Parcels Per Household (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Non-GTGP Parcels')
hh_size_2014.plot()
plt.title('Average Household Size (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of People in Household')
num_labor_2014.plot()
plt.title('Average # of Laborers in Household (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('# of Laborers in Household')
income_2014.plot()
plt.title('Average Yearly Household Income (2014 data)')
plt.xlabel('Years (Steps)')
plt.ylabel('Income (Yuan)')
plt.show() # comment or uncomment this line to see or hide the graphs
# The text elements below update with every step.
class MapLegend(TextElement):
def __init__(self):
pass
def render(self, model):
# image created on MS Paint and uploaded to internet, but also featured in this folder for reference
return ("<img src = 'http://i64.tinypic.com/f41pwi.png'>" + "<br>"
+ "Non-GTGP Land Parcels: " + str(len(nongtgplist))
+ " | GTGP Land Parcels: " + str(len(gtgplist))
+ " | Total Land Parcels: " + str(len(nongtgplist) + len(gtgplist))
+ " | Step: " + str(i)
+ "<br><br></h3>")
# text0 = MapLegend()
#
# server = ModularServer(ABM, [agent_canvas, text0],
# "GTGP Enrollment of Land Over Time", 100, 10, 10)
#
# if __name__ == "__main__":
# server.port = 8521 # default
# server.launch() # actual run line