Debian Codegraph @

docs/examples/Machineshop_OO.py

raw symbols SCIP
1 from SimPy.Simulation import *
2 import random
3 """ Machineshop_OO.py
4 Machineshop Model
5
6 An example showing interrupts and priority queuing.
7
8 Scenario:
9
10 A workshop has n identical machines. A stream of jobs (enough to keep
11 the machines busy) arrives. Each machine breaks down
12 periodically. Repairs are carried out by one repairman. The repairman
13 has other, less important tasks to perform, too. Once he starts one of
14 those, he completes it before starting with the machine repair. The
15 workshop works continously. """
16
17 ## Model components ------------------------
18
19 class Machine(Process):
20 def __init__(self,name,sim):
21 Process.__init__(self,name=name,sim=sim)
22 myBreaker=Breakdown(self,sim=sim)
23 sim.activate(myBreaker,myBreaker.breakmachine())
24 self.partsMade=0
25
26 def working(self):
27 while True:
28 yield hold,self,timePerPart()
29 if self.interrupted():
30 # broken down
31 parttimeleft=self.interruptLeft
32 yield request,self,self.sim.repairman,1
33 yield hold,self,repairtime
34 #repaired
35 yield release,self,self.sim.repairman
36 yield hold,self,parttimeleft
37 # part completed
38 self.partsMade += 1
39 else:
40 #part made
41 self.partsMade += 1
42
43 class Breakdown(Process):
44 def __init__(self,myMachine,sim):
45 Process.__init__(self,sim=sim)
46 self.myMachine=myMachine
47
48 def breakmachine(self):
49 while True:
50 yield hold,self,timeToFailure()
51 self.interrupt(self.myMachine)
52
53 class OtherJobs(Process):
54
55 def doingJobs(self):
56 while True:
57 yield request,self,self.sim.repairman,0
58 #starts working on jobs
59 yield hold,self,jobDuration
60 yield release,self,self.sim.repairman
61
62 def timePerPart():
63 return random.normalvariate(processingTimeMean,processingTimeSigma)
64
65 def timeToFailure():
66 return random.expovariate(mean)
67
68 ## Experiment data -------------------------
69
70 repairtime=30.0 # minutes
71 processingTimeMean=10.0 # minutes
72 processingTimeSigma=2.0
73 timeToFailureMean = 300.0 # minutes
74 mean=1/timeToFailureMean # per minute
75 jobDuration=30 # minutes
76 nrMachines=10
77 random.seed(111333555)
78 weeks = 4 # weeks
79 simTime = weeks*24*60*7 ## minutes
80
81 ## Model
82 class MachineshopModel(Simulation):
83 def run(self):
84 print 'Machineshop'
85 self.initialize()
86 self.repairman=Resource(capacity=1,qType=PriorityQ,sim=self)
87 self.m={}
88 for i in range(nrMachines):
89 self.m[i+1]=Machine(name="Machine %s" %(i+1),sim=self)
90 self.activate(self.m[i+1],self.m[i+1].working())
91 oj=OtherJobs(sim=self)
92 self.activate(oj,oj.doingJobs())
93 self.simulate(until=simTime) ## minutes
94
95 ## Experiment ------------------------------
96 model=MachineshopModel()
97 model.run()
98
99 ## Analysis/output -------------------------
100
101 print "Machineshop results after %s weeks"%weeks
102 for i in range(nrMachines):
103 print "Machine %s: %s" %(i+1,model.m[i+1].partsMade)
104