Пример #1
0
class CounterCollection(object):
    """
    CounterCollection is a collection of all counters and histograms that are used in the simulations.

    It contains several counters and histograms, that are used in the different tasks.
    Reporting is done by calling the report function. This function can be adapted, depending on which counters should
    report their results and print strings or plot histograms.
    """
    def __init__(self, sim):
        """
        Initialize the counter collection.
        :param sim: the simulation, the CounterCollection belongs to.
        """
        self.sim = sim

        # waiting time
        self.cnt_wt = TimeIndependentCounter()
        self.hist_wt = TimeIndependentHistogram(self.sim, "w")

        # queue length
        self.cnt_ql = TimeDependentCounter(self.sim)
        self.hist_ql = TimeDependentHistogram(self.sim, "q")

        # system utilization
        self.cnt_sys_util = TimeDependentCounter(self.sim)
        """
        # blocking probability
        self.cnt_bp = TimeIndependentCounter("bp")
        self.hist_bp = TimeIndependentHistogram(self.sim, "bp")

        # correlations
        self.cnt_iat_wt = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. waiting time")
        self.cnt_iat_st = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. service time")
        self.cnt_iat_syst = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. system time")
        self.cnt_st_syst = TimeIndependentCrosscorrelationCounter("service time vs. system time")
        self.acnt_wt = TimeIndependentAutocorrelationCounter("waiting time with lags 1 to 20", max_lag=20)
        """

    def reset(self):
        """
        Resets all counters and histograms.
        """
        self.cnt_wt.reset()
        self.hist_wt.reset()

        self.cnt_ql.reset()
        self.hist_ql.reset()

        self.cnt_sys_util.reset()
        """
        self.cnt_bp.reset()
        self.hist_bp.reset()

        self.cnt_iat_wt.reset()
        self.cnt_iat_st.reset()
        self.cnt_iat_syst.reset()
        self.cnt_st_syst.reset()
        self.acnt_wt.reset()
        """

    def report(self):
        """
        Calls the report function of the counters and histograms.
        Can be adapted, such that not all reports are printed
        """
        self.cnt_wt.report()
        self.hist_wt.report()

        self.cnt_ql.report()
        self.hist_ql.report()

        self.cnt_sys_util.report()
        """
        self.cnt_iat_wt.report()
        self.cnt_iat_st.report()
        self.cnt_iat_syst.report()
        self.cnt_st_syst.report()
        self.acnt_wt.report()
        """

    def count_packet(self, packet):
        """
        Count a packet. Its data is counted by the various counters
        """
        self.cnt_wt.count(packet.get_waiting_time())
        self.hist_wt.count(packet.get_waiting_time())
        """
        self.cnt_iat_wt.count(packet.get_interarrival_time(), packet.get_waiting_time())
        self.cnt_iat_st.count(packet.get_interarrival_time(), packet.get_service_time())
        self.cnt_iat_syst.count(packet.get_interarrival_time(), packet.get_system_time())
        self.cnt_st_syst.count(packet.get_service_time(), packet.get_system_time())
        self.acnt_wt.count(packet.get_waiting_time())
        """

    def count_queue(self):
        """
        Count the number of packets in the buffer and add the values to the corresponding (time dependent) histogram.
        This function should be called at least whenever the number of packets in the buffer changes.

        The system utilization is counted as well and can be counted from the counter cnt_sys_util.
        """
        self.cnt_ql.count(self.sim.system_state.get_queue_length())
        self.hist_ql.count(self.sim.system_state.get_queue_length())

        # TODO Task 2.5.1: Your code goes here
        if self.sim.system_state.server_busy:
            self.cnt_sys_util.count(1)
        else:
            self.cnt_sys_util.count(0)
