Skip to content

Commit

Permalink
Merge pull request #14 from The-Firefighters/master
Browse files Browse the repository at this point in the history
Algorithm Comparisons
  • Loading branch information
erelsgl authored Aug 14, 2024
2 parents 8ab879a + bd81fbe commit 53681be
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 173 deletions.
45 changes: 25 additions & 20 deletions experiments/firefighter_problem/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def Compare_NonSpread():
This function runs multiple experiments on randomly generated layered networks
and plots the results comparing the budget used by different algorithms.
"""
ex1 = experiments_csv.Experiment("./experiments/", "non_spreading.csv", backup_folder=None)
ex1 = experiments_csv.Experiment("./experiments/firefighter_problem/", "non_spreading.csv", backup_folder=None)
ex1.clear_previous_results() # to clear previous experiments

input_ranges = {
Expand All @@ -144,7 +144,7 @@ def multiple_runs(runs=30):
ex1.run_with_time_limit(multiple_runs, input_ranges={}, time_limit=0.9)

# Preprocess the DataFrame to extract numeric budget values
results_csv_file = "./experiments/non_spreading_minbudget.csv"
results_csv_file = "./experiments/firefighter_problem/non_spreading_minbudget.csv"
results = pd.read_csv(results_csv_file)

# Extract the numeric budget from the 'Budget' column
Expand All @@ -164,7 +164,7 @@ def extract_budget_numeric(budget):
results = results.dropna(subset=['Budget_numeric'])

# Save the preprocessed DataFrame to a temporary CSV file
preprocessed_csv_file = "./experiments/non_spreading_minbudget_preprocessed.csv"
preprocessed_csv_file = "./experiments/firefighter_problem/non_spreading_minbudget_preprocessed.csv"
results.to_csv(preprocessed_csv_file, index=False)

print("\n DataFrame-NonSpread: \n", results)
Expand All @@ -177,7 +177,7 @@ def extract_budget_numeric(budget):
y_field="Budget_numeric",
z_field="algorithm",
mean=True,
save_to_file="./experiments/non_spreading.png"
save_to_file="./experiments/firefighter_problem/non_spreading.png"
)

print("\n DataFrame-NonSpread: \n", ex1.dataFrame)
Expand All @@ -189,7 +189,7 @@ def Compare_SpreadingMaxSave():
This function runs multiple experiments on randomly generated directed graphs
and plots the results comparing the number of nodes saved by different algorithms.
"""
ex2 = experiments_csv.Experiment("./experiments/", "spreading_maxsave.csv", backup_folder=None)
ex2 = experiments_csv.Experiment("./experiments/firefighter_problem/", "spreading_maxsave.csv", backup_folder=None)
ex2.clear_previous_results() # to clear previous experiments

input_ranges = {
Expand Down Expand Up @@ -224,7 +224,7 @@ def multiple_runs(runs=10):

## DATA ISSUE WE HAD SO THIS IS A FIX ##
# Load the results
results_csv_file = "./experiments/spreading_maxsave.csv"
results_csv_file = "./experiments/firefighter_problem/spreading_maxsave.csv"
results = pd.read_csv(results_csv_file)

# Ensure 'algorithm' column is of type string
Expand All @@ -245,7 +245,7 @@ def multiple_runs(runs=10):
results['Nodes_Saved'] = results['Nodes_Saved'].astype(int)

# Save the cleaned DataFrame to a new CSV file (optional, for debugging)
cleaned_csv_file = "./experiments/spreading_maxsave_preprocessed.csv"
cleaned_csv_file = "./experiments/firefighter_problem/spreading_maxsave_preprocessed.csv"
results.to_csv(cleaned_csv_file, index=False)

# Plot the results using the cleaned DataFrame
Expand All @@ -261,7 +261,7 @@ def multiple_runs(runs=10):
sharex=True,
sharey=True,
mean=True,
save_to_file="./experiments/spreading_maxsave_budget.png"
save_to_file="./experiments/firefighter_problem/spreading_maxsave_budget.png"
)

