I was reading a book on modeling and the first problem in the first chapter asked the reader to model the use of an elevator during the morning rush at an office. There was the implicit assumption that employees would only be going up, and that there wouldn’t be any intra-level travel. I though this problem was interesting, so I thought I’d start to model this with Python. (I turned out that it was a rabbit hole.) I think that this is interesting for two reasons, you can model the wait-time for a very common situation, this can be abstracted to a more general constrained resource problem. This is my first whack:
class Elevator: def __init__( self ): self.people = 0 class Person: def __init__( self, nfloors ): self.current_floor = 0 self.nfloors = nfloors self.next_floor = self.get_next_floor() self.on_elevator = False def get_next_floor( self ): while True: low, high = 0, self.nfloors i = scipy.stats.randint( low, high ).rvs() if i != self.current_floor: self.next_floor = i return i def enter_elevator( self, elevator ): if self.on_elevator: pass else: if elevator.people >= 6: self.on_elevator = False else: self.on_elevator = True elevator.people += 1 self.current_floor = self.next_floor self.next_floot = self.get_next_floor() def exit_elevator( self, elevator ): if self.on_elevator: self.on_elevator = False elevator.people -= 1
Then a simple scenario can be built as:
npeople = 10 nfloors = 5 people = [ Person( nfloors ) for i in range( npeople ) ] e = Elevator() for person in people: print person.current_floor, print print '-'*19 for i in range( 5 ): for person in people: person.enter_elevator( e ) print person.current_floor, for person in people: person.exit_elevator( e ) print
So, this is super simple, but it’s a start. I had initially started out with a very large elevator class, but then I ended up off-loading all of that logic onto the person class. For example, I had set a capacity variable in the elevator class, but realistically, elevators don’t know how many passengers they’re holding, engineers just rely on people not wanting people to squish against each other. I let person objects just look into the elevator and decide if they wanted to board according to the number of people that were already in the elevator. I could replace this with a random variable at a later point, for more reals, but I think this works for now. I also realized I’d need a way for the code to track people throughout the building in order to get a better handle on wait times.