def test_event_chain(self): """ Test module EventChain. Add and remove SimEvents and check the correct order. """ # priorities: SC = 0, CA = 1, ST = 2 e = EventChain() e.insert(CustomerArrival(None, 10)) e.insert(SimulationTermination(None, 10)) e.insert(ServiceCompletion(None, 10)) e.insert(CustomerArrival(None, 5)) e.insert(ServiceCompletion(None, 2)) results = [[2, 0], [5, 1], [10, 0], [10, 1], [10, 2]] for r in results: ev = e.remove_oldest_event() self.assertEqual( [ev.timestamp, ev.priority], r, msg= "Error in EventChain or SimEvent. Events are sorted or returned in the wrong order." ) self.assertEqual( len(e.event_list), 0, msg="Error in EventChain or SimEvent. EventChain should be empty.")
def do_simulation_n_limit(self, n, first_batch): """ Call this function, if the simulation should stop after a given number of packets Do one simulation run. Initialize simulation and create first event. After that, one after another event is processed. :param n: number of customers, that are processed before the simulation stops :return: SimResult object """ # insert first event if not first_batch: # if this is a first batch self.event_chain.insert(CustomerArrival(self, 0)) # start simulation (run) while not self.sim_state.stop: # TODO Task 4.3.2: Your code goes here # TODO Task 5.2.2: Your code goes here e = self.event_chain.remove_oldest_event() if e: if self.sim_state.now <= e.timestamp: self.sim_state.now = e.timestamp self.counter_collection.count_queue() e.process() if (self.sim_state.num_packets) >= n: self.sim_state.stop = True else: self.sim_state.stop = True #pass # gather results for sim_result object self.sim_result.gather_results() return self.sim_result
def do_simulation(self): """ Do one simulation run. Initialize simulation and create first and last event. After that, one after another event is processed. :return: SimResult object """ # insert first and last event self.event_chain.insert(CustomerArrival(self, 0)) self.event_chain.insert( SimulationTermination(self, self.sim_param.SIM_TIME)) # start simulation (run) while not self.sim_state.stop: # TODO Task 1.4.1: Your code goes here """ Hint: You can use and adapt the following lines in your realization e = self.event_chain.remove_oldest_event() e.process() """ pass # TODO Task 2.4.3: Your code goes here somewhere # gather results for sim_result object self.sim_result.gather_results() return self.sim_result
def test_customer_arrival(self): """ Test CustomerArrival process function. Check, whether adding customers to server or queue or dropping them works correctly. """ DESTest.sim.reset() # initialize system DESTest.sim.system_state.buffer_content = DESTest.sim.sim_param.S - 1 DESTest.sim.system_state.server_busy = False self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [DESTest.sim.sim_param.S - 1, False], msg="Error in CustomerArrival test. System initialization failed.") # first ca should be served by the server, not enqueued CustomerArrival(DESTest.sim, 0).process() self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [DESTest.sim.sim_param.S - 1, True], msg="Error in CustomerArrival. Packet has not been added to server.") self.assertEqual(len(DESTest.sim.event_chain.event_list), 2, msg="Error in CustomerArrival. Wrong number of new SimEvents created in process function.") # second ca should be enqueued CustomerArrival(DESTest.sim, 0).process() self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [DESTest.sim.sim_param.S, True], msg="Error in CustomerArrival. Packet should have been enqueued.") self.assertEqual(len(DESTest.sim.event_chain.event_list), 3, msg="Error in CustomerArrival. Wrong number of new SimEvents created in process function.") # third ca should be dropped CustomerArrival(DESTest.sim, 0).process() self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [DESTest.sim.sim_param.S, True], msg="Error in CustomerArrival. Packet should have been dropped.") self.assertEqual(len(DESTest.sim.event_chain.event_list), 4, msg="Error in CustomerArrival. Wrong number of new SimEvents created in process function.") self.assertGreater(DESTest.sim.sim_state.num_blocked_packets, 0, msg="Error in CustomerArrival. Should have counted at least one dropped packet.")
def do_simulation(self): """ Do one simulation run. Initialize simulation and create first and last event. After that, one after another event is processed. :return: SimResult object """ # insert first and last event self.event_chain.insert(CustomerArrival(self, 0)) self.event_chain.insert( SimulationTermination(self, self.sim_param.SIM_TIME)) # start simulation (run) while not self.sim_state.stop: # TODO Task 1.4.1: Your code goes here """ Hint: You can use and adapt the following lines in your realization e = self.event_chain.remove_oldest_event() e.process() """ e = self.event_chain.remove_oldest_event() if e: # if event exists and timestamps are ok, process the event if self.sim_state.now <= e.timestamp: self.sim_state.now = e.timestamp self.counter_collection.count_queue() e.process() else: print "NOW: " + str( self.sim_state.now) + ", EVENT TIMESTAMP: " + str( e.timestamp) raise RuntimeError( "ERROR: TIMESTAMP OF EVENT IS SMALLER THAN CURRENT TIME." ) else: print "Event chain is empty. Abort" self.sim_state.stop = True # TODO Task 2.4.3: Your code goes here somewhere #self.counter_collection.count_queue() # gather results for sim_result object self.sim_result.gather_results() return self.sim_result
def do_simulation_n_limit(self, n, new_batch=False): """ Call this function, if the simulation should stop after a given number of packets Do one simulation run. Initialize simulation and create first event. After that, one after another event is processed. :param n: number of customers, that are processed before the simulation stops :return: SimResult object """ # insert first event only if no new batch has been started if not new_batch: self.event_chain.insert(CustomerArrival(self, 0)) # start simulation (run) while not self.sim_state.stop: # get next simevent from events e = self.event_chain.remove_oldest_event() if e: # if event exists and timestamps are ok, process the event if self.sim_state.now <= e.timestamp: self.sim_state.now = e.timestamp self.counter_collection.count_queue() e.process() if self.sim_state.num_packets >= n: self.sim_state.stop = True else: print "NOW: " + str( self.sim_state.now) + ", EVENT TIMESTAMP: " + str( e.timestamp) raise RuntimeError( "ERROR: TIMESTAMP OF EVENT IS SMALLER THAN CURRENT TIME." ) else: print "Event chain is empty. Abort" self.sim_state.stop = True # gather results for sim_result object self.sim_result.gather_results() return self.sim_result
def do_simulation_n_limit(self, n): """ Call this function, if the simulation should stop after a given number of packets Do one simulation run. Initialize simulation and create first event. After that, one after another event is processed. :param n: number of customers, that are processed before the simulation stops :return: SimResult object """ # insert first event self.event_chain.insert(CustomerArrival(self, 0)) # start simulation (run) while not self.sim_state.stop: # TODO Task 4.3.2: Your code goes here # TODO Task 5.2.2: Your code goes here pass # gather results for sim_result object self.sim_result.gather_results() return self.sim_result
def do_simulation(self): """ Do one simulation run. Initialize simulation and create first and last event. After that, one after another event is processed. :return: SimResult object """ # insert first and last event self.event_chain.insert(CustomerArrival(self, 0)) self.event_chain.insert( SimulationTermination(self, self.sim_param.SIM_TIME)) # start simulation (run) while not self.sim_state.stop: # get next simevent from events e = self.event_chain.remove_oldest_event() if e: # if event exists and timestamps are ok, process the event if self.sim_state.now <= e.timestamp: self.sim_state.now = e.timestamp self.counter_collection.count_queue() e.process() else: print('NOW: ' + str(self.sim_state.now) + ', EVENT TIMESTAMP: ' + str(e.timestamp)) raise RuntimeError( "ERROR: TIMESTAMP OF EVENT IS SMALLER THAN CURRENT TIME." ) else: print('Event chain is empty. Abort') self.sim_state.stop = True # gather results for sim_result object self.sim_result.gather_results() return self.sim_result