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)
assert len(self.got) == toGet print('%s Get widget weights %s' % (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
M2=Machine('M2','Machine2', mean=0.75) Q2=Queue('Q2','Queue2',capacity=infinity) E=Exit('E1','Exit') #define predecessors and successors for the objects S.defineRouting([M1]) M1.defineRouting([S],[Q1]) Q1.defineRouting([M1],[M2]) M2.defineRouting([Q1],[Q2]) 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')
# # replace original imports with ones that trace # if options.trace: from SimPy.SimulationTrace import Process, Monitor, initialize, hold, passivate, activate, reactivate, simulate, now # # use a seed if set, otherwise use some OS source as the seed # if options.seed: random.seed(options.seed) else: random.seed() initialize() # # start up initial servers # for _ in range(options.capacity): s = Server(options.max_wait, options.instance_type, options.latency) activate(s, s.execute()) e = Sources('Sources') activate(e, e.execute(options.rate, file, options.latency, options.si_period, options.si_amplitude, options.si_phase, options.si_offset, options.profile,
def main(): initialize() testAlarm() testThread() simulate(until=1000) testTanjanAlgo()
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 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)
Q1.defineRouting([M1], [M2]) M2.defineRouting([Q1], [Q2]) 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")
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()