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.26k stars 330 forks source link

Operators upgrade #88

Closed jsboige closed 1 year ago

jsboige commented 3 years ago

This pull request introduces an important change: it decouples chromosome creation and gene initialisation. As I understand, that was always the original intent, with for instance the IPopulation interface and TplPopulation implementation entirely based on the premise that those two should be decouples, but in practice, many chromosomes required to have a global context for individual genes creation (such as the TSP chromosome). Accordingly, it was tempting to initialise the genes within the constructor, which was mostly the case.

That in turn defeated entirely the original intent, because that meant every CreateNew call from a Crossover would trigger unneeded gene initialization with potentially great penalty costs. The new proper way to do global context-based initialization rather than individually through the usual GenerateGene method is through overriding the CreateGenes method from the base class.

I went through all existing code to update the new behavior. Note that it shouldn't break things if a chromosome does early gene intialisation in the constructor. It just does not have to do it anymore, and shouldn't for better performances, since CreateNew will be explicitly called on each chromosome during first generation initialization, yielding dual initialisation if that's the case.

giacomelli commented 1 year ago

Closing due to inactivity.

Feel free to fix the CI errors and reopen the PR.