parallelize runs
This commit is contained in:
45
src/main.py
45
src/main.py
@@ -1,9 +1,12 @@
|
|||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import random
|
import random
|
||||||
|
import os
|
||||||
|
import time
|
||||||
from heapq import heappush, heappop
|
from heapq import heappush, heappop
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from scipy.stats import t
|
from scipy.stats import t
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from multiprocessing import Pool
|
||||||
|
|
||||||
class Event:
|
class Event:
|
||||||
def __init__(self, event_type, request):
|
def __init__(self, event_type, request):
|
||||||
@@ -131,10 +134,26 @@ class Simulation:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def run_single_simulation(args):
|
||||||
|
c, lambda_val, simulation_time = args
|
||||||
|
# for different seed in each process
|
||||||
|
random.seed(time.time() + os.getpid())
|
||||||
|
try:
|
||||||
|
sim = Simulation(c, lambda_val)
|
||||||
|
sim.run(simulation_time)
|
||||||
|
if len(sim.response_times) > 0:
|
||||||
|
run_mean = sum(sim.response_times) / len(sim.response_times)
|
||||||
|
loss_rate = sim.loss_rate
|
||||||
|
return (run_mean, loss_rate)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
except ValueError: # Loss rate too high
|
||||||
|
return None
|
||||||
|
|
||||||
def simulation_wrapper():
|
def simulation_wrapper():
|
||||||
C_values = [1, 2, 3, 6]
|
C_values = [1, 2, 3, 6]
|
||||||
simulation_time = 1000
|
simulation_time = 1000
|
||||||
num_runs = 10
|
num_runs = 12
|
||||||
min_runs = 5
|
min_runs = 5
|
||||||
confidence_level = 0.95
|
confidence_level = 0.95
|
||||||
|
|
||||||
@@ -142,6 +161,7 @@ def simulation_wrapper():
|
|||||||
|
|
||||||
plt.figure(figsize=(12, 8))
|
plt.figure(figsize=(12, 8))
|
||||||
|
|
||||||
|
with Pool() as pool: # pool of workers
|
||||||
for c in C_values:
|
for c in C_values:
|
||||||
lambda_points = []
|
lambda_points = []
|
||||||
means = []
|
means = []
|
||||||
@@ -150,22 +170,14 @@ def simulation_wrapper():
|
|||||||
print(f"\nProcessing C={c}")
|
print(f"\nProcessing C={c}")
|
||||||
|
|
||||||
for lambda_val in lambda_vals:
|
for lambda_val in lambda_vals:
|
||||||
run_results = []
|
|
||||||
loss_rates = []
|
|
||||||
|
|
||||||
# run num_runs simulation for each lambda
|
# run num_runs simulation for each lambda
|
||||||
for _ in range(num_runs):
|
args_list = [(c, lambda_val, simulation_time) for _ in range(num_runs)]
|
||||||
try:
|
results = pool.map(run_single_simulation, args_list)
|
||||||
sim = Simulation(c, lambda_val)
|
|
||||||
sim.run(simulation_time)
|
|
||||||
|
|
||||||
if len(sim.response_times) > 0:
|
# collect results from successful simulations
|
||||||
run_mean = sum(sim.response_times)/len(sim.response_times)
|
successful_results = [res for res in results if res is not None]
|
||||||
run_results.append(run_mean)
|
run_results = [res[0] for res in successful_results]
|
||||||
loss_rates.append(sim.loss_rate)
|
loss_rates = [res[1] for res in successful_results]
|
||||||
|
|
||||||
except ValueError: # lossrate too high
|
|
||||||
continue
|
|
||||||
|
|
||||||
# reject if not enough successful run
|
# reject if not enough successful run
|
||||||
if len(run_results) >= min_runs:
|
if len(run_results) >= min_runs:
|
||||||
@@ -207,5 +219,6 @@ def simulation_wrapper():
|
|||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
simulation_wrapper()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user