Skip to content

Commit 181ddf2

Browse files
ordering
1 parent 08d9d37 commit 181ddf2

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

aco_routing/aco.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@ def __post_init__(self):
3232
for edge in self.graph.edges:
3333
self.graph_api.set_edge_pheromones(edge[0], edge[1], 1.0)
3434

35+
def find_shortest_path(
36+
self,
37+
source: str,
38+
destination: str,
39+
num_ants: int,
40+
) -> Tuple[List[str], float]:
41+
"""Finds the shortest path from the source to the destination in the graph
42+
43+
Args:
44+
source (str): The source node in the graph
45+
destination (str): The destination node in the graph
46+
num_ants (int): The number of search ants to be deployed
47+
48+
Returns:
49+
List[str]: The shortest path found by the ants
50+
float: The cost of the computed shortest path
51+
"""
52+
self._deploy_search_ants(
53+
source,
54+
destination,
55+
num_ants,
56+
)
57+
solution_ant = self._deploy_solution_ant(source, destination)
58+
return solution_ant.path, solution_ant.path_cost
59+
3560
def _deploy_forward_search_ants(self) -> None:
3661
"""Deploy forward search ants in the graph"""
3762
for ant in self.search_ants:
@@ -101,28 +126,3 @@ def _deploy_solution_ant(self, source: str, destination: str) -> Ant:
101126
while not ant.reached_destination():
102127
ant.take_step()
103128
return ant
104-
105-
def find_shortest_path(
106-
self,
107-
source: str,
108-
destination: str,
109-
num_ants: int,
110-
) -> Tuple[List[str], float]:
111-
"""Finds the shortest path from the source to the destination in the graph
112-
113-
Args:
114-
source (str): The source node in the graph
115-
destination (str): The destination node in the graph
116-
num_ants (int): The number of search ants to be deployed
117-
118-
Returns:
119-
List[str]: The shortest path found by the ants
120-
float: The cost of the computed shortest path
121-
"""
122-
self._deploy_search_ants(
123-
source,
124-
destination,
125-
num_ants,
126-
)
127-
solution_ant = self._deploy_solution_ant(source, destination)
128-
return solution_ant.path, solution_ant.path_cost

aco_routing/ant.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ def reached_destination(self) -> bool:
3838
"""
3939
return self.current_node == self.destination
4040

41+
def take_step(self) -> None:
42+
"""Compute and update the ant position"""
43+
# Mark the current node as visited
44+
self.visited_nodes.add(self.current_node)
45+
46+
# Pick the next node of the ant
47+
next_node = self._choose_next_node()
48+
49+
# Check if ant is stuck at current node
50+
if not next_node:
51+
# TODO: optimization: set ant as unfit
52+
return
53+
54+
self.path.append(next_node)
55+
self.path_cost += self.graph_api.get_edge_cost(self.current_node, next_node)
56+
self.current_node = next_node
57+
58+
def deposit_pheromones_on_path(self) -> None:
59+
"""Updates the pheromones along all the edges in the path"""
60+
for i in range(len(self.path) - 1):
61+
u, v = self.path[i], self.path[i + 1]
62+
new_pheromone_value = 1 / self.path_cost
63+
self.graph_api.deposit_pheromones(u, v, new_pheromone_value)
64+
4165
def _get_unvisited_neighbors(self) -> List[str]:
4266
"""Returns a subset of the neighbors of the node which are unvisited
4367
@@ -134,27 +158,3 @@ def _choose_next_node(self) -> Union[str, None]:
134158

135159
# Pick the next node based on the roulette wheel selection technique
136160
return utils.roulette_wheel_selection(probabilities)
137-
138-
def take_step(self) -> None:
139-
"""Compute and update the ant position"""
140-
# Mark the current node as visited
141-
self.visited_nodes.add(self.current_node)
142-
143-
# Pick the next node of the ant
144-
next_node = self._choose_next_node()
145-
146-
# Check if ant is stuck at current node
147-
if not next_node:
148-
# TODO: optimization: set ant as unfit
149-
return
150-
151-
self.path.append(next_node)
152-
self.path_cost += self.graph_api.get_edge_cost(self.current_node, next_node)
153-
self.current_node = next_node
154-
155-
def deposit_pheromones_on_path(self) -> None:
156-
"""Updates the pheromones along all the edges in the path"""
157-
for i in range(len(self.path) - 1):
158-
u, v = self.path[i], self.path[i + 1]
159-
new_pheromone_value = 1 / self.path_cost
160-
self.graph_api.deposit_pheromones(u, v, new_pheromone_value)

0 commit comments

Comments
 (0)