21 #include "gas/parallelga.h"
22 #include "core/reproduction.h"
23 #include "core/genome.h"
24 #include "core/evaluation.h"
25 #include "configurationhelper.h"
26 #include <QThreadPool>
27 #include <QtConcurrentMap>
28 using namespace QtConcurrent;
32 ParallelGA::ParallelGA()
41 future =
new QFuture<void>();
45 foreach( ParallelGA::evaluationThread* e, evalThreads ) {
73 Descriptor d =
addTypeDescription( type,
"Parallel Genetic Algorithm",
"Respect to SimpleGA and others type of Genetic Algorithm, the implementation of the parallelization is more efficient" );
74 d.
describeInt(
"numThreads" ).
limits( 1, 32 ).
def(1).
help(
"Number of threads to parallelize the evaluation of individuals" );
75 d.
describeInt(
"ngenerations" ).
limits( 1, INT_MAX ).
def( 1000 ).
help(
"Number of the generations of the evolutionary process" );
82 if ( numThreads < 1 ) {
83 qWarning(
"The number of Threads must be greater than one!!" );
86 "ParallelGA::setNumThreads",
87 "This method can only called before initialize of ParallelGA" );
89 "ParallelGA::setNumThreads",
90 "This method must be called after an Evaluation object has been setted by ParallelGA::setEvaluation" );
101 this->fitfunc->
setGA(
this );
110 QVector<Evaluation*> ev;
111 foreach( ParallelGA::evaluationThread* e, evalThreads ) {
112 ev.append( e->eval );
119 this->reprod->
setGA(
this );
129 "ParallelGA::initialize",
130 "You have to setup the Evaluation object of ParallelGA (Fitness Function)" );
132 "ParallelGA::initialize",
133 "You have to setup the Reproduction operator of ParallelGA" );
142 for(
int i=0; i<evalThreads.size(); i++ ) {
143 delete (evalThreads[i]);
146 for(
int i=0; i<numThreadv; i++ ) {
147 evalThreads.append(
new evaluationThread(
fitfunc ) );
148 evalThreads.last()->eval->initGeneration(0);
151 QThreadPool::globalInstance()->setMaxThreadCount( numThreadv );
157 for(
int i=0; i<numThreadv; i++ ) {
158 evalThreads[i]->sequence.clear();
160 for(
int i=0; i<(int)
genome()->
size(); i++ ) {
161 evalThreads[ i%numThreadv ]->sequence.append( i );
163 for(
int i=0; i<numThreadv; i++ ) {
164 evalThreads[i]->idSeq = 0;
165 evalThreads[i]->id = evalThreads[i]->sequence[ evalThreads[i]->idSeq ];
166 evalThreads[i]->eval->setGenome(
genome() );
171 (*future) = map( evalThreads, ParallelGA::runStepWrapper );
175 if ( future->isFinished() ) {
179 case nextGeneration_pass1:
183 for(
int i=0; i<numThreadv; i++ ) {
184 evalThreads[i]->eval->endGeneration(
generation() );
192 case nextGeneration_pass2: {
202 for(
int i=0; i<numThreadv; i++ ) {
203 evalThreads[i]->eval->initGeneration(
generation() );
212 qFatal(
"Default switch in ParallelGA::gaStep" );
235 ParallelGA::evaluationThread::evaluationThread(
Evaluation* eProto )
237 eval = eProto->
clone();
239 eval->setGA( eProto->
GA() );
242 ParallelGA::evaluationThread::~evaluationThread() {
247 void ParallelGA::evaluationThread::runStep() {
250 eval->initialize( eval->GA()->genome()->at(
id ) );
252 int nextIdSeq = idSeq + 1;
254 eval->genotype()->setRank( eval->genotype()->fitness() );
255 if ( nextIdSeq >= sequence.size() ) {
259 int nextId = sequence[ idSeq ];
264 void ParallelGA::runStepWrapper( ParallelGA::evaluationThread* e ) {