-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeople.py
75 lines (61 loc) · 2.16 KB
/
People.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
from Route import Route
import random
WAITING = 0
BOARDED = 1
COST_PER_TIMESTEP = 1
class People:
def __init__(self):
self.timeWaited = 0
self.status = WAITING
def chooseDisembarkLocation(curStop,route):
# fetch route details
routeDetails = Route.getDetails(route)
# function to check distance in number of bus stops from start
# we call this distance removal
def fn(reqd_stop,deets):
deets[reqd_stop]['removal'] = 0
x = 0
# forward pass - add distance in bus stops in direction of the loop
stop = deets[reqd_stop]['nextStop']
deets[stop]['prev'] = reqd_stop
while stop!= reqd_stop:
x+=1
deets[stop]['removal'] = x
nextStop = deets[stop]['nextStop']
deets[nextStop]['prev'] = stop
stop = nextStop
# backward pass - add distance in bus stops opposite direction of the loop
prev = deets[stop]['prev']
x= 1
deets[prev]['removal'] = min(deets[prev]['removal'],x)
while prev!= reqd_stop:
prev = deets[prev]['prev']
x+=1
deets[prev]['removal'] = min(deets[prev]['removal'],x)
return deets
# take total of removals
routeDetails = fn(curStop,routeDetails)
total = 0
for key,value in routeDetails.items():
if key==curStop:
continue
total+=value['removal']
# generate random value from 0 to total - 1
randVal = random.randint(0,total-1)
# pick the cumulative region in which the random value falls and return that region
total = 0
for key,value in routeDetails.items():
if key==curStop:
continue
total+=value['removal']
if randVal<total:
break
return key
def board(self):
self.status = BOARDED
def step(self):
if self.status == WAITING:
self.timeWaited += 1
return COST_PER_TIMESTEP
else:
return 0