This repo is the implementation of a memetic algorithm, including a genetic algorithm and local seach for a specific Minesweeper.
We defind Minesweeper as an optimization question: given a 2D board, our algorithm returns the minimum number of total clicks to uncover all the empty cells.
A typical Minesweeper is given a m * n board and at the beginning, all the cells are covered. The player should pick cells to open and there are usually two cases that could happen. Assume the cell been clicked is called focused_cell, the following two cases are given:
- focused_cell is safe/empty (not a mine). Then focused_cell would be uncovered and a number ranging from 1 or 8 will show in this cell. This number indicates the total count of the mines in its 8 directed neighbors (left, right, up, down, upper-left, upper-right, lower-left, lower-right). To be strict, if focused_cell is a corner cell, it has 3 neighbors; if focused_cell is a border cell, it has 5 neighbors and otherwise, it has 8 neighbors. The same uncovering rule will be applied to focused_cell’s neighbors until they do not have any neighbor that is a mine.
- focused_cell is a mine. The game will end immediately and the player fails.
- Start Generate random population of n chromosomes (suitable solutions for the problem)
- Fitness Evaluate the fitness f(x) of each chromosome x in the population
- New population: Create a new population by repeating following steps until the new population is complete
- Selection: Select two parent chromosomes from a population according to their fitness (the better fitness, the bigger chance to be selected)
- Crossover: With a crossover probability cross over the parents to form a new offspring (children). If no crossover was performed, offspring is an exact copy of parents.
- Mutation: With a mutation probability mutate new offspring at each locus (position in chromosome).
- Accepting: Place new offspring in a new population
- Replace Use new generated population for a further run of algorithm
- Test If the end condition is satisfied, stop, and return the best solution in current population
- Loop Go to step 2
Reference: Genetic Algorithms