def run(self, forward=True): """ used in DejaVu/scenarioInterface/animationGUI.py animClip.makeAndRunMAA """ if len(self.maas) == 0: #print "no actors" return initialize() redraw = self.redrawActor actors = self.actors afterAnimation = self.afterAnimation start = self.firstPosition end = self.lastPosition if redraw: redraw.object.stopAutoRedraw() proc = RedrawActorProcess(redraw) activate(proc, proc.execute(), at=start, prior=True) #print 'activated redraw', for actor in actors: if actor == redraw: continue proc = ActorProcess(actor) activate(proc, proc.execute(), at=start, prior=True) simulate(until=end) #self.currentFrame = self.endFrame if forward: #self.currentFrame = self.endFrame self.currentFrame = end else: self.currentFrame = start # call callback after animation completes for f in afterAnimation: f() #self.afterAnimation_cb() if redraw: redraw.object.startAutoRedraw()
def run(self, forward=True): """ used in DejaVu/scenarioInterface/animationGUI.py animClip.makeAndRunMAA """ if len(self.maas)==0: #print "no actors" return initialize() redraw = self.redrawActor actors = self.actors afterAnimation = self.afterAnimation start = self.firstPosition end = self.lastPosition if redraw: redraw.object.stopAutoRedraw() proc = RedrawActorProcess(redraw) activate(proc, proc.execute(), at=start, prior=True) #print 'activated redraw', for actor in actors: if actor == redraw:continue proc = ActorProcess(actor) activate(proc, proc.execute(), at=start, prior=True) simulate(until=end) #self.currentFrame = self.endFrame if forward: #self.currentFrame = self.endFrame self.currentFrame = end else: self.currentFrame = start # call callback after animation completes for f in afterAnimation: f() #self.afterAnimation_cb() if redraw: redraw.object.startAutoRedraw()
def main(): #create an empty list to store all the objects in G.ObjList=[] #user inputs the id of the JSON file topologyId=raw_input("give the path to the CMSD file\n") try: G.CMSDFile=open(str(topologyId), "r") except IOError: print "no such file. The programm is terminated" sys.exit() start=time.time() #start counting execution time #read the input from the CMSD file and create the line G.InputData=G.CMSDFile.read() G.CMSDData=parseString(G.InputData) readGeneralInput() readResources() readProcessIdentifiersSequence() readProcesses() setPredecessorIDs() setSuccessorIDs() setTopology() #run the experiment (replications) for i in range(G.numberOfReplications): print "start run number "+str(i+1) G.seed+=1 G.Rnd=Random(G.seed) initialize() #initialize the simulation initializeObjects() activateObjects() simulate(until=G.maxSimTime) #start the simulation #carry on the post processing operations for every object in the topology for j in range(len(G.ObjList)): G.ObjList[j].postProcessing(G.maxSimTime) #output trace to excel if(G.trace=="Yes"): G.traceFile.save('trace'+str(i+1)+'.xls') G.traceIndex=0 #index that shows in what row we are G.sheetIndex=1 #index that shows in what sheet we are G.traceFile = xlwt.Workbook() #create excel file G.traceSheet = G.traceFile.add_sheet('sheet '+str(G.sheetIndex), cell_overwrite_ok=True) #create excel sheet G.outputSheet.write(G.outputIndex,0, "Execution Time") G.outputSheet.write(G.outputIndex,1, str(time.time()-start)+" seconds") G.outputIndex+=2 #output data to excel for every object in the topology for j in range(len(G.ObjList)): G.ObjList[j].outputResultsXL(G.maxSimTime) G.outputFile.save("output.xls") print "execution time="+str(time.time()-start)
def main(): start = time.time() parser = CustomArgsParser(optFlags=['--verify']) parser.parse(sys.argv[1:]) if len(parser.getPosArgs()) < 1: print 'python sim.py <config dir> --verify' sys.exit(-1) path = parser.getPosArg(0) configFile = '%s/__config__' % path configs = Configuration() configs.read(configFile) if parser.getOption('--verify'): configs['system.should.verify'] = True #simulation init #logging.basicConfig(level=logging.DEBUG) logging.config.fileConfig('%s/__logcfg__' % path) logger = logging.getLogger(__name__) #simpy initialize initialize() #system initialize RTI.initialize(configs) txnGenCls = loadClass(configs['txn.gen.impl']) txnGen = txnGenCls(configs) systemCls = loadClass(configs['system.impl']) system = systemCls(configs) for txn, at in txnGen.generate(): system.schedule(txn, at) system.start() #pdb.set_trace() #simulate logger.info('\n##### START SIMULATION #####\n') simulate(until=configs.get('simulation.duration', 600000)) logger.info('simulated time: %s' % now()) logger.info('\n##### END #####\n') ##verify try: if parser.getOption('--verify'): logger.info('\n##### START VERIFICATION #####\n') v = Verifier() v.check(system) logger.info('VERIFICATION SUCCEEDS\n') logger.info('\n##### END #####\n') except: logger.error('Verification failed.') #get profile logger.info('\n##### PROFILING RESULTS #####\n') system.profile() #system.printMonitor() logger.info('\n##### END #####\n') end = time.time() logger.info('\n##### SIMULATION TIME: %s seconds #####\n' % (end - start))
def main(): start = time.time() parser = CustomArgsParser(optFlags=['--verify']) parser.parse(sys.argv[1:]) if len(parser.getPosArgs()) < 1: print 'python sim.py <config dir> --verify' sys.exit(-1) path = parser.getPosArg(0) configFile = '%s/__config__' %path configs = Configuration() configs.read(configFile) if parser.getOption('--verify'): configs['system.should.verify'] = True #simulation init #logging.basicConfig(level=logging.DEBUG) logging.config.fileConfig('%s/__logcfg__' %path) logger = logging.getLogger(__name__) #simpy initialize initialize() #system initialize RTI.initialize(configs) txnGenCls = loadClass(configs['txn.gen.impl']) txnGen = txnGenCls(configs) systemCls = loadClass(configs['system.impl']) system = systemCls(configs) for txn, at in txnGen.generate(): system.schedule(txn, at) system.start() #pdb.set_trace() #simulate logger.info('\n##### START SIMULATION #####\n') simulate(until=configs.get('simulation.duration', 600000)) logger.info('simulated time: %s' %now()) logger.info('\n##### END #####\n') ##verify try: if parser.getOption('--verify'): logger.info('\n##### START VERIFICATION #####\n') v = Verifier() v.check(system) logger.info('VERIFICATION SUCCEEDS\n') logger.info('\n##### END #####\n') except: logger.error('Verification failed.') #get profile logger.info('\n##### PROFILING RESULTS #####\n') system.profile() #system.printMonitor() logger.info('\n##### END #####\n') end = time.time() logger.info('\n##### SIMULATION TIME: %s seconds #####\n' %(end - start))
def run(self, forward=True): """ used in DejaVu/scenarioInterface/animationPanels.py AnimationPanel.runMAA """ if len(self.actors) == 0: #print "no actors" return initialize() redraw = self.redrawActor start = 0 #end = self.endFrame self.maxFrame = 0 maxFrame = self.getLastFrameWithChange() if maxFrame is not None: self.maxFrame = maxFrame.pos end = self.maxFrame gui = self.gui if gui: if not (gui.startFrame == 0 and gui.stopFrame == 1): start = gui.startFrame end = gui.stopFrame self.maxFrame = end + start self.moveForward = forward if redraw: redraw.object.stopAutoRedraw() proc = RedrawActorProcess(redraw) activate(proc, proc.execute(), at=start, prior=True) #print 'activated redraw', for actor in self.actors: if actor.name != "redraw": proc = ActorProcess(actor) activate(proc, proc.execute(), at=start, prior=True) #print 'activated ', actor.name, action #print 'simulate', self.endFrame simulate(until=end) #self.currentFrame = self.endFrame if forward: #self.currentFrame = self.endFrame self.currentFrame = end else: self.currentFrame = start # call callback after animation completes for f in self.afterAnimation: f() #self.afterAnimation_cb() if self.redrawActor: vi = self.redrawActor.object vi.startAutoRedraw()
def run(self, forward=True): """ used in DejaVu/scenarioInterface/animationPanels.py AnimationPanel.runMAA """ if len(self.actors)==0: #print "no actors" return initialize() redraw = self.redrawActor start = 0 #end = self.endFrame self.maxFrame = 0 maxFrame = self.getLastFrameWithChange() if maxFrame is not None: self.maxFrame = maxFrame.pos end = self.maxFrame gui = self.gui if gui: if not (gui.startFrame == 0 and gui.stopFrame == 1): start = gui.startFrame end = gui.stopFrame self.maxFrame = end + start self.moveForward = forward if redraw: redraw.object.stopAutoRedraw() proc = RedrawActorProcess(redraw) activate(proc, proc.execute(), at=start, prior=True) #print 'activated redraw', for actor in self.actors: if actor.name != "redraw": proc = ActorProcess(actor) activate(proc, proc.execute(), at=start, prior=True) #print 'activated ', actor.name, action #print 'simulate', self.endFrame simulate(until=end) #self.currentFrame = self.endFrame if forward: #self.currentFrame = self.endFrame self.currentFrame = end else: self.currentFrame = start # call callback after animation completes for f in self.afterAnimation: f() #self.afterAnimation_cb() if self.redrawActor: vi = self.redrawActor.object vi.startAutoRedraw()
def test(): try: numZones = int(sys.argv[1]) numSNodes = int(sys.argv[2]) except: numZones = 2 numSNodes = 2 print numZones, numSNodes #initialize logging.basicConfig(level=logging.DEBUG) configs = { 'max.num.txns.per.storage.node' : 1, 'nw.latency.within.zone' : ('fixed', 0), 'nw.latency.cross.zone' : ('fixed', 0), } groups = {} for i in range(numSNodes): groups[i] = 128 configs['dataset.groups'] = groups configs['num.zones'] = numZones configs['num.storage.nodes.per.zone'] = numSNodes initialize() RTI.initialize(configs) system = BaseSystem(configs) #txn generation curr = 0 for i in range(TEST_NUM_TXNS): txnID = i zoneID = random.randint(0, configs['num.zones'] - 1) gid = random.randint(0, numSNodes - 1) txn = FakeTxn(txnID, zoneID, gid) at = curr + RandInterval.get( 'expo', TEST_TXN_ARRIVAL_PERIOD / TEST_NUM_TXNS).next() curr = at system.schedule(txn, at) logging.info('txnID=%s, zoneID=%s, gids=%s at=%s' %(txnID, zoneID, txn.gids, at)) #start simulation system.start() simulate(until=2 * TEST_TXN_ARRIVAL_PERIOD) #profile system.profile() #calculate m/m/s loss rate lambd = float(TEST_NUM_TXNS) / TEST_TXN_ARRIVAL_PERIOD mu = 1 / float(100) print erlangLoss(lambd / numZones / numSNodes, mu, 1) print erlangLoss(lambd / numZones, mu, numSNodes)
def test(): try: numZones = int(sys.argv[1]) numSNodes = int(sys.argv[2]) except: numZones = 2 numSNodes = 2 print numZones, numSNodes #initialize logging.basicConfig(level=logging.DEBUG) configs = { 'max.num.txns.per.storage.node': 1, 'nw.latency.within.zone': ('fixed', 0), 'nw.latency.cross.zone': ('fixed', 0), } groups = {} for i in range(numSNodes): groups[i] = 128 configs['dataset.groups'] = groups configs['num.zones'] = numZones configs['num.storage.nodes.per.zone'] = numSNodes initialize() RTI.initialize(configs) system = BaseSystem(configs) #txn generation curr = 0 for i in range(TEST_NUM_TXNS): txnID = i zoneID = random.randint(0, configs['num.zones'] - 1) gid = random.randint(0, numSNodes - 1) txn = FakeTxn(txnID, zoneID, gid) at = curr + RandInterval.get( 'expo', TEST_TXN_ARRIVAL_PERIOD / TEST_NUM_TXNS).next() curr = at system.schedule(txn, at) logging.info('txnID=%s, zoneID=%s, gids=%s at=%s' % (txnID, zoneID, txn.gids, at)) #start simulation system.start() simulate(until=2 * TEST_TXN_ARRIVAL_PERIOD) #profile system.profile() #calculate m/m/s loss rate lambd = float(TEST_NUM_TXNS) / TEST_TXN_ARRIVAL_PERIOD mu = 1 / float(100) print erlangLoss(lambd / numZones / numSNodes, mu, 1) print erlangLoss(lambd / numZones, mu, numSNodes)
def testRTI(): print '\n>>> testRTI\n' configs = { 'nw.latency.within.zone' : ('uniform', 10, {'lb' : 5, 'ub' : 15}), 'nw.latency.cross.zone' : ('norm', 100, {'lb' : 50, 'ub' : 150, 'sigma' : 100}), } initialize() RTI.initialize(configs) server0 = Server('zone0/server') server1 = Server('zone1/server') client = Client('zone0/client', [server0, server1]) server0.start() server1.start() client.start() simulate(until=1000)
def test(nodeadlock=True): logging.basicConfig(level=logging.DEBUG) initialize() tanks = [] threads = [] for i in range(NUM_TANKS): tank = Tank(i) tank.value = TOTAL_FLOW / NUM_TANKS tanks.append(tank) tanks[0].value += (TOTAL_FLOW - TOTAL_FLOW / NUM_TANKS * NUM_TANKS) for i in range(NUM_FLOWERS): flow = Flow(i, tanks, nodeadlock) flow.start() threads.append(flow) for i in range(NUM_CHECKERS): if i == 0: checker = Checker(i, tanks, True) elif i == 1: checker = Checker(i, tanks, not nodeadlock) else: checker = Checker(i, tanks, False) checker.start() threads.append(checker) simulate(until=10 * OPERATION_TIME) verifyTanks(tanks) verifyThreads(threads) print 'TEST PASSED' print('numFlowTxns=%s, numCheckTxns=%s, numUpFlowTxns=%s' % (numFlowTxns, numCheckTxns, numUpFlowTxns)) print( 'numAbortedFlowTxns=%s, numAbortedCheckTxns=%s, numAbortedUpFlowTxns=%s' % (numAbortedFlowTxns, numAbortedCheckTxns, numAbortedUpFlowTxns)) for thread in threads: runTime, std, histo, count = \ thread.monitor.getElapsedStats('.*txn') waitTime, std, histo, count = \ thread.monitor.getElapsedStats('.*%s'%LockThread.LOCK_BLOCK_KEY) print '%s runtime=%s, waittime=%s' % (thread.ID, runTime, waitTime)
def test(nodeadlock=True): logging.basicConfig(level=logging.DEBUG) initialize() tanks = [] threads = [] for i in range(NUM_TANKS): tank = Tank(i) tank.value = TOTAL_FLOW / NUM_TANKS tanks.append(tank) tanks[0].value += (TOTAL_FLOW - TOTAL_FLOW / NUM_TANKS * NUM_TANKS) for i in range(NUM_FLOWERS): flow = Flow(i, tanks, nodeadlock) flow.start() threads.append(flow) for i in range(NUM_CHECKERS): if i == 0: checker = Checker(i, tanks, True) elif i == 1: checker = Checker(i, tanks, not nodeadlock) else: checker = Checker(i, tanks, False) checker.start() threads.append(checker) simulate(until=10 * OPERATION_TIME) verifyTanks(tanks) verifyThreads(threads) print 'TEST PASSED' print ('numFlowTxns=%s, numCheckTxns=%s, numUpFlowTxns=%s' %(numFlowTxns, numCheckTxns, numUpFlowTxns)) print ('numAbortedFlowTxns=%s, numAbortedCheckTxns=%s, numAbortedUpFlowTxns=%s' %(numAbortedFlowTxns, numAbortedCheckTxns, numAbortedUpFlowTxns)) for thread in threads: runTime, std, histo, count = \ thread.monitor.getElapsedStats('.*txn') waitTime, std, histo, count = \ thread.monitor.getElapsedStats('.*%s'%LockThread.LOCK_BLOCK_KEY) print '%s runtime=%s, waittime=%s' %(thread.ID, runTime, waitTime)
Q2.defineRouting([M2]) argumentDict={'from':'Q2','to':'E1','safetyStock':70,'consumption':20} EG=EventGenerator(id="EV", name="ExcessEntitiesMover" ,start=60, interval=60, method=Globals.moveExcess, argumentDict=argumentDict) G.ObjList=[S,M1,M2,E,Q1,Q2,EG] initialize() #initialize the simulation (SimPy method) for object in G.ObjList: object.initialize() for object in G.ObjList: activate(object, object.run()) G.maxSimTime=400 simulate(until=G.maxSimTime) #run the simulation #carry on the post processing operations for every object in the topology for object in G.ObjList: object.postProcessing() ExcelHandler.outputTrace('TRACE') print "the system produced", E.numOfExits, "parts" print "the waiting ratio of", M1.objName, "is", (M1.totalWaitingTime/G.maxSimTime)*100, "%" print "the waiting ratio of", M2.objName, "is", (M2.totalWaitingTime/G.maxSimTime)*100, "%"
class Federation(Process): def fight(self): # simulate Federation operations print('Three %s attempting to escape!' % (target.name)) while True: if random.randint(0, 10) < 2: # check for hit on player target.damage += 1 # hit! increment damage to player if target.damage <= 5: # target survives print('Ha! %s hit! Damage= %i' % (target.name, target.damage)) else: if (target.lives - 1) == 0: print('No more %s left!' % (target.name)) else: print('Now only %i %s left!' % (target.lives - 1, target.name)) yield hold, self, 1 initialize() gameOver = 100 # create a Player object named "Romulans" target = Player(lives=3, name='Romulans') activate(target, target.life()) # create a Federation object shooter = Federation() activate(shooter, shooter.fight()) simulate(until=gameOver) print(target.message)
options.profile, options.rate)) w = Watcher(options.max_wait, options.instance_type, options.latency, options.as_period, options.as_breach_duration, options.as_min_size, options.as_max_size, options.as_cooldown, options.as_lower_threshold, options.as_lower_breach_scale_increment, options.as_upper_threshold, options.as_upper_breach_scale_increment) activate(w, w.execute()) simulate(until=options.duration) Server.total() Server.total_capacity.observe(len(Server.busy) + len(Server.idle)) print print "server utilization :%6.2f%%" % Watcher.cpu_utilization.timeAverage() count = 0 for i, y in itertools.izip(itertools.count(), sorted(Server.time_in_system.yseries())): count = i + 1 if y > 30.0: break print "percentage of requests served within 30s:%6.2f%%" % (count * 100.0 / (Server.jobs_timed_out + Server.jobs_processed)) print "percentage timed out (%3d seconds) :%6.2f%%" % (options.max_wait, Server.jobs_timed_out * 100.00 / (Server.jobs_timed_out + Server.jobs_processed)) print "server time wasted :%6.2f%%" % (Server.server_time_wasted * 100.0 / Server.server_time_used) print "mean time in system (seconds) : %.2f" % Server.time_in_system.mean() print "total jobs processed : %d" % Server.jobs_processed
tripleft = self.interruptLeft # time to finish the trip self.interruptReset() # end interrupt state reactivate(br, delay=repairduration) # restart breakdown br yield hold, self, repairduration # delay for repairs print('Bus repaired at %s' % now()) else: break # no more breakdowns, bus finished trip print('Bus has arrived at %s' % now()) class Breakdown(Process): def __init__(self, myBus): Process.__init__(self, name='Breakdown ' + myBus.name) self.bus = myBus def breakBus(self, interval): # process execution method while True: yield hold, self, interval # breakdown interarrivals if self.bus.terminated(): break self.interrupt(self.bus) # breakdown to myBus initialize() b = Bus('Bus') # create a bus object activate(b, b.operate(repairduration=20, triplength=1000)) br = Breakdown(b) # create breakdown br to bus b activate(br, br.breakBus(300)) simulate(until=4000) print('SimPy: No more events at time %s' % now())
def __init__(self, epidemic_params): """The constructor of the class. Full params' list: - nr_individuals: number of individuals in the population - initial_infects: number of infects at the start of the simulation - initial_immunes: number of immunes at the start of the simulation - infect_prob: probability of getting infected after a contact with an infect - contact_rate: rate of the contact between individuals - recover_rate: rate of recover from infection - immune_after_recovery: if true, individuals becomes immunes after recovery - immunization_vanish_rate: if not zero, immunization is temporary with given rate - death_rate: rate of death caused by epidemic - newborn_can_be_infect: if true, newborn can be infect - newborn_can_be_immune: if true, newborn can be immune - newborn_prob: probability that a new individual born after a contact - natural_death_prob: probability of death not caused by epidemic - run_time: time duration of the simulation - debug: show more info about the running simulation - process_debug: show more info about SimPy processes during the simulation - progress: show a progress indicator during the simulation - stats: show some stats at the end of the simulation - plot: show a plot representing the evolution of the population at the end of the simulation """ # setting up the epidemic's parameters with metaprogramming for param in epidemic_params: self.__dict__[param] = epidemic_params.get(param) # setting the uninitialized parameters to their default values self.check_and_set_default_value(['initial_immunes', 'recover_rate', 'death_rate', 'immunization_vanish_rate', 'newborn_prob', 'natural_death_prob'], ['immune_after_recovery', 'newborn_can_be_immune', 'newborn_can_be_infect', 'debug', 'process_debug', 'progress', 'stats', 'plot']) # setting the random number generator using the python standard one self.rng = random.Random() # checking some features of the model from parameters passed to the constructor self.model_has_immunization = self.model_has_immunization() self.model_immunization_is_permanent = self.model_immunization_is_permanent() self.model_has_recovering = self.model_has_recovering() self.model_has_death = self.model_has_death() self.model_has_vital_dynamics = self.model_has_vital_dynamics() self.model_newborns_always_susceptibles = self.model_newborns_always_susceptibles() self.model_has_new_susceptibles = self.model_has_new_susceptibles() self.model_has_new_infects = self.model_has_new_infects() # initialize the population counters self.total_infects = self.initial_infects self.total_immunes = self.initial_immunes self.total_susceptibles = self.nr_individuals - self.initial_infects - self.initial_immunes self.total_newborns = 0 self.total_natural_deaths = 0 self.total_deaths = 0 # setting up the monitors for watching interesting variables self.m_suscettibili = Monitor(name="Suscettibili", ylab="suscettibili") self.m_suscettibili.append([0, self.total_susceptibles]) self.m_infetti = Monitor(name="Infetti", ylab='infetti') self.m_infetti.append([0, self.initial_infects]) if self.model_has_immunization: self.m_immuni = Monitor(name="Immuni", ylab='immuni') self.m_immuni.append([0, self.initial_immunes]) # setting up the array of all the individuals partecipating to the simulation self.all_individuals = [] # initialize the simulation environment (time, events, ...) initialize() for i in range(self.nr_individuals): # add individuals to the simulation with the specified health_status if i >= (self.nr_individuals - self.initial_infects): ind = self.Individual(self, ind_id=i, health_status='infect') elif i >= (self.nr_individuals - self.initial_infects - self.initial_immunes): ind = self.Individual(self, ind_id=i, health_status='immune') else: ind = self.Individual(self, ind_id=i) # activate it with function live() activate(ind, ind.live(), at=0.0) self.start_time = time.time() if self.process_debug: self.show_processes_status() # start the simulation simulate(until=self.run_time) self.stop_time = time.time() if self.process_debug: self.show_processes_status() # show final stats if required by params if self.stats: self.show_stats() # show plot if required by params if self.plot: self.show_plot()
def run(self, *a, **kw): kw['until'] = kw.get('until', getattr(self, 'runtime', 100)) self.runtime = kw['until'] initialize() self.start(*a, **kw) simulate(*a, **kw)
interval=60, method=Globals.moveExcess, argumentDict=argumentDict, ) G.ObjList = [S, M1, M2, E, Q1, Q2, EG] initialize() # initialize the simulation (SimPy method) for object in G.ObjList: object.initialize() for object in G.ObjList: activate(object, object.run()) G.maxSimTime = 400 simulate(until=G.maxSimTime) # run the simulation # carry on the post processing operations for every object in the topology for object in G.ObjList: object.postProcessing() ExcelHandler.outputTrace("TRACE") print(("the system produced", E.numOfExits, "parts")) print(( "the waiting ratio of", M1.objName, "is", (M1.totalWaitingTime / G.maxSimTime) * 100, "%", ))
# excess of demand over current stock amount if dd > stock.amount: # can't supply requested amount yield get, self, stock, stock.amount # supply all available amount self.stockout += ds # add unsupplied demand to self.stockout print('day %6.4f, demand = %6.4f, shortfall = %6.4f' % (now(), dd, -ds)) else: # can supply requested amount yield get, self, stock, dd print('day %6.4f, supplied %6.4f, now amount = %6.4f' % (now(), dd, stock.amount)) stock = Level(monitored=True) # 'unbounded' capacity and other defaults seed(99999) initialize() offeror = Deliver() activate(offeror, offeror.deliver()) requester = Demand() activate(requester, requester.demand()) simulate(until=49.9) result = (stock.bufferMon.mean(), requester.stockout) print('') print('Summary of results through end of day %6.4f:' % int(now())) print('average stock = %6.4f, cumulative stockout = %6.4f' % result)
from SimPy.Simulation import Process, activate, hold, initialize, simulate class Customer(Process): def buy(self, budget=0): print('Here I am at the shops %s' % self.name) t = 5.0 for i in range(4): yield hold, self, t # executed 4 times at intervals of t time units print('I just bought something %s' % self.name) budget -= 10.00 print('All I have left is %s I am going home %s' % (budget, self.name)) initialize() # create a customer named "Evelyn", C = Customer(name='Evelyn') # and activate her with a budget of 100 activate(C, C.buy(budget=100), at=10.0) simulate(until=100.0)
def main(): #create an empty list to store all the objects in G.ObjList = [] #user inputs the id of the JSON file topologyId = raw_input("give the path to the CMSD file\n") try: G.CMSDFile = open(str(topologyId), "r") except IOError: print "no such file. The programm is terminated" sys.exit() start = time.time() #start counting execution time #read the input from the CMSD file and create the line G.InputData = G.CMSDFile.read() G.CMSDData = parseString(G.InputData) readGeneralInput() readResources() readProcessIdentifiersSequence() readProcesses() setPredecessorIDs() setSuccessorIDs() setTopology() #run the experiment (replications) for i in range(G.numberOfReplications): print "start run number " + str(i + 1) G.seed += 1 G.Rnd = Random(G.seed) initialize() #initialize the simulation initializeObjects() activateObjects() simulate(until=G.maxSimTime) #start the simulation #carry on the post processing operations for every object in the topology for j in range(len(G.ObjList)): G.ObjList[j].postProcessing(G.maxSimTime) #output trace to excel if (G.trace == "Yes"): G.traceFile.save('trace' + str(i + 1) + '.xls') G.traceIndex = 0 #index that shows in what row we are G.sheetIndex = 1 #index that shows in what sheet we are G.traceFile = xlwt.Workbook() #create excel file G.traceSheet = G.traceFile.add_sheet( 'sheet ' + str(G.sheetIndex), cell_overwrite_ok=True) #create excel sheet G.outputSheet.write(G.outputIndex, 0, "Execution Time") G.outputSheet.write(G.outputIndex, 1, str(time.time() - start) + " seconds") G.outputIndex += 2 #output data to excel for every object in the topology for j in range(len(G.ObjList)): G.ObjList[j].outputResultsXL(G.maxSimTime) G.outputFile.save("output.xls") print "execution time=" + str(time.time() - start)
(now(), [x.weight for x in self.got])) yield hold, self, 11 class Widget(Lister): def __init__(self, weight=0): self.weight = weight widgbuf = [] for i in range(10): widgbuf.append(Widget(5)) initialize() buf = Store(capacity=11, initialBuffered=widgbuf, monitored=True) for i in range(3): # define and activate 3 producer objects p = ProducerD() activate(p, p.produce()) for i in range(3): # define and activate 3 consumer objects c = ConsumerD() activate(c, c.consume()) simulate(until=50) print('LenBuffer: %s' % buf.bufferMon) # length of buffer print('getQ: %s' % buf.getQMon) # length of getQ print('putQ %s' % buf.putQMon) # length of putQ
def main(): initialize() testAlarm() testThread() simulate(until=1000) testTanjanAlgo()
from SimPy.Simulation import Process, activate, initialize, hold, now, simulate class Message(Process): """A simple Process""" def __init__(self, i, len): Process.__init__(self, name='Message' + str(i)) self.i = i self.len = len def go(self): print('%s %s %s' % (now(), self.i, 'Starting')) yield hold, self, 100.0 print('%s %s %s' % (now(), self.i, 'Arrived')) initialize() p1 = Message(1, 203) # new message activate(p1, p1.go()) # activate it p2 = Message(2, 33) activate(p2, p2.go(), at=6.0) simulate(until=200) print('Current time is %s' % now()) # will print 106.0
print('%s car %s done by %s' % (now(), self.name, whichWash)) class CarGenerator(Process): def generate(self): i = 0 while True: yield hold, self, 2 c = Car('%d' % i) activate(c, c.lifecycle()) i += 1 washtime = 5 initialize() # put four cars into the queue of waiting cars for j in range(1, 5): c = Car(name='%d' % -j) activate(c, c.lifecycle()) waitingCars = Store(capacity=40) for i in range(2): cw = Carwash('Carwash %s' % i) activate(cw, cw.lifecycle()) cg = CarGenerator() activate(cg, cg.generate()) simulate(until=30) print('waitingCars %s' % [x.name for x in waitingCars.theBuffer])