multi_plot_results(
Expand All @@ -276,7 +276,7 @@ def multiple_runs(runs=10):
sharex=True,
sharey=True,
mean=True,
save_to_file="./experiments/spreading_maxsave_100_edge_prob.png"
save_to_file="./experiments/firefighter_problem/spreading_maxsave_100_edge_prob.png"
)

multi_plot_results(
Expand All @@ -291,7 +291,7 @@ def multiple_runs(runs=10):
sharex=True,
sharey=True,
mean=True,
save_to_file="./experiments/spreading_maxsave_200_edge_prob.png"
save_to_file="./experiments/firefighter_problem/spreading_maxsave_200_edge_prob.png"
)

multi_plot_results(
Expand All @@ -306,7 +306,7 @@ def multiple_runs(runs=10):
sharex=True,
sharey=True,
mean=True,
save_to_file="./experiments/spreading_maxsave_400_edge_prob.png"
save_to_file="./experiments/firefighter_problem/spreading_maxsave_400_edge_prob.png"
)

print("\n DataFrame-NonSpread: \n", ex2.dataFrame)
Expand All @@ -318,7 +318,7 @@ def Compare_SpreadingMinBudget():
This function runs multiple experiments on randomly generated directed graphs
and plots the results comparing the budget used by different algorithms.
"""
ex3 = experiments_csv.Experiment("./experiments/", "spreading_minbudget.csv", backup_folder=None)
ex3 = experiments_csv.Experiment("./experiments/firefighter_problem/", "spreading_minbudget.csv", backup_folder=None)
ex3.clear_previous_results() # to clear previous experiments

input_ranges = {
Expand All @@ -328,7 +328,7 @@ def Compare_SpreadingMinBudget():
node_counts = [100, 200, 400]
edge_probabilities = [0.1, 0.5, 0.8]

def multiple_runs(runs=10):
def multiple_runs(runs=15):
for num_nodes in node_counts:
for edge_prob in edge_probabilities:
graph = generate_random_DiGraph(num_nodes=num_nodes, edge_probability=edge_prob, seed=None)
Expand All @@ -349,7 +349,7 @@ def multiple_runs(runs=10):
ex3.run_with_time_limit(multiple_runs, input_ranges={}, time_limit=0.9)

# Preprocess the DataFrame to extract numeric budget values
results_csv_file = "./experiments/spreading_minbudget.csv"
results_csv_file = "./experiments/firefighter_problem/spreading_minbudget.csv"
results = pd.read_csv(results_csv_file)

# Extract the numeric budget from the 'Budget' column
Expand All @@ -369,7 +369,7 @@ def extract_budget_numeric(budget):
results = results.dropna(subset=['Budget_numeric'])

# Save the preprocessed DataFrame to a temporary CSV file
preprocessed_csv_file = "./experiments/spreading_minbudget_preprocessed.csv"
preprocessed_csv_file = "./experiments/firefighter_problem/spreading_minbudget_preprocessed.csv"
results.to_csv(preprocessed_csv_file, index=False)

print("\n DataFrame-NonSpread: \n", results)
Expand All @@ -382,17 +382,22 @@ def extract_budget_numeric(budget):
y_field="Budget_numeric",
z_field="algorithm",
mean=True,
save_to_file="./experiments/spreading_minbudget.png"
save_to_file="./experiments/firefighter_problem/spreading_minbudget.png"
)

single_plot_results(
multi_plot_results(
results_csv_file=preprocessed_csv_file,
filter={"edge_probability":0.1},
x_field="graph_nodes",
filter={},
subplot_rows=3,
subplot_cols=1,
x_field="edge_probability",
y_field="Budget_numeric",
z_field="algorithm",
subplot_field="graph_nodes",
sharex=True,
sharey=True,
mean=True,
save_to_file="./experiments/spreading_minbudget_edge.png"
save_to_file="./experiments/firefighter_problem/spreading_minbudget_edge_prob.png"
)

print("\n DataFrame-NonSpread: \n", ex3.dataFrame)
Expand Down
Loading

0 comments on commit 53681be

Please sign in to comment.