[galib] Fwd: Parallelization of genome evaluation

Paul Smith phhs80 at gmail.com
Fri Sep 2 19:44:20 EDT 2011


Thanks a lot, Ramon! It seems quite useful!

I am going now to study your example, and I will be back if I have questions.

Paul


On Fri, Sep 2, 2011 at 10:49 PM, Ramon Hugo de Souza
<ramon at scadaplus.com.br> wrote:
> We have done that at our research project using a thread-safe Epanet as the
> Genome Evaluator.
> An example follows below:
>
> #include <pthread.h>
>
> int main(int argc, char* argv[]) {
>     GARandomSeed();
>
>     GARealAlleleSetArray alleles;
>     for (int i=0; i< numberOfGenes; i++) {
>         alleles.add(your minimum value, your maximum value);
>     }
>
>     GARealGenome genome(alleles,  objective);
>     genome.initializer(:: initializer);
>     GASteadyStateGA ga(genome);
>     GASigmaTruncationScaling trunc;
>     ga.minimize();
>     ga.populationSize(initialPopulation);
>     ga.nGenerations (generations);
>     ga.pCrossover (crossoverProb);
>     ga.pMutation (mutationProb);
>     ga.pReplacement (replacementProb);
>     ga.scaling(trunc);
>     ga.scoreFrequency(10);
>     ga.flushFrequency(10);
>     ga.selectScores(GAStatistics::AllScores);
>
>     GAPopulation *pop = const_cast< GAPopulation *> ( &(ga.population()) );
>     pop->evaluator(myEvaluator);
>
>     while(!ga.done()) {
>         ga.step();
>         genome = ga.statistics().bestIndividual();
>     }
> }
>
>
>
> static void *thread_function(void *parameters) {
>     ((GARealGenome *)parameters)->evaluate();
>     return 0;
> }
>
> void myEvaluator(GAPopulation& p) {
>
>     // this could be used to limit the maximum number of threads according
> to the number of cores you have, for example
>     const int MAXTHREADS = 4; // or p.size()
>
>     pthread_t task_threads[MAXTHREADS];
>
>     for(int i=0; i<p.size(); ) {
>
>         int nParallel = MAXTHREADS;
>         if ((p.size() - i) < MAXTHREADS)
>             nParallel = p.size()- i;
>
>         for (int j=0; j < nParallel; j++) {
>
>             int pCreate = pthread_create(&task_threads[j], NULL,
> thread_function, (void *) (GARealGenome*)&p.individual(i));
>             if(pCreate != 0) {
>                 std::cerr << "pthread_create ERROR: " << pCreate <<
> std::endl;
>             }
>             i++;
>         }
>
>         for (int j=0; j < nParallel; j++) {
>             int pJoin = pthread_join(task_threads[j], NULL);
>             if(pJoin != 0) {
>                 std::cerr << "pthread_join ERROR: " << pJoin << std::endl;
>             }
>         }
>     }
> }
>
> float objective(GAGenome& g) {
>
>     GARealGenome & genome = (GARealGenome  &)g;
>     long double fitness = your-thread-safe-evaluation-function(genome);
>     // or implement your evaluation here
>
>     return(fitness);
> }
>
> void initializer(GAGenome& g) {
>     GARealGenome & genome = (GARealGenome &)g;
>
>     // generate random values for coefficients
>     for(int i = 0 ; i < numberOfGenes; i++) {
>         genome.gene(i, GARandomFloat(your minimum value, your maximum
> value));
>     }
> }
>
> --
> M.Sc. Ramon Hugo de Souza
> Engenheiro de Software @ Projeto SCADAPlus
> www.scadaplus.com.br
>
>
> ---------- Forwarded message ----------
> From: Paul Smith <phhs80 at gmail.com>
> To: galib at mit.edu
> Date: Fri, 2 Sep 2011 10:46:34 +0100
> Subject: [galib] Parallelization of genome evaluation
> Dear All,
>
> Is it possible to parallelize the genome evaluation?
>
> Thanks in advance,
>
> Paul
> _______________________________________________
> galib mailing list
> galib at mit.edu
> http://mailman.mit.edu/mailman/listinfo/galib
>
> _______________________________________________
> galib mailing list
> galib at mit.edu
> http://mailman.mit.edu/mailman/listinfo/galib
>
>




More information about the galib mailing list