Пример #2
0
def do_simulation_study(sim,
                        print_queue_length=False,
                        print_waiting_time=True):
    """
    This simulation study is different from the one made in assignment 1. It is mainly used to gather and visualize
    statistics for different buffer sizes S instead of finding a minimal number of spaces for a desired quality.
    For every buffer size S (which ranges from 5 to 7), statistics are printed (depending on the input parameters).
    Finally, after all runs, the results are plotted in order to visualize the differences and giving the ability
    to compare them. The simulations are run first for 100s, then for 1000s. For each simulation time, two diagrams are
    shown: one for the distribution of the mean waiting times and one for the average buffer usage
    :param sim: the simulation object to do the simulation
    :param print_queue_length: print the statistics for the queue length to the console
    :param print_waiting_time: print the statistics for the waiting time to the console
    """

    # counters for mean queue length and waiting time
    counter_mean_queue_length = TimeIndependentCounter()
    hist_mean_queue_length = TimeIndependentHistogram(sim, "q")

    counter_mean_waiting_time = TimeIndependentCounter()
    hist_mean_waiting_time = TimeIndependentHistogram(sim, "w")

    # step through number of buffer spaces...
    for S in sim.sim_param.S_VALUES:
        sim.sim_param.S = S

        counter_mean_queue_length.reset()
        hist_mean_queue_length.reset()
        counter_mean_waiting_time.reset()
        hist_mean_waiting_time.reset()

        sim.sim_param.SIM_TIME = 100000
        sim.sim_param.NO_OF_RUNS = 1000

        # repeat simulation
        for run in range(sim.sim_param.NO_OF_RUNS):
            # print(run)
            sim.reset()
            sim.do_simulation()
            # add simulation result to counters and histograms (always use the mean)
            counter_mean_queue_length.count(
                sim.counter_collection.cnt_ql.get_mean())
            hist_mean_queue_length.count(
                sim.counter_collection.cnt_ql.get_mean())
            counter_mean_waiting_time.count(
                sim.counter_collection.cnt_wt.get_mean())
            hist_mean_waiting_time.count(
                sim.counter_collection.cnt_wt.get_mean())

        pyplot.subplot(221)
        pyplot.xlabel("Mean waiting time [ms] (SIM_TIME = 100.000ms)")
        pyplot.ylabel("Distribution over n")
        hist_mean_waiting_time.report()

        pyplot.subplot(222)
        pyplot.xlabel("Mean queue length (SIM_TIME = 100.000ms)")
        pyplot.ylabel("Distribution over n")
        hist_mean_queue_length.report()

        # if desired, print statistics for queue length and waiting time
        if print_queue_length:
            print('Buffer size: ' + str(sim.sim_param.S) +
                  ', simulation time: ' + str(sim.sim_param.SIM_TIME) +
                  ', Mean buffer content: ' +
                  str(counter_mean_queue_length.get_mean()) + ' Variance: ' +
                  str(counter_mean_queue_length.get_var()))

        if print_waiting_time:
            print('Buffer size: ' + str(sim.sim_param.S) +
                  ', simulation time: ' + str(sim.sim_param.SIM_TIME) +
                  ', Mean waiting time: ' +
                  str(counter_mean_waiting_time.get_mean()) + ' Variance: ' +
                  str(counter_mean_waiting_time.get_var()))

        counter_mean_queue_length.reset()
        hist_mean_queue_length.reset()
        counter_mean_waiting_time.reset()
        hist_mean_waiting_time.reset()

        sim.sim_param.SIM_TIME = 1000000
        sim.sim_param.NO_OF_RUNS = 1000

        # repeat simulation
        for run in range(sim.sim_param.NO_OF_RUNS):
            # print(run)
            sim.reset()
            sim.do_simulation()
            # add simulation result to counters and histograms (always use the mean)
            counter_mean_queue_length.count(
                sim.counter_collection.cnt_ql.get_mean())
            hist_mean_queue_length.count(
                sim.counter_collection.cnt_ql.get_mean())
            counter_mean_waiting_time.count(
                sim.counter_collection.cnt_wt.get_mean())
            hist_mean_waiting_time.count(
                sim.counter_collection.cnt_wt.get_mean())

        pyplot.subplot(223)
        pyplot.xlabel("Mean waiting time [ms] (SIM_TIME = 1.000.000ms)")
        pyplot.ylabel("Distribution over n")
        hist_mean_waiting_time.report()

        pyplot.subplot(224)
        pyplot.xlabel("Mean queue length (SIM_TIME = 1.000.000ms)")
        pyplot.ylabel("Distribution over n")
        hist_mean_queue_length.report()

        # if desired, print statistics for queue length and waiting time
        if print_queue_length:
            print('Buffer size: ' + str(sim.sim_param.S) +
                  ', simulation time: ' + str(sim.sim_param.SIM_TIME) +
                  ', Mean buffer content: ' +
                  str(counter_mean_queue_length.get_mean()) + ' Variance: ' +
                  str(counter_mean_queue_length.get_var()))

        if print_waiting_time:
            print('Buffer size: ' + str(sim.sim_param.S) +
                  ', simulation time: ' + str(sim.sim_param.SIM_TIME) +
                  ', Mean waiting time: ' +
                  str(counter_mean_waiting_time.get_mean()) + ' Variance: ' +
                  str(counter_mean_waiting_time.get_var()))

    # set axis ranges for better comparison and display accumulated plot
    pyplot.subplot(221)
    pyplot.xlim([0, 3500])
    pyplot.subplot(223)
    pyplot.xlim([0, 3500])
    pyplot.subplot(222)
    pyplot.xlim([-.5, sim.sim_param.S_MAX + .5])
    pyplot.subplot(224)
    pyplot.xlim([-.5, sim.sim_param.S_MAX + .5])
    pyplot.show()
