From 0eb5688a3b60a1b01619efe83c239e538ee4268c Mon Sep 17 00:00:00 2001 From: Erel Segal-Halevi Date: Thu, 13 Jun 2024 15:31:09 +0300 Subject: [PATCH] logs --- .../Firefighter_Problem.py | 2 + .../firefighter_problem/Utils.py | 7 +++- .../approximation/firefighter_problem/main.py | 37 ++++++++++++------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py b/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py index 3f573d6..c78f5c2 100644 --- a/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py +++ b/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py @@ -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: diff --git a/networkz/algorithms/approximation/firefighter_problem/Utils.py b/networkz/algorithms/approximation/firefighter_problem/Utils.py index 1d7746e..d3f1139 100644 --- a/networkz/algorithms/approximation/firefighter_problem/Utils.py +++ b/networkz/algorithms/approximation/firefighter_problem/Utils.py @@ -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) @@ -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) @@ -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: @@ -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: @@ -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) diff --git a/networkz/algorithms/approximation/firefighter_problem/main.py b/networkz/algorithms/approximation/firefighter_problem/main.py index e5c8fe2..9ec6877 100644 --- a/networkz/algorithms/approximation/firefighter_problem/main.py +++ b/networkz/algorithms/approximation/firefighter_problem/main.py @@ -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 @@ -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))