def test_auto_correlation(self):
        """
        Test the basic implementation of the auto covariance counter.
        """
        tiacc = TimeIndependentAutocorrelationCounter(max_lag=5)

        tiacc.count(1)
        tiacc.count(2)
        tiacc.count(3)
        tiacc.count(1)
        tiacc.count(2)
        tiacc.count(3)

        results_cov = [2. / 3., -.2, -.5, 2. / 3., 0., -1.]
        results_cor = [5. / 6., -.25, -.625, 5. / 6., 0., -1.25]

        for lag in range(5):
            self.assertAlmostEqual(
                tiacc.get_auto_cov(lag),
                results_cov[lag],
                delta=.01,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Covariance calculation is wrong."
            )
            self.assertAlmostEqual(
                tiacc.get_auto_cor(lag),
                results_cor[lag],
                delta=.01,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Correlation calculation is wrong."
            )
Exemple #2
0
    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(name="waiting time")
        self.hist_wt = TimeIndependentHistogram(self.sim, "w")
        self.acnt_wt = TimeIndependentAutocorrelationCounter(
            "waiting time with lags 1 to 20", max_lag=20)

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

        # system utilization
        self.cnt_sys_util = TimeDependentCounter(self.sim,
                                                 name="system utilization")

        # blocking probability
        self.cnt_bp = TimeIndependentCounter("bp")
        self.hist_bp = TimeIndependentHistogram(self.sim, "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 test_auto_correlation(self):
        """
        Test the basic implementation of the auto covariance counter.
        """
        tiacc = TimeIndependentAutocorrelationCounter(max_lag=5)

        for i in range(5000):
            tiacc.count(i % 25)

        results_cov = [52.0, 40.0, 29.0, 19.0, 10.0]
        results_cor = [0.9998, 0.7691, 0.5576, 0.3653, 0.1923]

        for lag in range(5):
            self.assertAlmostEqual(
                tiacc.get_auto_cov(lag),
                results_cov[lag],
                delta=.05,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Covariance calculation is wrong."
            )
            self.assertAlmostEqual(
                tiacc.get_auto_cor(lag),
                results_cor[lag],
                delta=.05,
                msg=
                "Error in TimeIndependentAutocorrelationCounter. Correlation calculation is wrong."
            )
def task_4_2_1():
    """
    Execute exercise 4.2.1, which is basically just a test for the auto correlation.
    """
    autocorrelation_test_counter = TimeIndependentAutocorrelationCounter("test sequence", max_lag=1)

    print "First test series:"

    # Test first series:
    for _ in range(5000):
        autocorrelation_test_counter.count(1)
        autocorrelation_test_counter.count(-1)

    print "Mean = " + str(autocorrelation_test_counter.get_mean())
    print "Var = " + str(autocorrelation_test_counter.get_var())
    autocorrelation_test_counter.report()

    print "____________________________________________"
    print "Second test series:"
    autocorrelation_test_counter.reset()
    autocorrelation_test_counter.set_max_lag(2)

    # Test second series
    for _ in range(5000):
        autocorrelation_test_counter.count(1)
        autocorrelation_test_counter.count(1)
        autocorrelation_test_counter.count(-1)

    # print results
    print "Mean = " + str(autocorrelation_test_counter.get_mean())
    print "Var = " + str(autocorrelation_test_counter.get_var())

    autocorrelation_test_counter.report()
def task_4_2_1():
    """
    Execute exercise 4.2.1, which is basically just a test for the auto correlation.
    """
    # TODO Task 4.2.1: Your code goes here
    list1 = [
        1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1., 1., -1.,
        1., -1., 1., -1., 1., -1., 1., -1.
    ]
    list2 = [
        1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1.,
        1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1.
    ]
    TIAC1 = TimeIndependentAutocorrelationCounter("EX1")
    TIAC2 = TimeIndependentAutocorrelationCounter("EX2")
    for i in range(len(list1)):
        TIAC1.count(list1[i])
    for i in range(len(list2)):
        TIAC2.count(list2[i])
    print "The Autocorrelation for the example 1 with  (lag%2 = 0) : ", TIAC1.get_auto_cor(
        0)
    print "The Autocorrelation for the example 1 with  (lag%2 = 1) : ", TIAC1.get_auto_cor(
        1)
    print "The Autocorrelation for the example 2 with  (lag%3 = 0) : ", TIAC2.get_auto_cor(
        0)
    print "The Autocorrelation for the example 2 with  (lag%3 = 1) : ", TIAC2.get_auto_cor(
        1)
    print "The Autocorrelation for the example 2 with  (lag%3 = 2) : ", TIAC2.get_auto_cor(
        2)
Exemple #6
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(name="waiting time")
        self.hist_wt = TimeIndependentHistogram(self.sim, "w")
        self.acnt_wt = TimeIndependentAutocorrelationCounter(
            "waiting time with lags 1 to 20", max_lag=20)

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

        # system utilization
        self.cnt_sys_util = TimeDependentCounter(self.sim,
                                                 name="system utilization")

        # blocking probability
        self.cnt_bp = TimeIndependentCounter("bp")
        self.hist_bp = TimeIndependentHistogram(self.sim, "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_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()

    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.acnt_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()

    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_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(self.sim.system_state.get_queue_length())
        self.hist_ql.count(self.sim.system_state.get_queue_length())

        if self.sim.system_state.server_busy:
            self.cnt_sys_util.count(1)
        else:
            self.cnt_sys_util.count(0)
Exemple #7
0
def task_4_2_1():
    """
    Execute exercise 4.2.1, which is basically just a test for the auto correlation.
    """
    # TODO Task 4.2.1: Your code goes here
    counter = TimeIndependentAutocorrelationCounter(name="Demo Auto Correlation Counter", max_lag=4)
    for i in range(1000):
        if i % 2 == 0:
            counter.count(1)
        else:
            counter.count(-1)
    print "---First sequence----"
    counter.report()

    counter.reset()
    for i in range(1000):
        counter.count(1)
        counter.count(1)
        counter.count(-1)

    print "---Second sequence----"
    counter.report()
def task_4_2_1():
    """
    Execute exercise 4.2.1, which is basically just a test for the auto correlation.
    """
    # TODO Task 4.2.1: Your code goes here
    seq1 = TimeIndependentAutocorrelationCounter(max_lag=5)
    for i in range(5000):
        if i % 2 == 0:
            seq1.count(1)
        else:
            seq1.count(-1)

    for i in range(5):
        seq1.get_auto_cor(i)
        print("Auro correlation sequence 1 for lag " + str(i) + " = " +
              str(seq1.get_auto_cor(i)))

    seq2 = TimeIndependentAutocorrelationCounter(max_lag=5)

    for i in range(5000):
        if (i + 1) % 3 == 0:
            seq2.count(-1)
        else:
            seq2.count(1)

    for i in range(5):
        seq1.get_auto_cor(i)
        print("Auro correlation sequence 2 for lag " + str(i) + " = " +
              str(seq1.get_auto_cor(i)))