Simulated Annealing: Teaching Metal to Forget
Annealing copper wire means heating it until the crystal lattice relaxes, then cooling it slowly so the atoms settle into low-energy arrangements. The metal becomes softer, more workable. Kirkpatrick borrowed this in 1983 for combinatorial optimization: start hot (accept bad moves freely), cool gradually (become pickier), end cold (locked into a good solution).
The acceptance probability is exp(-ΔE / T) where ΔE is how much worse a candidate solution is, and T is temperature. Early on, high T means you’ll take a 10% worse solution with 90% probability. Late, low T means you reject anything that doesn’t improve. This lets you escape local minima by occasionally climbing uphill.
package main
import ("fmt"; "math"; "math/rand")
func anneal(energy func(float64) float64, x0, T0, alpha float64, steps int) float64 {
x, E := x0, energy(x0)
for T := T0; steps > 0; steps, T = steps-1, T*alpha {
xNew := x + rand.NormFloat64()*T
dE := energy(xNew) - E
if dE < 0 || rand.Float64() < math.Exp(-dE/T) {
x, E = xNew, energy(xNew)
}
}
return x
}
func main() {
f := func(x float64) float64 { return math.Pow(x-3.2, 2) + math.Sin(x*5)*0.3 }
fmt.Printf("%.4f\n", anneal(f, 0, 5.0, 0.95, 1000))
}
function anneal(energy, x0, T0, alpha, steps)
local x, E = x0, energy(x0)
for i = 1, steps do
local T = T0 * alpha^i
local xNew = x + (math.random()*2 - 1) * T
local dE = energy(xNew) - E
if dE < 0 or math.random() < math.exp(-dE / T) then
x, E = xNew, energy(xNew)
end
end
return x
end
function f(x) return (x - 3.2)^2 + math.sin(x*5)*0.3 end
print(string.format("%.4f", anneal(f, 0, 5.0, 0.95, 1000)))
The cooling schedule (T *= alpha each step) is critical. Cool too fast and you freeze into the first decent valley you find. Too slow and you waste time wandering. Metallurgists measured this in hours per degree; we measure it in iteration budgets. Both are watching a system lose its freedom to explore, trading randomness for stability. Wire training does the same—early bends are forgiving, late adjustments risk snapping lignified wood.