Skip to content

Commit cd53ce3

Browse files
authored
Merge pull request ariel-research#18 from The-Firefighters/shaked/Comparisons
Shaked/comparisons
2 parents 040353a + 8fc3d61 commit cd53ce3

7 files changed

Lines changed: 134 additions & 9 deletions

File tree

networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def non_spreading_minbudget(Graph:nx.DiGraph, source:int, targets:list)->int:
246246
logger.info(f"Returning minimum budget: {min_budget}")
247247
return min_budget
248248

249-
def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, src:int, targets:list)->int:
249+
def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, source:int, targets:list)->int:
250250
"""
251251
"Approximability of the Firefighter Problem - Computing Cuts over Time",
252252
by Elliot Anshelevich, Deeparnab Chakrabarty, Ameya Hate, Chaitanya Swamy (2010)
@@ -264,18 +264,18 @@ def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, src:int, targets:list)->
264264
>>> non_spreading_dirlaynet_minbudget(G4,0,[1,2,3,4,5])
265265
2
266266
"""
267-
validate_parameters(Graph, src, targets)
267+
validate_parameters(Graph, source, targets)
268268
if not is_dag(Graph):
269269
logger.error("Problem with graph, its not a DAG, thus cannot run algorithm")
270270
return
271271

272272
#display_graph(Graph)
273-
logger.info(f"Starting the non_spreading_dirlaynet_minbudget function with source node {src} and targets: {targets}")
273+
logger.info(f"Starting the non_spreading_dirlaynet_minbudget function with source node {source} and targets: {targets}")
274274

275-
layers = adjust_nodes_capacity(Graph, src)
275+
layers = adjust_nodes_capacity(Graph, source)
276276
G = create_st_graph(Graph, targets)
277277
#display_graph(G)
278-
G_reduction_min_cut = min_cut_with_node_capacity(G, source=src, target='t')
278+
G_reduction_min_cut = min_cut_with_node_capacity(G, source=source, target='t')
279279
N_groups = min_cut_N_groups(G_reduction_min_cut,layers)
280280
vacc_matrix = calculate_vaccine_matrix(layers, N_groups)
281281
integer_matrix = matrix_to_integers_values(vacc_matrix)

networkz/algorithms/approximation/firefighter_problem/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# and the bottom one needs to be exclusive for running this from terminal to work
3131
from networkz.algorithms.approximation.firefighter_problem.Utils import *
3232
from networkz.algorithms.approximation.firefighter_problem.Firefighter_Problem import *
33-
from networkz.algorithms.tests.test_firefighter_problem.test_non_spreading_dirlaynet_minbudget import generate_layered_network
33+
from networkz.algorithms.approximation.tests.test_firefighter_problem.test_non_spreading_dirlaynet_minbudget import generate_layered_network
3434
import networkz.algorithms.approximation.firefighter_problem.Firefighter_Problem as firefighter_problem # to run the doctest on the firefighter_problem files
3535

