21 #include "gas/simplega.h"
22 #include "core/reproduction.h"
23 #include "core/genome.h"
24 #include "core/evaluation.h"
25 #include <QThreadPool>
26 #include <QtConcurrentMap>
27 using namespace QtConcurrent;
43 foreach( SimpleGA::evaluationThread* e, evalThreads ) {
56 qWarning(
"Setting number of Generations to ZERO!! Check your config file" );
75 d.
describeInt(
"numThreads" ).
limits( 1, 32 ).
help(
"Number of threads to parallelize the evaluation of individuals" );
76 d.
describeInt(
"ngenerations" ).
limits( 1, INT_MAX ).
def( 1000 ).
help(
"Number of the generations of the evolutionary process" );
83 if ( numThreads < 1 ) {
84 qWarning(
"The number of Threads must be greater than one!!" );
87 "SimpleGA::setNumThreads",
88 "This method can only called before initialize of SimpleGA" );
90 "SimpleGA::setNumThreads",
91 "This method must be called after an Evaluation object has been setted by SimpleGA::setEvaluation" );
102 this->fitfunc->
setGA(
this );
111 QVector<Evaluation*> ev;
112 foreach( SimpleGA::evaluationThread* e, evalThreads ) {
113 ev.append( e->eval );
120 this->reprod->
setGA(
this );
130 "SimpleGA::initialize",
131 "You have to setup the Evaluation object of SimpleGA (Fitness Function)" );
133 "SimpleGA::initialize",
134 "You have to setup the Reproduction operator of SimpleGA" );
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(
this,
fitfunc ) );
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() );
167 evalThreads[i]->eval->initialize(
genome()->at( evalThreads[i]->
id ) );
168 evalThreads[i]->blocked =
false;
173 nextGeneration =
true;
174 if ( numThreadv == 1 ) {
176 evalThreads[0]->runStep();
178 QFuture<void> future = map( evalThreads, SimpleGA::runStepWrapper );
179 future.waitForFinished();
181 if ( nextGeneration ) {
186 case nextGeneration_pass1:
196 case nextGeneration_pass2: {
213 qFatal(
"Default switch in SimpleGA::gaStep" );
237 : parent(p), id(0), blocked(false) {
238 eval = eProto->
clone();
243 SimpleGA::evaluationThread::~evaluationThread() {
248 void SimpleGA::evaluationThread::runStep() {
253 parent->nextGeneration =
false;
254 eval->evaluateStep();
255 if ( eval->isEvaluationDone() ) {
256 int nextIdSeq = idSeq + 1;
258 eval->genotype()->setRank( eval->genotype()->fitness() );
259 if ( nextIdSeq >= sequence.size() ) {
264 int nextId = sequence[ idSeq ];
266 eval->initialize( parent->genome()->at(
id ) );
270 void SimpleGA::runStepWrapper( SimpleGA::evaluationThread* e ) {