0
0
SciPydata~10 mins

Simulated annealing (dual_annealing) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Simulated annealing (dual_annealing)
Start with initial guess
Evaluate objective function
Generate new candidate solution
Calculate energy difference
Accept new solution?
Update current
Lower temperature
Temperature > min?
Stop
Yes
Repeat steps
The algorithm starts with a guess, evaluates it, tries a new guess, decides to accept it or not based on energy difference and temperature, then lowers temperature and repeats until stopping.
Execution Sample
SciPy
from scipy.optimize import dual_annealing

def f(x):
    return (x[0]-3)**2 + (x[1]+1)**2

result = dual_annealing(f, bounds=[(-5,5), (-5,5)])
print(result.x, result.fun)
This code finds the minimum of a simple function using dual_annealing from scipy.
Execution Table
StepCurrent SolutionCandidate SolutionEnergy CurrentEnergy CandidateEnergy DiffTemperatureAccept CandidateReason
1[0, 0][0.5, -0.3]108.34-1.662500YesCandidate better (energy lower)
2[0.5, -0.3][1.2, -0.8]8.344.13-4.212400YesCandidate better
3[1.2, -0.8][2.5, -1.5]4.130.5-3.632300YesCandidate better
4[2.5, -1.5][3.5, -0.5]0.51.250.752200NoCandidate worse, rejected
5[2.5, -1.5][3.1, -1.1]0.50.02-0.482100YesCandidate better
6[3.1, -1.1][2.9, -0.9]0.020.080.062000YesCandidate worse but accepted by probability
...........................
N[3.0, -1.0][3.0, -1.0]0.00.00.01YesReached minimum, temperature low
ExitFinal solution found0.00StopTemperature reached minimum threshold
💡 Temperature reached minimum threshold, algorithm stops with best found solution.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6...Final
Current Solution[0, 0][0.5, -0.3][1.2, -0.8][2.5, -1.5][2.5, -1.5][3.1, -1.1][2.9, -0.9]...[3.0, -1.0]
Temperature250024002300220021002000......0
Key Moments - 3 Insights
Why does the algorithm sometimes accept a worse candidate solution?
Because of the temperature and energy difference, the algorithm accepts worse solutions with some probability to avoid local minima, as shown in step 6 where energy difference is positive but candidate is accepted.
What causes the algorithm to stop?
The algorithm stops when the temperature reaches a minimum threshold, as shown in the exit row where temperature is 0 and no further steps occur.
Why does the current solution sometimes stay the same after a candidate is rejected?
When the candidate solution has higher energy and is not accepted, the current solution remains unchanged, as in step 4 where candidate is worse and rejected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the reason the candidate solution was rejected?
ACandidate solution had lower energy
BCandidate solution had higher energy and was rejected
CTemperature was too high
DAlgorithm reached stopping condition
💡 Hint
Check the 'Energy Diff' and 'Accept Candidate' columns at step 4 in the execution table.
According to the variable tracker, what is the temperature after step 3?
A2100
B2200
C2300
D2400
💡 Hint
Look at the 'Temperature' row and the 'After 3' column in the variable tracker.
If the temperature never decreased, how would the acceptance of worse candidates change?
AWorse candidates would be accepted more often
BWorse candidates would never be accepted
CAcceptance would be random
DAlgorithm would stop immediately
💡 Hint
Recall that higher temperature increases probability to accept worse solutions, see step 6 in execution table.
Concept Snapshot
Simulated annealing (dual_annealing):
- Starts with initial guess and temperature
- Generates new candidate solutions
- Accepts better or sometimes worse solutions based on temperature
- Gradually lowers temperature
- Stops when temperature is low
- Helps find global minimum by escaping local minima
Full Transcript
Simulated annealing with dual_annealing starts by picking a guess and temperature. It checks the function value (energy). Then it tries a new guess nearby. If the new guess is better, it accepts it. If worse, it might still accept it depending on temperature. Temperature lowers step by step. This repeats until temperature is very low. The final guess is the best found minimum. This method helps avoid getting stuck in bad local minimum by sometimes accepting worse guesses early on.