All pastes #797232 Raw Edit

Airport Simulation

public python v1 · immutable
#797232 ·published 2007-11-25 21:12 UTC
rendered paste body
#!/usr/bin/pythonfrom __future__ import generatorsfrom SimPy.Simulation import *from random import expovariate, normalvariate# ConstantsNUM_GATE = 5   # number of gateNUM_RUNWAY = 2  # number of runwayT_RUNWAY = 10  # runway occupationT_GATES = 30   # gate occupationPG_INT = 15.0  # average new plane comespcnt = 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 += 1class 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 -= 1initialize()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)