Skip to content
Home » Simulated Annealing

Simulated Annealing

Simulated Annealing in Artificial Intelligence

1. Introduction

Simulated Annealing (SA) is an informed local search and optimization algorithm inspired by the physical process of annealing in metallurgy.

In metallurgy, a material is heated to a high temperature and then slowly cooled. During cooling, atoms gradually settle into a stable, low-energy structure.

Similarly, in AI:

Simulated Annealing starts with a solution, explores neighboring solutions, and sometimes accepts a worse solution temporarily so that it can escape local maxima/minima and eventually reach a better global solution.

This is the major difference between Hill Climbing and Simulated Annealing.


2. Why Do We Need Simulated Annealing?

Consider Hill Climbing.

Suppose we want to maximize a function:

                 Global Maximum
                      /\
                     /  \
          /\        /    \
         /  \______/      \
        / Local Maximum    \
_______/____________________\____
   Start

Hill Climbing always moves toward a better neighboring state.

The problem is that it may reach a local maximum:

                 Global Maximum
                      /\
                     /  \
                    /    \
       Local       /      \
      Maximum     /        \
        /\       /          \
       /  \_____/            \
______/_______________________\___
     ↑
   Hill Climbing
     gets stuck

Once all neighboring states are worse, Hill Climbing stops.

Simulated Annealing solves this problem

It can occasionally accept a worse move:

Current State
     ↓
Better Move → Accept
     ↓
Worse Move → Sometimes Accept
     ↓
Escape Local Maximum
     ↓
Explore New Region
     ↓
Better Solution

As the algorithm progresses, the probability of accepting worse moves gradually decreases.


3. Definition

Exam Definition

Simulated Annealing is a probabilistic local search algorithm inspired by the annealing process in metallurgy. It searches for an optimal solution by accepting better neighboring states and occasionally accepting worse states with a probability controlled by a temperature parameter. The temperature is gradually reduced so that the algorithm becomes less likely to accept worse solutions over time.

Simple Definition

Simulated Annealing = Hill Climbing + controlled acceptance of worse moves.


4. Real-World Analogy

Imagine you are searching for the lowest point in a large mountainous area.

If you use ordinary Hill Climbing:

          /\                /\
         /  \              /  \
        /    \      /\    /    \
_______/      \____/  \__/      \____
               ↑
            Local valley

You may reach a small valley and stop.

But if you are allowed to temporarily move uphill, you can cross the mountain and potentially reach a deeper valley.

Simulated Annealing behaves this way.


5. Metallurgy Analogy

The algorithm gets its name from annealing.

In metallurgy:

Heat material
     ↓
Atoms move freely
     ↓
Slow cooling
     ↓
Atoms settle into stable structure
     ↓
Low-energy state

In AI:

High Temperature
     ↓
More exploration
     ↓
Worse moves can be accepted
     ↓
Temperature decreases
     ↓
Less exploration
     ↓
Mostly better moves accepted
     ↓
Final solution

6. Important Concept: Temperature

The most important parameter in Simulated Annealing is Temperature, represented by:\[ T \]

At the beginning:\[ T = \text{High} \]

At the end:\[ T \rightarrow 0 \]

High Temperature

The algorithm is willing to explore.

High T
↓
High probability of accepting worse solutions
↓
More exploration

Low Temperature

The algorithm becomes conservative.

Low T
↓
Low probability of accepting worse solutions
↓
Mostly better solutions accepted

Therefore:

Temperature controls the balance between exploration and exploitation.


7. Acceptance Probability

Suppose the algorithm is solving a minimization problem.

If the new solution is better:\[ \Delta E < 0 \]

it is normally accepted.

If the new solution is worse:\[ \Delta E > 0 \]

it may still be accepted.

The probability is:\[ P = e^{-\Delta E/T} \]

where:

  • \(P\) = probability of accepting worse solution
  • \(e\) = Euler’s number, approximately 2.718
  • \(\Delta E\) = increase in cost/energy
  • \(T\) = current temperature

