Skip to content

Commit

Permalink
run doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Jun 13, 2024
1 parent ff99b20 commit eaa155a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,8 @@
import networkx.algorithms.connectivity as algo
import math
import logging
try:
from networkz.algorithms.approximation.firefighter_problem.graph_flow_reduction import graph_flow_reduction
except:
from graph_flow_reduction import *

# 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
try:
from networkz.algorithms.approximation.firefighter_problem.Utils import *
except ImportError:
from Utils import *
from networkz.algorithms.approximation.firefighter_problem.graph_flow_reduction import max_flow_with_node_capacity
from networkz.algorithms.approximation.firefighter_problem.Utils import *

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -198,7 +189,7 @@ def spreading_minbudget(Graph:nx.DiGraph, source:int, targets:list)-> int:
max_value = middle
best_strategy = strategy
else:
logger.warning(f"The current budget {middle} didn't save all the targets!")
logger.info(f"The current budget {middle} didn't save all the targets!")
min_value = middle + 1

middle = math.floor((min_value + max_value) / 2)
Expand Down Expand Up @@ -285,7 +276,7 @@ def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, src:int, targets:list)->
layers = adjust_nodes_capacity(Graph, src)
G = create_st_graph(Graph, targets)
display_graph(G)
G_reduction = graph_flow_reduction(G, source=src, target='t')
G_reduction = max_flow_with_node_capacity(G, source=src, target='t')
N_groups = min_cut_N_groups(G_reduction, src,layers)
vacc_matrix = calculate_vaccine_matrix(layers, N_groups)
min_budget = min_budget_calculation(vacc_matrix)
Expand Down Expand Up @@ -412,7 +403,7 @@ def heuristic_minbudget(Graph:nx.DiGraph, source:int, targets:list, spreading:bo
max_value = middle
best_strategy = strategy
else:
logger.warning(f"The current budget {middle} didn't save all the targets!")
logger.info(f"The current budget {middle} didn't save all the targets!")
min_value = middle + 1

middle = math.floor((min_value + max_value) / 2)
Expand All @@ -423,6 +414,6 @@ def heuristic_minbudget(Graph:nx.DiGraph, source:int, targets:list, spreading:bo

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

logger = logging.getLogger(__name__)

def graph_flow_reduction(graph: nx.DiGraph, source: int = None, target: int = None) -> nx.DiGraph:
def max_flow_with_node_capacity(graph: nx.DiGraph, source: int = None, target: int = None) -> nx.DiGraph:
"""
This function transforms a given directed graph into a new graph where each node
is split into two nodes (an "in" node and an "out" node) connected by an edge
with a capacity (weight). The transformation is used for flow problems where
nodes have capacities instead of edges - allowing to run algorithms which
were originally designed to be used for edge-flow problems.
Computes a maximum flow in the given graph, where each node has a capacity
Parameters:
----------
Expand All @@ -30,6 +26,11 @@ def graph_flow_reduction(graph: nx.DiGraph, source: int = None, target: int = No
Notes:
-----
This function transforms a given directed graph into a new graph where each node
is split into two nodes (an "in" node and an "out" node) connected by an edge
with a capacity (weight). The transformation is used for flow problems where
nodes have capacities instead of edges - allowing to run algorithms which
were originally designed to be used for edge-flow problems.
- If a node does not have a 'capacity' attribute, a default capacity of 1
is used.
- There is infinite capacity between two different edge_out & edge_in
Expand All @@ -43,12 +44,12 @@ def graph_flow_reduction(graph: nx.DiGraph, source: int = None, target: int = No
>>> G.add_edge(1, 2)
>>> G.add_edge(2, 3)
>>> G.add_edge(1, 3)
>>> H = graph_flow_reduction(G, 1, 3)
>>> H = max_flow_with_node_capacity(G, 1, 3)
>>> sorted(list(H.nodes))
['1_in', '1_out', '2_in', '2_out', '3_in', '3_out']
>>> sorted(list(H.edges(data=True)))
[('1_in', '1_out', {'weight': inf}), ('1_out', '2_in', {'weight': inf}), ('1_out', '3_in', {'weight': inf}), ('2_in', '2_out', {'weight': 15}), ('2_out', '3_in', {'weight': inf}), ('3_in', '3_out', {'weight': inf})]
>>> H = graph_flow_reduction(G)
>>> H = max_flow_with_node_capacity(G)
>>> sorted(list(H.nodes))
['1_in', '1_out', '2_in', '2_out', '3_in', '3_out']
>>> sorted(list(H.edges(data=True)))
Expand Down Expand Up @@ -83,5 +84,5 @@ def graph_flow_reduction(graph: nx.DiGraph, source: int = None, target: int = No

if __name__ == "__main__":
import doctest
result = doctest.testmod(verbose=True)
logger.info(f"Doctest results: {result}")
result = doctest.testmod(verbose=False)
print(f"Doctest results: {result}")
9 changes: 3 additions & 6 deletions networkz/algorithms/approximation/firefighter_problem/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@

# 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
try:
from networkz.algorithms.approximation.firefighter_problem.Utils import *
except ImportError:
from Utils import *
from Firefighter_Problem import *
import Firefighter_Problem as firefighter_problem # to run the doctest on the firefighter_problem files
from networkz.algorithms.approximation.firefighter_problem.Utils import *
from networkz.algorithms.approximation.firefighter_problem.Firefighter_Problem import *
import networkz.algorithms.approximation.firefighter_problem.Firefighter_Problem as firefighter_problem # to run the doctest on the firefighter_problem files

def setup_global_logger(level: int = logging.DEBUG):
log_format = "|| %(asctime)s || %(levelname)s || %(message)s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from networkz.algorithms.approximation.firefighter_problem.Utils import adjust_nodes_capacity
from networkz.algorithms.approximation.firefighter_problem.Utils import create_st_graph
from networkz.algorithms.approximation.firefighter_problem.Utils import parse_json_to_networkx
from networkz.algorithms.approximation.firefighter_problem.graph_flow_reduction import graph_flow_reduction
from networkz.algorithms.approximation.firefighter_problem.graph_flow_reduction import max_flow_with_node_capacity
from networkz.algorithms.approximation.firefighter_problem.Utils import calculate_vaccine_matrix
from networkz.algorithms.approximation.firefighter_problem.Utils import min_cut_N_groups
from networkz.algorithms.approximation.firefighter_problem.Utils import matrix_to_integers_values
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_source_is_target(graph_key, source, targets):
layers_1 = adjust_nodes_capacity(graph_1, 0) # src is 0
targets_1 = [1, 2, 3] # saving 1,2,3
G1 = create_st_graph(graph_1, targets_1)
reduction_G1 = graph_flow_reduction(G1, 0)
reduction_G1 = max_flow_with_node_capacity(G1, 0)
N_1_groups = min_cut_N_groups(reduction_G1, 0,layers_1)
matrix_1 = calculate_vaccine_matrix(layers_1, N_1_groups)
integer_matrix_1 = matrix_to_integers_values(matrix_1)
Expand All @@ -147,7 +147,7 @@ def test_source_is_target(graph_key, source, targets):
layers_2 = adjust_nodes_capacity(graph_2, 0) # src is 0
targets_2 = [2, 4] # saving 2,4
G2 = create_st_graph(graph_2, targets_2)
reduction_G2 = graph_flow_reduction(G2, 0)
reduction_G2 = max_flow_with_node_capacity(G2, 0)
N_2_groups = min_cut_N_groups(reduction_G2, 0, layers_2)
matrix_2 = calculate_vaccine_matrix(layers_2, N_2_groups)
integer_matrix_2 = matrix_to_integers_values(matrix_2)
Expand All @@ -159,7 +159,7 @@ def test_source_is_target(graph_key, source, targets):
layers_3 = adjust_nodes_capacity(graph_3, 0) # src is 0
targets_3 = [1, 5, 7] # saving 1,5,7
G3 = create_st_graph(graph_3, targets_3)
reduction_G3 = graph_flow_reduction(G3, 0)
reduction_G3 = max_flow_with_node_capacity(G3, 0)
N_3_groups = min_cut_N_groups(reduction_G3, 0, layers_3)
matrix_3 = calculate_vaccine_matrix(layers_3, N_3_groups)
integer_matrix_3 = matrix_to_integers_values(matrix_3)
Expand All @@ -171,7 +171,7 @@ def test_source_is_target(graph_key, source, targets):
layers_4 = adjust_nodes_capacity(graph_4, 0) # src is 0
targets_4 = [4, 5, 6, 8] # saving 4,5,6,8
G4 = create_st_graph(graph_4, targets_4)
reduction_G4 = graph_flow_reduction(G4, 0)
reduction_G4 = max_flow_with_node_capacity(G4, 0)
N_4_groups = min_cut_N_groups(reduction_G4, 0, layers_4)
matrix_4 = calculate_vaccine_matrix(layers_4, N_4_groups)
integer_matrix_4 = matrix_to_integers_values(matrix_4)
Expand Down

0 comments on commit eaa155a

Please sign in to comment.