fork download
  1. import SimPy.Simulation as sp
  2. import random as rd
  3. import numpy as np
  4.  
  5.  
  6. def interarriv(): # Generates the interarrival time between two clients
  7. return 3 # Original version: probability distribution
  8.  
  9.  
  10. def service(): # Generates the time needed to execute a job
  11. return 2 # Original version: probability distribution
  12.  
  13.  
  14. class ServerClass(sp.Process):
  15. Busy = [] # List of busy servers
  16. Idle = [] # List of available servers
  17. Queue = [] # List of jobs to be executed
  18.  
  19. def __init__(self):
  20. sp.Process.__init__(self)
  21. ServerClass.Idle.append(self) # Saves the server in the list of the available ones
  22.  
  23. def Run(self):
  24. while 1:
  25. yield sp.passivate, self # Waits for a new client to be served
  26. ServerClass.Idle.remove(self) # Moves the server from the idle list to the busy one
  27. ServerClass.Busy.append(self)
  28. while ServerClass.Queue != []:
  29. # Takes the job
  30. J = ServerClass.Queue.pop(0)
  31. # Performs the job
  32. yield sp.hold, self, service()
  33. ServerClass.Busy.remove(self)
  34. ServerClass.Idle.append(self)
  35.  
  36.  
  37. class JobClass:
  38. def __init__(self):
  39. self.ArrivalTime = sp.now()
  40.  
  41.  
  42. class ArrivalClass(sp.Process):
  43. def __init__(self):
  44. sp.Process.__init__(self)
  45.  
  46. def Run(self):
  47. while 1:
  48. # Waits for the arrival of a new client
  49. yield sp.hold, self, interarriv()
  50. J = JobClass()
  51. # Stores the job to be performed
  52. ServerClass.Queue.append(J)
  53. if ServerClass.Idle != []: # If there is an available machine
  54. sp.reactivate(ServerClass.Idle[0])
  55.  
  56.  
  57. if __name__ == '__main__':
  58. NMachines = 1
  59. sp.initialize()
  60.  
  61. for I in range(NMachines):
  62. M = ServerClass()
  63. sp.activate(M,M.Run())
  64.  
  65. A = ArrivalClass()
  66. sp.activate(A,A.Run())
  67.  
  68. MaxSimTime = 104.0
  69. sp.simulate(until=MaxSimTime)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/py_compile.py", line 117, in compile
    raise py_exc
py_compile.PyCompileError: Sorry: IndentationError: ('unindent does not match any outer indentation level', ('prog.py', 24, 16, '       while 1:\n'))
stdout
Standard output is empty