Solving Sudoku with Simulated Annealing

advertisement
Bret Wilson
 Analog of physical process of annealing in metals
 Heat, then cool slowly
 Start with random configuration
 Try nearby neighbors
 Lower temperature = become pickier
 Only consider states in which each small square has
exactly 1 of each digit 0 – 9
 Why? It’s trivial to solve either small squares, rows, or
columns by themselves – I chose small squares.
 -1 point for each different number in each row and
column
 Minimum (best) score = -9 x 9 x 2 = -162
 Swap any 2 digits in same small square -> candidate
 Accept new state if e-∆S/T – R > 0
 (Metropolis-Hastings Algorithm)
 R is random number in range [0,1]
 Always accept better states
 Accept worse states more often when T is higher
1
5
6
9
4
7
1
7
5
7
2
3
5
8
3
2
3
9
8
4
9
1
2
6
6
4
8
6
9
2
9
5
1
4
7
1
8
1
3
4
7
3
9
6
5
4
7
5
8
2
6
3
2
8
6
1
5
8
1
5
2
9
3
7
4
2
3
6
9
1
7
6
8
3
9
4
2
7
8
4
5
1
5
6
9
4
7
1
7
5
7
2
3
5
8
3
2
3
9
8
4
9
1
2
6
6
4
8
6
9
2
3
5
1
4
7
1
8
1
3
4
7
9
9
6
5
4
7
5
8
2
6
3
2
8
6
1
5
8
1
5
2
9
3
7
4
2
3
6
9
1
7
6
8
3
9
4
2
7
8
4
5
 ∆S = -1
 e-∆S/T will always be > 1, no matter what T and R are.
 So we accept.
 What should our initial value for T be?
 Can find by trial and error
 How fast should we decrease T?
 Linear: T <= T – i where i > 0
 Geometric progression: T <= c*T where 0 < c < 1
 Change T based on current score
currState <= createInitialState()
currScore <= score(currState)
bestState <= currState
bestScore <= currScore
while (T > END)
newState <= generateNeighbor(currState)
newScore <= score(newState)
if (exp((currScore - newScore)/T) - rand(0,1) > 0)
currState <= newState
currScore <= newScore
if (currScore < bestScore)
bestState <= currState
bestScore <= currScore
T <= c*T
return bestState
 Good: Quickly finds a minimum
 Bad: May not find global minimum (best solution)
 Increasing temperature makes it slower, but less likely
we will get stuck in local minimum
 Carr, Roger. "Simulated Annealing." From MathWorld-
-A Wolfram Web Resource, created by Eric W.
Weisstein.
http://mathworld.wolfram.com/SimulatedAnnealing.
html
 “Simulated Annealing.” Wikipedia.
http://en.wikipedia.org/wiki/Simulated_annealing
 “Simulated Annealing Applet.” Heaton Research.
http://www.heatonresearch.com/articles/64/page1.ht
ml
Download