-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.py
289 lines (190 loc) · 9.56 KB
/
Simulator.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
# Simulator
## This script is imported to run the simulator in different notebooks
## Import Packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy import random
## Load Data
pay_prob_df = pd.read_csv('Data/probs.csv', names = ['PAY', 'PROB'], header = 0)
## Create the results arrays
def create_users_df(num_riders):
users = {'USER_ID': np.arange(1, num_riders+1),
'ACTIVE': np.repeat(0, num_riders),
'REQ_M1': np.repeat(0, num_riders),
'REQ_M2': np.repeat(0, num_riders),
'REQ_M3': np.repeat(0, num_riders),
'REQ_M4': np.repeat(0, num_riders),
'REQ_M5': np.repeat(0, num_riders),
'REQ_M6': np.repeat(0, num_riders),
'REQ_M7': np.repeat(0, num_riders),
'REQ_M8': np.repeat(0, num_riders),
'REQ_M9': np.repeat(0, num_riders),
'REQ_M10': np.repeat(0, num_riders),
'REQ_M11': np.repeat(0, num_riders),
'REQ_M12': np.repeat(0, num_riders),
'RATE_M1': np.repeat(0, num_riders),
'RATE_M2': np.repeat(0, num_riders),
'RATE_M3': np.repeat(0, num_riders),
'RATE_M4': np.repeat(0, num_riders),
'RATE_M5': np.repeat(0, num_riders),
'RATE_M6': np.repeat(0, num_riders),
'RATE_M7': np.repeat(0, num_riders),
'RATE_M8': np.repeat(0, num_riders),
'RATE_M9': np.repeat(0, num_riders),
'RATE_M10': np.repeat(0, num_riders),
'RATE_M11': np.repeat(0, num_riders),
'RATE_M12': np.repeat(0, num_riders),
'RATE_M13': np.repeat(0, num_riders)
}
users_df = pd.DataFrame(users)
return(users_df)
def create_rides_df():
rides = {'USER_ID': [],
'MONTH': [],
'RIDE_ID': [],
'ACCEPTED': [],
'PAY': [],
'PROFIT': []
}
rides_df = pd.DataFrame(rides)
return(rides_df)
## The process
### Generate new active users
def generate_new_active_users(month, users_df):
lower = (month - 1) * 1000
upper = (month) * 1000
# Flag new users as active
users_df.ACTIVE[lower:upper] = 1
# This is the rate at which the new users request rides
users_df["RATE_M"+f"{month}"][lower:upper] = 1
return(users_df)
### Find active users
def find_active_users(users_df):
# These users are active and will generate requests
active_users = users_df.index[users_df.ACTIVE == 1]
# This is the number of active users
num_active_users = len(active_users)
return(active_users, num_active_users, users_df)
### Generate requests
def generate_requests(active_users, num_active_users, month, users_df):
# This is the rate at which active users request new rides
rate = users_df["RATE_M"+f"{month}"][active_users]
# These are the number of requests per user this month
users_df["REQ_M"+f"{month}"][active_users] = random.poisson(lam=rate, size = num_active_users)
# These are the active users with 0 requests
non_returning_users = active_users[users_df["REQ_M"+f"{month}"][active_users] == 0]
# Set these users to never active again
users_df.ACTIVE[non_returning_users] = -1
# These are the users with requests greater than 0
users_requesting = users_df.index[users_df["REQ_M"+f"{month}"] > 0]
# These are the requests that we need to generate acceptances for
requests_oi = users_df["REQ_M"+f"{month}"][users_requesting]
return(users_requesting, requests_oi, users_df)
### Generate acceptances: Fixed rate
def generate_acceptances_individually(users_requesting, pay, pay_probs, month, users_df, rides_df):
# Extract the number of rides of each user
ids_and_requests = users_df.loc[users_requesting, ['USER_ID', "REQ_M"+f"{month}"]]
# Calculate the total number of rides requested
num_rides = sum(ids_and_requests["REQ_M"+f"{month}"])
# Set up an array to store the results
rides_this_month = np.zeros((num_rides, len(rides_df.columns)))
# A count of the number of rides processed
ride_row = 0
# Look up the acceptance probability of the Pay
prob_acc = pay_probs.PROB[pay_probs.PAY == pay].values[0]
# Loop through each ride and generate if it accepted or not
# and how the Pay, and store the results in the temp array
for row in range(0, len(ids_and_requests)):
user_id_j = ids_and_requests.USER_ID.iloc[row]
requests_j = ids_and_requests["REQ_M"+f"{month}"].iloc[row]
total_acc_j = 0
for ride in range(1, requests_j+1):
acc_j_r = random.random() < prob_acc
rides_this_month[ride_row, :] = [user_id_j, month, ride, acc_j_r, pay, 30-pay]
ride_row = ride_row + 1
total_acc_j = total_acc_j + acc_j_r
users_df["RATE_M"+f"{month+1}"][users_requesting[row]] = total_acc_j
# These are the users that had none of their ride requests accepted this month
non_returning_users_2 = users_requesting[users_df["RATE_M"+f"{month+1}"][users_requesting] == 0]
# Set these users to never active again
users_df.ACTIVE[non_returning_users_2] = -1
# Convert the temp ride array to a data frame
rides_this_month_df = pd.DataFrame(rides_this_month, columns=rides_df.columns)
# Append the temp array to the rides results df
rides_df = pd.concat([rides_df, rides_this_month_df], ignore_index=True)
return(users_df, rides_df)
### Generate acceptances: Adaptive rate
def generate_acceptances_adaptive(users_requesting, pays, probs, pay_probs, month, users_df, rides_df):
# Extract the number of rides of each user
ids_and_requests = users_df.loc[users_requesting, ['USER_ID', "REQ_M"+f"{month}"]]
# Calculate the total number of rides requested
num_rides = sum(ids_and_requests["REQ_M"+f"{month}"])
# Set up an array to store the results
rides_this_month = np.zeros((num_rides, len(rides_df.columns)))
# A count of the number of rides processed
ride_row = 0
# Loop through each ride and generate if it accepted or not
# and how the Pay, and store the results in the temp array
for row in range(0, len(ids_and_requests)):
user_id_j = ids_and_requests.USER_ID.iloc[row]
requests_j = ids_and_requests["REQ_M"+f"{month}"].iloc[row]
total_acc_j = 0
for ride in range(1, requests_j+1):
if total_acc_j > 0:
pay = pays[1]
prob_acc = probs[1]
else:
pay = pays[0]
prob_acc = probs[0]
acc_j_r = random.random() < prob_acc
rides_this_month[ride_row, :] = [user_id_j, month, ride, acc_j_r, pay, 30-pay]
ride_row = ride_row + 1
total_acc_j = total_acc_j + acc_j_r
users_df["RATE_M"+f"{month+1}"][users_requesting[row]] = total_acc_j
# These are the users that had none of their ride requests accepted this month
non_returning_users_2 = users_requesting[users_df["RATE_M"+f"{month+1}"][users_requesting] == 0]
# Set these users to never active again
users_df.ACTIVE[non_returning_users_2] = -1
# Convert the temp ride array to a data frame
rides_this_month_df = pd.DataFrame(rides_this_month, columns=rides_df.columns)
# Append the temp array to the rides results df
rides_df = pd.concat([rides_df, rides_this_month_df], ignore_index=True)
return(users_df, rides_df)
### Generate acceptances: Time dependent rate
def generate_acceptances_half(users_requesting, pays, probs, pay_probs, month_change, month, users_df, rides_df):
# Extract the number of rides of each user
ids_and_requests = users_df.loc[users_requesting, ['USER_ID', "REQ_M"+f"{month}"]]
# Calculate the total number of rides requested
num_rides = sum(ids_and_requests["REQ_M"+f"{month}"])
# Set up an array to store the results
rides_this_month = np.zeros((num_rides, len(rides_df.columns)))
# A count of the number of rides processed
ride_row = 0
# Loop through each ride and generate if it accepted or not
# and how the Pay, and store the results in the temp array
for row in range(0, len(ids_and_requests)):
user_id_j = ids_and_requests.USER_ID.iloc[row]
requests_j = ids_and_requests["REQ_M"+f"{month}"].iloc[row]
total_acc_j = 0
for ride in range(1, requests_j+1):
if month > month_change:
pay = pays[1]
prob_acc = probs[1]
else:
pay = pays[0]
prob_acc = probs[0]
acc_j_r = random.random() < prob_acc
rides_this_month[ride_row, :] = [user_id_j, month, ride, acc_j_r, pay, 30-pay]
ride_row = ride_row + 1
total_acc_j = total_acc_j + acc_j_r
users_df["RATE_M"+f"{month+1}"][users_requesting[row]] = total_acc_j
# These are the users that had none of their ride requests accepted this month
non_returning_users_2 = users_requesting[users_df["RATE_M"+f"{month+1}"][users_requesting] == 0]
# Set these users to never active again
users_df.ACTIVE[non_returning_users_2] = -1
# Convert the temp ride array to a data frame
rides_this_month_df = pd.DataFrame(rides_this_month, columns=rides_df.columns)
# Append the temp array to the rides results df
rides_df = pd.concat([rides_df, rides_this_month_df], ignore_index=True)
return(users_df, rides_df)