Machineshop_OO.py :  » Development » SimPy » SimPy-2.1.0beta » SimPyModels » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SimPy 
SimPy » SimPy 2.1.0beta » SimPyModels » Machineshop_OO.py
from SimPy.Simulation import *
import random
""" Machineshop_OO.py
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,sim):
        Process.__init__(self,name=name,sim=sim)
        myBreaker=Breakdown(self,sim=sim)
        sim.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,self.sim.repairman,1
                yield hold,self,repairtime
                #repaired
                yield release,self,self.sim.repairman
                yield hold,self,parttimeleft
                # part completed
                self.partsMade += 1
            else:
                #part made
                self.partsMade += 1

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

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

class OtherJobs(Process):

    def doingJobs(self):
        while True:
            yield request,self,self.sim.repairman,0
            #starts working on jobs
            yield hold,self,jobDuration
            yield release,self,self.sim.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
class MachineshopModel(Simulation):
    def run(self):
        print 'Machineshop'
        self.initialize()
        self.repairman=Resource(capacity=1,qType=PriorityQ,sim=self)
        self.m={}
        for i in range(nrMachines):
            self.m[i+1]=Machine(name="Machine %s" %(i+1),sim=self)
            self.activate(self.m[i+1],self.m[i+1].working())
        oj=OtherJobs(sim=self)
        self.activate(oj,oj.doingJobs())
        self.simulate(until=simTime) ## minutes
        
## Experiment ------------------------------
model=MachineshopModel()
model.run()

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

print "Machineshop results after %s weeks"%weeks
for i in range(nrMachines):
    print "Machine %s: %s" %(i+1,model.m[i+1].partsMade)
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.