Demo entry 6344701

airport_simulation

   

Submitted by anonymous on Jan 23, 2017 at 17:07
Language: Python 3. Code size: 13.2 kB.

import random, math

class Customer(object):
    def __init__(self, cN = 0, aTime = 0, wTime = 0, idTime = 0, bodyTime = 0, bagsTime = 0):
        self.cN = cN##Customer number
        self.aTime = aTime##arrival time
        self.wTime = wTime##wait time
        self.idTime = idTime##id scanning time
        self.bodyTime = bodyTime## body scanning time
        self.bagsTime = bagsTime## bags scanning time
    def getWTime(self):
        return self.wTime
    def incWTime(self):
        self.wTime += 1
    def setWTime(self, wTime):
        self.wTime = wTime
    def getATime(self):
        return self.aTime
    def getidTime(self):
        return self.idTime    
    def getbodyTime(self):
        return self.bodyTime
    def getbagsTime(self):
        return self.bagsTime
    def getCNumber(self):
        return self.cN
    
class idServer(object):
    def __init__(self):
        self.status = 'free'
        self.tTime = 0
        self.customer = None
    def isFree(self):
        return self.status == 'free'
    def setBusy(self):
        self.status = 'busy'
    def setFree(self):
        self.status = 'free'
    def setTTime(self, t):
        self.tTime = t
    def setTTimeByCustomer(self):
        self.tTime = self.customer.getidTime()
    def decTTime(self):
        self.tTime = self.tTime - 1
    def getRemTTime(self):##rest of serving time
        return self.tTime    
    def setCustomer(self, c):
        self.customer = c
    def getCustomerNum(self):
        return self.customer.getCNumber()
    def getCustomerATime(self):
        return self.customer.getATime()
    def getCustomerWTime(self):
        return self.customer.getWTime() 
    def getCustomeridTime(self):
        return self.customer.getidTime()
    def getCustomerbodyTime(self):
        return self.customer.getbodyTime()
    def getCustomerbagsTime(self):
        return self.customer.getbagsTime()

class idServerList(object):
    def __init__(self, num = 1):
        self.numOfServer = num
        self.servers = []
        for i in range(num):
            self.servers.append(idServer())
    def getFreeServerID(self):
        id = -1
        for i in range(len(self.servers)):
            if self.servers[i].isFree():
                id = i
                break
        return id
    def getNumofBusyServer(self):
        busyServers = 0
        for s in self.servers:
            if (not s.isFree()):
                busyServers += 1
        return busyServers
    def setServerBusy(self, id, c, t):
        self.servers[id].setBusy()
        self.servers[id].setTTime(t)
        self.servers[id].setCustomer(c)
    def setServerBusyByCustomer(self, id, c):
        self.servers[id].setBusy()
        self.servers[id].setTTime(c.getidTime())
        self.servers[id].setCustomer(c)
    def updateServers(self):
        cl=[]
        for i in range(len(self.servers)):
            if (not self.servers[i].isFree()):
                self.servers[i].decTTime()
                if(self.servers[i].getRemTTime() == 0):
                    sb = self.servers[i].customer
                    cl.append(sb)
                    self.servers[i].setFree()
        return cl

class bodyServer(object):
    def __init__(self):
        self.status = 'free'
        self.tTime = 0
        self.customer = None
    def isFree(self):
        return self.status == 'free'
    def setBusy(self):
        self.status = 'busy'
    def setFree(self):
        self.status = 'free'
    def setTTime(self, t):
        self.tTime = t
    def setTTimeByCustomer(self):
        self.tTime = self.customer.getbodyTime()
    def decTTime(self):
        self.tTime -= 1
    def getRemTTime(self):##rest of serving time
        return self.tTime    
    def setCustomer(self, c):
        self.customer = c   
    def getCustomerNum(self):
        return self.customer.getCNumber()    
    def getCustomerATime(self):
        return self.customer.getATime()   
    def getCustomerWTime(self):
        return self.customer.getWTime()    
    def getCustomeridTime(self):
        return self.customer.getidTime()    
    def getCustomerbodyTime(self):
        return self.customer.getbodyTime()
    def getCustomerbagsTime(self):
        return self.customer.getbagsTime()

