def generate(self): """ Run simulation and collect statistic depending with specified strategy strategy = { single - run with fixed params range - run for specified range } strategy depends on input_range, if there is no range then single simulation will be executed """ generated_values = [] print("Input parameters has range =", self.has_range, "\n") if self.has_range: for var in np.arange(self.input_range[0], self.input_range[1] + 1, self.step): start_time = time.time() generated_values_storage = Generated_values_storage() for i in range(0, int(self.repeats)): sim = Simulation("m/m/c[c0]/r[l,h]", self.lambd if self.lambd != -1 else var, self.mu if self.mu != -1 else var, self.theta if self.theta != -1 else var, int(self.C) if self.C != -1 else var, int(self.c0) if self.c0 != -1 else var, int(self.L) if self.L != -1 else var, int(self.H) if self.H != -1 else var, int(self.simulation_time), int(self.Q) if self.Q != -1 else var, self.is_debug) sim.start() generated_values_storage.add(sim) end_time = time.time() print("Generated values added to storage for ", self.x_axis, " = ", var, "/", self.input_range[1], ", repeated ", len(range(0, int(self.repeats))), " times, ", "execution time = %s sec" % (end_time - start_time)) generated_values_storage.normalize(self.repeats) generated_values.append(generated_values_storage) else: for i in range(1, self.repeats): print("Repeat #", i) generated_values_storage = Generated_values_storage() sim = Simulation("m/m/c[c0]/r[l,h]", self.lambd, self.mu, self.theta, int(self.C), int(self.c0), int(self.L), int(self.H), int(self.simulation_time), int(self.Q), self.is_debug) generated_values_storage.add(sim) generated_values_storage.normalize(self.repeats) generated_values.append(generated_values_storage) return generated_values
def generate_statistics(self): generated_values = [] t1 = time.time() for lambd in self.range_dict[PARAM.LAMBDA]: for mu in self.range_dict[PARAM.MU]: for theta in self.range_dict[PARAM.THETA]: for C in self.range_dict[PARAM.C]: for c0 in self.range_dict[PARAM.c0]: for L in self.range_dict[PARAM.L]: for H in self.range_dict[PARAM.H]: for Q in self.range_dict[PARAM.Q]: if lambd <= 0: continue if mu <= 0: continue if theta <= 0: continue if C <= 0: continue if c0 <= 0: continue if L <= 0: continue if H <= 0: continue if Q <= 0: continue if c0 > C or c0 < 0: continue if H - L < 2: continue if Q - H <= 2: continue if H < L or H > Q: continue if L > Q: continue start_time = time.time() generated_values_storage = Generated_values_storage( ) for repeats in self.range_dict[ PARAM.REPEATS]: sim = Simulation( self.mode, lambd, mu, theta, C, c0, L, H, self.simulation_time, Q, self.is_debug) if (self.strategy == "time"): sim.start() else: sim.start_requests( self.simulation_time) generated_values_storage.add(sim) end_time = time.time() generated_values_storage.normalize( self.parameters[ PARAM.REPEATS].end_value) generated_values.append( generated_values_storage) self.log(lambd, mu, theta, C, c0, L, H, Q, start_time, end_time) t2 = time.time() print("Total simulation time = %.5f" % (t2 - t1)) return generated_values
def main(): if len(sys.argv) != 9: print("Input parameters must be: 'lambda mu theta C c0 Q simulation_time repeats'") else: start_time = time.time() lambd = float(sys.argv[1]) mu = float(sys.argv[2]) theta = float(sys.argv[3]) C = int(sys.argv[4]) c0 = int(sys.argv[5]) Q = int(sys.argv[6]) simulation_time = int(sys.argv[7]) repeats = int(sys.argv[8]) print("Test for M/M/C[c0]/R: lambda =", lambd, ", mu =", mu, ", theta = ", theta, ", C =", C, ", c0=", c0, ", Q =", Q, ", sim time =", simulation_time, ", repeats =", repeats) # unused parameters L = 1 H = 1 # B = 0 W = 0 N = 0 is_debug = False simulation = Simulation("m/m/c[c0]/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) for i in range(0, repeats): simulation = Simulation("m/m/c[c0]/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) simulation.start() B += simulation.queue.blocked / (simulation.queue.blocked + simulation.served_count) w = 0 for request in simulation.served_requests: w += request.w for request in simulation.queue.blocked_requests: w += request.w W += w / (simulation.served_count + len(simulation.queue.blocked_requests)) N += (w / (simulation.served_count + len(simulation.queue.blocked_requests))) * lambd B /= repeats W /= repeats N /= repeats end_time = time.time() print("B =", B, "\nW =", W, "\nN =", N) print("Execution time = %s seconds" % (end_time - start_time))
def main(): lambd = 2 mu = 1 theta = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 C = 5 c0 = 1 Q = 10 L = 3 H = 6 simulation_time = 1000 repeats = 1 served_requests = [ 10**2, 10**3, 10**4, 10**5, 10**6, 10**7, 10**8, 10**9, 10**10 ] files = [] for sr in served_requests: start_time = time.time() theoretical_mmcr = MMCR(lambd, mu, c0, Q) theoretical_result = theoretical_mmcr.run() B = theoretical_result[0] W = theoretical_result[1] N = theoretical_result[2] ### sim B2 = 0 W2 = 0 N2 = 0 served = 0 is_debug = False if is_debug: print("Q=", Q) simulation = Simulation("m/m/c/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) for i in range(0, repeats): simulation = Simulation("m/m/c/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) simulation.start(sr) B2 += simulation.queue.blocked / (simulation.queue.blocked + simulation.served_count) w = 0 for request in simulation.served_requests: w += request.w for request in simulation.queue.blocked_requests: w += request.w W2 += w / (simulation.served_count + len(simulation.queue.blocked_requests)) N2 += (w / (simulation.served_count + len(simulation.queue.blocked_requests))) * lambd B2 /= repeats W2 /= repeats N2 /= repeats end_time = time.time() abs_path = os.path.abspath(__file__) path = 'served_requests_test' + '-(lambda=%s,mu=%s,theta=%s,C=%s,c0=%s,L=%s,H=%s,sim_time=%s, served requests =%s)' % ( lambd, mu, theta, C, c0, L, H, (end_time - start_time), sr) path = path + ".csv" outfile = open(path, 'w') output = csv.writer(outfile, delimiter=';') output.writerow(['Served', 'theor B', 'imit B', 'diff']) outrow = [] outrow.append(B) outrow.append(B2) outrow.append(abs((B2 - B) / B)) output.writerow(outrow) output.writerow(['Served', 'theor N', 'imit N', 'diff']) outrow = [] outrow.append(N) outrow.append(N2) outrow.append(abs((N2 - N) / N)) output.writerow(outrow) output.writerow(['Served', 'theor W', 'imit W', 'diff']) outrow = [] outrow.append(W) outrow.append(W2) outrow.append(abs((W2 - W) / W)) output.writerow(outrow) outfile.close() files.append(path) print("theoretical B =", B, ", simulation B=", B2, ", Difference = ", abs((B2 - B) / B), "\ntheoretical W =", W, ", simulation W=", W2, ", Difference = ", abs((W2 - W) / W), "\ntheoretical N =", N, ", simulation N=", N2, ", Difference = ", abs((N2 - N) / N))
def main(): """ Main method to launch """ if len(sys.argv) < 12 or len(sys.argv) > 13: print("Input parameters must be: 'filename lambda mu C c0 Q theta L H simulation_time is_debug repeats(optionally)'") else: start_time = time.time() file_name = sys.argv[1] lambd = float(sys.argv[2]) mu = float(sys.argv[3]) C = int(sys.argv[4]) c0 = int(sys.argv[5]) Q = int(sys.argv[6]) theta = float(sys.argv[7]) L = int(sys.argv[8]) H = int(sys.argv[9]) simulation_time = float(sys.argv[10]); is_debug = True if sys.argv[11] == "True" else False; repeats = int(sys.argv[12]) if len(sys.argv) == 13 else 1; print("Simulation started for params: lambda =", lambd, ", mu =", mu, ", C =", C, ", c0 =", c0, ", Q =", Q, ", theta =", theta, ", L =", L, ", H =", H, ", repeats =", repeats) blocked = 0 served = 0 generated = 0 B = 0 N = 0 simulation = Simulation("m/m/c[c0]/r[l,h]", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) for i in range(0, repeats): simulation = Simulation("m/m/c[c0]/r[l,h]", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) simulation.start() blocked += simulation.queue.blocked served += simulation.served_count generated += simulation.flow.generated_count B += simulation.queue.blocked/(simulation.served_count+simulation.queue.blocked) N += simulation.served_count/simulation_time end_time = time.time() blocked = blocked/repeats served = served/repeats generated = generated/repeats B = B/repeats N = N/repeats print( "") print( "Summary results:") print( "blocked=", blocked, " served=", served, ", generated=", generated) print("B = ", B) print("N = ", N) print("Execution time = %s seconds" % (end_time - start_time)) print( "... to be implemented more summary ...") # write stats to file abs_path = os.path.abspath(__file__) path = os.path.relpath('stats', abs_path) path = os.path.join(path, file_name + '-(%s,%s,%s,%s,%s,%s,%s,%s).csv' % (lambd,mu,theta,C,c0,L,H,simulation_time)) outfile=open(path,'w') output = csv.writer(outfile, delimiter=';') output.writerow(['Request ID','Queue', 'Arrival_Time','Queue_Arrival_time','Server_Arrival_time','alpha','beta']) i=0 for request in simulation.served_requests: i=i+1 outrow=[] outrow.append(request.ID) outrow.append(request.queue_size_at_serving) outrow.append(request.arrival_time) outrow.append(request.queue_arrival_time) outrow.append(request.server_arrival_time) outrow.append(request.alpha) outrow.append(request.beta) output.writerow(outrow) outfile.close() return simulation
def main(): if len(sys.argv) != 9: print("Input parameters must be: 'lambda mu theta C c0 Q simulation_time repeats'") else: start_time = time.time() lambd = float(sys.argv[1]) mu = float(sys.argv[2]) theta = float(sys.argv[3]) C = int(sys.argv[4]) c0 = int(sys.argv[5]) Q = int(sys.argv[6]) simulation_time = int(sys.argv[7]) repeats = int(sys.argv[8]) # unused parameters L = 1 H = 1 # print("Difference test for M/M/C[c0]/R: lambda =", lambd, ", mu =", mu, ", C =", C, ", c0=", c0,", Q =", Q, ", theta = ", theta, ", sim time =", simulation_time, ", repeats =", repeats) print("c0 will be used for theoretical m/m/c/r calculation") ### theoretical theoretical_mmcr = MMCR(lambd, mu, c0, Q) theoretical_result = theoretical_mmcr.run() B = theoretical_result[0] W = theoretical_result[1] N = theoretical_result[2] ### sim B2 = 0 W2 = 0 N2 = 0 is_debug = False if is_debug: print("Q=",Q) simulation = Simulation("m/m/c[c0]/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) for i in range(0, repeats): simulation = Simulation("m/m/c[c0]/r", lambd, mu, theta, C, c0, L, H, simulation_time, Q, is_debug) simulation.start() B2 += simulation.queue.blocked / (simulation.queue.blocked + simulation.served_count) w = 0 for request in simulation.served_requests: w += request.w for request in simulation.queue.blocked_requests: w += request.w W2 += w / (simulation.served_count + len(simulation.queue.blocked_requests)) N2 += (w / (simulation.served_count + len(simulation.queue.blocked_requests))) * lambd B2 /= repeats W2 /= repeats N2 /= repeats end_time = time.time() print("theoretical B =", B, ", simulation B=", B2, ", Difference = ", abs((B2 - B) / B), "\ntheoretical W =", W, ", simulation W=", W2, ", Difference = ", abs((W2 - W) / W), "\ntheoretical N =", N, ", simulation N=", N2, ", Difference = ", abs((N2 - N) / N)) print("Execution time = %s seconds" % (end_time - start_time))
adapters = get_available_adapters(arguments.device, device_package=arguments.device_package) protocols = {adapter.protocol for adapter in adapters.values()} for p in protocols: print(p) exit() device = device_type(**parameters) adapter = import_adapter(arguments.device, arguments.protocol, device_package=arguments.device_package)( device, arguments.adapter_args) simulation = Simulation(device=device, adapter=adapter) simulation.cycle_delay = arguments.cycle_delay simulation.speed = arguments.speed if arguments.rpc_host: simulation.control_server = ControlServer( { 'device': device, 'simulation': ExposedObject(simulation, exclude=('start', 'control_server')) }, *arguments.rpc_host.split(':')) simulation.start()
def main(): if len(sys.argv) != 7: print( "Input parameters must be: 'lambda mu C Q simulation_time repeats'" ) else: start_time = time.time() print( "Difference test for theoretical M/M/C/R model and 'm/m/c/r' imitation mode." ) lambd = float(sys.argv[1]) mu = float(sys.argv[2]) C = int(sys.argv[3]) R = int(sys.argv[4]) simulation_time = int(sys.argv[5]) repeats = int(sys.argv[6]) theoretical_mmcr = MMCR(lambd, mu, C, R) theoretical_result = theoretical_mmcr.run() B = theoretical_result[0] W = theoretical_result[1] N = theoretical_result[2] Wq = theoretical_result[3] Q = theoretical_result[4] ### sim B2 = 0 W2 = 0 N2 = 0 Wq2 = 0 Q2 = 0 theta = 1 L = 1 H = 1 c0 = C is_debug = False simulation = Simulation("m/m/c/r", lambd, mu, theta, C, c0, L, H, simulation_time, R, is_debug) for i in range(0, repeats): simulation = Simulation("m/m/c/r", lambd, mu, theta, C, c0, L, H, simulation_time, R, is_debug) simulation.start() B2 += simulation.queue.blocked / (simulation.queue.blocked + simulation.served_count) w = 0 for request in simulation.served_requests: w += request.w for request in simulation.queue.blocked_requests: w += request.w W2 += w / (simulation.served_count + len(simulation.queue.blocked_requests)) N2 += (w / (simulation.served_count + len(simulation.queue.blocked_requests))) * lambd wq = 0 for request in simulation.served_requests: wq += request.wq for request in simulation.queue.blocked_requests: wq += request.wq Wq2 += wq / (simulation.served_count + len(simulation.queue.blocked_requests)) Q2 += (wq / (simulation.served_count + len(simulation.queue.blocked_requests))) * lambd B2 /= repeats W2 /= repeats N2 /= repeats Wq2 /= repeats Q2 /= repeats end_time = time.time() print("theoretical B =", B, ", simulation B=", B2, ", Difference = ", abs((B2 - B) / B), "\ntheoretical W =", W, ", simulation W=", W2, ", Difference = ", abs((W2 - W) / W), "\ntheoretical N =", N, ", simulation N=", N2, ", Difference = ", abs((N2 - N) / N), "\ntheoretical Wq =", Wq, ", simulation Wq=", Wq2, ", Difference = ", abs((Wq2 - Wq) / Wq), "\ntheoretical Q =", Q, ", simulation Q=", Q2, ", Difference = ", abs((Q2 - Q) / Q)) print("Execution time = %s seconds" % (end_time - start_time))