9 Ant Colony Optimization Details and Socratic Method Introduction
Algorithmic Components of ACO
Initialize parameters, heuristic functions, and pheromone levels.
Ants construct solutions by traversing the graph.
Optionally apply local search (skipped here).
Update pheromone levels based on the solutions found.
Repeat the process for a number of generations or iterations.
A more detailed view: For each iteration, for each ant, for each step, follow a stochastic transition rule.
Two main points: stochastic state transition rule and updating the pheromone.
Data Structures: Matrices
This implementation uses three matrices: distance matrix, heuristic matrix, and pheromone matrix.
This is just one approach; other data structures can be used.
Step 1: Initialize the Distance Matrix
The instructor asks students to write down the distance matrix for the five-city graph.
Example first row: 0, 10, 12, 11, 14 (where 0 represents the distance from a city to itself).
Step 2: Derive the Heuristic Matrix
The heuristic function guides the ant's next move.
For TSP, the heuristic is one over the distance to neighboring nodes ( ).
This prioritizes shorter nodes to minimize the tour length.
To compute the heuristic matrix, take the distance matrix and compute one over each non-zero entry.
The heuristic matrix represents the preference for moving from one node to another.
Step 3: Initialize the Pheromone Matrix
Typically, the pheromone matrix is initialized with random numbers between zero and one.
For simplicity, in this example, the pheromone matrix is initialized with all ones.
All matrices refer to the construction graph, with each entry representing a weight on an edge.
Iteration and Ant Movement
Assume three ants for this iteration.
For each ant:
Initialize allowed nodes (initially all nodes).
Remove the current node from the allowed nodes.
Compute transition probabilities to other allowed nodes.
Select a node based on these probabilities and move.
The instructor asks how to keep track of nodes not allowed for each ant.
A list of visited cities for each ant is a possible solution.
Matrix Trick: Setting Columns to Zero
To prevent an ant from visiting a node (e.g., node 1), set the corresponding column (column 1) in the heuristic matrix to zero.
This makes the heuristic strongly disfavor that node.
Stochastic State Transition Rule
Remind the formula:
Where:
is the probability of ant k at time t moving from node i to node j.
is the pheromone level on edge (i, j) at time t.
is the heuristic value for edge (i, j).
and are parameters.
The formula balances pheromone and heuristic information.
Applying the Formula with Matrices
The instructor asks how to use the transition probability formula with the matrices.
Compute probabilities from the pheromone and heuristic matrices.
Given: Ant k = 1 (ANT1), current node i = 1, time t = 1, , .
Numerator Calculation
Compute the numerators for ant one moving from node one to each of the other nodes.
Example:
Probability of ant one moving from node one to one: pheromone(1,1)^1 * heuristic(1,1)^2 = 1^1 * 0^2 = 0.
The zero comes from the heuristic matrix where the column has been zeroed out to prevent revisits.
Denominator and Normalization
The denominator is the sum of all numerators for the allowed moves.
Normalize the numerators by dividing each by the denominator to get the probabilities.
Roulette Wheel Selection
Use roulette wheel selection (or rule at will selection) to make probabilistic decisions.
Compute cumulative probabilities.
Generate a random number between 0 and 1.
Select the city corresponding to the cumulative probability that is just above the random number.
Example of Node Selection
Given cumulative probabilities: 0, 0.207, 0.453, 0.838, 1.0.
If the random number is 0.6841, select city four (cumulative probability 0.838).
The instructor emphasizes selecting the closest higher probability.
Moving and Updating Allowed Cities
After moving to city four, remove city four from the allowable cities by setting zeros in column four of the heuristic matrix.
Recompute the probabilities using the updated heuristic matrix.
Completing the Iteration
Repeat the process for all five cities for ant one.
Then, repeat the entire process for ant two and ant three.
Fitness Measurement
Example tour lengths:
Ant 1: 1 -> 4 -> 3 -> 5 -> 2 -> 1
Ant 2: [Tour]
Ant 3: [Tour]
Measure the fitness (total tour length) for each ant.
Example: Fitness for ant one is 52, for ant two is 60, for ant three is 60.
Pheromone Update
Remind the formula:
Where:
is the pheromone level on edge (i, j) at time t+1.
is the evaporation rate.
is the amount of pheromone ant k deposits on edge (i, j).
if ant k used edge (i, j) in its tour, otherwise 0.
is a constant (e.g., 1).
is the length of the tour of ant k.
Matrix Form of Pheromone Update
Evaporation: Multiply every entry in the pheromone matrix by .
With , an initial pheromone of 1 becomes 0.5.
Pheromone Deposition
For each edge traversed by an ant, add to the corresponding entry in the pheromone matrix.
If an edge was not traversed, add zero.
Example: Ant one went from city one (0 in Python) to city four (3), so add one over its tour length (1/52) to pheromone[0][3].
This updates pheromone levels based on tour quality.
Completing the Pheromone Update
Update the pheromone matrix for each ant (ant two and ant three).
After updating pheromones for all ants, the first loop is complete.
The second loop repeats the entire process again.
Conclusion
Using matrices simplifies the implementation of ACO.
Careful handling of the matrices is important.
Matrices are a good way of implementing ACO.