-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_parking.py
264 lines (212 loc) · 9.54 KB
/
sample_parking.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
import plotly.graph_objects as go
import numpy as np
# import time
import random
import scipy.optimize
days = 20
sigma = 1
day = []
parking_demand = []
for i in range(days+1):
day.append(i)
parking_demand.append(0)
# Split into smaller time segments and add some randomness
scatters = []
while day[1] - day[0] > 1/(24*60):
tmp = []
tmp_day = []
for i in range(len(parking_demand) - 1):
tmp.append(parking_demand[i])
tmp.append(0.5 * (parking_demand[i]
+ parking_demand[i+1])
+ np.random.normal(0, sigma))
tmp_day.append(day[i])
tmp_day.append(0.5 * (day[i] + day[i+1]))
tmp.append(parking_demand[-1])
tmp_day.append(day[-1])
parking_demand = tmp
day = tmp_day
sigma *= 0.5
day = np.array(day)
parking_demand = np.array(parking_demand)
# Some random function for demand over time
parking_demand += 2 # Some constant demand
# Should add some randomness to the demand as well.
# Probably fractal noise would look neat...
# Some periodic demand change
parking_demand += 7.25 * (np.sin(day * 2 * np.pi) + 1)
# Cant have negative demand...
parking_demand = np.clip(parking_demand, 0, 1e9)
# Lets say that we only have X spots
num_spots = 12
# Cant have more parked than spots. People also come as whole numbers.
parking_used = np.clip(np.round(parking_demand), 0, num_spots)
# Sample observations
num_samples = min([100, len(day)])
sample_ind = sorted(random.sample(range(len(day)), num_samples))
sample_day = day[sample_ind]
sample_used = parking_used[sample_ind]
optimal_sample_used = parking_used[sample_ind]
# Add noise to the observations
# TODO: binomial distribution for missing
# Some binomial/poisson style distribution for duplicates
# Some random chance for false positives
for i, sample in enumerate(sample_used):
offset = random.choice([-2, -1, 0, 1, 2])
sample_used[i] += offset
# Make an estimate
def predict_demand(estimate, day):
return estimate[0] * np.ones(day.shape) + \
estimate[1] * (np.sin(day * 2 * np.pi) + 1)
def predict_used(estimate, day):
return np.clip(predict_demand(estimate, day), 0, num_spots)
def residual(estimate):
return np.clip(np.round(sample_used), 0, num_spots) \
- predict_used(estimate, sample_day)
estimate = scipy.optimize.least_squares(residual, [0, 1]).x
def optimal_residual(estimate):
return np.clip(np.round(optimal_sample_used), 0, num_spots) \
- predict_used(estimate, sample_day)
optimal_estimate = scipy.optimize.least_squares(optimal_residual, [0, 1]).x
print(estimate, optimal_estimate)
view_day = np.linspace(day[0], day[-1], int(1e4))
predicted_demand = predict_demand(estimate, view_day)
predicted_usage = predict_used(estimate, view_day)
optimal_predicted_demand = predict_demand(optimal_estimate, view_day)
optimal_predicted_usage = predict_used(optimal_estimate, view_day)
# Create scatters
parking_scatter = [go.Scatter(x=[day[0], day[-1]],
y=[num_spots, num_spots],
line_color='black', line_width=2,
name="Parking spots")]
demand_scatter = [go.Scatter(x=day,
y=parking_demand,
line_width=4,
line_color='Blue',
name="Spot demand")]
usage_scatter = [go.Scatter(x=day,
y=parking_used,
line_width=2,
line_color='Red',
name="Spot usage")]
lack_parking_scatter = [go.Scatter(x=day, y=np.clip(parking_demand,
num_spots,
np.inf),
fill='tonexty',
mode='lines',
opacity=0,
fillcolor='rgba(255,0,0,0.2)',
name='Angry citizens driving around')]
unused_spots_scatter = [go.Scatter(x=day, y=parking_used,
fill='tonexty',
mode='lines',
opacity=0,
fillcolor='rgba(0,0,255,0.2)',
name='Unused space = wasted space')]
optimal_obs_scatter = [go.Scatter(x=sample_day, y=optimal_sample_used,
mode='markers',
marker_line_width=2,
marker_size=8, name="Observation points")]
optimal_demand_estimate_scatter = [
go.Scatter(x=view_day, y=optimal_predicted_demand,
line_color='magenta',
line_width=3,
name="Estimated demand")]
optimal_usage_estimate_scatter = [
go.Scatter(x=view_day, y=np.round(optimal_predicted_usage),
line_width=3,
line_color='lime',
name="Estimated usage")]
obs_scatter = [go.Scatter(x=sample_day,
y=sample_used,
mode='markers',
marker_symbol='star',
marker_line_width=2,
marker_size=8,
name="Noisy observation points")]
demand_estimate_scatter = [
go.Scatter(x=view_day,
y=predicted_demand,
line_color='blue',
line_width=6,
name="Noisy measurements -> Estimated demand")]
usage_estimate_scatter = [
go.Scatter(x=view_day,
y=np.round(predicted_usage),
line_color='red',
line_width=6,
name="Noisy measurements -> Estimated usage")]
# Set up drawing
layout = go.Layout(title="parking", width=1800, height=1000,
yaxis_title="Number of cars",
margin=dict(l=80, r=80, t=100, b=99),
xaxis_title="Day", xaxis_dtick=1, yaxis_dtick=1,
xaxis_range=[0, days], yaxis_range=[-0.1, 18.5],
showlegend=True)
layout.update(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01))
# Demand
layout.title.text = "Parking spot demand over time"
go.Figure(data=demand_scatter,
layout=layout).write_image("parking_spot_demand.png")
# Demand + usage
layout.title.text = "Parking spot demand and usage over time"
go.Figure(data=parking_scatter+demand_scatter+usage_scatter,
layout=layout).write_image("parking_spot_usage.png")
# Demand > spots
layout.title.text = "Lack of parking spots"
go.Figure(data=parking_scatter + lack_parking_scatter +
demand_scatter + usage_scatter,
layout=layout).write_image("parking_lack_spots.png")
# Demand < spots
layout.title.text = "Wasted parking spot space over time"
go.Figure(data=parking_scatter + unused_spots_scatter +
demand_scatter + usage_scatter,
layout=layout).write_image("parking_unused_spots.png")
# optimal observations + usage
layout.title.text = "Parking usage observations over time"
go.Figure(data=usage_scatter+optimal_obs_scatter,
layout=layout).write_image("parking_optimal_observations.png")
# Optimal obs
layout.title.text = "Parking observations over time"
go.Figure(data=optimal_obs_scatter,
layout=layout).write_image("parking_optimal_observations_raw.png")
# Obs
layout.title.text = "Creating Noisy parking observations"
go.Figure(data=obs_scatter + optimal_obs_scatter,
layout=layout).write_image("prkng_noisy_vs_optimal_observations_raw.png")
# Obs
layout.title.text = "Noisy parking observations"
go.Figure(data=obs_scatter,
layout=layout).write_image("parking_observations_raw.png")
# optimal predictions + observations + demand + usage
layout.title.text = "Parking spot usage estimate"
go.Figure(data=usage_scatter + optimal_obs_scatter +
optimal_usage_estimate_scatter,
layout=layout).write_image("parking_optimal_usage_estimated.png")
# optimal predictions + observations + demand + usage
layout.title.text = "Parking spot demand estimate"
go.Figure(data=demand_scatter + optimal_demand_estimate_scatter,
layout=layout).write_image("parking_optimal_demand_estimated.png")
# optimal predictions + observations + demand + usage
layout.title.text = "Parking spot noisy vs optimal observations usage estimate"
go.Figure(data=usage_estimate_scatter + optimal_usage_estimate_scatter,
layout=layout).write_image("parking_noisy_vs_optimal_usage_estimated.png")
# optimal predictions + observations + demand + usage
layout.title.text = "Parking spot noisy vs optimal demand estimate"
go.Figure(data=demand_estimate_scatter + optimal_demand_estimate_scatter,
layout=layout).write_image("prkng_noisy_vs_optimal_demand_estimated.png")
# Observations + demand + usage
# layout.title.text = "Parking usage noisy observations over time"
# go.Figure(data=scatters+obs_scatter,
# layout=layout).write_image("parking_observations.png")
# noisy + Predictions + observations + demand + usage
# layout.title.text = "Parking spot demand and usage estimates over time"
# go.Figure(data=scatters+estimates,
# layout=layout).write_image("parking_estimated.png")
# Noisy vs optimal estimates
# layout.title.text = "Parking spot demand and usage estimates over time"
# go.Figure(data=optimal_estimates+estimates,
# layout=layout).write_image("parking_estimated_noisy_vs_optimal.png")
# Layout for the plot
# fig.show()
# fig.save_html("sample_parking.html")