Advertisement
roninkoi

Plane boarding Monte Carlo

Oct 8th, 2020
1,793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class Passenger:
  4.     walkspd = 0.5 # walking speed
  5.     storespd = 30 # luggage storing speed
  6.     swapspd = 15 # seat swapping speed
  7.  
  8.     def __init__(self, aislerow, seatrow, seatcol):
  9.         self.x = 0 # at gate
  10.         self.aislerow = int(aislerow)
  11.         self.seatrow = int(seatrow)
  12.         self.seatcol = int(seatcol)
  13.         self.actiont = 0 # time of action (storing, swapping)
  14.         self.atrow = False # reached row of own seat?
  15.         self.stored = False # already stored luggage?
  16.         self.swapped = False # swapped to seat?
  17.         self.atseat = False # currently at seat?
  18.  
  19.     # printing
  20.     def __repr__(self):
  21.         longprint = False
  22.         if (longprint):
  23.             return "Passenger, seat (" + \
  24.                 str(self.seatrow) + " = " + str(self.aislerow) + ", " + \
  25.                 str(self.seatcol) + "), x: " + \
  26.                 str(self.x) + ", action t: " + \
  27.                 str(self.actiont) + ", at row: " + \
  28.                 str(self.atrow) + ", stored: " + \
  29.                 str(self.stored) + ", at seat: " + \
  30.                 str(self.atseat) + "\n"
  31.         else:
  32.             return str(4 * self.seatrow + self.seatcol + 1)
  33.  
  34.     # check whether aisle slot x empty
  35.     def aislefree(aisle, x):
  36.         if (x >= len(aisle)):
  37.             return False
  38.         if (aisle[x] == None):
  39.             return True
  40.        
  41.         return False
  42.  
  43.     # check whether seat row is free
  44.     def rowfree(row, aisleseats):
  45.         for s in aisleseats:
  46.             if (row[s] != None): # aisle seat not empty
  47.                 return False
  48.            
  49.         return True
  50.  
  51.     # walk forward in aisle
  52.     def walk(self, aisle, aisledx, dt):
  53.         dx = self.walkspd * dt / aisledx
  54.  
  55.         if (Passenger.aislefree(aisle, int(self.x) + 1)):
  56.             aisle[int(self.x)] = None
  57.             self.x += dx
  58.             aisle[int(self.x)] = self
  59.            
  60.         self.atrow = int(self.x) >= self.aislerow
  61.  
  62.     # store luggage
  63.     def store(self):
  64.         self.actiont = -self.storespd
  65.         self.stored = True
  66.  
  67.     # swap to get to window seat
  68.     def swap(self, seats, aisleseats):
  69.         self.swapped = True
  70.         if (Passenger.rowfree(seats[self.seatrow], aisleseats)):
  71.             self.actiont = -self.swapspd
  72.  
  73. class Plane: # Embraer E190
  74.     rows = 25 # number of seat rows
  75.     cols = 4 # seats per row
  76.     aisledx = 0.5 # aisle separation
  77.     seatdx = 1 # seat separation
  78.     gatedist = 25 # distance from gate to first seat
  79.     aisleseats = (1, 2) # seats that are next to aisle
  80.  
  81.     # assign passengers
  82.     def assign(self, passengers):
  83.         self.passengers = passengers
  84.         self.aisle = np.full(int((self.gatedist + self.rows) / self.aisledx), None)
  85.         self.seats = np.full((self.rows, self.cols), None)
  86.  
  87.     # printing
  88.     def __repr__(self):
  89.         return "\nAisle: \n" + str(self.aisle) + "\n Seats: \n" + str(self.seats)
  90.  
  91.     # run time step
  92.     def tick(self, dt):
  93.         for p in self.passengers:
  94.             if (p.actiont >= 0 and not p.atseat): # if not performing action
  95.                 if (not p.atrow): # if not at row
  96.                     p.walk(self.aisle, self.aisledx, dt)
  97.                 elif (not p.stored): # if luggage not stored
  98.                     p.store()
  99.                 elif (not p.swapped): # swap to seat
  100.                     p.swap(self.seats, self.aisleseats)
  101.                 else:
  102.                     p.atseat = True
  103.                     self.seats[p.seatrow, p.seatcol] = p
  104.                     self.aisle[p.aislerow] = None
  105.                    
  106.             p.actiont += dt
  107.  
  108. # Performs plane boarding time Monte Carlo simulation
  109. # for number of samples n with random seat distribution.
  110. # Returns an array of boarding times.
  111. def planemc(n):
  112.     time = np.zeros(n) # boarding times
  113.  
  114.     plane = Plane()
  115.     nseats = plane.rows * plane.cols
  116.     npass = nseats
  117.  
  118.     tmax = 1e6
  119.  
  120.     printing = True # print state of plane
  121.     singlestep = False # step one dt at a time by pressing enter
  122.  
  123.     for i in range(n):
  124.         pseats = np.arange(nseats)
  125.         np.random.shuffle(pseats) # randomly shuffle seats
  126.        
  127.         pvec = np.vectorize(Passenger)
  128.         passengers = pvec(np.floor(pseats / plane.cols) * (plane.seatdx / plane.aisledx) + \
  129.                           plane.gatedist / plane.aisledx, np.floor(pseats / plane.cols), \
  130.                           pseats % plane.cols)
  131.  
  132.         if (printing):
  133.             print("Passenger seats:", len(pseats), pseats)
  134.             print("Passengers:", len(passengers), passengers)
  135.        
  136.         plane.assign(passengers)
  137.  
  138.         t = 0 # time (s)
  139.         dt = 1
  140.         boarded = False # fully boarded
  141.         while not boarded and t < tmax:
  142.             plane.tick(dt)
  143.            
  144.             boarded = True
  145.             for p in passengers: # check if all passengers seated
  146.                 if (not p.atseat):
  147.                     boarded = False
  148.                     break
  149.                
  150.             if (printing):
  151.                 print("Plane:", plane)
  152.  
  153.             if (singlestep):
  154.                 input()
  155.  
  156.             t += dt
  157.            
  158.         time[i] = t
  159.  
  160.     return time
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement