This project implements a genetic algorithm to solve a Sudoku puzzle.
The project consists of the following main parts:
- Defining genes and chromosomes
- Making the first generation
- Fitness function
- Crossover and mutation
- Implementing the genetic algorithm
A gene represents a row of the Sudoku puzzle and is a permutation of the set {1,2,3,4,5,6,7,8,9}. A chromosome consists
of 9 genes, each gene representing a row of the actual Sudoku puzzle. The make_gene
function creates a gene, while
the make_chromosome
function creates a chromosome.
The make_population
function creates a population of a specified size. Each member of the population is a Sudoku
puzzle represented as a chromosome.
The fitness function calculates how "fit" a chromosome (puzzle) is based on the following criteria:
- For each column: subtract (number of times a number is seen) - 1 from the fitness for that number
- For each 3x3 square: subtract (number of times a number is seen) - 1 from the fitness for that number
The higher the fitness, the closer the puzzle is to being solved.
The crossover function takes two chromosomes as input and makes two new chromosomes by combining them. This crossover function decides the parent of each gene separately, so the result is independent of the location of the genes.
The mutation function applies a random change to a chromosome with a specified probability. In this case, the mutation function randomly changes a gene in a chromosome.
def mutation(ch, pm, initial):
for i in range(9):
x = rndm.randint(0, 100)
if x < pm * 100:
ch[i] = make_gene(initial[i])
return ch
The genetic algorithm consists of the following steps:
- Read the puzzle from the input file.
- Create the initial population.
- Calculate the fitness of each chromosome.
- Select the mating pool from the current population.
- Generate the offspring from the mating pool using crossover and mutation.
- Replace the current population with the offspring.
- Repeat steps 3-6 for a specified number of generations.