class bodyServerList(object):
    def __init__(self, num = 1):
        self.numOfServer = num
        self.servers = []
        for i in range(num):
            self.servers.append(bodyServer())    
    def getFreeServerID(self):
        id = -1
        for i in range(len(self.servers)):
            if self.servers[i].isFree():
                id = i
                break
        return id    
    def getNumofBusyServer(self):
        busyServers = 0
        for s in self.servers:
            if (not s.isFree()):
                busyServers += 1
        return busyServers    
    def setServerBusy(self, id, c, t):
        self.servers[id].setBusy()
        self.servers[id].setTTime(t)
        self.servers[id].setCustomer(c)
    def setServerBusyByCustomer(self, id, c):
        self.servers[id].setBusy()
        self.servers[id].setTTime(c.getbodyTime())
        self.servers[id].setCustomer(c)
    def updateServers(self):
        for i in range(len(self.servers)):
            if (not self.servers[i].isFree()):
                self.servers[i].decTTime()
                if(self.servers[i].getRemTTime() == 0):
                    self.servers[i].setFree()
                    
class bagsServer(object):
    def __init__(self):
        self.status = 'free'
        self.tTime = 0
        self.customer = None
    def isFree(self):
        return self.status == 'free'
    def setBusy(self):
        self.status = 'busy'
    def setFree(self):
        self.status = 'free'
    def setTTime(self, t):
        self.tTime = t
    def setTTimeByCustomer(self):
        self.tTime = self.customer.getbagsTime()
    def decTTime(self):
        self.tTime -= 1
    def getRemTTime(self):##rest of serving time
        return self.tTime   
    def setCustomer(self, c):
        self.customer = c    
    def getCustomerNum(self):
        return self.customer.getCNumber()    
    def getCustomerATime(self):
        return self.customer.getATime()    
    def getCustomerWTime(self):
        return self.customer.getWTime()    
    def getCustomeridTime(self):
        return self.customer.getidTime()   
    def getCustomerbodyTime(self):
        return self.customer.getbodyTime()
    def getCustomerbagsTime(self):
        return self.customer.getbagsTime()
    
class bagsServerList(object):
    def __init__(self, num = 1):
        self.numOfServer = num
        self.servers = []
        for i in range(num):
            self.servers.append(bagsServer())  
    def getFreeServerID(self):
        id = -1
        for i in range(len(self.servers)):
            if self.servers[i].isFree():
                id = i
                break
        return id    
    def getNumofBusyServer(self):
        busyServers = 0
        for s in self.servers:
            if (not s.isFree()):
                busyServers += 1
        return busyServers    
    def setServerBusy(self, id, c, t):
        self.servers[id].setBusy()
        self.servers[id].setTTime(t)
        self.servers[id].setCustomer(c)
    def setServerBusyByCustomer(self, id, c):
        self.servers[id].setBusy()
        self.servers[id].setTTime(c.getbagsTime())
        self.servers[id].setCustomer(c)    
    def updateServers(self):
        for i in range(len(self.servers)):
            if (not self.servers[i].isFree()):
                self.servers[i].decTTime()
                if(self.servers[i].getRemTTime() == 0):
                    self.servers[i].setFree()
 
class WQueue(object):
    def __init__(self):
        self.queue = []
    def updateQueue(self):
        for i in range(len(self.queue)):
            self.queue[i].incWTime()    
    def addCustomer(self, c):
        self.queue.append(c)    
    def removeCustomer(self):
        return self.queue.pop(0)  
    def isEmpty(self):
        return len(self.queue) == 0    
    def length(self):
        return len(self.queue)

