Skip to content

Simulated Annealing (SA)

A method that sometimes accepts worse solutions, less often as the temperature falls, so it can leave local optima. It works on continuous problems and, with space="native", directly on permutations, selections and binary variables (see Native operators).

SA

SA(*, max_sub_iter=10, initial_temp=1000.0, final_temp=1.0, move_count=5, mutation_rate=0.1, mutation_step_size=0.1, mutation_step_size_damp=0.99, pop_size=10, space='encoded', operators=None, mutation=None, selection='pool', **kwargs)

Simulated Annealing with a population.

Each of the pop_size solutions makes move_count random moves per sub iteration. With selection="pool" (default) all moves are ranked together, the solutions are ranked too, and the k-th best move becomes the candidate of the k-th best solution, so a promising solution can spread several candidates. With selection="chain" each solution keeps its own best move, giving independent chains, which can do better on hard rotated or hybrid functions and on combinatorial problems such as tours. A candidate is accepted if it is better, or else with probability exp(-delta / T). With pop_size=1 and move_count=1 this is classic SA.

delta follows the problem's comparison rules: with feasibility rules (the default) it is the difference in violation when the violations differ and the difference in objective otherwise, and with a penalty it is the difference in penalized objective. Set initial_temp to match the scale of these differences.

Parameters

  • max_sub_iter: sub iterations per iteration, all at the same temperature. Each sub iteration makes move_count moves per solution.
  • initial_temp, final_temp: the temperature falls geometrically from initial_temp to final_temp over the planned length of the run, and never below final_temp
  • move_count: moves per solution in each sub iteration
  • selection: "pool" (default) or "chain", how candidates are chosen from the moves
  • mutation_rate: probability that a component changes in a move (at least one changes)
  • mutation_step_size: standard deviation of the Gaussian move (in code units)
  • mutation_step_size_damp: the step size is multiplied by this after every iteration
  • space: "encoded" (default) moves in the [0, 1] codes. "native" applies the native mutation of each variable type, and types without one use the Gaussian move.
  • operators (dict, optional): Maps a variable name to an object with a mutate(x, rng) method, or a tuple (None, mutate), used in place of the default move with space="native".
  • mutation (ypea.operators.Mutation or None): An operator in place of the Gaussian move, for example ypea.operators.PolynomialMutation(). A code operator needs space="encoded", a native one (NativeMutation) needs space="native" and replaces the move of the variable types it lists. None (default) keeps the default moves.

Reference: S. Kirkpatrick, C. D. Gelatt and M. P. Vecchi, "Optimization by simulated annealing", Science 220(4598), 1983, 671 to 680.

Example

import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=20, lb=-10, ub=10),
    lambda sol: ypea.benchmarks.sphere(sol.x),
    vectorized=True,
    name="sphere",
)

alg = ypea.SA(max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
SA on sphere
  best objective value: 1.60572
  feasible: True (violation 0)
  evaluations: 20000, iterations: 40, time: 0.0464 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=20, lb=-10, ub=10), ...
    objective=@(sol) ypea.benchmarks.sphere(sol.x), ...
    vectorized=true, ...
    name="sphere");

alg = ypea.SA(max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
SA on sphere
  best objective value: 1.66694
  feasible: true (violation 0)
  evaluations: 20000, iterations: 40, time: 0.345 s, stopped by: max_nfe

examples/tsp_sa.py solves a traveling salesman problem with SA.

Search on Rastrigin

SA solutions on Rastrigin at four iterations, and its convergence
SA with default settings, 50 iterations on Rastrigin in two dimensions (seed 1). The red points are the current solutions after each temperature stage. The open diamond is the best solution so far, and the plus sign the global minimum.