Simulated annealing
- class SA(domain: Domain, fitness_function: Callable[[Solution], float], warmup_iterations: int = 5, max_iterations: int = 20, alteration_limit: int = 1, initial_temp: float = 50.0, cooling_rate: float = 0.99, neighbor_population_size: int = 1, distributed=False, log_dir: str = 'logs/SA')
Bases:
MetaheuristicSimulated Annealing (SA) algorithm for optimization problems.
This class implements the Simulated Annealing metaheuristic which uses temperature-based probabilistic acceptance of worse solutions to escape local optima. The temperature gradually decreases according to a cooling schedule, reducing the probability of accepting worse solutions over time.
- Parameters:
domain (Domain) – The problem domain that defines the solution space
fitness_function (Callable[[Solution], float]) – Function to evaluate solutions
max_iterations (int, optional) – Maximum number of iterations to run, defaults to 20
alteration_limit (float, optional) – Maximum proportion of solution to alter when generating neighbors, defaults to 0.1
initial_temp (float, optional) – Initial temperature for annealing process, defaults to 50.0
cooling_rate (float, optional) – Rate at which temperature decreases, defaults to 0.99
neighbor_population_size (int, optional) – Number of neighbors to generate in each iteration, defaults to 1
distributed (bool, optional) – Whether to use distributed computation, defaults to False
log_dir (str, optional) – Directory for logging, defaults to “logs/SA”
- Variables:
max_iterations (int) – Maximum number of iterations
alteration_limit (float) – Maximum proportion of solution to alter
initial_temp (float) – Current temperature in the annealing process
cooling_rate (float) – Rate of temperature decrease
neighbor_population_size (int) – Number of neighbors per iteration
Initialize the Simulated Annealing algorithm.
- Parameters:
domain (Domain) – The problem domain that defines the solution space
fitness_function (Callable[[Solution], float]) – Function to evaluate solutions
max_iterations (int, optional) – Maximum number of iterations to run, defaults to 20
alteration_limit (float, optional) – Maximum proportion of solution to alter when generating neighbors, defaults to 0.1
initial_temp (float, optional) – Initial temperature for annealing process, defaults to 50.0
cooling_rate (float, optional) – Rate at which temperature decreases, defaults to 0.99
neighbor_population_size (int, optional) – Number of neighbors to generate in each iteration, defaults to 1
distributed (bool, optional) – Whether to use distributed computation, defaults to False
log_dir (str, optional) – Directory for logging, defaults to “logs/SA”
- initialize(num_solutions: int = 1) Tuple[List[Solution], Solution]
Initialize the Simulated Annealing algorithm with random solutions.
- iterate(solutions: List[Solution]) Tuple[List[Solution], Solution]
Execute one iteration of the Simulated Annealing algorithm.
In each iteration, multiple neighbor solutions are generated, and the best one is selected. The Metropolis criterion determines whether the selected neighbor replaces the current solution based on the current temperature. The temperature is then decreased according to the cooling schedule.
- stopping_criterion() bool
Check if the algorithm should stop.