def simAir():
    lq1=[]##record queue lenth of any time
    lq2=[]
    
    sTime = int(input("Enter the simulation time: "))
    nidServers = int(input("Enter the number of idservers: "))
    nbodyServers = int(input("Enter the number of bodyservers: "))
    nbagsServers = int(input("Enter the number of bagsservers: "))

    tid = [8.0, 5.0, 11.0, 10.0, 9.0, 9.0, 13.0, 15.0, 12.0, 15.0, 12.0, 15.0, 20.0, 8.0, 8.0, 11.0]
    t1 = [11.0, 12.0, 4.0, 7.0, 8.0, 13.0, 11.0, 9.0, 8.0, 11.0, 15.0, 11.0, 17.0, 13.0, 8.0, 7.0, 12.0, 19.0, 9.0, 37.0, 11.0, 12.0, 12.0, 13.0, 11.0, 7.0, 8.0, 14.0, 7.0, 11.0, 10.0, 28.0, 9.0, 10.0, 11.0, 10.0, 9.0, 12.0, 7.0]
    t2 = [3.0, 2.0, 2.0, 11.0, 2.0, 2.0, 17.0, 26.0, 2.0, 9.0, 2.0, 2.0, 8.0]
    p1 = p2 = p3 =1.35##parameters 
    tid = [int(p1*i) for i in tid]
    t1 = [int(p2*i) for i in tid]
    t2 = [int(p3*i) for i in tid]
    
    idTime = random.choice(tid)
    bodyTime = random.choice(t1)
    bagsTime = random.choice(t2)
   
    tBetweenCArrival = eval(input("Enter the time between customer arrivals: "))
    idsl = idServerList(nidServers)
    bodysl = bodyServerList(nbodyServers)
    bagssl = bagsServerList(nbagsServers)
    wq1 = WQueue()
    wq2 = WQueue()
    waitTime=[]
    totWTime = 0
    aveWTime = 0
    numOfCustomers = 0
    
    y = math.exp(-1.0/tBetweenCArrival)
    for t in range(1, sTime + 1):
        ##Update the server list to decrement the transaction time of each 
        ##busy server by one time unit.
        clist = idsl.updateServers()
        bodysl.updateServers()
        bagssl.updateServers()
        if len(clist)>0:
            for gg in clist:
                wq2.addCustomer(gg)
        ##If the customer’s queue is nonempty, increment the waiting time of 
        ##each customer by one time unit.
        if not wq1.isEmpty():
            wq1.updateQueue()
            lq1.append(len(wq1.queue))
        if not wq2.isEmpty():
            wq2.updateQueue()
            lq2.append(len(wq2.queue))
        ##If a customer arrives, increment the number of customers by 1 and add 
        ##the new customer to the queue.
        if random.uniform(0, 1) > y:
            idT = random.choice(tid)
            bodyT = random.choice(t1)
            bagsT = random.choice(t2)
            wq1.addCustomer(Customer(cN = numOfCustomers, aTime = t, wTime = 0,idTime = idT, bodyTime = bodyT, bagsTime = bagsT))
            numOfCustomers += 1
        
        ##If a server is free and the customer’s queue is nonempty, remove a
        ##customer from the front of the queue and send the customer to
        ##the free server.
        freeID1 = idsl.getFreeServerID()
        while (not wq1.isEmpty()) and (freeID1 != -1):
            c = wq1.removeCustomer()
            idsl.setServerBusyByCustomer(freeID1, c)
            freeID1 = idsl.getFreeServerID()
        freeID2 = bodysl.getFreeServerID()
        while (not wq2.isEmpty()) and (freeID2 != -1):
            c = wq2.removeCustomer()
            totWTime = totWTime + c.getWTime()
            waitTime.append(c.getWTime())
            bodysl.setServerBusyByCustomer(freeID2, c)
            freeID2 = bodysl.getFreeServerID()

    print("The simulation ran for " + str(sTime) + " time units")
    print("Number of idservers: " + str(nidServers))
    print("Number of bodyservers: " + str(nbodyServers))
    print("Number of bagsservers: " + str(nbagsServers))
    print("Average arrival time difference between customers: " + str(tBetweenCArrival))
    print("Total waiting time: " + str(totWTime))
    print("Number of customers that completed a check: " +
          str(numOfCustomers - wq1.length() -wq2.length() - idsl.getNumofBusyServer() - bodysl.getNumofBusyServer()))
    print("The number of customers left in idcheck queue: " + str(wq1.length()))
    print("The number of customers left in bodycheck queue: " + str(wq2.length()))
    print("Number of customers left in the idservers: " + str(idsl.getNumofBusyServer()))
    print("Number of customers left in the bodyservers: " + str(bodysl.getNumofBusyServer()))
    print("Average waiting time: " + str(round(totWTime * 1.0 / numOfCustomers,2)))
    print("Maximum length of waiting idcheck queue: "+str(max(lq1)))
    print("Maximum length of waiting bodycheck queue: "+str(max(lq2)))
    print("Average length of waiting idcheck queue: "+str(math.ceil(sum(lq1)/len(lq1))))
    print("Average length of waiting bodycheck queue: "+str(math.ceil(sum(lq2)/len(lq2))))
    print("sum of queue length of all times: "+str(sum(lq1)+sum(lq2)))
    print("sum of id queue length of all times: "+str(sum(lq1)))
    print("sum of body queue length of all times: "+str(sum(lq2)))
      
simAir()##model with first modefication    (one->all)

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).