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 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_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_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_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 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_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 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()
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 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")