8. Understanding the Formula

\[ P = e^{-\Delta E/T} \]

Suppose:\[ \Delta E = 5 \]

Case 1: High temperature

\[ T=100 \]\[ P=e^{-5/100} \]\[ P=e^{-0.05}\approx0.951 \]

So there is approximately a 95.1% probability of accepting the worse solution.

Case 2: Low temperature

\[ T=10 \]\[ P=e^{-5/10} \]\[ P=e^{-0.5}\approx0.607 \]

Probability ≈ 60.7%.

Case 3: Very low temperature

\[ T=1 \]\[ P=e^{-5}\approx0.0067 \]

Only about 0.67% probability.

So:

Temperature decreases
        ↓
Acceptance probability decreases
        ↓
Algorithm becomes more selective

9. How Does the Algorithm Decide?

Suppose current state has cost:\[ E_{current}=20 \]

A neighboring state has:\[ E_{new}=25 \]

Since:\[ 25 > 20 \]

the new state is worse in a minimization problem.

Calculate:\[ \Delta E=25-20=5 \]

Then:\[ P=e^{-5/T} \]

Generate a random number between 0 and 1.

For example:

Probability = 0.60
Random number = 0.35

Since:\[ 0.35 < 0.60 \]

the worse solution is accepted.

This helps the algorithm escape a local optimum.


10. Basic Algorithm

Step-by-step

Step 1: Choose an initial solution.

Step 2: Set a high initial temperature.

Step 3: Generate a neighboring solution.

Step 4: Calculate the change in cost.

Step 5: If the new solution is better, accept it.

Step 6: If it is worse, calculate the acceptance probability.

Step 7: Generate a random number.

Step 8: Accept the worse solution if the random number is less than the probability.

Step 9: Reduce the temperature.

Step 10: Repeat until the stopping condition is reached.


11. Flowchart

              START
                |
                ↓
       Generate Initial State
                |
                ↓
        Set Initial Temperature
                |
                ↓
       Generate Neighbor State
                |
                ↓
       Is Neighbor Better?
          /           \
        YES            NO
         |              |
         ↓              ↓
      Accept       Calculate P=e^(-ΔE/T)
                        |
                        ↓
                Generate Random r
                        |
                ┌───────┴────────┐
                ↓                ↓
              r < P?           r >= P?
                |                |
               YES               NO
                |                |
                ↓                ↓
             Accept           Reject
                \                /
                 \              /
                  ↓            ↓
                 Reduce Temperature
                        |
                        ↓
                  Stopping?
                   /     \
                 NO       YES
                 |         |
                 ↓         ↓
              Repeat      STOP

12. Pseudocode

Simulated-Annealing(problem):

    current = initial state
    T = initial temperature

    while T > minimum_temperature:

        next = randomly selected neighbor

        ΔE = cost(next) - cost(current)

        if ΔE < 0:
            current = next

        else:
            P = e^(-ΔE/T)

            generate random number r

            if r < P:
                current = next

        T = reduce(T)

    return current

For a maximization problem, the evaluation/cost convention can be reversed, or we can define energy so that lower is better.


13. Cooling Schedule

The temperature must gradually decrease.

One common approach is:\[ T_{new}=\alpha T_{old} \]

where:\[ 0<\alpha<1 \]

For example:\[ \alpha=0.9 \]

Starting with:\[ T=100 \]

we get:

Iteration    Temperature
------------------------
1            100
2             90
3             81
4             72.9
5             65.61
...

This is called a cooling schedule.

Common cooling schedules

  1. Geometric cooling\[ T_{new}=\alpha T \]
  2. Linear cooling\[ T_{new}=T-\delta \]
  3. Logarithmic cooling

The cooling schedule strongly affects the performance of Simulated Annealing.


14. Example: 8-Queens Problem

Simulated Annealing can be applied to the 8-Queens problem.

We want:

