コード例 #1
0
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"

        if no_seed:
            #if the mean = 1.0, then 1/lambda_ = 1.0 -> lambda_ = 1
            self.rng = RNG(ExponentialRNS(1.0),
                           ExponentialRNS(1. / float(self.sim_param.RHO)))
        else:
            self.rng = RNG(
                ExponentialRNS(1.0, self.sim_param.SEED_IAT),
                ExponentialRNS(1. / float(self.sim_param.RHO),
                               self.sim_param.SEED_ST))
コード例 #2
0
 def __init__(self, setting):
     # Load simulation parameters
     self.sim_param = SimParam(setting)
     # Load simtime
     if setting.dynamictest:
         self.SIMTIME = setting.dynamictest.simtime
     self.freeaccess = False
     if setting.secondwindow.test_values[3]:
         self.freeaccess = True
     # Load the simulation state parameters
     self.sim_state = SimState()
     # Load the result parameters
     self.sim_result = SimResult()
     # Load the class which perfomrs all the methods governing a simple slot
     self.slot = TreeSlot()
     # Load the branch node which keeps track of a tree
     self.branch_node = BranchNode()
     # Create an array of integers of which will contain all active nodes.
     self.active_array = []
     # For gated access, the arrived packets are put into a queue
     self.queue_array = []
     # The number of packets generated in a single slot
     self.packets_gen = 0
     # THe result of a slot
     self.result = 0
     # The current slot no
     self.slot_no = 0
     # Load the parameters for single tree resolution
     self.tree_state = TreeState(self)
コード例 #3
0
 def reset(self):
     """
     Reset the Simulation object.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     self.counter_collection = CounterCollection(self)
     self.rng.iat_rns.set_parameters(1.)
     self.rng.st_rns.set_parameters(1. / float(self.sim_param.RHO))
コード例 #4
0
ファイル: simulation.py プロジェクト: gundoganalperen/ams-des
 def reset(self, no_seed=False):
     """
     Reset the Simulation object.
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
     a specific seed.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     # TODO Task 2.4.3: Uncomment the line below
     self.counter_collection = CounterCollection(self)
     # TODO Task 3.1.2: Uncomment the line below and replace the "None"
     """
コード例 #5
0
ファイル: simulation.py プロジェクト: gundoganalperen/ams-des
 def __init__(self, sim_param=SimParam(), no_seed=False):
     """
     Initialize the Simulation object.
     :param sim_param: is an optional SimParam object for parameter pre-configuration
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
     a specific seed.
     """
     self.sim_param = sim_param
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     # TODO Task 2.4.3: Uncomment the line below
     self.counter_collection = CounterCollection(self)
     # TODO Task 3.1.2: Uncomment the line below and replace the "None"
     """
コード例 #6
0
ファイル: simulation.py プロジェクト: gundoganalperen/ams-des
 def reset(self, no_seed=False):
     """
     Reset the Simulation object.
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
     a specific seed.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     # TODO Task 2.4.3: Uncomment the line below
     self.counter_collection = CounterCollection(self)
     # TODO Task 3.1.2: Uncomment the line below and replace the "None"
     if no_seed:
         self.rng = RNG(ExponentialRNS(1.0), ExponentialRNS(1./float(self.sim_param.RHO)))
     else:
         self.rng = RNG(ExponentialRNS(1.0, self.sim_param.SEED_IAT), ExponentialRNS(1./float(self.sim_param.RHO),self.sim_param.SEED_ST))
コード例 #7
0
 def reset(self, setting):
     self.sim_param = SimParam(setting)
     if setting.dynamictest:
         self.SIMTIME = setting.dynamictest.simtime
     self.freeaccess = False
     if setting.secondwindow.test_values[3]:
         self.freeaccess = True
     self.sim_state = SimState()
     self.sim_result = SimResult()
     self.slot = TreeSlot()
     self.active_array = []
     self.queue_array = []
     self.packets_gen = 0
     self.result = 0
     self.slot_no = 0
     self.tree_state = TreeState(self)
     self.branch_node.reset()
コード例 #8
0
 def reset(self, no_seed=False):
     """
     Reset the Simulation object.
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
     a specific seed.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     self.counter_collection = CounterCollection(self)
     if no_seed:
         self.rng = RNG(ExponentialRNS(1),
                        ExponentialRNS(1. / float(self.sim_param.RHO)))
     else:
         self.rng = RNG(
             ExponentialRNS(1, self.sim_param.SEED_IAT),
             ExponentialRNS(1. / float(self.sim_param.RHO),
                            self.sim_param.SEED_ST))
