Solving Sudoku with Simulated Annealing

Download Report

Transcript Solving Sudoku with Simulated Annealing

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

7 8 1 7 8 4 6 8 6 4 3 5 2 1 7 1 4 9 2 9 6 3 3 5 5 9 2 3 4 9 5 4 8 8 1 9 6 2 4 8 7 2 1 2 5 9 7 7 3 3 6 5 6 1 1 8 1 2 9 3 2 6 4 7 4 7 3 6 2 9 4 7 6 5 5 9 5 8 3 8 1

7 8 1 7 8 4 6 8 6 4 3 5 2 1 7 1 4 9 2 9 6 3 3 5 5 9 2 3 4 9 5 4 8 8 1 3 6 2 4 8 7 2 1 2 5 9 7 7 3 9 6 5 6 1 1 8 1 2 9 3 2 6 4 7 4 7 3 6 2 9 4 7 6 5 5 9 5 8 3 8 1

 ∆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