File: Machineshop.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (101 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from SimPy.Simulation import *
import random
"""
Machineshop Model

An example showing interrupts and priority queuing.

Scenario:

A workshop has n identical machines. A stream of jobs (enough to keep
the machines busy) arrives. Each machine breaks down
periodically. Repairs are carried out by one repairman. The repairman
has other, less important tasks to perform, too. Once he starts one of
those, he completes it before starting with the machine repair. The
workshop works continously.  """

## Model components ------------------------

class Machine(Process):
    def __init__(self,name):
        Process.__init__(self,name)
        myBreaker=Breakdown(self)
        activate(myBreaker,myBreaker.breakmachine())
        self.partsMade=0

    def working(self):
        while True:
            yield hold,self,timePerPart()
            if self.interrupted():
                # broken down
                parttimeleft=self.interruptLeft
                yield request,self,repairman,1
                yield hold,self,repairtime
                #repaired
                yield release,self,repairman
                yield hold,self,parttimeleft
                # part completed
                self.partsMade += 1
            else:
                #part made
                self.partsMade += 1

class Breakdown(Process):
    def __init__(self,myMachine):
        Process.__init__(self)
        self.myMachine=myMachine

    def breakmachine(self):
        while True:
            yield hold,self,timeToFailure()
            self.interrupt(self.myMachine)

class OtherJobs(Process):
    def __init__(self):
        Process.__init__(self)  

    def doingJobs(self):
        while True:
            yield request,self,repairman,0
            #starts working on jobs
            yield hold,self,jobDuration
            yield release,self,repairman
            
def timePerPart():
    return random.normalvariate(processingTimeMean,processingTimeSigma)

def timeToFailure():
    return random.expovariate(mean)

## Experiment data -------------------------

repairtime=30.0           # minutes
processingTimeMean=10.0   # minutes
processingTimeSigma=2.0
timeToFailureMean = 300.0 # minutes
mean=1/timeToFailureMean  # per minute
jobDuration=30            # minutes
nrMachines=10
random.seed(111333555)
weeks = 4 # weeks
simTime = weeks*24*60*7 ## minutes

## Model/Experiment ------------------------------

print 'Machineshop'
initialize()
repairman=Resource(capacity=1,qType=PriorityQ)
m={}
for i in range(nrMachines):
    m[i+1]=Machine(name="Machine %s" %(i+1))
    activate(m[i+1],m[i+1].working())
oj=OtherJobs()
activate(oj,oj.doingJobs())
simulate(until=simTime) ## minutes

## Analysis/output -------------------------

print "Machineshop results after %s weeks"%weeks
for i in range(nrMachines):
    print "Machine %s: %s" %(i+1,m[i+1].partsMade)