**Example, i.e., not great, strategy.** The elevator starts on the ground floor and moves all the way to the top floor, stopping at every floor in between. When it reaches the top floor, the elevator changes direction and moves all the way back down to the ground floor, again stopping at every floor in between. At every floor where the elevator stops any passengers who want to get off at that floor leave, and any passengers who want to get on enter, as long as there is space in the elevator. If the elevator is full, passengers on that floor have to wait. Upon reaching the ground floor, the elevator repeats the cycle of moving all the way up and all the way down the building.

In [18]:

import numpy as np import random

In [19]:

class Elevator(): """ Elevator class .. """ def __init__(self, num_floors, requests, curr_floor=0, up=True, capacity=5, num_passengers=0): self.curr_floor = curr_floor #the floor that the elevator is currently on self.requests = requests #list of requests from all the passengers, this is specified at once in the start by user self.up = up #indicator whether the elevator is moving up or down self.capacity = capacity #maximum number of passengers that the elevator could carry self.num_floors = num_floors #number of floors in the building self.num_passengers = num_passengers #number of passengers currently in the elevator def add_request(self, passenger): #add a request into the list of requests self.requests.append(passenger) #this is not used in the simulation because we assume all requests were added at once before the simulation started def remove_request(self, passenger): #remove a request from the list of requests self.requests.remove(passenger) def move(self): #the elevator moves by one floor, either up or down #The elevator moves up if it is in the base floor if self.curr_floor == 0: self.up = True #The elevator changes direction if it is in the top floor if self.curr_floor == self.num_floors-1: self.up = False #If the direction is up, it continues to move up if self.up == True: self.curr_floor += 1 #If the direction is down, it continues to move down if self.up == False: self.curr_floor -= 1 class Passenger(): """ Passenger class .. """ def __init__(self, index, num_floors, current=0, destination=0, in_elevator=False, served=False): self.current=np.random.randint(0,num_floors) #the floor which the passenger comes from self.destination=np.random.randint(0,num_floors) #the floor which the passenger wants to go to self.index=index #index of the passenger from 1 to total number of passengers self.in_elevator=in_elevator #indicator whether the passenger is currently in the elevator or waiting outside #self.served=served #indicator whether the passenger is already delivered to the destination. this is not used for this particular strategy. class Building(): """ Building class .. """ def __init__(self, num_floors, passenger_list): self.num_floors = num_floors #number of floors in the building, floor will be indexed from 0 to num_floors-1 self.passenger_list = passenger_list #list of passengers def main(): try: #User inputs number of passengers and number of floors total_passengers = int(input("How many passengers are there?: ")) total_floors = int(input("How many floors are there?: ")) #Generate a list of passengers based on total number of passengers #This would also be the list of requests as we assume each passenger uses the elevator only once. list_of_passengers = [Passenger(i, total_floors) for i in range(1,total_passengers+1)] #If you want to see the current and destination of passengers, uncomment print(passengers) passengers=[] for p in list_of_passengers: passengers.append((p.current,p.destination)) #print (passengers) #Generate an object "elevator" elevator = Elevator(total_floors, list_of_passengers) #Generate an object "building" building = Building(total_floors, list_of_passengers) #The elevator keeps moving until we have no request left in our list of requests. while len(elevator.requests) > 0: for passenger in elevator.requests: # Pick-up: # if the passenger is on the floor that the elevator stops, is outside the elevator, and # the number of passengers in the elevator is smaller than its capacity, the passenger is picked up. if passenger.current == elevator.curr_floor \ and passenger.in_elevator == False \ and elevator.num_passengers < elevator.capacity: passenger.in_elevator = True elevator.num_passengers+=1 print("Elevator picks up passenger %s at floor %s" % (passenger.index, elevator.curr_floor)) # Drop-off: # if the passenger destination is on the floor that the elevator stops #the passenger is inside the elevator, the passenger is dropped off. if passenger.destination == elevator.curr_floor \ and passenger.in_elevator == True: elevator.remove_request(passenger) elevator.num_passengers-=1 print ("Elevator drops off passenger %s at floor %s" % (passenger.index, elevator.curr_floor)) #if there is no request left in the list of requests, we end the simulation and break the loop. if len(elevator.requests)==0: print ("All passengers have been served - End of simulation") break elevator.move() if elevator.up == True: print ("Elevator moves from floor %s to floor %s" % (elevator.curr_floor-1,elevator.curr_floor)) elif elevator.up == False: print ("Elevator moves from floor %s to floor %s" % (elevator.curr_floor+1,elevator.curr_floor)) except ValueError: print ("Invalid input, please try again") main() if __name__ == "__main__": main()

How many passengers are there?:

How many floors are there?:

Elevator picks up passenger 3 at floor 0
Elevator moves from floor 0 to floor 1
Elevator moves from floor 1 to floor 2
Elevator picks up passenger 1 at floor 2
Elevator drops off passenger 1 at floor 2
Elevator drops off passenger 3 at floor 2
Elevator moves from floor 2 to floor 3
Elevator picks up passenger 2 at floor 3
Elevator drops off passenger 2 at floor 3
Elevator moves from floor 3 to floor 2
Elevator moves from floor 2 to floor 1
Elevator moves from floor 1 to floor 0
Elevator moves from floor 0 to floor 1
Elevator moves from floor 1 to floor 2
Elevator moves from floor 2 to floor 3
Elevator picks up passenger 4 at floor 3
Elevator moves from floor 3 to floor 2
Elevator moves from floor 2 to floor 1
Elevator drops off passenger 4 at floor 1
All passengers have been served - End of simulation

In [ ]:

```
```