#!/usr/bin/python from __future__ import generators from SimPy.Simulation import * from random import expovariate, normalvariate # Constants NUM_GATE = 5 # number of gate NUM_RUNWAY = 2 # number of runway T_RUNWAY = 10 # runway occupation T_GATES = 30 # gate occupation PG_INT = 15.0 # average new plane comes pcnt = 10 # class PlaneGenerator(Process): def __init__(self,name): Process.__init__(self,name) self.cnt = 0 def generate(self): while True: p = Plane('Plane #' + str(self.cnt)) activate(p, p.execute()) yield hold,self,r.expovariate(1/PG_INT) self.cnt += 1 class Plane(Process): def __init__(self,name): Process.__init__(self,name) def execute(self): # landing start = now() yield request, self, runways finish = now() yield hold, self, T_RUNWAY yield release, self, runways mrwl.observe(finish - start) am.add() # gate start = now() yield request, self, gates finish = now() am.sub() yield hold, self, T_GATES yield release, self, gates mg.observe(finish - start) am.add() # take off start = now() yield request, self, runways finish = now() am.sub() yield hold, self, T_RUNWAY yield release, self, runways mrwt.observe(finish - start) class AirportMonitor: def __init__(self): self.cnt = 0 self.max = 0 def add(self): self.cnt += 1 if (self.cnt > self.max): self.max = self.cnt def sub(self): self.cnt -= 1 initialize() r = random.Random() am = AirportMonitor() mrwl = Monitor("runway landing") mrwt = Monitor("runway takeoff") mg = Monitor("gate") runways = Resource(NUM_RUNWAY) gates = Resource(NUM_GATE) pg = PlaneGenerator('Source') activate(pg, pg.generate(), 0.0) simulate(until=24*60) print "total planes generated: " + str(pg.cnt) print "average waiting time for runway (landing): " + str(mrwl.mean()) print "average waiting time for runway (takeoff): " + str(mrwt.mean()) print "average waiting time for gate: " + str(mg.mean()) print "max planes waiting for gate/runway: " + str(am.max)