Skip to content

Commit

Permalink
dirlay returns a list instead of dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Almog-David committed Aug 2, 2024
1 parent b4a3202 commit c2077cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, source:int, targets:list
>>> G4.add_nodes_from([0,1,2,3,4,5], status="vulnerable")
>>> G4.add_edges_from([(0,1),(0,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,5),(4,5)])
>>> non_spreading_dirlaynet_minbudget(G4,0,[1,2,3,4,5])
(2, {0: [2, 1]})
(2, [(1, 1), (2, 1)])
"""

validate_parameters(Graph, source, targets)
Expand All @@ -349,7 +349,7 @@ def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, source:int, targets:list
vacc_matrix = calculate_vaccine_matrix(layers, N_groups)
integer_matrix = matrix_to_integers_values(vacc_matrix)
min_budget = min_budget_calculation(vacc_matrix)
strategy = dirlay_vaccination_startegy(integer_matrix, N_groups)
strategy = dirlay_vaccination_strategy(integer_matrix, N_groups)

logger.info(f"Returning minimum budget: {min_budget} and the vaccination strategy: {strategy}")
return min_budget, strategy
Expand Down
25 changes: 12 additions & 13 deletions networkz/algorithms/approximation/firefighter_problem/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def min_budget_calculation(matrix: np.matrix) -> int:
logger.info(f"Min budget needed to save the target nodes: {min_budget}")
return min_budget

def dirlay_vaccination_startegy(vacc_matrix: np.matrix, ni_groups: dict) -> dict:
def dirlay_vaccination_strategy(vacc_matrix: np.matrix, ni_groups: dict) -> list:
"""
Determines a feasible vaccination strategy given the vaccine matrix and the nodes from each layer.
Expand All @@ -728,31 +728,30 @@ def dirlay_vaccination_startegy(vacc_matrix: np.matrix, ni_groups: dict) -> dict
Returns:
----------
strategy : dict
A dictionary where keys are time steps and values are lists of nodes to vaccinate at each time step.
strategy : list
A list of tuples where each tuple represents a node to vaccinate and the corresponding time step index.
"""
logger.info("Calculating the stategy")
logger.info("Calculating the strategy")

num_steps, num_layers = vacc_matrix.shape
strategy = {}
strategy = []
for i in range(num_steps):
nodes_to_vaccinate = []
for j in range(0, num_layers):
for j in range(num_layers):
num_nodes_to_vaccinate = int(vacc_matrix[i, j])

logger.debug(f"On time step {i} needs to vaccinate: {num_nodes_to_vaccinate} nodes" )
logger.debug(f"On time step {i+1} needs to vaccinate: {num_nodes_to_vaccinate} nodes")

if num_nodes_to_vaccinate > 0:
# Extract the nodes to vaccinate
selected_nodes = ni_groups[j+1][:num_nodes_to_vaccinate]

logger.debug(f"The selected nodes to vaccinate {selected_nodes}")

nodes_to_vaccinate.extend(selected_nodes)

if nodes_to_vaccinate:
strategy[i] = nodes_to_vaccinate

# Create tuples (node, i) and add them directly to strategy
strategy.extend((node, i+1) for node in selected_nodes)

# sort the strategy by time stamp.
strategy.sort(key=lambda x: (x[1], x[0]))
return strategy

# =========================== End Non-Spreading Max-Save ============================
Expand Down

0 comments on commit c2077cf

Please sign in to comment.