AntColonyOptimization
Class
The AntColonyOptimization
class implements the Ant Colony Optimization (ACO) algorithm. ACO is a probabilistic technique for solving computational problems which can be reduced to finding good paths through graphs.
Attributes
goal
(str): The goal string to be optimized.num_ants
(int): Number of ants.evaporation_rate
(float): Evaporation rate.alpha
(int): The relative importance of the pheromone.beta
(int): The relative importance of the heuristic information.num_iterations
(int): The number of iterations.pheromones
(torch.Tensor): The pheromone levels.solutions
(list): The solutions found by the ants.
Methods
__init__(self, goal: str = None, num_ants: int = 10000, evaporation_rate: float = 0.1, alpha: int = 1, beta: int = 1, num_iterations: int = 10010)
The constructor for the AntColonyOptimization
class. Initializes the pheromone levels and the solutions.
Parameters
goal
(str, optional): The goal string to be optimized.num_ants
(int, optional): Number of ants. Default is 10000.evaporation_rate
(float, optional): Evaporation rate. Default is 0.1.alpha
(int, optional): The relative importance of the pheromone. Default is 1.beta
(int, optional): The relative importance of the heuristic information. Default is 1.num_iterations
(int, optional): The number of iterations. Default is 10010.
Example
fitness(self, solution)
Computes the fitness of a solution. The fitness is the negative of the Euclidean distance between the solution and the goal.
Parameters
solution
(torch.Tensor): The solution to compute the fitness for.
Returns
fitness
(float): The fitness of the solution.
Example
update_pheromones(self)
Updates the pheromone levels based on the fitness of the solutions.
Example
choose_next_path(self)
Chooses the next path based on the pheromone levels. The probability of choosing a path is proportional to the pheromone level of the path.
Returns
path
(int): The chosen path.
Example
optimize(self)
Optimizes the goal string. Updates the solutions and the pheromone levels for a given number of iterations and returns the best solution.
Returns
best_solution
(str): The best solution.
Example
Usage Examples
Example 1: Optimize a String
In this example, we will optimize the string "Hello ACO" using 10000 ants and 1000 iterations.
aco = AntColonyOptimization(goal="Hello ACO", num_ants=10000, num_iterations=1000)
best_solution = aco.optimize()
print("Best Matched String:", best_solution)
Example 2: Using a Different Number of Ants
In this example, we will optimize the string "Hello ACO" using 5000 ants and 1000 iterations.
aco = AntColonyOptimization(goal="Hello ACO", num_ants=5000, num_iterations=1000)
best_solution = aco.optimize()
print("Best Matched String:", best_solution)
Example 3: Using a Different Evaporation Rate
In this example, we will optimize the string "Hello ACO" using 10000 ants, an evaporation rate of 0.2, and 1000 iterations.