<div id="_htmlarea_default_style_" style="font:10pt arial,helvetica,sans-serif">We have done that at our research project using a thread-safe Epanet
as the Genome Evaluator.<br>An example follows below:<br><br>#include <pthread.h><br><br>int main(int argc, char* argv[])
{<br> GARandomSeed();<br><br> GARealAlleleSetArray alleles;<br> for (int i=0; i<
numberOfGenes; i++) {<br> alleles.add(your minimum value, your maximum value);<br>
}<br><br> GARealGenome genome(alleles, objective);<br> genome.initializer(::
initializer);<br> GASteadyStateGA ga(genome);<br> GASigmaTruncationScaling trunc;<br>
ga.minimize();<br> ga.populationSize(initialPopulation);<br> ga.nGenerations (generations);<br>
ga.pCrossover (crossoverProb);<br> ga.pMutation (mutationProb);<br> ga.pReplacement
(replacementProb);<br> ga.scaling(trunc);<br> ga.scoreFrequency(10);<br>
ga.flushFrequency(10);<br> ga.selectScores(GAStatistics::AllScores);<br><br> GAPopulation *pop = const_cast<
GAPopulation *> ( &(ga.population()) );<br> pop->evaluator(myEvaluator);<br><br> while(!ga.done())
{<br> ga.step();<br> genome =
ga.statistics().bestIndividual();<br> }<br>}<br><br><br><br>static void *thread_function(void *parameters) {<br>
((GARealGenome *)parameters)->evaluate();<br> return 0;<br>}<br><br>void myEvaluator(GAPopulation& p)
{<br><br> // this could be used to limit the maximum number of threads according to the number of cores you have, for
example<br> const int MAXTHREADS = 4; // or p.size()<br><br> pthread_t
task_threads[MAXTHREADS];<br><br> for(int i=0; i<p.size(); ) {<br><br> int nParallel =
MAXTHREADS;<br> if ((p.size() - i) < MAXTHREADS)<br>
nParallel = p.size()- i;<br><br> for (int j=0; j < nParallel; j++) {<br><br>
int pCreate = pthread_create(&task_threads[j], NULL, thread_function, (void *)
(GARealGenome*)&p.individual(i));<br> if(pCreate != 0) {<br>
std::cerr << "pthread_create ERROR: " << pCreate <<
std::endl;<br> }<br>
i++;<br> }<br><br> for (int j=0; j < nParallel; j++)
{<br> int pJoin = pthread_join(task_threads[j], NULL);<br>
if(pJoin != 0) {<br> std::cerr
<< "pthread_join ERROR: " << pJoin << std::endl;<br> }<br>
}<br> }<br>}<br><br>float objective(GAGenome& g) {<br><br> GARealGenome & genome =
(GARealGenome &)g;<br> long double fitness = your-thread-safe-evaluation-function(genome);<br> // or
implement your evaluation here<br><br> return(fitness);<br>}<br><br>void initializer(GAGenome& g) {<br>
GARealGenome & genome = (GARealGenome &)g;<br><br> // generate random values for coefficients<br> for(int
i = 0 ; i < numberOfGenes; i++) {<br> genome.gene(i, GARandomFloat(your minimum value, your maximum
value));<br> }<br>}<br><br>
--
<br>M.Sc. Ramon Hugo de Souza<br>Engenheiro de Software @ Projeto
SCADAPlus<br>www.scadaplus.com.br<br></div>