3636
def setup_global_logger(level: int = logging.DEBUG):
Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,120 @@
1-
import expirements_csv
2-
# add the comparisons test from both heuristic files + add a function that will plot the graphs.
3-
# TBD
1+
"""
42
3+
The Paper -
4+
Approximability of the Firefighter Problem Computing Cuts over Time
5+
6+
Paper Link -
7+
https://www.math.uwaterloo.ca/~cswamy/papers/firefighter-journ.pdf
8+
9+
Authors -
10+
Elliot Anshelevich
11+
Deeparnab Chakrabarty
12+
Ameya Hate
13+
Chaitanya Swamy
14+
15+
Developers -
16+
Yuval Bubnovsky
17+
Almog David
18+
Shaked Levi
19+
20+
"""
21+
22+
import experiments_csv
23+
from experiments_csv import *
24+
import logging
25+
logger = logging.getLogger(__name__)
26+
27+
28+
from networkz.algorithms.approximation.firefighter_problem.Utils import *
29+
from networkz.algorithms.approximation.firefighter_problem.Firefighter_Problem import *
30+
from networkz.algorithms.approximation.tests.test_firefighter_problem.test_non_spreading_dirlaynet_minbudget import generate_layered_network
31+
from matplotlib import pyplot as plt
32+
33+
def setup_global_logger(level: int = logging.DEBUG):
34+
log_format = "|| %(asctime)s || %(levelname)s || %(message)s"
35+
date_format = '%H:%M:%S'
36+
formatter = logging.Formatter(log_format, datefmt=date_format)
37+
handler = logging.StreamHandler()
38+
handler.setFormatter(formatter)
39+
40+
root_logger = logging.getLogger()
41+
root_logger.setLevel(level)
42+
root_logger.addHandler(handler)
43+
44+
G_dirlay_random = generate_layered_network()
45+
46+
def runner_no_spreading(algorithm):
47+
graph = G_dirlay_random.copy()
48+
source = 0
49+
targets = [2,4,6,7,8,9,20,15]
50+
51+
if algorithm == heuristic_minbudget:
52+
return {"Budget" : (algorithm(Graph=graph,source=source,targets=targets,spreading=False))}
53+
54+
else:
55+
return {"Budget" : (algorithm(Graph=graph,source=source,targets=targets))}
56+
57+
58+
def runner_spreading(algorithm):
59+
graph = G_dirlay_random.copy()
60+
source = 0
61+
targets = [2,4,6,7,8,9,20,15]
62+
63+
if algorithm == heuristic_minbudget:
64+
return {"Budget" : (algorithm(Graph=graph,source=source,targets=targets,spreading=True))}
65+
66+
if algorithm == heuristic_maxsave:
67+
return {"Budget" : (algorithm(Graph=graph,budget = 1, source=source,targets=targets,spreading=True))}
68+
69+
if algorithm == spreading_maxsave:
70+
return {"Budget" : (algorithm(Graph=graph,budget = 1, source=source,targets=targets))}
71+
72+
else:
73+
return {"Budget" : (algorithm(Graph=graph,source=source,targets=targets))}
74+
75+
76+
if __name__ == "__main__":
77+
setup_global_logger(level=logging.DEBUG)
78+
79+
80+
G_dirlay_random = generate_layered_network() #random graph generator for dirlay testings/ can also fit other algorithms but dirlay
81+
82+
ex1 = experiments_csv.Experiment("./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/", "non_spreading.csv", backup_folder=None)
83+
ex1.clear_previous_results() # to clear previous experminets..
84+
85+
input_ranges = {
86+
"algorithm":[non_spreading_dirlaynet_minbudget,non_spreading_minbudget,heuristic_minbudget],
87+
}
88+
ex1.run_with_time_limit(runner_no_spreading,input_ranges, time_limit=0.9)
89+
90+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
91+
92+
ex2 = experiments_csv.Experiment("./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/", "spreading.csv", backup_folder=None)
93+
ex2.clear_previous_results() # to clear previous experminets..
94+
95+
input_ranges = {
96+
"algorithm":[spreading_minbudget,spreading_maxsave,heuristic_minbudget,heuristic_maxsave],
97+
}
98+
ex2.run_with_time_limit(runner_spreading,input_ranges, time_limit=0.9)
99+
100+
101+
#Plotting:
102+
103+
single_plot_results("./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/non_spreading.csv",
104+
filter = {},
105+
x_field="algorithm",
106+
y_field="runtime",
107+
z_field="Budget",
108+
save_to_file="./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/non_spreading.png")
109+
110+
print("\n DataFrame-NonSpread: \n", ex1.dataFrame)
111+
112+
113+
single_plot_results("./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/spreading.csv",
114+
filter = {},
115+
x_field="algorithm",
116+
y_field="runtime",
117+
z_field="Budget",
118+
save_to_file="./networkz/algorithms/approximation/tests/test_firefighter_problem/comparisons/spreading.png")
119+
120+
print("\n DataFrame-Spreading: \n", ex2.dataFrame)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
algorithm,Budget,runtime
2+
non_spreading_dirlaynet_minbudget,7,0.037856300001294585
3+
non_spreading_minbudget,8,0.0023252999999385793
4+
heuristic_minbudget,"(8, [])",0.08596559999932651
18 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
algorithm,Budget,runtime
2+
spreading_minbudget,"(5, [(2, 1), (4, 1), (6, 1), (7, 1), (8, 1)])",0.06528040000193869
3+
spreading_maxsave,"([(2, 1), (10, 2), (19, 3)], {9, 2, 20, 15})",0.012378399998851819
4+
heuristic_minbudget,"(8, [])",0.03716120000171941
5+
heuristic_maxsave,"([(2, 1), (10, 2)], {9, 2, 20, 15})",0.0065484000006108545
30.8 KB
Loading

0 commit comments

Comments
 (0)