Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Jun 13, 2024
1 parent eaa155a commit 0eb5688
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,14 @@ def heuristic_maxsave(Graph:nx.DiGraph, budget:int, source:int, targets:list, sp
logger.info(f"Starting the heuristic_maxsave function with source node {source}, budget {budget}, targets: {targets}, and spreading: {spreading}")

clean_graph(Graph)
display_graph(Graph)
infected_nodes = []
vaccinated_nodes = []
vaccination_strategy = []
can_spread = True
Graph.nodes[source]['status'] = Status.INFECTED.value
infected_nodes.append(source)
display_graph(Graph)
time_step = 1

while can_spread:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def spread_virus(graph:nx.DiGraph, infected_nodes:list)->bool:
graph.nodes[neighbor]['status'] = Status.INFECTED.value
new_infected_nodes.append(neighbor)
logger.debug("SPREAD VIRUS: Node " + f'{neighbor}' + " has been infected from node " + f'{node}')
display_graph(graph)
infected_nodes.clear()
for node in new_infected_nodes:
infected_nodes.append(node)
Expand Down Expand Up @@ -375,6 +376,7 @@ def spread_vaccination(graph:nx.DiGraph, vaccinated_nodes:list)->None:
graph.nodes[neighbor]['status'] = Status.VACCINATED.value
new_vaccinated_nodes.append(neighbor)
logger.debug("SPREAD VACCINATION: Node " + f'{neighbor}' + " has been vaccinated from node " + f'{node}')
display_graph(graph)
vaccinated_nodes.clear()
for node in new_vaccinated_nodes:
vaccinated_nodes.append(node)
Expand All @@ -401,6 +403,7 @@ def vaccinate_node(graph:nx.DiGraph, node:int)->None:
"""
graph.nodes[node]['status'] = Status.DIRECTLY_VACCINATED.value
logger.info("Node " + f'{node}' + " has been directly vaccinated")
display_graph(graph)
return

def clean_graph(graph:nx.DiGraph)->None:
Expand Down Expand Up @@ -577,7 +580,7 @@ def calculate_vaccine_matrix(layers:list, min_cut_nodes_grouped:dict)->np.matrix
value = N_j / (j + 1)
matrix[i][j] = value

logger.info(f"Vaccination Matrix Before Conversion: {matrix}")
logger.info(f"Vaccination Matrix Before Conversion:\n{matrix}")
return matrix

def matrix_to_integers_values(matrix: np.matrix) -> np.matrix:
Expand Down Expand Up @@ -649,7 +652,7 @@ def matrix_to_integers_values(matrix: np.matrix) -> np.matrix:
else:
integral_matrix[i, j] = np.floor(matrix[i, j])

logger.info(f"Integral and final Matrix: {integral_matrix}")
logger.info(f"Integral and final Matrix:\n{integral_matrix}")
return np.matrix(integral_matrix)


Expand Down
37 changes: 23 additions & 14 deletions networkz/algorithms/approximation/firefighter_problem/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"""

import logging
import json
logger = logging.getLogger(__name__)


# This is a fix for an issue where the top one has to be exclusive for pytest to work
# and the bottom one needs to be exclusive for running this from terminal to work
Expand All @@ -38,22 +41,28 @@ def setup_global_logger(level: int = logging.DEBUG):
root_logger.setLevel(level)
root_logger.addHandler(handler)

setup_global_logger()

logger = logging.getLogger(__name__)


if __name__ == "__main__":
import doctest
result = doctest.testmod(firefighter_problem, verbose=True)
logger.info(f"Doctest results: {result}")

#G3 = nx.DiGraph()
#G3.add_nodes_from([0,1,2,3,4,5,6,7,8], status="vulnerable")
#G3.add_edges_from([(0,2),(0,4),(0,5),(2,1),(2,3),(4,1),(4,6),(5,3),(5,6),(5,7),(6,7),(6,8),(7,8)])
#logger.info("=" * 150)
#logger.info(spreading_minbudget(G3,0,[2,6,1,8]))
#print(spreading_maxsave(G3,1, 0,[2,6,1,8])[1])
setup_global_logger(level=logging.DEBUG)

# result = doctest.testmod(firefighter_problem, verbose=True)
# logger.info(f"Doctest results: {result}")

G3 = nx.DiGraph()
G3.add_nodes_from([0,1,2,3,4,5,6,7,8])
G3.add_edges_from([(0,2),(0,4),(0,5),(2,1),(2,3),(4,1),(4,6),(5,3),(5,6),(5,7),(6,7),(6,8),(7,8)])
logger.info("=" * 150)
#print(spreading_maxsave(G3,source=0,targets=[2,6,1,8],budget=1))
#print(spreading_minbudget(G3,source=0,targets=[2,6,1,8]))

# with open("networkz/algorithms/tests/test_firefighter_problem/graphs.json", "r") as file:
# json_data = json.load(file)
# graphs = parse_json_to_networkx(json_data)

# G2 = graphs["Dirlay_Graph-2"]
# print(non_spreading_dirlaynet_minbudget(G2,src=0,targets=[2,4]))
print(spreading_maxsave(G3,1, 0,[2,6,1,8])[1])
# logger.info("=" * 150)
# logger.info(heuristic_minbudget(G3,0,[2,6,1,8], True))
#logger.info(heuristic_minbudget(G3,0,[2,6,1,8], True))

0 comments on commit 0eb5688

Please sign in to comment.