Example #1
0
    def test_confidence(self):
        """
        Test the basic implementation of the confidence calculation in the time independent counter.
        """
        tic = TimeIndependentCounter()
        tic.count(0)
        tic.count(3)
        tic.count(5)
        tic.count(2)
        tic.count(5)
        tic.count(8)
        tic.count(1)
        tic.count(2)
        tic.count(1)

        self.assertAlmostEqual(tic.report_confidence_interval(.05, print_report=True), 1.96, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")
        self.assertAlmostEqual(tic.report_confidence_interval(.1, print_report=False), 1.58, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")
        self.assertAlmostEqual(tic.report_confidence_interval(.2, print_report=False), 1.187, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")

        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.05), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.05), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")

        self.assertEqual(tic.is_in_confidence_interval(5.0, alpha=.05), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.1), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.1), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")

        self.assertEqual(tic.is_in_confidence_interval(5.0, alpha=.1), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.2), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.2), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.0, alpha=.2), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")

        lower, upper = tic.report_bootstrap_confidence_interval(alpha=.05, resample_size=10000)
        self.assertAlmostEqual(lower, 1.33333, delta=0.01,
                               msg="Error in bootstrap confidence interval calculation. Wrong lower boundary.")
        self.assertAlmostEqual(upper, 4.44444, delta=0.01,
                               msg="Error in bootstrap confidence interval calculation. Wrong upper boundary.")
        self.assertEqual(tic.is_in_bootstrap_confidence_interval(4, resample_size=5000, alpha=.05), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_bootstrap_confidence_interval(1, resample_size=5000, alpha=.05), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
Example #2
0
def task_5_2_4(rho, alpha, sim_time, num):
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    # TODO Task 5.2.4: Your code goes here

    #rho = 0.5 / alpha = 0.1 / Sim time = 100s
    TIC_SU = TimeIndependentCounter("System Utilization")
    TIC_CI = []
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = sim_time
    sim.sim_param.S = 100000
    sim.sim_param.RHO = rho
    random.seed(sim.sim_param.SEED_IAT)
    random.seed(sim.sim_param.SEED_ST)
    for i in range(100):
        for j in range(30):
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                TIC_SU.count(sim.do_simulation().system_utilization)
                sim.reset()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC_CI.append(
                (TIC_SU.get_mean() - TIC_SU.report_confidence_interval(alpha),
                 TIC_SU.get_mean() + TIC_SU.report_confidence_interval(alpha)))
        TIC_SU.reset()
    plot_confidence(sim, 100, TIC_CI, rho, "alpha=" + str(alpha), num, alpha)
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.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.2: Your code goes here
    bp = []
    hw = []
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = .9
    err = .0015
    half_width = 1.0
    count_bp = TimeIndependentCounter()
    i = 0
    for batch in [100, 1000]:
        for alpha in [.1, .05]:
            first_batch = False
            count_bp.reset()
            sim.reset()
            while 1:
                blocking_pro = sim.do_simulation_n_limit(
                    batch, first_batch).blocking_probability
                first_batch = True  #after first batch
                count_bp.count(blocking_pro)
                half_width = count_bp.report_confidence_interval(alpha)
                sim.sim_state.stop = False  #set the parameter back to original value
                sim.counter_collection.reset()
                sim.sim_state.num_blocked_packets = 0
                sim.sim_state.num_packets = 0
                if half_width < err:
                    break
            results[i] = sim.sim_state.now
            bp.append(count_bp.get_mean())
            hw.append(half_width)
            i += 1

    # print and return results
    print("BATCH SIZE:  100; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[0] / 1000) + "; Blocking Probability Mean: " +
          str(bp[0]) + "; Half width: " + str(hw[0]))
    print("BATCH SIZE:  100; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[1] / 1000) + "; Blocking Probability Mean: " +
          str(bp[1]) + "; Half width: " + str(hw[1]))
    print("BATCH SIZE: 1000; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[2] / 1000) + "; Blocking Probability Mean: " +
          str(bp[2]) + "; Half width: " + str(hw[2]))
    print("BATCH SIZE: 1000; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[3] / 1000) + "; Blocking Probability Mean: " +
          str(bp[3]) + "; Half width: " + str(hw[3]))
    return results
Example #4
0
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a significance level alpha. Simulation is performed for 100s and 1000s and for alpha = 10% and 5%.
    """
    sim = Simulation()

    # set parameters
    sim.sim_param.RHO = .9
    sim.reset()
    sim.sim_param.EPSILON = .0015
    sim.sim_param.S = 4

    # simulate
    results = []
    for sim_time in [100, 1000]:
        sim.sim_param.SIM_TIME = sim_time * 1000
        for alpha in [.1, .05]:
            sim.sim_param.ALPHA = alpha
            counter = TimeIndependentCounter("Blocking Probability")
            counter.reset()
            tmp = 1.0
            while len(counter.values) < 5 or tmp > sim.sim_param.EPSILON:
                sim.reset()
                sim_result = sim.do_simulation()
                bp = sim_result.blocking_probability
                counter.count(bp)
                tmp = counter.report_confidence_interval(
                    alpha=sim.sim_param.ALPHA, print_report=False)
            results.append(len(counter.values))
            counter.report_confidence_interval(alpha=sim.sim_param.ALPHA,
                                               print_report=True)

    # print and return results
    print('SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: ' + str(results[0]) +
          '; TOTAL SIMULATION TIME (SECONDS): ' + str(results[0] * 100))
    print('SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: ' + str(results[1]) +
          '; TOTAL SIMULATION TIME (SECONDS): ' + str(results[1] * 100))
    print('SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  ' + str(results[2]) +
          '; TOTAL SIMULATION TIME (SECONDS): ' + str(results[2] * 1000))
    print('SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  ' + str(results[3]) +
          '; TOTAL SIMULATION TIME (SECONDS): ' + str(results[3] * 1000))
    return results
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a confidence level alpha. Simulation is performed for 100s and 1000s and for alpha = 90% and 95%.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.1: Your code goes here
    bp = []
    hw = []
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = .9
    count_bp = TimeIndependentCounter()
    err = .0015
    i = 0
    for sim_time in [100000, 1000000]:
        sim.sim_param.SIM_TIME = sim_time
        for alpha in [.1, .05]:
            count_bp.reset()
            while 1:
                sim.reset()
                blocking_pro = sim.do_simulation().blocking_probability
                count_bp.count(blocking_pro)
                half_width = count_bp.report_confidence_interval(alpha=alpha)
                if half_width < err:
                    break

            results[i] = len(count_bp.values)
            bp.append(count_bp.get_mean())
            hw.append(half_width)
            i += 1


# print and return results
    print("SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: " + str(results[0]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[0] * 100) +
          "; Blocking Probability Mean: " + str(bp[0]) + "; Half width: " +
          str(hw[0]))
    print("SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: " + str(results[1]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[1] * 100) +
          "; Blocking Probability Mean: " + str(bp[1]) + "; Half width: " +
          str(hw[1]))
    print("SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  " + str(results[2]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[2] * 1000) +
          "; Blocking Probability Mean: " + str(bp[2]) + "; Half width: " +
          str(hw[2]))
    print("SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  " + str(results[3]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[3] * 1000) +
          "; Blocking Probability Mean: " + str(bp[3]) + "; Half width: " +
          str(hw[3]))
    return results
Example #6
0
    def test_confidence(self):
        """
        Test the basic implementation of the confidence calculation in the time independent counter.
        """
        tic = TimeIndependentCounter()
        tic.count(0)
        tic.count(3)
        tic.count(5)
        tic.count(2)
        tic.count(5)
        tic.count(8)
        tic.count(1)
        tic.count(2)
        tic.count(1)
        self.assertAlmostEqual(tic.report_confidence_interval(.95, print_report=False), 1.96, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")
        self.assertAlmostEqual(tic.report_confidence_interval(.9, print_report=False), 1.58, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")
        self.assertAlmostEqual(tic.report_confidence_interval(.8, print_report=False), 1.187, delta=.01,
                               msg="Error in Confidence interval calculation. Wrong size of half interval returned.")

        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.95), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.95), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_confidence_interval(5.0, alpha=.95), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.9), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.9), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(5.0, alpha=.9), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.5, alpha=.8), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(1.3, alpha=.8), False,
                         msg="Error in Confidence interval calculation. Value id in interval, but shouldn't.")
        self.assertEqual(tic.is_in_confidence_interval(4.0, alpha=.8), True,
                         msg="Error in Confidence interval calculation. Value should be in interval, but isn't.")
Example #7
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.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.2: Your code goes here
    conf_level = .0015
    sim = Simulation()
    sim.sim_param.RHO = 0.9
    sim.sim_param.S = 4
    i = 0
    tic = TimeIndependentCounter()

    for batch_size in [100, 1000]:
        for alpha in [0.1, 0.05]:
            tic.reset()
            check = False
            new_batch = False
            sim.reset()
            while not check:
                sim_result = sim.do_simulation_n_limit(batch_size, new_batch)
                tic.count(sim_result.blocking_probability)
                if len(tic.values) > 5 and tic.report_confidence_interval(
                        alpha) < conf_level:
                    check = True
                else:
                    sim.sim_state.num_blocked_packets = 0
                    sim.sim_state.num_packets = 0
                    sim.sim_state.stop = False
                    sim.counter_collection.reset()
                    new_batch = True

            results[i] = sim.sim_state.now
            i += 1

    # print and return results
    print "BATCH SIZE:  100; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " + str(
        results[0] / 1000)
    print "BATCH SIZE:  100; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " + str(
        results[1] / 1000)
    print "BATCH SIZE: 1000; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " + str(
        results[2] / 1000)
    print "BATCH SIZE: 1000; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " + str(
        results[3] / 1000)
    return results
def task_5_2_4():
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    # TODO Task 5.2.4: Your code goes here

    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 40000000  #infinite M/M/1/inf
    err = .0015
    plt_no = 1
    for rho in [0.5, 0.9]:
        sim.sim_param.RHO = rho
        for alpha in [0.1, 0.05]:
            for sim_time in [100000, 1000000]:
                sim.sim_param.SIM_TIME = sim_time
                print(" Sim time " + str(sim.sim_param.SIM_TIME / 1000) +
                      "s " + " Alpha " + str(alpha) + " RHO " + str(rho))
                count_util = TimeIndependentCounter()
                mean_count = TimeIndependentCounter()
                y_low = []
                y_high = []
                x = []
                for repeat in range(100):
                    count_util.reset()
                    for sim_run in range(30):
                        sim.reset()
                        count_util.count(
                            sim.do_simulation().system_utilization)

                    mean = count_util.get_mean()
                    half_width = count_util.report_confidence_interval(
                        alpha=alpha)
                    mean_count.count(mean)
                    y_low.append(mean - half_width)
                    y_high.append(mean + half_width)
                    x.append(repeat + 1)

                pyplot.subplot(2, 2, plt_no)
                plt_no += 1
                plot_confidence(sim, x, y_low, y_high, mean_count.get_mean(),
                                sim.sim_param.RHO, "Utilization", alpha)

        pyplot.show()
        plt_no = 1
Example #9
0
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a confidence level alpha. Simulation is performed for 100s and 1000s and for alpha = 90% and 95%.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.1: Your code goes here
    conf_level = .0015
    sim = Simulation()
    sim.sim_param.RHO = 0.9
    sim.sim_param.S = 4
    i = 0
    tic = TimeIndependentCounter()

    for sim_time in [100000, 1000000]:
        sim.sim_param.SIM_TIME = sim_time
        for alpha in [0.1, 0.05]:
            tic.reset()
            count = 0
            check = False
            while not check:
                sim.reset()
                sim_result = sim.do_simulation()
                tic.count(sim_result.blocking_probability)
                count += 1
                if tic.report_confidence_interval(alpha) < conf_level:
                    check = True

            results[i] = count
            i += 1

    # print and return results
    print "SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: " + str(
        results[0]) + "; TOTAL SIMULATION TIME (SECONDS): " + str(
            results[0] * 100)
    print "SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: " + str(
        results[1]) + "; TOTAL SIMULATION TIME (SECONDS): " + str(
            results[1] * 100)
    print "SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  " + str(
        results[2]) + "; TOTAL SIMULATION TIME (SECONDS): " + str(
            results[2] * 1000)
    print "SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  " + str(
        results[3]) + "; TOTAL SIMULATION TIME (SECONDS): " + str(
            results[3] * 1000)
    return results
Example #10
0
def task_5_2_4():
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    # TODO Task 5.2.4: Your code goes here
    sim = Simulation()
    sim.sim_param.S = 10000
    tic_sys_util = TimeIndependentCounter()
    i = 1
    pyplot.subplots_adjust(hspace=0.6)
    for rho in [.5, .9]:
        sim.sim_param.RHO = rho
        sim.reset()
        for alpha in [.1, .05]:
            for sim_time in [100000, 1000000]:
                sim.sim_param.SIM_TIME = sim_time
                upper_bounds = []
                lower_bounds = []
                means = []

                for _ in range(100):
                    tic_sys_util.reset()
                    for _ in range(30):
                        sim.reset()
                        sim_result = sim.do_simulation()
                        tic_sys_util.count(sim_result.system_utilization)
                    conf_interval = tic_sys_util.report_confidence_interval(
                        alpha)
                    sample_mean = tic_sys_util.get_mean()
                    lower_bounds.append(sample_mean - conf_interval)
                    upper_bounds.append(sample_mean + conf_interval)
                    means.append(sample_mean)

                pyplot.subplot(4, 2, i)
                plot_confidence(sim, range(1, 101), lower_bounds, upper_bounds,
                                np.mean(means), rho, "Sys Util", alpha)
                i += 1
    pyplot.show()
Example #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]
Example #12
0
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a confidence level alpha. Simulation is performed for 100s and 1000s and for alpha = 90% and 95%.
    """
    results = [None, None, None, None]
    TIC = TimeIndependentCounter("bp")

    # TODO Task 5.2.1: Your code goes here
    #SIM TIME:  100s; ALPHA: 10%
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 100000
    sim.sim_param.S = 4
    sim.sim_param.RHO = 0.9
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                results[0] = i
                break
            sim.reset()
    #SIM TIME:  100s; ALPHA:  5%
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[1] = i
                break
            sim.reset()
    #SIM TIME: 1000s; ALPHA: 10%
    sim.sim_param.SIM_TIME = 1000000
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[2] = i
                break
            sim.reset()
    #SIM TIME: 1000s; ALPHA:  5%
    sim.sim_param.SIM_TIME = 1000000
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[3] = i
                break
            sim.reset()

    # print and return results
    print "SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: " + str(results[0])
    print "SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: " + str(results[1])
    print "SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  " + str(results[2])
    print "SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  " + str(results[3])
    return results
Example #13
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 probability for the batch and calculate the significance 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.
    """
    sim = Simulation()

    # set parameters
    sim.sim_param.RHO = .9
    sim.sim_param.EPSILON = .0015
    sim.sim_param.S = 4

    results = []

    for batch_packets in [100, 1000]:
        for alpha in [.1, .05]:
            dn = batch_packets
            n = dn
            sim.sim_param.ALPHA = alpha
            counter = TimeIndependentCounter("Blocking Probability")
            counter.reset()
            confid_level_reached = False
            sim.reset()

            # execute simulation
            while not confid_level_reached:
                r = sim.do_simulation_n_limit(dn, new_batch=(n != dn))
                counter.count(r.blocking_probability)
                if len(counter.values
                       ) > 5 and counter.report_confidence_interval(
                           sim.sim_param.ALPHA,
                           print_report=False) < sim.sim_param.EPSILON:
                    confid_level_reached = True
                else:
                    n += dn
                    sim.counter_collection.reset()
                    sim.sim_state.num_blocked_packets = 0
                    sim.sim_state.num_packets = 0
                    sim.sim_state.stop = False

            counter.report_confidence_interval(sim.sim_param.ALPHA,
                                               print_report=True)
            print('Number of batches (n=' + str(dn) + ' for blocking probability confidence): ' + str(n / dn) + \
                  '; simulation time: ' + str(int(sim.sim_state.now / 1000)) + 's.')

            results.append(sim.sim_state.now)

    # print and return results
    print('BATCH SIZE:  100; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): ' +
          str(results[0] / 1000))
    print('BATCH SIZE:  100; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): ' +
          str(results[1] / 1000))
    print('BATCH SIZE: 1000; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): ' +
          str(results[2] / 1000))
    print('BATCH SIZE: 1000; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): ' +
          str(results[3] / 1000))

    return results
Example #14
0
def task_5_2_4():
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    sim = Simulation()
    sim.sim_param.S = 10000

    for sys_util in [.5, .9]:
        sim.sim_param.RHO = sys_util
        sim.reset()
        for alpha in [.1, .05]:
            sim.sim_param.ALPHA = alpha
            for time in [100, 1000]:
                sim.sim_param.SIM_TIME = time * 1000

                sys_util_counter = TimeIndependentCounter("su")
                mean_counter = TimeIndependentCounter("mc")
                y_min = []
                y_max = []
                x = []

                for run in range(100):
                    sys_util_counter.reset()
                    for _ in range(30):
                        sim.reset()
                        sim_result = sim.do_simulation()
                        su = sim_result.system_utilization
                        sys_util_counter.count(su)
                    h = sys_util_counter.report_confidence_interval(
                        alpha=sim.sim_param.ALPHA, print_report=False)
                    m = sys_util_counter.get_mean()
                    mean_counter.count(m)
                    y_min.append(m - h)
                    y_max.append(m + h)
                    x.append(run + 1)

                mean_calc = sim.sim_param.RHO
                mean_real = mean_counter.get_mean()
                total = len(x)
                good = 0
                good_real = 0
                for i in range(len(x)):
                    if y_min[i] <= mean_calc <= y_max[i]:
                        good += 1
                    if y_min[i] <= mean_real <= y_max[i]:
                        good_real += 1
                print(
                    str(good) + '/' + str(total) +
                    ' cover theoretical mean, ' + str(good_real) + '/' +
                    str(total) + ' cover sample mean.')

                if alpha == .1:
                    if time == 100:
                        pyplot.subplot(221)
                    else:
                        pyplot.subplot(223)
                else:
                    if time == 100:
                        pyplot.subplot(222)
                    else:
                        pyplot.subplot(224)
                plot_confidence(sim, x, y_min, y_max, mean_counter.get_mean(),
                                sim.sim_param.RHO, "system utilization")

        pyplot.show()