Mario Luis
Mario Luis

Reputation: 93

GAlib C++ Get the best of each generation

I'm working with GAlib from http://lancet.mit.edu/ga/ library in C++. I've created a typical genetic algorithm with this code:

GA1DBinaryStringGenome genoma(trips.size(), Genotype::evaluator);
GASimpleGA ga(genoma);
ga.populationSize(popSize);
ga.nGenerations(genCant);
ga.pMutation(0.03);
ga.pCrossover(0.90);
ga.evolve(); // Launch

Then I get the best individual with

wladi << ga.statistics().bestIndividual();

Which is pretty much the standard. But my question is:

How can I get an Array with the best fitness of each generation?

Upvotes: 1

Views: 341

Answers (1)

Eduard
Eduard

Reputation: 21

I am dealing with the same problem right now. The only work-around I came up with is to create my own terminator function (in the main):

GABoolean GATerminateUponGenerationWithStatePrintout(GAGeneticAlgorithm &ga)
{
    cout << "Generation " << ga.generation() << " - best individual " << ga.statistics().bestIndividual() << endl;
    return(ga.generation() < ga.nGenerations() ? gaFalse : gaTrue);
}

And then link the customized termination to your GA object:

GASimpleGA ga(genoma);
ga.terminator(GATerminateUponGenerationWithStatePrintout);

This way I can get a link to when the GA checks for termination condition at the end of each generation.

Upvotes: 2

Related Questions