Beispiel #1
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    # TODO Task 3.2.1: Your code goes here
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim_param.RHO = 0.01
    sim = Simulation(sim_param)
    rns_iat = ExponentialRNS(1.0)
    rns_st = ExponentialRNS(1.0/sim.sim_param.RHO)
    rns_uniform = UniformRNS((2,4))
    hist1 = TimeIndependentHistogram(sim, "Line")
    hist2 = TimeIndependentHistogram(sim, "Line")
    hist3 = TimeIndependentHistogram(sim, "bp")
    for i in range(1000000):
        hist1.count(rns_iat.next())
        hist2.count(rns_st.next())
        hist3.count(rns_uniform.next())
    hist1.report()
    hist2.report()
    hist3.report()
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)
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
    for i in sim.sim_param.S_VALUES:
        sim.sim_param.S = i
        mean_waiting_time_histogram = TimeIndependentHistogram(sim, "bp")
        for j in range(sim.sim_param.NO_OF_RUNS):
            sim.reset()
            sim_result = sim.do_simulation()
            mean_waiting_time_histogram.count(
                sim.counter_collection.cnt_wt.get_mean())
        mean_waiting_time_histogram.report()

    mean_queue_length_histogram1 = TimeIndependentCounter("1")
    mean_queue_length_histogram2 = TimeIndependentCounter("2")
    mean_queue_length_histogram3 = TimeIndependentCounter("3")
    width = 0.1
    sim.sim_param.S = 5
    for j in range(sim.sim_param.NO_OF_RUNS):
        sim.reset()
        sim_result = sim.do_simulation()
        mean_queue_length_histogram1.count(
            sim.counter_collection.cnt_ql.get_mean())
    sim.sim_param.S = 6
    for j in range(sim.sim_param.NO_OF_RUNS):
        sim.reset()
        sim_result1 = sim.do_simulation()
        mean_queue_length_histogram2.count(
            sim.counter_collection.cnt_ql.get_mean())
    sim.sim_param.S = 7
    for j in range(sim.sim_param.NO_OF_RUNS):
        sim.reset()
        sim_result2 = sim.do_simulation()
        mean_queue_length_histogram3.count(
            sim.counter_collection.cnt_ql.get_mean())

    histogram1, bins1 = numpy.histogram(mean_queue_length_histogram1.values,
                                        25, (0.0, 7.0))
    histogram2, bins2 = numpy.histogram(mean_queue_length_histogram2.values,
                                        25, (0.0, 7.0))
    histogram3, bins3 = numpy.histogram(mean_queue_length_histogram3.values,
                                        25, (0.0, 7.0))
    fig, ax = pyplot.subplots()
    bins2 = array(bins2.tolist())
    bins2 = bins2 + ones(len(bins2.tolist())) * width
    bins3 = array(bins3.tolist())
    bins3 = bins3 + ones(len(bins3.tolist())) * 2.0 * width
    rects1 = ax.bar(bins1.tolist(),
                    histogram1.tolist() + [0],
                    width,
                    color='r')
    rects2 = ax.bar(bins2.tolist(),
                    histogram2.tolist() + [0],
                    width,
                    color='g')
    rects3 = ax.bar(bins3.tolist(),
                    histogram3.tolist() + [0],
                    width,
                    color='b')
    ax.legend((rects1[0], rects2[0], rects3[0]), ('S5', 'S6', 'S7'))
    pyplot.show()
Beispiel #4
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()
Beispiel #5
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())