giacomelli / GeneticSharp

GeneticSharp is a fast, extensible, multi-platform and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).
MIT License
1.27k stars 332 forks source link

Improve Sudoku evolution to allow for lower population sizes #47

Closed jsboige closed 2 years ago

jsboige commented 5 years ago

As part of a recently merged pull-request, a new sample was added to solve Sudokus. The sample includes several strategies, and the better ones seem successful at solving most Sudokus. However, the only way I figured to ensure the population does not get stuck on a partial solution local maximum is to increase the population size, and that considerably for hard Sudokus. This results approximately in the corresponding metrics:

This is raises the following questions:

Some additional thoughts to get started:

giacomelli commented 5 years ago

Hi @jsboige, can you point me any of your unit tests that can be used to verify the items below?

Very Easy Sudoku: 250 chromosomes, <1 sec Easy Sudoku: 5000 chromosomes, 10 sec Medium Sudoku: 100000 chromosomes, 5-10 min Hard Sudoku: 300000 chromosomes, 1-2h

jsboige commented 5 years ago

Hi, those tests were not performed in the unit tests but rather manually in the GTK interface: You'll find the corresponding sudokus in the corresponding controller, and they correspond to Sudokus 1-4 loaded by default. I then had to set the population numbers manually. The corresponding unit-test only tests solving the easy sudoku, which it loads from the Test helper.

giacomelli commented 5 years ago

In your tests what option did you use on "Genetics" dropdown? screen shot 2019-01-16 at 10 18 45

giacomelli commented 5 years ago

@jsboige another question:

What is the expected fitness value return from SudokuFitness's Evaluate method when a board is resolved?

jsboige commented 5 years ago

In your tests what option did you use on "Genetics" dropdown? screen shot 2019-01-16 at 10 18 45

The cells genetics has pretty poor performances as compared to the permutations one, since the search space is much larger with no benefits AFAIK in terms of getting away from local maxima.

@jsboige another question:

What is the expected fitness value return from SudokuFitness's Evaluate method when a board is resolved?

The expected fitness is 0 for a solved Sudoku, (it counts the number of misplaced cells, I believe I placed a dual termination criterion accordingly in the unit tests).

For now, the evolution looks like a single collapse to all the local maxima closest to the initial population without any room for lateral exploration, meaning the only way I found to get to the solution is making sure the initial population is large enough so that the global maxima is reachable during that collapse. IMHO, this makes it an interesting problem to push this Framework's limits in terms of lateral exploration. Now the default parameter are quite aggressive as far as I understand, so with your knowledge of ways to soften that a bit, you might be able to find the right tuning.

Otherwise, what I believe might be missing in order to achieve some better performances and avoid the whole population collapsing to a local maximum is some kind of tabu search for parts of the population to force divergence and diversity.

Have you had a look at https://dev.heuristiclab.com/trac.fcgi/, which seems to be one of the main competing stacks in the .Net ecosystem? They do have those kind of advanced features that might be interesting to add to your Framework.

giacomelli commented 2 years ago

The performance of GeneticSharp has been improved a lot in the latest versions, but probably won't solve the points mentioned by you about sudoku's sample.

I'll close the issue right now, but feel free to re-open it with a PR associated. This PR should add benchmark methods to our BenchmarkDotNet project, This benchmarks should demonstrate the performance of the points you mentioned. Please, in the same PR add the changes (no breaking changes) suggestions to improve the performance on those points.