import random import string import copy
class Building(): def init(self, num_of_floor, num_of_passenger): self.num_of_floor = num_of_floor self.num_of_passenger = num_of_passenger self.passengers = [] #list of all passengers self.random_passengers() #generate random passengers
###########################################################################
class Passenger(): def init(self, origin, destination): self.name = "".join(random.choice(string.ascii_uppercase)) + "".join(random.choice(string.ascii_lowercase) for _ in range(random.randrange(2, 8))) # a random name for the passenger for console display self.origin = origin self.destination = destination self.deliver = 0 # marker of if the passenger is delivered, 0=undelivered 1=delivered self.cost = 0 # the cost to deliver the passenger
###########################################################################
class Elevator(): def init(self, capacity, position=0, direction=1): self.capacity = capacity # max allowed people in the elevator self.position = position # current position of the elevator, defalut to 0 self.movement = 0 # count the total movement of the elevator, used to calculate cost function self.direction = direction # last moving direction of the elevator, 0=down 1=up, default to 1 self.carrying = [] # a collection of passengers currently inside the elevator
###########################################################################
Strategy 1 has 3 principles:
1. The elevator will always favour its current moving direction.
2. Unless reached its capacity, the elevator will take all encountered passengers that are going in the same direction.
3. The elevator will reverse its current moving direction if:
1. There is no passenger currently inside the elevator.
2. There is no undelivered passenger in the elevator's current direction
def strategy_1(b, e): #inputs a building and an elevator awaiting = copy.copy(b.passengers) #list of passengers in the building e.carrying = [] #reset elevator while len(awaiting) > 0 or len(e.carrying) > 0: #while there are people to be picked up or people to deliver print("The elevator is at", e.position, "carrying", len(e.carrying), "passengers")
###########################################################################
#Strategy 2:
Elevator goes all the way up and then down and opens door at every floor
def strategy_2(b, e): #inputs a building and an elevator awaiting = copy.copy(b.passengers) #list of passengers in the building e.carrying = [] #reset elevator while len(awaiting) > 0 or len(e.carrying) > 0: #while there are people to be picked up or people to deliver print("The elevator is at", e.position, "carrying", len(e.carrying), "passengers")
###########################################################################
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-f2f634e0e7a0> in <module>()
----> 1 building_1 = Building(50, 25)
2 elevator_1 = Elevator(10)
3
4 strategy_1(copy.copy(building_1), copy.copy(elevator_1))
NameError: name 'Building' is not defined