コード例 #9
0
 def __init__(self, sim_param=SimParam(), no_seed=False):
     """
     Initialize the Simulation object.
     :param sim_param: is an optional SimParam object for parameter pre-configuration
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
     a specific seed.
     """
     self.sim_param = sim_param
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     self.counter_collection = CounterCollection(self)
     if no_seed:
         self.rng = RNG(ExponentialRNS(1),
                        ExponentialRNS(1. / float(self.sim_param.RHO)))
     else:
         self.rng = RNG(
             ExponentialRNS(1, self.sim_param.SEED_IAT),
             ExponentialRNS(1. / float(self.sim_param.RHO),
                            self.sim_param.SEED_ST))
コード例 #10
0
class Simulation(object):
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"

        if no_seed:
            #if the mean = 1.0, then 1/lambda_ = 1.0 -> lambda_ = 1
            self.rng = RNG(ExponentialRNS(1.0),
                           ExponentialRNS(1. / float(self.sim_param.RHO)))
        else:
            self.rng = RNG(
                ExponentialRNS(1.0, self.sim_param.SEED_IAT),
                ExponentialRNS(1. / float(self.sim_param.RHO),
                               self.sim_param.SEED_ST))

    def reset(self, no_seed=False):
        """
        Reset the Simulation object.
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
        a specific seed.
        """
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"
        self.rng.iat_rns.set_parameters(1.)
        self.rng.st_rns.set_parameters(1. / float(self.sim_param.RHO))

    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 self.sim_state.now <= e.timestamp:
                    self.sim_state.now = e.timestamp
                    self.counter_collection.count_queue()
                    e.process()

            else:
                self.sim_state.stop = True

            #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 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
コード例 #11
0
def task_5_2_2():
    """
    Run simulation in batches. Start the simulation with running until a customer count of n=100 or (n=1000) and
    continue to increase the number of customers by dn=n.
    Count the blocking proabability for the batch and calculate the confidence interval width of all values, that have
    been counted until now.
    Do this until the desired confidence level is reached and print out the simulation time as well as the number of
    batches.
    """
    num_batches1 = 0
    num_batches2 = 0
    num_batches3 = 0
    num_batches4 = 0
    TIC = TimeIndependentCounter("bp")

    # TODO Task 5.2.2: Your code goes here
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = 0.9
    ## n = 100
    # ALPHA: 5%
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)

    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(100, True).blocking_probability)
            print TIC.report_confidence_interval(0.05)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                num_batches1 = i + 1
                break
    t1 = sim.sim_state.now

    # ALPHA: 10%
    sim.reset()
    TIC.reset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)
    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(100, True).blocking_probability)
            print TIC.report_confidence_interval(0.1)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                num_batches2 = i + 1
                break
    t2 = sim.sim_state.now

    sim.reset()
    ## n = 1000
    # ALPHA: 5%
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)

    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(1000, True).blocking_probability)
            print TIC.report_confidence_interval(0.05)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                num_batches3 = i + 1
                break
    t3 = sim.sim_state.now

    # ALPHA: 10%
    sim.reset()
    TIC.reset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)
    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(1000, True).blocking_probability)
            print TIC.report_confidence_interval(0.1)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                num_batches4 = i + 1
                break
    t4 = sim.sim_state.now

    # print and return both results
    print "N:  100; ALPHA:  5%; NUMBER OF BATCHES: " + str(
        num_batches1) + " and SIM TIME: " + str(t1)
    print "N:  100; ALPHA: 10%; NUMBER OF BATCHES: " + str(
        num_batches2) + " and SIM TIME: " + str(t2)
    print "N: 1000; ALPHA:  5%; NUMBER OF BATCHES: " + str(
        num_batches3) + " and SIM TIME: " + str(t3)
    print "N: 1000; ALPHA: 10%; NUMBER OF BATCHES: " + str(
        num_batches4) + " and SIM TIME: " + str(t4)

    return [t1, t2, t3, t4]
