Skip to content

Simulated Annealing Optimizer

The Simulated Annealing algorithm draws inspiration from metallurgy to search for optimal solutions. By introducing randomness and probabilistic decisions, the algorithm allows particles to explore various regions of the solution space and eventually converge toward the optimal solution.

Detailed Explanation

When to Use the Simulated Annealing Optimizer

The Simulated Annealing Optimizer is particularly suitable for scenarios where: - Stochastic Optimization: When a problem involves noise or randomness, making it difficult to use gradient-based optimization methods. - Escape Local Minima: To overcome the challenge of getting trapped in local minima or suboptimal solutions, as the algorithm may escape these by accepting suboptimal solutions with a certain probability. - Exploration-Exploitation Trade-off: For striking a balance between exploring the solution space and exploiting promising regions to find optimal solutions.

Key Features

  • Probabilistic Movement: Instead of strictly following gradients, particles move probabilistically, allowing them to explore and escape local minima.
  • Annealing Strategy: The algorithm starts with a high "temperature," which gradually decreases. This annealing strategy helps the algorithm escape from poor solutions at the beginning and converge towards better solutions as the temperature reduces.
  • Acceptance Probability: The algorithm accepts worse solutions with a certain probability, enabling it to explore the solution space more thoroughly and potentially find better solutions.


  • Instantiate the Optimizer: Create an instance of the SimulatedAnnealingOptimizer class, specifying the minimum and maximum values, initial values, initial temperature, cooling rate, and any other optional parameters.
  • Define Your Fitness Evaluation Function: Develop a function that evaluates the fitness of a given solution. This function guides the optimizer in determining the quality of a solution.
  • Run the Optimization: Invoke the Optimize method on your optimizer instance, passing your fitness evaluation function and the desired number of optimization epochs.
  • Retrieve Optimized Results: The optimizer will return the optimized solution, ready to be integrated into your Unity project.


Here's an example demonstrating how to use the Simulated Annealing Optimizer to optimize an integer function:

// Define your evaluation function
async Task<float> EvaluateFunction(int[] values)
    // Calculate the fitness based on a custom function
    float fitness = -values[0] * values[0] + 3 * values[0] - 5;

    return fitness;

// Initialize the Simulated Annealing Optimizer
var optimizer = new SimulatedAnnealingOptimizer(minimum, maximum, initialValues, initialTemperature, coolingRate);

// Optimize the function
int epochs = 100;
int[] optimizedValues = await optimizer.Optimize(EvaluateFunction, epochs);

In this example, the Simulated Annealing Optimizer is used to optimize an integer function. The optimizer simulates the annealing process to explore the solution space and find the optimal solution.