Genetic algorithms to solve Sudoku puzzels
Uni work has been non stop at the moment. It's a funny thing when you see several other groups of people coming into the same labs to work on assignments on weekends and consultation week. This weeks flavour of work for most was either Enterprise Java (I cannot wait till this one is over) and AI.
I'll leave my thoughts on Enterprise Java for another time (because it will most likely turn into a rant ;)
Current explorations into how genetic algorithms work has been quite interesting. The idea of borrowing ideas from nature (evolutionary pressure, recombination and mutation) to solve problems is an interesting one. Especially for us programmers :)
My initial thoughts behind creating a sudoku solver was something like:
- Sudoku class: to have a main Sudoku class to handle file reading (sudoku puzzels), displaying the solution etc
- SudokuAlgorithm interface: providing a generic solve(Problem) interface for different algorithms to implement
- Problem class: stored the base problem (an 81 integer array (9x9) along with some other flags like 'isSolved', 'hasSolution' etc
- SudokuException: Sudoku exceptions :)
- Population class: A population class for things like InitPopulation, GetFittest, Mutate, Evaluate
- GA class: actual implementation of a GA
#region Properties public Gene[] Genes public Gene[] NextColumn public Gene[] NextRow public Gene[] NextBlock public List<Gene> UnderRepresentedGenes public List<Gene> OverRepresentedGenes #endregion #region Public Methods public void ResetRow(); public void ResetColumn(); public void ResetSquare(); public int GeneFrequency(Gene gene); public void GeneFrequenceyUpdate(); public void AddGene(Gene gene); public void SwitchGenes(Gene gene, Gene, gene2); public bool isUnderRepresented(Gene gene); public bool isOverRepresented(Gene gene); public void UpdateGeneFreqList(); public void ReplaceGene(int index, Gene gene); public void ReplaceGene(Gene toReplace, Gene gene); public object Clone();From above, you can see that you can actualy get quite a lot of information from the chromosome and have it do some nice things. When a chromosome becomes unbalanced, it essentialy knows how to balance itself by looking at how many under and over represented genes it has, then switching these genes around until equilibrium is achieved. Because a chromosome knows specific gene informatoin about rows, columns and blocks. Instead of the traditional crossover form of recombination, blocks, columns or rows could be recombined from each parent producing potentially better offspring. Especially if you do some pre analysis and take blocks etc that have a good fitness rating. Some solutions may never be found because a tradional crossover may never get the correct rows, blocks or columns from one part of the board into another part where it is actually needed. This method could help and thus prove to be very powerful inded. At this point I am now starting to solve basic sudoku puzzels with 20, 30 or 40 missing numbers. The biggest problem I am having is stagnation or convergence. My population often gets to around 60-70% of the solution before effectively cloning or converging together. Whilst mutation does help introduce new gene informatoin back into the pool, it's often not enough when you have reached total convergence. Unless of course you implemented a form of dynamic mutation that looked at how close the population was from convergence. Increased mutation as the population converged to a single chromosome point, or backed off mutation when there was a large amount of diversity already. This is something I might look at. All in all, from fitness functions, selection processes, recombination and mutation effects to smart chromosome structures and repair functions. Population sizes to generations running, there are a myriad of things that affect if, and how quickly a GA can get to a solution (if at all).

1 Comments:
Hello,
That is a real challenging sudoku puzzle to resolve.
Post a Comment
Links to this post:
Create a Link
<< Home