コード例 #12
0
class Simulation(object):
    """
    This Holds the entire Simulation object, whose parameters we update according to the outcomes
    """
    def __init__(self, setting):
        # Load simulation parameters
        self.sim_param = SimParam(setting)
        # Load simtime
        if setting.dynamictest:
            self.SIMTIME = setting.dynamictest.simtime
        self.freeaccess = False
        if setting.secondwindow.test_values[3]:
            self.freeaccess = True
        # Load the simulation state parameters
        self.sim_state = SimState()
        # Load the result parameters
        self.sim_result = SimResult()
        # Load the class which perfomrs all the methods governing a simple slot
        self.slot = TreeSlot()
        # Load the branch node which keeps track of a tree
        self.branch_node = BranchNode()
        # Create an array of integers of which will contain all active nodes.
        self.active_array = []
        # For gated access, the arrived packets are put into a queue
        self.queue_array = []
        # The number of packets generated in a single slot
        self.packets_gen = 0
        # THe result of a slot
        self.result = 0
        # The current slot no
        self.slot_no = 0
        # Load the parameters for single tree resolution
        self.tree_state = TreeState(self)

    def reset(self, setting):
        self.sim_param = SimParam(setting)
        if setting.dynamictest:
            self.SIMTIME = setting.dynamictest.simtime
        self.freeaccess = False
        if setting.secondwindow.test_values[3]:
            self.freeaccess = True
        self.sim_state = SimState()
        self.sim_result = SimResult()
        self.slot = TreeSlot()
        self.active_array = []
        self.queue_array = []
        self.packets_gen = 0
        self.result = 0
        self.slot_no = 0
        self.tree_state = TreeState(self)
        self.branch_node.reset()

    def do_simulation_simple_tree_dynamic(self):
        """
        Free access simulation, is run until the SIMTIME and thats it

        """
        # Run simulation for the number of slots
        self.tree_state.reset(self)
        for self.slot_no in range(1, self.SIMTIME):
            # Generate a packet according to poisson distribution
            self.packets_gen = np.random.poisson(self.sim_param.lmbda)
            # Add the number of packets to the active packet array
            packetlist.add_packets_to_tree(self)
            # Simulate the processes that would happen in the tx and rx in one slot, update the active array accordingly
            self.slot.oneslotprocess(self)
            # Update the metrics in sim_state depending on the result
            self.tree_state.update_metrics(self)
        # Update the results
        self.sim_state.update_metrics(self)
        self.sim_result.get_result(self)

    def do_simulation_simple_tree_static(self, collided_packets):
        """
        Static Simulation, when the number of in initial collided packets is given, it is essentially a single tree res
        :param collided_packets: the no of packets in the resolution

        """
        # Load active array with the collided packets
        if collided_packets > self.sim_param.K:
            self.packets_gen = collided_packets
            packetlist.add_packets_to_tree(self)
            self.tree_state.reset(self)
            # Run the simulation as long as all packets are processed and tree is over
            while self.tree_state.gate_open:
                # Increment the slot
                self.slot_no += 1
                # Simulate the processes that would happen in the tx and rx in one slot, update the active array accordingly
                self.slot.oneslotprocess(self)
                # Update the simstate metrics according to the result of the simulation
                self.tree_state.update_metrics(self)
                # check if all the packets are processed and the tree is at its last branch
                if len(self.active_array) == 0 and len(
                        self.branch_node.branch_status) == 0:
                    self.tree_state.gate_open = False
            # update the metrics from a tree to the simulation state
            self.sim_state.update_metrics(self)
            # Update the results
            self.sim_result.get_result(self)
        else:
            self.sim_result.throughput = 0
            self.sim_result.magic_throughput = 0

    def do_simulation_gated_access(self):
        """
        Gated access, when a resolution is ongoing, the other packets wait in a buffer

        """
        # Run the simulation where we at least simulate for the simtime and then wait for the last tree to be resolved.
        while self.tree_state.gate_open or self.slot_no < self.SIMTIME:
            # Generate a packet according to poisson distribution
            self.packets_gen = np.random.poisson(self.sim_param.lmbda)
            # Add the packet to the queue
            if self.slot_no < self.SIMTIME:
                packetlist.add_packets_to_queue(self)
            # if the active array is empty i.e the tree is resolved
            if len(self.active_array) == 0 and len(
                    self.branch_node.branch_status) == 0:
                # Update the Sim_state class for the previous tree
                if self.slot_no != 0:
                    self.sim_state.update_metrics(self)
                # Transfer the queue to the active array
                packetlist.copy_queue_to_active(self)
                # Clear the queue
                self.queue_array = []
                # Reset all the parameters as we start a new tree
                self.tree_state.reset(self)
                # Reset the branches of the tree
                self.branch_node.reset()
                if len(self.active_array) == 0:
                    self.tree_state.gate_open = False
            self.slot_no += 1
            # Simulate the processes that would happen in the tx and rx in one slot, update the active array accordingly
            self.slot.oneslotprocess(self)
            # Update the metrics in sim_state depending on the result
            self.tree_state.update_metrics(self)
        # Update the results
        self.sim_result.get_result(self)