Place 8 queens so that no two queens attack each other.

Define:\[ h(n)=\text{number of attacking pairs} \]

Goal:\[ h(n)=0 \]

Suppose current state has:\[ h=6 \]

A neighboring state has:\[ h=4 \]

Since:\[ 4<6 \]

the new state is better.

Therefore:

Current
h = 6
  ↓
Neighbor
h = 4
  ↓
Accept

Now suppose another neighbor has:\[ h=5 \]

It is worse than the current state \(h=4\).

Hill Climbing would reject it.

Simulated Annealing may accept it:

Current h = 4
      ↓
New h = 5
      ↓
Worse?
      ↓
Calculate probability
      ↓
Maybe accept

This temporary move can help escape a local minimum.


15. Hill Climbing vs Simulated Annealing

FeatureHill ClimbingSimulated Annealing
TypeLocal searchLocal search
Uses heuristicYesYes
Better moveAlways acceptedAccepted
Worse moveRejectedSometimes accepted
RandomnessUsually lowHigh
TemperatureNoYes
Escapes local optimumPoorlyBetter
MemoryVery lowVery low
Guaranteed optimalNoGenerally no in practical finite runs
Main strengthFast improvementEscape local optima
Main weaknessLocal maxima/minimaParameter tuning

Most important difference

Hill Climbing:
Better → Accept
Worse  → Reject

Simulated Annealing:
Better → Accept
Worse  → Maybe Accept

16. Hill Climbing Problem and Simulated Annealing Solution

Hill Climbing

Start
  ↓
Better
  ↓
Better
  ↓
Local Maximum
  ↓
STOP

Simulated Annealing

Start
  ↓
Better
  ↓
Better
  ↓
Worse
  ↓
Accept temporarily
  ↓
Escape local maximum
  ↓
Better region
  ↓
Better
  ↓
Final solution

This is the main reason Simulated Annealing is important.


17. Exploration vs Exploitation

This is a very important AI concept.

Exploration

Searching different areas of the solution space.

Exploitation

Improving the current promising solution.

Simulated Annealing balances both:

High Temperature
       ↓
    Exploration
       ↓
More random/worse moves
       ↓
Temperature decreases
       ↓
    Exploitation
       ↓
Focus on better solutions

Therefore:

High temperature encourages exploration, while low temperature encourages exploitation.


18. Advantages

1. Escapes Local Optima

Its biggest advantage is that it can temporarily accept worse solutions.

2. Low Memory Requirement

It generally maintains only the current solution and a candidate neighbor.

3. Suitable for Large Search Spaces

Useful where exhaustive search is too expensive.

4. Simple Concept

The algorithm is relatively easy to implement.

5. Useful for Optimization

It can be applied to scheduling, routing, configuration and resource allocation.

6. Less Sensitive to Initial State Than Basic Hill Climbing

Because it can move away from locally attractive states.


19. Disadvantages

1. No Guaranteed Optimal Solution in Practical Runs

A good solution may be found, but practical implementations do not guarantee the global optimum.

2. Parameter Selection

Performance depends on:

  • Initial temperature
  • Cooling rate
  • Stopping temperature
  • Number of iterations

3. Can Be Slow

If cooling is too slow, many iterations may be required.

4. Can Give Poor Results

If temperature decreases too quickly, the algorithm may behave almost like Hill Climbing.

5. Random Behavior

Different runs can produce different solutions.


20. Applications

Simulated Annealing is useful in many optimization problems.

1. Scheduling

Job → Machine → Time Slot

Finding an efficient schedule.

2. Travelling Salesman Problem

Finding a short route visiting multiple cities.

3. Network Optimization

Optimizing network configurations.

4. VLSI Design

Optimizing the placement of electronic components.

5. Resource Allocation

Allocating limited resources efficiently.

6. Timetabling

Creating university examination or class schedules.

7. 8-Queens

Finding a configuration with zero conflicts.

8. Machine Learning

