Skip to content

Commit

Permalink
Remove use of non-sparse matrix for dijkstra (using csr_array)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinefricou committed Jun 12, 2024
1 parent dddedc3 commit 337eaad
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions geotrek/core/path_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
from scipy.sparse import csr_array

from geotrek.common.utils import sqlfunction
from .models import Path
Expand Down Expand Up @@ -111,7 +111,7 @@ def set_cs_graph(self):
return

nb_of_nodes = len(self.nodes)
self.dijk_matrix = np.zeros((nb_of_nodes, nb_of_nodes))
self.dijk_matrix = csr_array((nb_of_nodes, nb_of_nodes), dtype=np.float32)

nodes_list = list(self.nodes.items())
for i, (key1, value1) in enumerate(nodes_list[:-1]):
Expand All @@ -138,8 +138,8 @@ def set_edge_weight(self, node1, node2_key, row_idx, col_idx):
# if not, the weight stays at 0
edge_weight = self.get_edge_weight(edge_id)
if edge_weight is not None:
self.dijk_matrix[row_idx][col_idx] = edge_weight
self.dijk_matrix[col_idx][row_idx] = edge_weight
self.dijk_matrix[row_idx, col_idx] = edge_weight
self.dijk_matrix[col_idx, row_idx] = edge_weight

def get_edge_weight(self, edge_id):
edge = self.edges.get(edge_id)
Expand Down Expand Up @@ -368,13 +368,9 @@ def split_edge_in_three(self, from_point, to_point):
return new_node_info_1, new_node_info_2

def add_steps_to_matrix(self, from_node_info, to_node_info):
length = len(self.dijk_matrix)
# Add the last two rows
new_rows = [np.zeros(length) for i in range(2)]
self.dijk_matrix = np.vstack((self.dijk_matrix, new_rows))
# add the last two columns
new_columns = np.zeros((length + 2, 2))
self.dijk_matrix = np.hstack((self.dijk_matrix, new_columns))
# Add two rows and two columns
length = self.dijk_matrix.get_shape()[0]
self.dijk_matrix.resize((length + 2, length + 2))

# Add the weights
from_node_key = from_node_info['node_id']
Expand All @@ -386,15 +382,10 @@ def add_steps_to_matrix(self, from_node_info, to_node_info):
self.set_edge_weight(node1, node2_key, row_idx, col_idx)

def remove_steps_from_matrix(self):
length = len(self.dijk_matrix)
# Remove the last two rows
self.dijk_matrix = np.delete(self.dijk_matrix, [length - 1, length - 2], 0)
# Remove the last two columns
self.dijk_matrix = np.delete(self.dijk_matrix, [length - 1, length - 2], 1)
length = self.dijk_matrix.get_shape()[0]
self.dijk_matrix.resize((length - 2, length - 2))

def get_shortest_path(self, from_node_id, to_node_id):
matrix = csr_matrix(self.dijk_matrix)

# List of all nodes IDs -> to interprete dijkstra results
self.nodes_ids = list(self.nodes.keys())

Expand All @@ -411,7 +402,7 @@ def get_node_id_per_idx(node_idx):

from_node_idx = get_node_idx_per_id(from_node_id)
to_node_idx = get_node_idx_per_id(to_node_id)
result = dijkstra(matrix, return_predecessors=True, indices=from_node_idx,
result = dijkstra(self.dijk_matrix, return_predecessors=True, indices=from_node_idx,
directed=False)

# Retrace the path ID by ID, from end to start
Expand Down

0 comments on commit 337eaad

Please sign in to comment.