コード例 #13
0
class Simulation(object):
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        self.counter_collection = CounterCollection(self)
        if no_seed:
            self.rng = RNG(ExponentialRNS(1),
                           ExponentialRNS(1. / float(self.sim_param.RHO)))
        else:
            self.rng = RNG(
                ExponentialRNS(1, self.sim_param.SEED_IAT),
                ExponentialRNS(1. / float(self.sim_param.RHO),
                               self.sim_param.SEED_ST))
        self.number_served_packets = 0

    def reset(self, no_seed=False):
        """
        Reset the Simulation object.
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
        a specific seed.
        """
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        self.counter_collection = CounterCollection(self)
        if no_seed:
            self.rng = RNG(ExponentialRNS(1),
                           ExponentialRNS(1. / float(self.sim_param.RHO)))
        else:
            self.rng = RNG(
                ExponentialRNS(1, self.sim_param.SEED_IAT),
                ExponentialRNS(1. / float(self.sim_param.RHO),
                               self.sim_param.SEED_ST))

    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

    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) and (self.number_served_packets <= n):

            # TODO Task 4.3.2: Your code goes here
            # TODO Task 5.2.2: Your code goes here
            # 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
コード例 #14
0
class Simulation(object):
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        # self.counter_collection = CounterCollection()
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"
        """
        if no_seed:
            self.rng = RNG(None, None)
        else:
            self.rng = RNG(None, None)
        """

    def reset(self, no_seed=False):
        """
        Reset the Simulation object.
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
        a specific seed.
        """
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        # self.counter_collection = CounterCollection()
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"
        """
        if no_seed:
            self.rng = RNG(None, None)
        else:
            self.rng = RNG(None, None)
        """

    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 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
コード例 #15
0
class Simulation(object):
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"
        rns1 = ExponentialRNS(1.0)
        rns2 = ExponentialRNS(1.0 / self.sim_param.RHO)
        rns1_seed = ExponentialRNS(1.0, self.sim_param.SEED_IAT)
        rns2_seed = ExponentialRNS(1.0 / self.sim_param.RHO,
                                   self.sim_param.SEED_ST)
        if no_seed:
            self.rng = RNG(rns1, rns2)
        else:
            self.rng = RNG(rns1_seed, rns2_seed)

    def reset(self, no_seed=False):
        """
        Reset the Simulation object.
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
        a specific seed.
        """
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"
        rns1 = ExponentialRNS(1.0)
        rns2 = ExponentialRNS(1.0 / self.sim_param.RHO)
        rns1_seed = ExponentialRNS(1.0, self.sim_param.SEED_IAT)
        rns2_seed = ExponentialRNS(1.0 / self.sim_param.RHO,
                                   self.sim_param.SEED_ST)
        if no_seed:
            self.rng = RNG(rns1, rns2)
        else:
            self.rng = RNG(rns1_seed, rns2_seed)

    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):
        """
        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