55Sample run with two cars, random seed = 4::
66
77 >>> main(num_taxis=2, seed=10)
8- taxi: 0 Event(time=0, actor_id =0, action='leave garage')
9- taxi: 0 Event(time=4, actor_id =0, action='pick up passenger')
10- taxi: 0 Event(time=10, actor_id =0, action='drop off passenger')
11- taxi: 1 Event(time=10, actor_id =1, action='leave garage')
12- taxi: 1 Event(time=11, actor_id =1, action='pick up passenger')
13- taxi: 0 Event(time=14, actor_id =0, action='pick up passenger')
14- taxi: 1 Event(time=28, actor_id =1, action='drop off passenger')
15- taxi: 0 Event(time=32, actor_id =0, action='drop off passenger')
16- taxi: 0 Event(time=33, actor_id =0, action='going home')
17- taxi: 1 Event(time=33, actor_id =1, action='pick up passenger')
18- taxi: 1 Event(time=35, actor_id =1, action='drop off passenger')
19- taxi: 1 Event(time=38, actor_id =1, action='pick up passenger')
20- taxi: 1 Event(time=42, actor_id =1, action='drop off passenger')
21- taxi: 1 Event(time=44, actor_id =1, action='pick up passenger')
22- taxi: 1 Event(time=75, actor_id =1, action='drop off passenger')
23- taxi: 1 Event(time=76, actor_id =1, action='going home')
8+ taxi: 0 Event(time=0, proc =0, action='leave garage')
9+ taxi: 0 Event(time=4, proc =0, action='pick up passenger')
10+ taxi: 0 Event(time=10, proc =0, action='drop off passenger')
11+ taxi: 1 Event(time=10, proc =1, action='leave garage')
12+ taxi: 1 Event(time=11, proc =1, action='pick up passenger')
13+ taxi: 0 Event(time=14, proc =0, action='pick up passenger')
14+ taxi: 1 Event(time=28, proc =1, action='drop off passenger')
15+ taxi: 0 Event(time=32, proc =0, action='drop off passenger')
16+ taxi: 0 Event(time=33, proc =0, action='going home')
17+ taxi: 1 Event(time=33, proc =1, action='pick up passenger')
18+ taxi: 1 Event(time=35, proc =1, action='drop off passenger')
19+ taxi: 1 Event(time=38, proc =1, action='pick up passenger')
20+ taxi: 1 Event(time=42, proc =1, action='drop off passenger')
21+ taxi: 1 Event(time=44, proc =1, action='pick up passenger')
22+ taxi: 1 Event(time=75, proc =1, action='drop off passenger')
23+ taxi: 1 Event(time=76, proc =1, action='going home')
2424 *** end of events ***
2525
2626"""
3636SEARCH_INTERVAL = 4
3737TRIP_DURATION = 10
3838
39- Event = collections .namedtuple ('Event' , 'time actor_id action' )
39+ Event = collections .namedtuple ('Event' , 'time proc action' )
4040
4141
4242def compute_delay (interval ):
@@ -59,16 +59,16 @@ def taxi_process(ident, trips, start_time=0):
5959# BEGIN TAXI_SIMULATOR
6060class Simulator :
6161
62- def __init__ (self , actors ):
62+ def __init__ (self , procs_map ):
6363 self .events = queue .PriorityQueue ()
64- self .actors = dict (actors )
64+ self .procs = dict (procs_map )
6565
6666
6767 def run (self , end_time ): # <1>
6868 """Schedule and display events until time is up"""
6969 # schedule the first event for each cab
70- for _ , actor in sorted (self .actors .items ()): # <2>
71- first_event = next (actor ) # <3>
70+ for _ , proc in sorted (self .procs .items ()): # <2>
71+ first_event = next (proc ) # <3>
7272 self .events .put (first_event ) # <4>
7373
7474 # main loop of the simulation
@@ -80,16 +80,16 @@ def run(self, end_time): # <1>
8080
8181 # get and display current event
8282 current_event = self .events .get () # <7>
83- print ('taxi:' , current_event .actor_id , # <8>
84- current_event .actor_id * ' ' , current_event )
83+ print ('taxi:' , current_event .proc , # <8>
84+ current_event .proc * ' ' , current_event )
8585
86- # schedule next action for current actor
86+ # schedule next action for current proc
8787 time = current_event .time # <9>
88- actor = self .actors [current_event .actor_id ] # <10>
88+ proc = self .procs [current_event .proc ] # <10>
8989 try :
90- next_event = actor .send (time ) # <11>
90+ next_event = proc .send (time ) # <11>
9191 except StopIteration :
92- del self .actors [current_event .actor_id ] # <12>
92+ del self .procs [current_event .proc ] # <12>
9393 else :
9494 self .events .put (next_event ) # <13>
9595 else : # <14>
@@ -99,7 +99,7 @@ def run(self, end_time): # <1>
9999
100100def main (end_time = DEFAULT_END_TIME , num_taxis = DEFAULT_NUMBER_OF_TAXIS ,
101101 seed = None ):
102- """Initialize random generator, build actors and run simulation"""
102+ """Initialize random generator, build procs and run simulation"""
103103 if seed is not None :
104104 random .seed (seed ) # get reproducible results
105105
@@ -133,36 +133,36 @@ def main(end_time=DEFAULT_END_TIME, num_taxis=DEFAULT_NUMBER_OF_TAXIS,
133133
134134# BEGIN TAXI_SAMPLE_RUN
135135$ $ clear; python3 taxi_sim.py -t 3 -s 19
136- taxi: 0 Event(time=0, actor_id =0, action='leave garage')
137- taxi: 0 Event(time=5, actor_id =0, action='pick up passenger')
138- taxi: 1 Event(time=10, actor_id =1, action='leave garage')
139- taxi: 1 Event(time=13, actor_id =1, action='pick up passenger')
140- taxi: 2 Event(time=20, actor_id =2, action='leave garage')
141- taxi: 0 Event(time=21, actor_id =0, action='drop off passenger')
142- taxi: 1 Event(time=21, actor_id =1, action='drop off passenger')
143- taxi: 1 Event(time=23, actor_id =1, action='pick up passenger')
144- taxi: 2 Event(time=23, actor_id =2, action='pick up passenger')
145- taxi: 1 Event(time=25, actor_id =1, action='drop off passenger')
146- taxi: 1 Event(time=27, actor_id =1, action='pick up passenger')
147- taxi: 2 Event(time=27, actor_id =2, action='drop off passenger')
148- taxi: 2 Event(time=29, actor_id =2, action='pick up passenger')
149- taxi: 1 Event(time=31, actor_id =1, action='drop off passenger')
150- taxi: 2 Event(time=31, actor_id =2, action='drop off passenger')
151- taxi: 1 Event(time=33, actor_id =1, action='pick up passenger')
152- taxi: 2 Event(time=33, actor_id =2, action='pick up passenger')
153- taxi: 2 Event(time=36, actor_id =2, action='drop off passenger')
154- taxi: 2 Event(time=37, actor_id =2, action='pick up passenger')
155- taxi: 2 Event(time=40, actor_id =2, action='drop off passenger')
156- taxi: 1 Event(time=42, actor_id =1, action='drop off passenger')
157- taxi: 1 Event(time=43, actor_id =1, action='going home')
158- taxi: 0 Event(time=44, actor_id =0, action='pick up passenger')
159- taxi: 2 Event(time=44, actor_id =2, action='pick up passenger')
160- taxi: 0 Event(time=49, actor_id =0, action='drop off passenger')
161- taxi: 0 Event(time=50, actor_id =0, action='going home')
162- taxi: 2 Event(time=58, actor_id =2, action='drop off passenger')
163- taxi: 2 Event(time=65, actor_id =2, action='pick up passenger')
164- taxi: 2 Event(time=71, actor_id =2, action='drop off passenger')
165- taxi: 2 Event(time=72, actor_id =2, action='going home')
136+ taxi: 0 Event(time=0, proc =0, action='leave garage')
137+ taxi: 0 Event(time=5, proc =0, action='pick up passenger')
138+ taxi: 1 Event(time=10, proc =1, action='leave garage')
139+ taxi: 1 Event(time=13, proc =1, action='pick up passenger')
140+ taxi: 2 Event(time=20, proc =2, action='leave garage')
141+ taxi: 0 Event(time=21, proc =0, action='drop off passenger')
142+ taxi: 1 Event(time=21, proc =1, action='drop off passenger')
143+ taxi: 1 Event(time=23, proc =1, action='pick up passenger')
144+ taxi: 2 Event(time=23, proc =2, action='pick up passenger')
145+ taxi: 1 Event(time=25, proc =1, action='drop off passenger')
146+ taxi: 1 Event(time=27, proc =1, action='pick up passenger')
147+ taxi: 2 Event(time=27, proc =2, action='drop off passenger')
148+ taxi: 2 Event(time=29, proc =2, action='pick up passenger')
149+ taxi: 1 Event(time=31, proc =1, action='drop off passenger')
150+ taxi: 2 Event(time=31, proc =2, action='drop off passenger')
151+ taxi: 1 Event(time=33, proc =1, action='pick up passenger')
152+ taxi: 2 Event(time=33, proc =2, action='pick up passenger')
153+ taxi: 2 Event(time=36, proc =2, action='drop off passenger')
154+ taxi: 2 Event(time=37, proc =2, action='pick up passenger')
155+ taxi: 2 Event(time=40, proc =2, action='drop off passenger')
156+ taxi: 1 Event(time=42, proc =1, action='drop off passenger')
157+ taxi: 1 Event(time=43, proc =1, action='going home')
158+ taxi: 0 Event(time=44, proc =0, action='pick up passenger')
159+ taxi: 2 Event(time=44, proc =2, action='pick up passenger')
160+ taxi: 0 Event(time=49, proc =0, action='drop off passenger')
161+ taxi: 0 Event(time=50, proc =0, action='going home')
162+ taxi: 2 Event(time=58, proc =2, action='drop off passenger')
163+ taxi: 2 Event(time=65, proc =2, action='pick up passenger')
164+ taxi: 2 Event(time=71, proc =2, action='drop off passenger')
165+ taxi: 2 Event(time=72, proc =2, action='going home')
166166*** end of events ***
167167# END TAXI_SAMPLE_RUN
168168
0 commit comments