22import random
33from typing import List , Tuple
44
5- from aco_routing .utils . graph import Graph
6- from aco_routing .utils . ant import Ant
5+ from aco_routing .graph import Graph
6+ from aco_routing .ant import Ant
77
88
99@dataclass
@@ -15,11 +15,11 @@ def _forward_ants(self, ants: List[Ant], max_iterations: int) -> None:
1515
1616 Args:
1717 ants (List[Ant]): A List of Ants.
18- max_iterations (int, optional ): The maximum number of steps an ant is allowed is to take in order to reach the destination.
19- If it fails to find a path, it is tagged as unfit. Defaults to 50.
18+ max_iterations (int): The maximum number of steps an ant is allowed is to take in order to reach the destination.
19+ If it fails to find a path, it is tagged as unfit.
2020 """
21- for idx , ant in enumerate (ants ):
22- for i in range (max_iterations ):
21+ for _ , ant in enumerate (ants ):
22+ for _ in range (max_iterations ):
2323 if ant .reached_destination ():
2424 ant .is_fit = True
2525 break
@@ -31,7 +31,7 @@ def _backward_ants(self, ants: List[Ant]) -> None:
3131 Args:
3232 ants (List[Ant]): A List of Ants.
3333 """
34- for idx , ant in enumerate (ants ):
34+ for _ , ant in enumerate (ants ):
3535 if ant .is_fit :
3636 self .graph .deposit_pheromones_along_path (ant .path )
3737
@@ -40,8 +40,8 @@ def _deploy_search_ants(
4040 source : str ,
4141 destination : str ,
4242 num_ants : int ,
43- random_spawns : bool = False ,
44- cycles : int = 100 ,
43+ cycles : int ,
44+ random_spawns : bool ,
4545 max_iterations : int = 50 ,
4646 ) -> None :
4747 """Deploys search ants which traverse the graph to find the shortest path.
@@ -50,12 +50,12 @@ def _deploy_search_ants(
5050 source (str): The source node in the graph.
5151 destination (str): The destination node in the graph.
5252 num_ants (int): The number of ants to be spawned.
53- random_spawns (bool ): A flag to determine if the ants should be spawned at random nodes or always at the source node .
54- cycles (int, optional ): The number of cycles of generating and deploying ants (forward and backward). Defaults to 100 .
55- max_iterations (int, optional): The maximum number of steps an ant is allowed is to take in order to reach the destination.
56- If it fails to find a path, it is tagged as unfit. Defaults to 50.
53+ cycles (int ): The number of cycles of generating and deploying ants (forward and backward) .
54+ random_spawns (bool ): Indicates if the search ants should spawn at random nodes in the graph .
55+ max_iterations (int, optional): The maximum number of steps an ant is allowed is to take in order to reach the destination,
56+ after which it is tagged as unfit. Defaults to 50.
5757 """
58- for cycle in range (cycles ):
58+ for _ in range (cycles ):
5959 ants : List [Ant ] = []
6060 for _ in range (num_ants ):
6161 spawn_point = (
@@ -79,24 +79,42 @@ def _deploy_solution_ant(self, source: str, destination: str) -> List[str]:
7979 List[str]: The shortest path found by the ants (A list of node IDs).
8080 """
8181 # Spawn an ant which favors pheromone values over edge costs.
82- ant = Ant (self .graph , source , destination , alpha = 0.99 , beta = 0.01 )
82+ ant = Ant (self .graph , source , destination , is_solution_ant = True )
8383 while not ant .reached_destination ():
8484 ant .take_step ()
8585 return ant .path
8686
8787 def find_shortest_path (
88- self , source : str , destination : str
88+ self ,
89+ source : str ,
90+ destination : str ,
91+ num_ants : int ,
92+ max_iterations : int ,
93+ cycles : int ,
94+ random_spawn : bool = True ,
8995 ) -> Tuple [List [str ], float ]:
9096 """Finds the shortest path from the source to the destination in the graph using the traditional Ant Colony Optimization technique.
9197
9298 Args:
9399 source (str): The source node in the graph.
94100 destination (str): The destination node in the graph.
101+ num_ants (int): The number of search ants to be deployed.
102+ max_iterations (int): The maximum number of steps an ant is allowed is to take in order to reach the destination,
103+ after which it is tagged as unfit. Defaults to 50.
104+ cycles (int): The number of cycles/waves of search ants to be deployed.
105+ random_spawn (bool, optional): Indicates if the search ants should spawn at random nodes in the graph. Defaults to True.
95106
96107 Returns:
97- List[str]: The shortest path found by the ants (A list of node IDs).
108+ List[str]: The shortest path found by the ants (a list of node IDs).
98109 float: The total travel time of the shortest path.
99110 """
100- self ._deploy_search_ants (source , destination , num_ants = 20 , random_spawns = True )
111+ self ._deploy_search_ants (
112+ source ,
113+ destination ,
114+ num_ants = num_ants ,
115+ max_iterations = max_iterations ,
116+ cycles = cycles ,
117+ random_spawns = random_spawn ,
118+ )
101119 shortest_path = self ._deploy_solution_ant (source , destination )
102120 return shortest_path , self .graph .compute_path_travel_time (shortest_path )
0 commit comments