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

Is it possible to know the number of iterations in advance? #64

Closed wolfcuring closed 5 years ago

wolfcuring commented 5 years ago

Hi giacomelli,

On the platform I am using(based on .Net 4.5), it is required to setup the Number of Iterations in advance. Sounds ridiculous, but nothing I can do about it.

Is it possible to know that before the program is running?

I just successfully managed to run GeneticSharp 1.20 (latest version available for .Net 4.5) on my platform. And this seems to be the last obstacle.

Thank you in advance.

Warren

giacomelli commented 5 years ago

Hi Warren,

You can specified the maximum number of generations (iterations) using the GenerationNumberTermination:

ga.Termination = new GenerationNumberTermination(1000);

The GA will run 1000 generations then it will be terminated.

More details about available terminations on the wiki page: https://github.com/giacomelli/GeneticSharp/wiki/terminations

wolfcuring commented 5 years ago

Hi Diego,

Thank you for your reply. I think your suggestion is the best solution available.

Setting up number of iterations in advance is in any way a compromise of the power of GA or a waste of resources.

While, my platform allows me to know the possible number of iterations (by brute force search) in advance. So, I can set up a number of iterations as a fraction of the possible number of iterations (for example 10-15%). So, if I can know whether the GA has reached its FitnessStagnationTermination(100), I will still be confident of the result.

Then, is it possible to return whether FitnessStagnationTermination(100) is true when I am using ga.Termination = new GenerationNumberTermination(1000) ?

giacomelli commented 5 years ago

Hi,

There are some approaches to know whether FitnessStagnationTermination(100) is true when I am using ga.Termination = new GenerationNumberTermination(1000).

The first and most simple one is use a OrTermination:

var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
var stagnationTermination = new FitnessStagnationTermination(100);
ga.Termination = new OrTermination(new GenerationNumberTermination(1000), stagnationTermination );
ga.Start();
Console.WriteLine("Fitness stagnation was reached: {0}", stagnationTermination.HasReached(ga));

The drawback of this approach is that the GA can terminate before the generation number reach the 1000, because its fitness stagnated in 100 generations before that.

The other approach you can try is implement a version of FitnessStagnationTermination that only counts the fitness stagnation, but always return false in the PerformHasReached method.

wolfcuring commented 5 years ago

Thank you Diego,

Thank quite helpful.

wolfcuring commented 5 years ago

Hi Diego,

I am sorry that I am asking two much questions. Meanwhile it is a lot of fun playing around with the GeneticSharp and my platform.

My new finding is a good news: the GeneticSharp can actually circumvent the platform's NumberOfIterations argument. And stagnationTermination.HasReached(ga) has always returned True !

So, can we know exactly how many iterations has run by the GeneticSharp when termination is set to FitnessStagnationTermination(100)? If so, that would be a perfect solution.

Thank you Diego, appreciate your help!

giacomelli commented 5 years ago

Nice, good news!

You can know how many generations has run using the GeneticAlgorithm.GenerationsNumber property.

wolfcuring commented 5 years ago

Hi Diego, So, var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation); int iterationNumber= ga.GenerationsNumber*ga.Population.MaxSize; Console.WriteLine("Iterations Run: {0}", iterationNumber); will give a rough estimate of the total number of iterations?
While the code above does not seem to work.

giacomelli commented 5 years ago

Hi,

I don't know a way to estimate the total number of iterations, what GeneticAlgorithm.GenerationsNumber property do is say how many generation has ran.

wolfcuring commented 5 years ago

Hi Diego,

Using GenerationsNumber is good enough then. Thank you!

I have been ask many questions, appreciate your help !