@@ -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