Can be used in certain parameter/hyperparameter or combinatorial optimization tasks.


21. Important Terms

TermMeaning
Current StatePresent solution
NeighborSlightly modified solution
Energy/CostQuality measure
TemperatureControls acceptance of worse moves
Cooling ScheduleMethod of reducing temperature
\(\Delta E\)Change in energy/cost
Acceptance ProbabilityProbability of accepting worse state
Global OptimumBest possible solution
Local OptimumBest solution in a local region

22. Key Formula

The most important formula is:\[ \boxed{P=e^{-\Delta E/T}} \]

where:\[ \Delta E=E_{new}-E_{current} \]

For minimization:

  • If \(\Delta E<0\): new solution is better → accept.
  • If \(\Delta E>0\): new solution is worse → accept with probability \(e^{-\Delta E/T}\).

Cooling:\[ \boxed{T_{new}=\alpha T_{old}} \]

where:\[ 0<\alpha<1 \]


23. Easy Numerical Example

Suppose:\[ E_{current}=10 \]

and:\[ E_{new}=12 \]

Therefore:\[ \Delta E=12-10=2 \]

Temperature:\[ T=20 \]

Acceptance probability:\[ P=e^{-2/20} \]\[ P=e^{-0.1} \]\[ P\approx0.905 \]

So the worse solution has approximately a 90.5% probability of being accepted.

Now suppose:\[ T=2 \]

Then:\[ P=e^{-2/2}=e^{-1}\approx0.368 \]

Only about 36.8% probability.

Thus:

High T → Worse move more likely accepted
Low T  → Worse move less likely accepted

24. Why Is It Called “Annealing”?

The name comes directly from physics.

MetallurgyAI
MaterialCandidate solution
EnergyCost/Evaluation
HeatingHigh temperature
Atoms move freelyMore exploration
CoolingTemperature reduction
Stable structureGood solution
Low energyLow cost

This analogy is frequently asked in exams.


25. Hill Climbing → Simulated Annealing Connection

This connection is particularly important for your syllabus:

Informed Search
      │
      ├── Hill Climbing
      │      │
      │      └── Problem: Local Maximum
      │
      └── Simulated Annealing
             │
             ├── Uses temperature
             ├── Allows worse moves
             ├── Escapes local optimum
             └── Gradually becomes selective

So when teaching this topic, I would explain it as:

Hill Climbing gets stuck because it never accepts a worse state. Simulated Annealing overcomes this limitation by sometimes accepting a worse state, with the probability controlled by temperature. As temperature decreases, the algorithm gradually changes from exploration to exploitation.


26. 5-Mark Exam Answer

Simulated Annealing is a probabilistic local search and optimization algorithm inspired by the annealing process in metallurgy. It starts with an initial solution and repeatedly generates neighboring solutions. Better solutions are accepted, while worse solutions may also be accepted with a probability given by \(P=e^{-\Delta E/T}\). The temperature \(T\) is gradually reduced according to a cooling schedule. At high temperature, the algorithm explores the search space and can accept worse solutions, helping it escape local optima. At low temperature, it becomes more selective and behaves similarly to Hill Climbing. Simulated Annealing is used in optimization problems such as scheduling, routing, resource allocation, VLSI design and the 8-Queens problem.


27. One-Page Revision

             SIMULATED ANNEALING
                     │
          Probabilistic Local Search
                     │
              Initial Solution
                     │
              High Temperature
                     │
              Generate Neighbor
                     │
          ┌──────────┴──────────┐
          │                     │
       Better                 Worse
          │                     │
       Accept             Calculate P
                                │
                         P = e^(-ΔE/T)
                                │
                         Random Decision
                                │
                         Accept / Reject
                                │
                         Reduce Temperature
                                │
                            Repeat
                                │
                         Final Solution

Remember:

SA = Search + Randomness + Temperature + Worse-Move Acceptance

And the most important formula:\[ \boxed{P=e^{-\Delta E/T}} \]