This is a project to use Genetic Algorithm to solve the Sudoku problem.
Several Concepts:
- Population
- Chromosome (also called Individual)
- Gene
Steps for Genetic Algorithm
- Generate initial population with random gene in each indiviual chromosome.
- Define the fitness function for Sudoku, which I used the valid solution count as the fitness value.
- To generate the next generation, you need do crossover, mutation. In my solution, I picked top 10 solutions and randomly pick 2 of them as parents, then crossover the last row, at last, to swap random 2 values for the mutation.
- Once generated a new generation, then loop above steps, until you have complete solution.
Places that you can tune.
- Crossover method, like swap with last several columns/rows, or alternatively take parentss' gene.
- Crossover count, like the percentage of chromosome for the crossover.
- Mutation method, like I provided, randomly change one field with a random word, or you can randomly swap 2 fields.
- Mutation count, like how many fields need to be mutated.
- Population Size.
- Parent Selection method, like top 10, or top 20.
Genetic algorithm, doesn't guarantee you will generate the final result within certain generations, it largely depends on your crossover and mutation method, it's has certain randomness there, but from the result, it shows if you choose good parents, the next generation mostly will getting better and better.
go mod tidy
go run -v ./...
Result:
0th generation:
Matrix: [R O D W W D O R R O W W W O R D] Valid Solution: 6
1th generation:
Matrix: [R O D W W D O R R O W D W O R D] Valid Solution: 7
2th generation:
Matrix: [R O D W W D O R R O W D D W R O] Valid Solution: 10
3th generation:
Matrix: [R O D W W D O R R O W D D W R O] Valid Solution: 10
4th generation:
Matrix: [R O D W W D O R R O W D D W R O] Valid Solution: 10
5th generation:
Matrix: [R O D W W D O R R O W D D W R O] Valid Solution: 10
6th generation:
Result:
[R O D W]
[W D O R]
[O R W D]
[D W R O]
Total Valid Solutions: 12
START
Generate the initial population
REPEAT
Compute fitness for all individual
Selection for parents
Crossover for next generation
Mutation each individual of the next generation
UNTIL population has converged
STOP
- After crossover several generations, all the individuals is going to be the same, so the mutation becomes to be important.
- https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3#:~:text=A%20genetic%20algorithm%20is%20a,offspring%20of%20the%20next%20generation.
- https://www.researchgate.net/publication/224180108_Solving_Sudoku_with_genetic_operations_that_preserve_building_blocks
- https://github.com/ctjacobs/sudoku-genetic-algorithm/blob/master/sudoku.py
