-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
225 lines (183 loc) · 7.61 KB
/
main.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
import geopandas as gpd
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import osmnx as ox
import pandas as pd
import pickle
EPSILON = 1e-3
stations = [
(40.69978508399301, -73.95003262834037, 'Flushing Av'),
(40.70391698978126, -73.94732896174611, 'Lorimer St'),
(40.695327543912875, -73.94917432148505, 'Myrtle-Willoughby Avs'),
(40.71178967550891, -73.94041959156081, 'Grand St'),
(40.70765825814149, -73.93994752279038, 'Montrose Av'),
(40.700273117710545, -73.94114915238782, 'Flushing Av'),
(40.69711710313728, -73.93557015782827, 'Myrtle Av')
]
load_g = True
if not load_g:
gdf = gpd.read_file('./data/tl_2022_us_zcta520/tl_2022_us_zcta520.shp')
gdf = gdf[gdf['ZCTA5CE20'] == '11206']
shape = gdf.iloc[0].geometry
g = ox.graph_from_polygon(shape, network_type='drive', simplify=True)
g = g.subgraph(max(nx.strongly_connected_components(g), key=len)).copy()
g = nx.convert_node_labels_to_integers(g)
with open('./data/williamsburg.pkl', 'wb') as file:
pickle.dump(g, file)
else:
with open('./data/williamsburg.pkl', 'rb') as file:
g = pickle.load(file)
amts = [ox.nearest_nodes(g, lon, lat) for lat, lon, _ in stations]
class City:
def __init__(self, g, rho=2):
self.rho = rho
self.g = g
for _, data in self.g.nodes(data=True):
data['amt'] = False
data['inh'] = set()
data['dow_thr'] = 0
data['upk'] = None
data['cmt'] = None
data['pop_hist'] = []
data['cmt_hist'] = []
self.diam = nx.diameter(self.g, weight='length')
self.dist = dict(nx.all_pairs_dijkstra_path_length(self.g, weight='length'))
self.dist = pd.DataFrame.from_dict(self.dist).sort_index() / self.diam
self.dist = self.dist.to_numpy()
self.amts = None
self.amts_dist = None
self.agts = None
self.agt_dows = None
def set_amts(self, amts):
self.amts = amts
for u in self.amts:
data = self.g.nodes[u]
data['amt'] = True
data['inh'] = None
data['dow_thr'] = None
data['upk'] = None
data['cmt'] = None
data['pop_hist'] = None
data['cmt_hist'] = None
self.amts_dist = np.array([min(self.dist[u][v] for v in self.amts) for u in self.g.nodes()])
def set_agts(self, agts):
self.agts = agts
self.agt_dows = np.array([a.dow for a in self.agts])
def update(self):
for u, data in self.g.nodes(data=True):
if data['amt']:
continue
pop = len(data['inh'])
cmt = np.average(self.agt_dows, weights=[(1 - self.dist[u][a.u]) ** 2 for a in self.agts])
if pop > 0:
if pop < self.rho:
data['dow_thr'] = 0
else:
data['dow_thr'] = sorted([a.dow for a in self.g.nodes[u]['inh']])[-self.rho]
data['upk'] = True
else:
data['dow_thr'] = 0
data['upk'] = False
data['cmt'] = cmt
data['pop_hist'].append(pop)
data['cmt_hist'].append(cmt)
def plot(self, cmap='YlOrRd', figkey=None):
for u, data in self.g.nodes(data=True):
if not data['amt']:
data['dow'] = np.average(self.agt_dows, weights=[a.avg_probabilities[u] for a in self.agts])
data['dow'] = (data['dow'] - min(city.agt_dows)) / (max(city.agt_dows) - min(city.agt_dows))
data['pop'] = np.sum([a.avg_probabilities[u] for a in self.agts])
else:
data['dow'] = np.nan
data['pop'] = np.nan
no_agts = len(self.agts)
node_size = [no_agts / 10 * data['pop'] if not data['amt'] else no_agts / 2.5 for _, data in self.g.nodes(data=True)]
node_color = ox.plot.get_node_colors_by_attr(self.g, 'dow', start=0, stop=1, na_color='b', cmap=cmap)
fig, ax = plt.subplots(figsize=(9, 6))
cb = fig.colorbar(
plt.cm.ScalarMappable(cmap=plt.colormaps[cmap]), ax=ax, location='bottom', shrink=0.5, pad=0.05
)
cb.set_label('Expected Endowment', fontsize=14)
ox.plot_graph(self.g, ax=ax, bgcolor='w', node_color=node_color, node_size=node_size)
plt.show()
if figkey is not None:
plt.savefig('./figures/{0}.pdf'.format(figkey), bbox_inches='tight', format='pdf')
class Agent:
def __init__(self, i, dow, city, alpha=0.5):
self.i = i
self.dow = dow
self.city = city
self.alpha = alpha
self.weights = None
self.probabilities = None
self.tot_probabilities = None
self.avg_probabilities = None
self.u = None
self.reset()
def __hash__(self):
return hash(self.i)
def __eq__(self, other):
return self.i == other.i
def reset(self):
self.weights = np.array([1.0 if not data['amt'] else 0 for _, data in city.g.nodes(data=True)])
self.probabilities = np.array(self.weights / self.weights.sum())
self.tot_probabilities = self.probabilities.copy()
self.u = np.random.choice(self.city.g.nodes(), p=self.probabilities)
self.city.g.nodes[self.u]['inh'].add(self)
def act(self):
self.city.g.nodes[self.u]['inh'].remove(self)
self.u = np.random.choice(self.city.g.nodes(), p=self.probabilities)
self.city.g.nodes[self.u]['inh'].add(self)
def learn(self):
for u in self.city.g.nodes():
if not self.city.g.nodes[u]['amt']:
self.weights[u] *= (1 - EPSILON * self.cost(u))
self.probabilities = np.array(self.weights / self.weights.sum())
self.tot_probabilities += self.probabilities
def cost(self, u):
aff = int(self.dow >= self.city.g.nodes[u]['dow_thr'])
loc = np.exp(- (1 - self.alpha) * self.city.amts_dist[u])
upk = int(self.city.g.nodes[u]['upk'])
cmt = np.exp(- self.alpha * np.abs(self.dow - self.city.g.nodes[u]['cmt']))
c = 1 - aff * loc * upk * cmt
return c
rho_l = [1, 2, 4, 8]
alpha_l = [0.25, 0.75]
t_max_l = [5000, 10000, 15000, 20000]
tau = 0.5
run_experiments = True
plot_cities = True
cty_key = 'williamsburg'
n = g.number_of_nodes() - len(amts)
if run_experiments:
for rho in rho_l:
for alpha in alpha_l:
np.random.seed(0)
city = City(g, rho=rho)
city.set_amts(amts)
agt_dows = np.diff([1 - (1 - x) ** tau for x in np.linspace(0, 1, n + 1)])
agts = [Agent(i, dow, city, alpha=alpha) for i, dow in enumerate(agt_dows)]
city.set_agts(agts)
city.update()
for t in range(max(t_max_l)):
print('t: {0}'.format(t))
for a in agts:
a.act()
city.update()
for a in agts:
a.learn()
if t + 1 in t_max_l:
for a in city.agts:
a.avg_probabilities = a.tot_probabilities / (t + 1)
with open('./data/{0}_{1}_{2}_{3}.pkl'.format(cty_key, rho, alpha, t + 1), 'wb') as file:
pickle.dump(city, file)
if plot_cities:
for rho in rho_l:
for alpha in alpha_l:
for t_max in t_max_l:
with open('./data/{0}_{1}_{2}_{3}.pkl'.format(cty_key, rho, alpha, t_max), 'rb') as file:
city = pickle.load(file)
cmap = 'YlOrRd'
figkey = './{0}_{1}_{2}_{3}'.format(cty_key, rho, alpha, t_max)
city.plot(cmap=cmap, figkey=figkey)