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" );
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" );
143 for(
int i=0; i<evalThreads.size(); i++ ) {
144 delete (evalThreads[i]);
147 for(
int i=0; i<numThreadv; i++ ) {
148 evalThreads.append(
new evaluationThread(
fitfunc ) );
149 evalThreads.last()->eval->initGeneration(
generation() );
152 QThreadPool::globalInstance()->setMaxThreadCount( numThreadv );
158 for(
int i=0; i<numThreadv; i++ ) {
159 evalThreads[i]->sequence.clear();
161 for(
int i=0; i<(int)
genome()->
size(); i++ ) {
162 evalThreads[ i%numThreadv ]->sequence.append( i );
164 for(
int i=0; i<numThreadv; i++ ) {
165 evalThreads[i]->idSeq = 0;
166 evalThreads[i]->id = evalThreads[i]->sequence[ evalThreads[i]->idSeq ];
167 evalThreads[i]->eval->setGenome(
genome() );
172 (*future) = map( evalThreads, ParallelGA::runStepWrapper );
176 if ( future->isFinished() ) {
180 case nextGeneration_pass1:
184 for(
int i=0; i<numThreadv; i++ ) {
185 evalThreads[i]->eval->endGeneration(
generation() );
195 case nextGeneration_pass2: {
205 for(
int i=0; i<numThreadv; i++ ) {
206 evalThreads[i]->eval->initGeneration(
generation() );
215 qFatal(
"Default switch in ParallelGA::gaStep" );
238 ParallelGA::evaluationThread::evaluationThread(
Evaluation* eProto )
240 eval = eProto->
clone();
242 eval->setGA( eProto->
GA() );
245 ParallelGA::evaluationThread::~evaluationThread() {
250 void ParallelGA::evaluationThread::runStep() {
253 eval->initialize( eval->GA()->genome()->at(
id ) );
255 int nextIdSeq = idSeq + 1;
257 eval->genotype()->setRank( eval->genotype()->fitness() );
258 if ( nextIdSeq >= sequence.size() ) {
262 int nextId = sequence[ idSeq ];
267 void ParallelGA::runStepWrapper( ParallelGA::evaluationThread* e ) {