-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassengers.py
executable file
·329 lines (271 loc) · 11.8 KB
/
passengers.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
from itertools import count
import numpy as np
class Passengers(object):
"""Passenger Object"""
__num_of_passengers = count(0)
__beta = 0.0002
def __init__(self, cart_stop, arrival_time=0,
current_state='waiting at the stop', passengerType='single', destination=None):
self._passengerID = next(self.__num_of_passengers)
self._passengerType = passengerType # couple, triple, family
self._arrival_time = arrival_time
self._getin_time = None
self._getoff_time = None
self._waiting_duration = 0
self._current_state = current_state # ['going home', 'in cart']
self._cart_stop = cart_stop
self._destination = destination
if cart_stop is not None:
self._beta = self.cart_stop._beta # for waiting cost, discount factor
else:
self._beta = 0.0002
self._waiting_cost = None
@property
def origin(self):
return self._cart_stop.stopID
@property
def destination(self):
return self._destination
@destination.setter
def destination(self, value):
self._destination = value
@property
def passengerType(self):
return self._passengerType
@passengerType.setter
def passengerType(self, value):
self._passengerType = value
@property
def passengerID(self):
return self._passengerID
@property
def cart_stop(self):
return self._cart_stop
@property
def getin_time(self):
return self._getin_time
@getin_time.setter
def getin_time(self, value):
self._getin_time = value
@property
def getoff_time(self):
return self._getoff_time
@getoff_time.setter
def getoff_time(self, value):
self._getoff_time = value
@property
def arrival_time(self):
return self._arrival_time
@arrival_time.setter
def arrival_time(self, value):
self._arrival_time = value
def waiting_duration(self, sim_time):
# waiting = None
#
# if self.getin_time:
# waiting = self.getin_time - self.arrival_time
# else:
# waiting = sim_time - self.arrival_time
waiting = sim_time - self.arrival_time
return waiting
# def waiting_cost(self, ta=None, ta_prime=None, type='stop'):
# '''
# type = stop for R, arrival for inter-decision passenger arrival
# picked-by for picked passengers
# delivered for delivered passengers
#
# When an action is taken (tx), this value is called
# After that, we have a look at the new state, and take the
# maximizing action (ty) and call this value again.
#
# ** THE COST at t0 and t1 is only computed passengers waited from tx to ty **
#
# action x (tx), chosen, --> New state --> Maximizing action y (ty)
#
# if new event occurs between these to time values
# cost = R is updated by computing deltaR for each car
#
# NEW EVENTS;
#
# * a passenger comes between the actions and picked up
# by the other cart
#
# * passenger gets-in the car
# * passenger gets-off the car
# * passenger arrives
# * decision is made
#
# R + dR of this new event
#
# t0 is the time of last event
# t1 is the time of the current event
#
# BARTO PAPER THE INTEGRAL IS missing detail
# '''
# cost0 = 0
# cost1 = 0
# normalizing_constant = 1
# beta = self._beta
#
# if type == 'stop':
# wp0 = self.waiting_duration(ta_prime)
# wp1 = wp0 + ta_prime - ta
#
# ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
# cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
#
# ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
# cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
#
# elif type == 'arrival':
# '''
# first event is passenger arrival t0
# second event is decision time ta_prime - still waiting
# since time ta
# '''
# wp0 = 0 # waiting time at the arrival
# wp1 = wp0 + ta_prime - self.arrival_time # waiting time until the ta_prime
#
# ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
# cost0 = np.exp(-beta * (self.arrival_time - ta)) * ctau0 * normalizing_constant
#
# ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
# cost1 = np.exp(-beta * (ta_prime - ta)) * ctau1 * normalizing_constant
#
#
# elif type == 'picked-by':
# '''
# first event is passenger arrival t0
# second event is get-in time - ceased to wait
# since time ta
# '''
# wp0 = 0 # waiting time at the arrival
# wp1 = wp0 + self.getin_time - self.arrival_time # waiting time until get-in
#
# ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
# cost0 = np.exp(-beta * (self.arrival_time - ta)) * ctau0 * normalizing_constant
#
# ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
# cost1 = np.exp(-beta * (self.getin_time - ta)) * ctau1 * normalizing_constant
#
# elif type == 'delivered':
# '''
# first event is passenger arrival t0
# second event is get-in time - ceased to wait
# since time ta
# '''
# wp0 = 0 # waiting time at the arrival
# wp1 = wp0 + self.getin_time - self.arrival_time # waiting time until get-in
#
# ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
# cost0 = np.exp(-beta * (self.arrival_time - ta)) * ctau0 * normalizing_constant
#
# ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
# cost1 = np.exp(-beta * (self.getin_time - ta)) * ctau1 * normalizing_constant
#
# elif type == 'static':
# ''' Given waiting time how much is the cost, I added this as passenger independent func'''
# wp0 = 0
# wp1 = wp0 + ta_prime - ta
#
# ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
# cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
#
# ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
# cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
#
# total_cost = cost0 - cost1
# return total_cost
def waiting_cost(self, ta=None, ta_prime=None, type='stop'):
'''
type = stop for R, arrival for inter-decision passenger arrival
picked-by for picked passengers
delivered for delivered passengers
When an action is taken (tx), this value is called
After that, we have a look at the new state, and take the
maximizing action (ty) and call this value again.
** THE COST at t0 and t1 is only computed passengers waited from tx to ty **
action x (tx), chosen, --> New state --> Maximizing action y (ty)
if new event occurs between these to time values
cost = R is updated by computing deltaR for each car
NEW EVENTS;
* a passenger comes between the actions and picked up
by the other cart
* passenger gets-in the car
* passenger gets-off the car
* passenger arrives
* decision is made
R + dR of this new event
t0 is the time of last event
t1 is the time of the current event
BARTO PAPER THE INTEGRAL IS missing detail
'''
cost0 = 0
cost1 = 0
normalizing_constant = 1e-5
beta = self._beta
if type == 'stop':
wp0 = self.waiting_duration(ta_prime) / 60
wp1 = wp0 + (ta_prime - ta) / 60
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
elif type == 'arrival':
'''
first event is passenger arrival t0
second event is decision time ta_prime - still waiting
since time ta
'''
wp0 = 0 # waiting time at the arrival
wp1 = (ta_prime - self.arrival_time) / 60 # waiting time until the ta_prime
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
elif type == 'picked-by':
'''
first event is passenger arrival t0
second event is get-in time - ceased to wait
since time ta
'''
wp0 = self.waiting_duration(ta) / 60 # waiting time at the arrival
wp1 = wp0 + (self.getin_time - ta) / 60 # waiting time until get-in
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta *wp1) * ctau1 * normalizing_constant
elif type == 'delivered':
'''
first event is passenger arrival t0
second event is get-in time - ceased to wait
since time ta
'''
wp0 = 0 # waiting time at the arrival
wp1 = (wp0 + self.getin_time - self.arrival_time) / 60 # waiting time until get-in
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
elif type == 'static':
''' Given waiting time how much is the cost, I added this as passenger independent func'''
wp0 = 0
wp1 = (wp0 + ta_prime - ta) / 60
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
total_cost = cost0 - cost1
return total_cost
@classmethod
def waiting_cost_cls(cls, beta, ta, ta_prime):
# beta = cls._beta
normalizing_constant = 1.0
wp0 = 0
wp1 = (wp0 + ta_prime - ta) / 60
ctau0 = (2 / beta ** 3 + 2 * wp0 / beta ** 2 + (wp0 ** 2) / beta)
cost0 = np.exp(-beta * wp0) * ctau0 * normalizing_constant
ctau1 = (2 / beta ** 3 + 2 * wp1 / beta ** 2 + (wp1 ** 2) / beta)
cost1 = np.exp(-beta * wp1) * ctau1 * normalizing_constant
total_cost = cost0 - cost1
return np.log10(total_cost) / 10