Simulated Annealing in Python

Simulated annealing is a probabilistic technique for approximating the global optimum of a given function. It's often used for optimization problems.

Here's a step-by-step guide to create a simple simulated annealing algorithm in Python. We'll use a basic example of finding the minimum of a function.

Step 1: Define the Objective Function

First, let's define the objective function that we want to minimize. For this example, we'll use the Rastrigin function, which is a common test function for optimization algorithms.

import math

def rastrigin(x):
    A = 10
    return A * len(x) + sum([(xi**2 - A * math.cos(2 * math.pi * xi)) for xi in x])

Step 2: Define the Simulated Annealing Algorithm

Next, we'll define the simulated annealing algorithm. This includes the initial solution, the cooling schedule, and the acceptance probability function.

import random
import math

def simulated_annealing(objective_function, initial_solution, max_iterations, initial_temperature, cooling_rate):
    current_solution = initial_solution
    current_energy = objective_function(current_solution)
    best_solution = current_solution
    best_energy = current_energy
    temperature = initial_temperature

    for iteration in range(max_iterations):
        # Generate a new candidate solution
        new_solution = [x + random.uniform(-1, 1) for x in current_solution]
        new_energy = objective_function(new_solution)

        # Calculate the change in energy
        energy_change = new_energy - current_energy

        # Accept the new solution if it's better, or with a certain probability if it's worse
        if energy_change < 0 or random.uniform(0, 1) < math.exp(-energy_change / temperature):
            current_solution = new_solution
            current_energy = new_energy

            # Update the best solution found so far
            if current_energy < best_energy:
                best_solution = current_solution
                best_energy = current_energy

        # Cool down the temperature
        temperature *= cooling_rate

    return best_solution, best_energy

Step 3: Run the Simulated Annealing Algorithm

Finally, we'll run the simulated annealing algorithm with some initial parameters.

# Initial solution (randomly chosen)
initial_solution = [random.uniform(-5.12, 5.12) for _ in range(2)]

# Parameters for the simulated annealing algorithm
max_iterations = 1000
initial_temperature = 1000
cooling_rate = 0.99

# Run the simulated annealing algorithm
best_solution, best_energy = simulated_annealing(rastrigin, initial_solution, max_iterations, initial_temperature, cooling_rate)

print(f"Best solution: {best_solution}")
print(f"Best energy: {best_energy}")

Explanation