Пример #3
0
def do_simulation_study(sim,
                        print_queue_length=False,
                        print_waiting_time=True):
    """
    This simulation study is different from the one made in assignment 1. It is mainly used to gather and visualize
    statistics for different buffer sizes S instead of finding a minimal number of spaces for a desired quality.
    For every buffer size S (which ranges from 5 to 7), statistics are printed (depending on the input parameters).
    Finally, after all runs, the results are plotted in order to visualize the differences and giving the ability
    to compare them. The simulations are run first for 100s, then for 1000s. For each simulation time, two diagrams are
    shown: one for the distribution of the mean waiting times and one for the average buffer usage
    :param sim: the simulation object to do the simulation
    :param print_queue_length: print the statistics for the queue length to the console
    :param print_waiting_time: print the statistics for the waiting time to the console
    """
    # TODO Task 2.7.1: Your code goes here
    # TODO Task 2.7.2: Your code goes here

    hist_wt = TimeIndependentHistogram(
        sim, "w")  #w is used to draw mean waiting time.

    # I can also create a variable from TimeIndependentHistogram class to use its plotting methods,
    # even if the queue length is depend on the time dependent system. Because I have already take this account for
    # calculation of its mean. Now, I have an array with the mean queue values for each run. Then, I can plot side by side
    # using the method of TimeIndependentHistogram.
    hist_ql = TimeIndependentHistogram(sim, "q")

    for i in range(sim.sim_param.S_VALUES[0], sim.sim_param.S_VALUES[-1] + 1):
        mean_queue_length = []
        mean_waiting_times = []
        sim.counter_collection.cnt_ql.reset()
        sim.counter_collection.cnt_wt.reset()
        hist_wt.reset()
        hist_ql.reset()
        sim.sim_param.S = i
        for sim_run in range(sim.sim_param.NO_OF_RUNS):
            sim.reset()
            mean_queue_length.append(sim.do_simulation().mean_queue_length)
            mean_waiting_times.append(sim.do_simulation().mean_waiting_time)

        if print_waiting_time:
            print("Queue Size = " + str(i) + " Number of Runs = " +
                  str(sim.sim_param.NO_OF_RUNS) + " Sim.Time = " +
                  str(sim.sim_param.SIM_TIME) + "ms" +
                  " Mean waiting times = " +
                  str(numpy.mean(mean_waiting_times)) +
                  " Mean Queue Length = " +
                  str(numpy.mean((mean_queue_length))) + " Variance = " +
                  str(numpy.var(mean_waiting_times)))
        #if print_queue_length:
        # print ("Queue Size = " + str(i) + " Number of Runs = " + str(sim.sim_param.NO_OF_RUNS) + " Sim.Time = " + str(sim.sim_param.SIM_TIME) + "ms" +  " Mean Queue Length = " + str(numpy.mean(mean_queue_length)) + " Mean Queue Length = " + str(numpy.mean((mean_queue_length))))

        hist_ql.values = mean_queue_length
        hist_wt.values = mean_waiting_times

        plt.subplot(121)
        plt.xlabel("Mean Waiting Time[ms] Sim.Time = " +
                   str(sim.sim_param.SIM_TIME) + "ms")
        plt.ylabel("Probability Distribution")
        hist_wt.report()

        plt.subplot(122)
        plt.xlabel("Mean Queue Length Sim.Time = " +
                   str(sim.sim_param.SIM_TIME) + "ms")
        plt.ylabel("Probability Distribution")
        hist_ql.report()

    plt.show()
