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 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 test_TIC(self): """ Test the TimeIndependentCounter """ tic = TimeIndependentCounter() tic.count(3) tic.count(2) tic.count(5) tic.count(0) self.assertEqual(tic.get_mean(), 2.5, msg="Error in TimeIndependentCounter. Wrong mean calculation or wrong counting.") self.assertEqual(tic.get_var(), numpy.var([3, 2, 5, 0], ddof=1), msg="Error in TimeIndependentCounter. Wrong variance calculation or wrong counting.") self.assertEqual(tic.get_stddev(), numpy.std([3, 2, 5, 0], ddof=1), msg="Error in TimeIndependentCounter. Wrong std dev calculation or wrong counting.") tic.reset() tic.count(3.) tic.count(2.) tic.count(5.) tic.count(0.) self.assertEqual(tic.get_mean(), 2.5, msg="Error in TimeIndependentCounter. Wrong mean calculation or wrong counting.") self.assertEqual(tic.get_var(), numpy.var([3, 2, 5, 0], ddof=1), msg="Error in TimeIndependentCounter. Wrong variance calculation or wrong counting.") self.assertEqual(tic.get_stddev(), numpy.std([3, 2, 5, 0], ddof=1), msg="Error in TimeIndependentCounter. Wrong std dev calculation or wrong counting.")
def task_3_2_2(): """ Here, we execute task 3.2.2 and print the results to the console. The first result string keeps the results for 100s, the second one for 1000s simulation time. """ sim = Simulation() cnt = TimeIndependentCounter("sys_util") sim.sim_param.S = 5 sim.sim_param.SIM_TIME = 100000 results100 = [] for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() cnt.reset() for _ in range(100): cnt.count(sim.do_simulation().system_utilization) results100.append(cnt.get_mean()) sim.sim_param.SIM_TIME = 1000000 results1000 = [] for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() cnt.reset() for _ in range(100): cnt.count(sim.do_simulation().system_utilization) results1000.append(cnt.get_mean()) print "Results for simulation time of 100s (rho = 0.01, 0.5, 0.8 and 0.9)" print results100 print "Results for simulation time of 1000s (rho = 0.01, 0.5, 0.8 and 0.9)" print results1000
def task_3_2_2(): """ Here, we execute task 3.2.2 and print the results to the console. The first result string keeps the results for 100s, the second one for 1000s simulation time. """ sim = Simulation() cnt = TimeIndependentCounter("sys_util") sim.sim_param.S = 5 sim.sim_param.SIM_TIME = 100000 print('Results for simulation time of 100s') for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() cnt.reset() for _ in range(100): cnt.count(sim.do_simulation().system_utilization) print('rho = %s, real system utilization/throughput = %.4f' % (rho, cnt.get_mean())) sim.sim_param.SIM_TIME = 1000000 print('\nResults for simulation time of 1000s') for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() cnt.reset() for _ in range(100): cnt.count(sim.do_simulation().system_utilization) print("rho = %s, real system utilization/throughput = %.4f" % (rho, cnt.get_mean()))
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
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
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.")
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
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_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
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) """
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_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()
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.")
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')
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()
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()
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()
from simparam import SimParam from simulation import Simulation from matplotlib import pyplot from counter import TimeIndependentCounter from histogram import TimeIndependentHistogram """ This file should be used to keep all necessary code that is used for the simulation study in part 2 of the programming assignment. It contains the tasks 2.7.1 and 2.7.2. The function do_simulation_study() should be used to run the simulation routine, that is described in the assignment. """ sim_param = SimParam() random.seed(sim_param.SEED) sim = Simulation(sim_param) ql_count = TimeIndependentCounter() ql_hist = TimeIndependentHistogram(sim, "q") wt_count = TimeIndependentCounter() wt_hist = TimeIndependentHistogram(sim, "w") def task_2_7_1(): """ Here, you should execute task 2.7.1 (and 2.7.2, if you want). """ # TODO Task 2.7.1: Your code goes here return do_simulation_study(sim) def task_2_7_2():
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
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
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]
def task_3_2_2(): """ Here, we execute task 3.2.2 and print the results to the console. The first result string keeps the results for 100s, the second one for 1000s simulation time. """ # TODO Task 3.2.2: Your code goes here sim_param = SimParam() sim = Simulation(sim_param) count_sys = TimeIndependentCounter() sim_param.S = 5 print("S = " + str(sim.sim_param.S)) sim_param.SIM_TIME = 100000 #100s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=100s") sim_param.SIM_TIME = 1000000 #1000s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=1000s") sim_param = SimParam() sim = Simulation(sim_param) count_sys = TimeIndependentCounter() sim_param.S = 100000 print("S = " + str(sim.sim_param.S)) sim_param.SIM_TIME = 100000 #100s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=100s") sim_param.SIM_TIME = 1000000 #1000s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=1000s") sim_param = SimParam() sim = Simulation(sim_param) count_sys = TimeIndependentCounter() sim_param.S = 1 print("S = " + str(sim.sim_param.S)) sim_param.SIM_TIME = 100000 #100s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=100s") sim_param.SIM_TIME = 1000000 #1000s for rho in [0.01, 0.5, 0.8, 0.9]: sim.sim_param.RHO = rho sim.reset() count_sys.reset() for k in range(sim.sim_param.NO_OF_RUNS): r = sim.do_simulation().system_utilization count_sys.count(r) print("system_utilization = " + str(count_sys.get_mean()) + " RHO "+str(rho) + " Sim.Time=1000s")