44
55Sample run with two cars, random seed = 4::
66
7- >>> main(num_taxis=2, seed=7)
8- taxi: 0 Event(time=2, actor_id=0, action='pick up passenger')
7+ >>> 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')
912 taxi: 1 Event(time=11, actor_id=1, action='pick up passenger')
10- taxi: 1 Event(time=12, actor_id=1, action='drop off passenger')
11- taxi: 0 Event(time=13, actor_id=0, action='drop off passenger')
12- taxi: 0 Event(time=15, actor_id=0, action='pick up passenger')
13- taxi: 0 Event(time=16, actor_id=0, action='drop off passenger')
14- taxi: 1 Event(time=16, actor_id=1, action='pick up passenger')
15- taxi: 0 Event(time=17, actor_id=0, action='going home')
16- taxi: 1 Event(time=24, actor_id=1, action='drop off passenger')
17- taxi: 1 Event(time=25, actor_id=1, action='pick up passenger')
18- taxi: 1 Event(time=31, actor_id=1, action='drop off passenger')
19- taxi: 1 Event(time=32, actor_id=1, action='pick up passenger')
20- taxi: 1 Event(time=33, actor_id=1, action='drop off passenger')
21- taxi: 1 Event(time=34, actor_id=1, action='going home')
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')
2224 *** end of events ***
2325
2426"""
3133
3234DEFAULT_NUMBER_OF_TAXIS = 3
3335DEFAULT_END_TIME = 80
34- FIND_PASSENGER_INTERVAL = 4
36+ SEARCH_INTERVAL = 4
3537TRIP_DURATION = 10
3638
3739Event = collections .namedtuple ('Event' , 'time actor_id action' )
@@ -44,61 +46,65 @@ def compute_delay(interval):
4446
4547def taxi_process (ident , trips , start_time = 0 ):
4648"""Yield to simulator issuing event at each state change"""
47- time = start_time
49+ time = yield Event ( start_time , ident , 'leave garage' )
4850for i in range (trips ):
49- prowling_ends = time + compute_delay (FIND_PASSENGER_INTERVAL )
51+ prowling_ends = time + compute_delay (SEARCH_INTERVAL )
5052time = yield Event (prowling_ends , ident , 'pick up passenger' )
5153
5254trip_ends = time + compute_delay (TRIP_DURATION )
5355time = yield Event (trip_ends , ident , 'drop off passenger' )
5456
5557yield Event (trip_ends + 1 , ident , 'going home' )
5658
57-
59+ # BEGIN TAXI_SIMULATOR
5860class Simulator :
5961
6062def __init__ (self , actors ):
6163self .events = queue .PriorityQueue ()
6264self .actors = dict (actors )
6365
6466
65- def run (self , end_time ):
66- """Schedule and execute events until time is up"""
67- for ident , actor in sorted (self .actors .items ()):
68- first_event = next (actor ) # prime each coroutine
69- self .events .put (first_event )
67+ def run (self , end_time ): # <1>
68+ """Schedule and display events until time is up"""
69+ # schedule the first event for each cab
70+ for _ , actor in sorted (self .actors .items ()): # <2>
71+ first_event = next (actor ) # <3>
72+ self .events .put (first_event ) # <4>
73+
74+ # main loop of the simulation
7075time = 0
71- while time < end_time :
72- if self .events .empty ():
76+ while time < end_time :# <5>
77+ if self .events .empty ():# <6>
7378print ('*** end of events ***' )
7479break
7580
7681# get and display current event
77- current_event = self .events .get ()
78- print ('taxi:' , current_event .actor_id ,
82+ current_event = self .events .get ()# <7>
83+ print ('taxi:' , current_event .actor_id ,# <8>
7984current_event .actor_id * ' ' , current_event )
8085
8186# schedule next action for current actor
82- actor = self . actors [ current_event .actor_id ]
83- time = current_event .time
87+ time = current_event .time # <9>
88+ actor = self . actors [ current_event .actor_id ] # <10>
8489try :
85- next_event = actor .send (time )
90+ next_event = actor .send (time )# <11>
8691except StopIteration :
87- del self .actors [current_event .actor_id ]
92+ del self .actors [current_event .actor_id ]# <12>
8893else :
89- self .events .put (next_event )
90- else :
94+ self .events .put (next_event )# <14>
95+ else :# <15>
9196msg = '*** end of simulation time:{} events pending ***'
9297print (msg .format (self .events .qsize ()))
93-
98+ # END TAXI_SIMULATOR
9499
95100def main (end_time = DEFAULT_END_TIME , num_taxis = DEFAULT_NUMBER_OF_TAXIS ,
96101seed = None ):
97102"""Initialize random generator, build actors and run simulation"""
98103if seed is not None :
99104random .seed (seed ) # get reproducible results
100105
101- taxis = {i : taxi_process (i , (i + 1 )* 2 , i * 10 ) for i in range (num_taxis )}
106+ taxis = {i : taxi_process (i , (i + 1 )* 2 , i * 10 )
107+ for i in range (num_taxis )}
102108sim = Simulator (taxis )
103109sim .run (end_time )
104110
@@ -125,34 +131,39 @@ def main(end_time=DEFAULT_END_TIME, num_taxis=DEFAULT_NUMBER_OF_TAXIS,
125131"""
126132Sample run:
127133
128- $ clear; python3 taxi_sim.py -s 19
134+ # BEGIN TAXI_SAMPLE_RUN
135+ $ $ clear; python3 taxi_sim.py -t 3 -s 19
136+ taxi: 0 Event(time=0, actor_id=0, action='leave garage')
129137taxi: 0 Event(time=5, actor_id=0, action='pick up passenger')
130- taxi: 0 Event(time=13, actor_id=0, action='drop off passenger')
131- taxi: 0 Event(time=16, actor_id=0, action='pick up passenger')
132- taxi: 1 Event(time=17, actor_id=1, 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')
133142taxi: 1 Event(time=21, actor_id=1, action='drop off passenger')
134- taxi: 1 Event(time=22 , actor_id=1, action='pick up passenger')
143+ taxi: 1 Event(time=23 , actor_id=1, action='pick up passenger')
135144taxi: 2 Event(time=23, actor_id=2, action='pick up passenger')
136- taxi: 1 Event(time=26, actor_id=1, action='drop off 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')
137147taxi: 2 Event(time=27, actor_id=2, action='drop off passenger')
138- taxi: 1 Event(time=28, actor_id=1, action='pick up passenger')
139148taxi: 2 Event(time=29, actor_id=2, action='pick up passenger')
140- taxi: 1 Event(time=30, actor_id=1, action='drop off passenger')
141- taxi: 1 Event(time=32, actor_id=1, action='pick up passenger')
142- taxi: 2 Event(time=33, actor_id=2, action='drop off passenger')
143- taxi: 2 Event(time=34, actor_id=2, action='pick up passenger')
144- taxi: 2 Event(time=35, actor_id=2, action='drop off passenger')
145- taxi: 2 Event(time=36, actor_id=2, action='pick up passenger')
146- taxi: 1 Event(time=41, actor_id=1, action='drop off passenger')
147- taxi: 1 Event(time=42, actor_id=1, action='going home')
148- taxi: 2 Event(time=44, actor_id=2, action='drop off passenger')
149- taxi: 2 Event(time=46, actor_id=2, action='pick up passenger')
150- taxi: 2 Event(time=60, actor_id=2, action='drop off passenger')
151- taxi: 2 Event(time=67, actor_id=2, action='pick up passenger')
152- taxi: 2 Event(time=73, actor_id=2, action='drop off passenger')
153- taxi: 0 Event(time=74, actor_id=0, action='drop off passenger')
154- taxi: 2 Event(time=74, actor_id=2, action='going home')
155- taxi: 0 Event(time=75, actor_id=0, action='going home')
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')
156166*** end of events ***
167+ # END TAXI_SAMPLE_RUN
157168
158169"""
0 commit comments