Why would you want so solve a Sudoku with a Genetic Algorithm? That’s a fair question. Much easier would be to google on solving Sudoku’s and you’d probably have a method in 10 minutes. Like this one.
Okay, but suppose we didn’t know anything about sudoku’s (only the rules), or -more likely- we would have a slightly different problem, where googling wouldn’t help? Get it? ‘Because we can’ is also valid I think, though. Besides, I wanted to test my solving problem solving (no error) skills. If you’re relatively new to GA, it may help you understand.
Here’s a 4 star sudoku (I have not tested extensively with other sudoku’s)
First we have to define a Sudoku in Python. A list of integers is the easiest thing. One could argue for a 2D matrix (array), but this doesn’t add much. Besides, it makes implementing a Genetic Algoritmn complicated, because generally a genome is a 1D thing. Pyevolve has a lot more options for 1D lists that for 2D.
# define sudoku problem. 0's have to be filled
Now let’s implement the rules (one of each number in a row, column and block). I’ve added three functions to give me the rows, columns an blocks:
#function that returs a list consisting of row
for index in range(9):
def column(sd, columnnum):
for index in range(9):
#returns block numbered form left to right and then down
for indexX in range(3):
for indexY in range(3):
#this is the fitness function. No errors means the solution
for index in range (9):
for element in [row(sdsol,index),column(sdsol,index),block(sdsol,index)]:
for getal in [1,2,3,4,5,6,7,8,9]:
# check on double numbers
for i,getal in enumerate(sdsol):
# check on numbers that don't match the startnumbners
if getal!=sudoku[i] and sudoku[i]!=0:
That’s all for the Sudoku. Note that the errors function, an implementation of the rules, is also the fitness function for the genetic algorithm.
The brilliant thing about Python is all the libaries. Why re-invent it all ourselves? I’m using Pyevolve here.
Now, a genome (81 numbers) represents a possible solution. A genome is a list containing inputs for the fitness function. If you don’t understand this, you might want to read some more basics on GA first. It may be full of errors (double numbers), doesn’t matter, we’ll let the error function deal with that. Keeping the numbers between 1 and 9 helps the search speed. This is all there is to it, the code pretty much explains itself (considering you know the basics of GA):
genome = G1DList.G1DList(81) #defining the genome as a list of 81 integers
genome.evaluator.set(errors) #sets fitness function to errors
ga = GSimpleGA.GSimpleGA(genome)
ga.setMinimax(Consts.minimaxType["minimize"]) #0 errors wanted
Were trying to “minimze” the errors of course and if it is 0, we can stop (terminationCriteria). Now, the most important lesson for me was that a GA takes tuning. What’s your population size? What’s the crossover method? Probably the most used method is single point crossover, but does this work here? The mutation rate is also important. Too high will result in slow search and to low means you might not reach the solution in 10000 generations. Try some numbers if you like.
Here’s the result.
Gen. 4270 (42.70%): Max/Min/Avg Fitness(Raw) [17.68(23.00)/9.48(0.00)/14.73(14.73)]
Total time elapsed: 99.557 seconds.
List size: 81
6, 3, 8, 2, 5, 9, 7, 1, 4,
2, 5, 9, 7, 1, 4, 6, 3, 8,
7, 1, 4, 6, 3, 8, 2, 5, 9,
3, 6, 2, 8, 9, 5, 1, 4, 7,
8, 9, 5, 1, 4, 7, 3, 6, 2,
1, 4, 7, 3, 6, 2, 8, 9, 5,
5, 8, 6, 9, 2, 3, 4, 7, 1,
9, 2, 3, 4, 7, 1, 5, 8, 6,
4, 7, 1, 5, 8, 6, 9, 2, 3]
It took my laptop 100 seconds at best. Better that human, but many times worse than a proper Sudoku solving routine. I did not expect it to be fast, I was just wondering if I could make it work. And I forced myself not to Goole for it, until it worked. Of course people did it before me. But it looks like mine is doing quite well. On multiple runs, the time can differ a lot though, and sometimes it will not find it at all. I guess the algorithm runs into a local optimum and mutation is the only way to get it out again. It seems attractive to raise the mutation rate, but if you raise it a bit to much, multiple genes in a single genome will change and that will set you back. I think the thing to do would be, to have multiple populations and migration between the populations. Where have I heard that before? I’ll try that some other time.