import SimPy.Simulation as sp
import random as rd
import numpy as np
def interarriv(): # Generates the interarrival time between two clients
return 3 # Original version: probability distribution
def service(): # Generates the time needed to execute a job
return 2 # Original version: probability distribution
class ServerClass(sp.Process):
Busy = [] # List of busy servers
Idle = [] # List of available servers
Queue = [] # List of jobs to be executed
def __init__(self):
sp.Process.__init__(self)
ServerClass.Idle.append(self) # Saves the server in the list of the available ones
def Run(self):
while 1:
yield sp.passivate, self # Waits for a new client to be served
ServerClass.Idle.remove(self) # Moves the server from the idle list to the busy one
ServerClass.Busy.append(self)
while ServerClass.Queue != []:
# Takes the job
J = ServerClass.Queue.pop(0)
# Performs the job
yield sp.hold, self, service()
ServerClass.Busy.remove(self)
ServerClass.Idle.append(self)
class JobClass:
def __init__(self):
self.ArrivalTime = sp.now()
class ArrivalClass(sp.Process):
def __init__(self):
sp.Process.__init__(self)
def Run(self):
while 1:
# Waits for the arrival of a new client
yield sp.hold, self, interarriv()
J = JobClass()
# Stores the job to be performed
ServerClass.Queue.append(J)
if ServerClass.Idle != []: # If there is an available machine
sp.reactivate(ServerClass.Idle[0])
if __name__ == '__main__':
NMachines = 1
sp.initialize()
for I in range(NMachines):
M = ServerClass()
sp.activate(M,M.Run())
A = ArrivalClass()
sp.activate(A,A.Run())
MaxSimTime = 104.0
sp.simulate(until=MaxSimTime)