@@ -79,14 +79,10 @@ def from_tsp_file(cls, file_path: str) -> "TSPGraph":
7979
8080
8181class AntTSP (CellAgent ):
82- """
83- An agent
84- """
82+ """An agent"""
8583
8684 def __init__ (self , model , alpha : float = 1.0 , beta : float = 5.0 ):
87- """
88- Customize the agent
89- """
85+ """Customize the agent"""
9086 super ().__init__ (model )
9187 self .alpha = alpha
9288 self .beta = beta
@@ -141,11 +137,9 @@ def decide_next_city(self):
141137 return new_city
142138
143139 def step (self ):
144- """
145- Modify this method to change what an individual agent will do during each step.
140+ """Modify this method to change what an individual agent will do during each step.
146141 Can include logic based on neighbors states.
147142 """
148-
149143 for _ in range (self .model .num_cities - 1 ):
150144 # Pick a random city that isn't in the list of cities visited
151145 new_city = self .decide_next_city ()
@@ -158,8 +152,7 @@ def step(self):
158152
159153
160154class AcoTspModel (mesa .Model ):
161- """
162- The model class holds the model-level attributes, manages the agents, and generally handles
155+ """The model class holds the model-level attributes, manages the agents, and generally handles
163156 the global level of our model.
164157
165158 There is only one model-level parameter: how many agents the model contains. When a new model
@@ -168,12 +161,16 @@ class AcoTspModel(mesa.Model):
168161
169162 def __init__ (
170163 self ,
164+ tsp_graph : TSPGraph | None ,
171165 num_agents : int = 20 ,
172- tsp_graph : TSPGraph = TSPGraph .from_random (20 ),
173166 max_steps : int = int (1e6 ),
174167 ant_alpha : float = 1.0 ,
175168 ant_beta : float = 5.0 ,
176169 ):
170+ # Don't create the TSPGraph.from_random as argument default: https://docs.astral.sh/ruff/rules/function-call-in-default-argument/
171+ if tsp_graph is None :
172+ tsp_graph = TSPGraph .from_random (20 )
173+
177174 super ().__init__ ()
178175 self .num_agents = num_agents
179176 self .tsp_graph = tsp_graph
@@ -224,15 +221,13 @@ def update_pheromone(self, q: float = 100, ro: float = 0.5):
224221 # Evaporate
225222 tau_ij = (1 - ro ) * self .grid .G [i ][j ]["pheromone" ]
226223 # Add ant's contribution
227- for k , delta_tau_ij_k in delta_tau_ij .items ():
224+ for _ , delta_tau_ij_k in delta_tau_ij .items ():
228225 tau_ij += delta_tau_ij_k .get ((i , j ), 0.0 )
229226
230227 self .grid .G [i ][j ]["pheromone" ] = tau_ij
231228
232229 def step (self ):
233- """
234- A model step. Used for activating the agents and collecting data.
235- """
230+ """A model step. Used for activating the agents and collecting data."""
236231 self .agents .shuffle_do ("step" )
237232 self .update_pheromone ()
238233
0 commit comments