Reputation: 11
I'm trying to use SimPy to simulate a bus that stops periodically in a station and then waits until it gets filled by N passengers (assume that the bus is empty each time it arrives at the station), being N the total amount of spots that the bus has; this means that, in the case the bus arrives and the amount of people waiting in queue is smaller than N, then the bus will fill the amount of seats equal to the amount of people what were in the queue at that moment and then wait for more people to arrive at the station until the entire N spots are filled.
Here's a portion of what I'm trying to do:
def arrival_bus(env, bus):
print("-----------------------------------")
print("<<<<<<<<<<<<< BUS ARRIVES TO STATION")
bus.free_spots = total_amount_of_spots
print("CURRENT PASSENGERS IN STATION: ",len(passengers_queue))
if(len(passengers_queue) < total_amount_of_spots):
bus.free_spots -= len(passengers_queue)
passengers_queue = []
print("BOARDED PASSENGERS, WAITING FOR MORE PASSENGERS TO FILL THE", bus.free_spots, "SPOTS REMAINING...")
while(len(passengers_queue) < bus.free_spots):
env.process(passenger_arrival(env))
def passenger_arrival(env):
while True:
time_between_arrivals = 5
yield env.timeout(time_between_arrivals)
print("PASSENGER ARRIVED")
passengers_queue(env.now)
print("PASSENGERS IN STATION: ",len(passengers_queue))
def bus_movement(env,bus):
while True:
time_between_arrivals = 20
yield env.timeout(time_between_arrivals)
arrival_bus(env, bus)
But it's not working. I think it has something to do with the while loop in arrival_bus that executes when the amount of people in queue is smaller than total_amount_of_spots (N), but I don't get how else I can do it, I'm pretty much a newbie using SimPy.
Any help would be much appreciated!
Upvotes: 1
Views: 216
Reputation: 1914
In this example I am using a broker pattern to match passengers to busses until a bus is full. I am using stores as my queues.
"""
Quick demo of loading a bus with passengers
Bus does not leave until all seats are filled
Programer: Michael R. Gibbs
"""
import random
import simpy
class Bus():
"""
Simple bus class that holds passengers
"""
last_id = 0
def __init__(self):
self.id = self.__class__.last_id + 1
self.__class__.last_id = self.id
self.passengers = []
class Passenger():
"""
Simple class of passengers with unique ids
"""
last_id = 0
def __init__(self):
self.id = self.__class__.last_id + 1
self.__class__.last_id = self.id
def passengers_arrive(env, passenger_q):
"""
models the arrival of passengers
"""
while True:
yield env.timeout(5)
passenger = Passenger()
print(f'{env.now} passenger {passenger.id} has arrived')
passenger_q.put(passenger)
def bus_arrives(env, bus_q):
"""
models the arrival of busses
"""
while True:
yield env.timeout(90)
bus = Bus()
print(f'{env.now} bus {bus.id} has arrived')
bus_q.put(bus)
def load_busses(env, bus_q, passenger_q):
"""
broker process loads one bus at a time
by matching waiting passengers onto waitng busses
bus is loaded when all seats are filled
"""
while True:
bus = yield bus_q.get()
print(f'{env.now} starting to load bus {bus.id}')
while len(bus.passengers) < 20:
passenger = yield passenger_q.get()
print(f'{env.now} passenger {passenger.id} is getting on bus {bus.id}')
bus.passengers.append(passenger)
print(f'{env.now} bus {bus.id} has finish loading')
# bootup
env = simpy.Environment()
bus_q = simpy.Store(env)
passenger_q = simpy.Store(env)
env.process(passengers_arrive(env, passenger_q))
env.process(bus_arrives(env, bus_q))
env.process(load_busses(env, bus_q, passenger_q))
env.run(210)
print('done')
Upvotes: 1