refactor code
This commit is contained in:
@@ -174,12 +174,12 @@ def simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, l
|
||||
run_results = [res[0] for res in successful_results]
|
||||
loss_rates = [res[1] for res in successful_results]
|
||||
|
||||
n = len(run_results)
|
||||
# reject if not enough successful run
|
||||
if len(run_results) >= min_runs:
|
||||
if n >= min_runs:
|
||||
# statistics
|
||||
mean_rt = np.mean(run_results)
|
||||
std_dev = np.std(run_results, ddof=1)
|
||||
n = len(run_results)
|
||||
|
||||
# confidence interval
|
||||
t_value = t.ppf((1 + confidence_level)/2, n-1)
|
||||
@@ -196,8 +196,8 @@ def simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, l
|
||||
losses.append(mean_loss)
|
||||
|
||||
print(f"C={c}, λ={lambda_val:.2f}, Mean RT={mean_rt:.2f} ± {ci:.2f}, Mean Loss Rate={mean_loss:.2%}")
|
||||
elif len(run_results) > 0:
|
||||
print(f"λ={lambda_val:.2f} skipped - only {len(run_results)} successful run(s)")
|
||||
elif n > 0:
|
||||
print(f"λ={lambda_val:.2f} skipped - only {n} successful run(s)")
|
||||
continue
|
||||
else:
|
||||
print(f"Stopped at λ={lambda_val:.2f} - no successful run")
|
||||
@@ -213,18 +213,6 @@ def simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, l
|
||||
|
||||
loss_data[c] = (np.array(lambda_points), np.array(losses))
|
||||
|
||||
# determine optimal C for lamba = 1
|
||||
if mean_at_lambda1:
|
||||
sorted_C = sorted(mean_at_lambda1.items(), key=lambda item: item[1][0])
|
||||
(best_C, (mean1, lower1, upper1)), (_, (_, lower2, _)) = sorted_C[:2]
|
||||
if upper1 < lower2:
|
||||
print(f"Optimal C at λ=1 is {best_C} with Mean RT = {mean1:.2f} (non-overlapping CIs)")
|
||||
else:
|
||||
print("Confidence intervals overlap between the two best C.")
|
||||
else:
|
||||
print("\nNo valid λ=1 data for any C.")
|
||||
|
||||
|
||||
# plot curves
|
||||
if len(lambda_vals)>1:
|
||||
ax_rt.set_xlim(left=0)
|
||||
@@ -234,12 +222,13 @@ def simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, l
|
||||
ax_rt.legend()
|
||||
ax_rt.grid(True)
|
||||
|
||||
return loss_data
|
||||
return loss_data, mean_at_lambda1
|
||||
|
||||
def simulation_wrapper2(simulation_time, num_runs, min_runs, confidence_level, lambda_vals, max_loss_value=0.1):
|
||||
fig, (ax_rt, ax_loss) = plt.subplots(2,1, figsize=(12,10), gridspec_kw={'height_ratios': [4, 3], 'hspace': 0.4}, sharex=False)
|
||||
loss_data = simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, lambda_vals, ax_rt)
|
||||
loss_data, mean_at_lambda1 = simulation_wrapper1(simulation_time, num_runs, min_runs, confidence_level, lambda_vals, ax_rt)
|
||||
|
||||
if len(lambda_vals) > 1:
|
||||
for c, (lams, losses) in loss_data.items():
|
||||
lambda_vals2 = []
|
||||
if len(lams)==0:
|
||||
@@ -302,6 +291,20 @@ def simulation_wrapper2(simulation_time, num_runs, min_runs, confidence_level, l
|
||||
ax_loss.plot(lambda_points, losses, label=f'C={c}')
|
||||
ax_loss.fill_between(lambda_points, ci_lower, ci_upper, alpha=0.2)
|
||||
|
||||
|
||||
# determine optimal C for lamba = 1
|
||||
if mean_at_lambda1:
|
||||
sorted_C = sorted(mean_at_lambda1.items(), key=lambda item: item[1][0])
|
||||
(best_C, (mean1, lower1, upper1)), (_, (_, lower2, _)) = sorted_C[:2]
|
||||
if upper1 < lower2:
|
||||
print(f"Optimal C at λ=1 is {best_C} with Mean RT = {mean1:.2f} (non-overlapping CIs)")
|
||||
else:
|
||||
print("Confidence intervals overlap between the two best C.")
|
||||
else:
|
||||
print("\nNo valid λ=1 data for any C.")
|
||||
|
||||
#plot curves
|
||||
if len(lambda_vals) > 1:
|
||||
ax_loss.set_xlim(left=2)
|
||||
ax_loss.set_xlabel('Arrival Rate (λ)')
|
||||
ax_loss.set_ylabel('Mean Loss Rate')
|
||||
|
Reference in New Issue
Block a user