Пример #4
0
class CounterCollection(object):

    """
    CounterCollection is a collection of all counters and histograms that are used in the simulations.

    It contains several counters and histograms, that are used in the different tasks.
    Reporting is done by calling the report function. This function can be adapted, depending on which counters should
    report their results and print strings or plot histograms.
    """

    def __init__(self, server):
        """
        Initialize the counter collection.
        :param sim: the simulation, the CounterCollection belongs to.
        """
        self.server = server
        #self.sim = server.slicesim

        # waiting time
        #self.cnt_wt = TimeIndependentCounter(self.server)
        #self.hist_wt = TimeIndependentHistogram(self.server, "w")
        #self.acnt_wt = TimeIndependentAutocorrelationCounter("waiting time with lags 1 to 20", max_lag=20)

        # system time(delay)
        self.cnt_syst = TimeIndependentCounter(self.server)
        self.hist_syst = TimeIndependentHistogram(self.server, "s")

        # queue length
        self.cnt_ql = TimeDependentCounter(self.server)
        self.hist_ql = TimeDependentHistogram(self.server, "q")

        # throughput
        self.cnt_tp = TimeDependentCounter(self.server, 'tp')
        self.cnt_tp2 = TimeDependentCounter(self.server, 'tp2')

        # system utilization
        #self.cnt_sys_util = TimeDependentCounter(self.server)

        # blocking probability
        #self.cnt_bp = TimeIndependentCounter(self.server, "bp")
        #self.hist_bp = TimeIndependentHistogram(self.server, "bp")

        # cross correlations
        #self.cnt_iat_wt = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. waiting time")
        #self.cnt_iat_st = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. service time")
        #self.cnt_iat_syst = TimeIndependentCrosscorrelationCounter("inter-arrival time vs. system time")
        #self.cnt_st_syst = TimeIndependentCrosscorrelationCounter("service time vs. system time")

    def reset(self):
        """
        Resets all counters and histograms.
        """
        #self.cnt_wt.reset()
        #self.hist_wt.reset()
        #self.acnt_wt.reset()

        self.cnt_syst.reset()
        self.hist_syst.reset()

        self.cnt_ql.reset()
        self.hist_ql.reset()

        self.cnt_tp.reset()

        #self.cnt_sys_util.reset()

        #self.cnt_bp.reset()
        #self.hist_bp.reset()

        #self.cnt_iat_wt.reset()
        #self.cnt_iat_st.reset()
        #self.cnt_iat_syst.reset()
        #self.cnt_st_syst.reset()
    
    def report(self, filename=''):
        """
        Calls the report function of the counters and histograms.
        Can be adapted, such that not all reports are printed
        """
        #self.cnt_wt.report(filename)
        #self.hist_wt.report(filename)
        #self.acnt_wt.report()

        self.cnt_syst.report(filename)
        self.hist_syst.report(filename)

        self.cnt_ql.report(filename)
        self.hist_ql.report(filename)

        self.cnt_tp.report(filename)

        #self.cnt_sys_util.report()

        #self.cnt_iat_wt.report()
        #self.cnt_iat_st.report()
        #self.cnt_iat_syst.report()
        #self.cnt_st_syst.report()

    def count_throughput(self, throughput):
        """
        Count a throughput. Its data is counted by the various counters
        tp in kilobits per second
        """
        self.cnt_tp.count(throughput)

    def count_throughput2(self, throughput):
        """
        Count a throughput. Its data is counted by the various counters
        tp in kilobits per second
        """
        self.cnt_tp2.count(throughput)

    def count_packet(self, packet):
        """
        Count a packet. Its data is counted by the various counters
        """
        #self.cnt_wt.count(packet.get_waiting_time())
        #self.hist_wt.count(packet.get_waiting_time())
        #self.acnt_wt.count(packet.get_waiting_time())

        self.cnt_syst.count(packet.get_system_time())
        self.hist_syst.count(packet.get_system_time())

        #self.cnt_iat_wt.count(packet.get_interarrival_time(), packet.get_waiting_time())
        #self.cnt_iat_st.count(packet.get_interarrival_time(), packet.get_service_time())
        #self.cnt_iat_syst.count(packet.get_interarrival_time(), packet.get_system_time())
        #self.cnt_st_syst.count(packet.get_service_time(), packet.get_system_time())

    def count_queue(self):
        """
        Count the number of packets in the buffer and add the values to the corresponding (time dependent) histogram.
        This function should be called at least whenever the number of packets in the buffer changes.

        The system utilization is counted as well and can be counted from the counter cnt_sys_util.
        """
        self.cnt_ql.count(int(self.server.get_queue_length()))
        self.hist_ql.count(self.server.get_queue_length())