Data for cities are collected from the same list used by the first algorithm.
The route of an ant starts from a random city, and then the probability of each city being chosen as the next one is calculated. The probability is computed by taking the distance between the current city in the route and the next city to the power of -β, where β is the parameter influencing the distance for the ant. This value is multiplied by the pheromone level on the edge between the current city and the next city to the power of α, where α is the parameter influencing the pheromone for the ant. Thus, this function can be formulated as follows:
Where:
is the probability of ant k going from city x to city y.
is the distance between city x and city y.
is the parameter influencing the distance for the ant.
is the amount of pheromone deposited on the edge between cities x and y.
is the parameter influencing the pheromone for the ant.
The negative exponent in
is used to invert the distance.
Thus, if
has a high value, the probability will be small, and if it has a low value, the probability will be large.
The variables β and α are parameters that can be adjusted to control the relative importance of distance and pheromone level in the probability calculation. A higher value of β gives more weight to the distance, while a higher value of α gives more weight to the pheromone level.
The calculation of
uses the geopy library to handle coordinate information and perform the calculation.
After all cities are chosen, the route is returned.
The algorithm starts by initializing the pheromones on all edges with a small amount. Pheromones are used to store information about the quality of routes that ants have previously made. In this case, pheromones are represented by a two-dimensional matrix, where each element represents the pheromone level on the edge between a pair of cities.
The algorithm executes a defined number of iterations, where in each iteration, a certain number of ants are generated. Each ant constructs a route using the function mentioned earlier, which employs a probability calculation based on distance and pheromone level to determine the next city to be added to the route. The routes built by the ants are stored in a list.
After all ants have constructed their routes in an iteration, the best route among the set (the one with the lowest cost) is chosen, and the pheromones on the edges of the best route are updated. Specifically, the pheromones are incremented by a value proportional to the inverse of the route cost, meaning that the edges of the best route will receive more pheromones, and the edges of a poor route will receive fewer. After this, the pheromones on all edges are evaporated by a small amount (specified by the evaporation parameter) to simulate the natural decay of pheromones. This encourages ants to explore new routes in subsequent iterations.
After all iterations are completed, the algorithm returns the best route found among the set of best routes and also calculates the cost of this route.
