14 this->population_ =
static_cast<PopulationP
> (
new Population);
15 this->crossover_ =
static_cast<CrossoverP
> (
new Crossover);
16 this->mutation_ =
static_cast<MutationP
> (
new Mutation);
19 XMLNode::setGlobalOptions(XMLNode::char_encoding_legacy);
21 bInitialized_ =
false;
22 bCommandLine_ =
false;
23 bAlgorithmSet_ =
false;
24 bGenotypeSet_ =
false;
25 bEvaluatorSet_ =
false;
26 bLoadMilestone_ =
false;
29 bBatchSingleMilestone_ =
false;
30 bBatchWriteStats_ =
false;
35 this->mAlgorithms_[alg->getName()] = alg;
37 this->mAlgorithms_[alg->getName()] = alg;
39 this->mAlgorithms_[alg->getName()] = alg;
41 this->mAlgorithms_[alg->getName()] = alg;
42 alg =
static_cast<AlgorithmP
> (
new XCS);
43 this->mAlgorithms_[alg->getName()] = alg;
45 this->mAlgorithms_[alg->getName()] = alg;
47 this->mAlgorithms_[alg->getName()] = alg;
49 this->mAlgorithms_[alg->getName()] = alg;
51 this->mAlgorithms_[alg->getName()] = alg;
53 this->mAlgorithms_[alg->getName()] = alg;
54 alg =
static_cast<AlgorithmP
> (
new Clonalg);
55 this->mAlgorithms_[alg->getName()] = alg;
56 alg =
static_cast<AlgorithmP
> (
new OptIA);
57 this->mAlgorithms_[alg->getName()] = alg;
59 this->mAlgorithms_[alg->getName()] = alg;
60 alg =
static_cast<AlgorithmP
> (
new AlgNSGA2);
61 this->mAlgorithms_[alg->getName()] = alg;
63 this->mAlgorithms_[alg->getName()] = alg;
68 this->mAlgorithms_[alg->getName()] = alg;
70 this->mAlgorithms_[alg->getName()] = alg;
72 this->mAlgorithms_[alg->getName()] = alg;
77 this->mGenotypes_[gen->getName()] = gen;
79 this->mGenotypes_[gen->getName()] = gen;
81 this->mGenotypes_[gen->getName()] = gen;
83 this->mGenotypes_[gen->getName()] = gen;
85 this->mGenotypes_[gen->getName()] = gen;
87 this->mGenotypes_[gen->getName()] = gen;
93 this->allTerminationOps_.push_back(op);
95 this->allTerminationOps_.push_back(op);
97 this->allTerminationOps_.push_back(op);
99 this->allTerminationOps_.push_back(op);
101 this->allTerminationOps_.push_back(op);
104 this->registry_ =
static_cast<RegistryP
> (
new Registry);
105 this->logger_ =
static_cast<LoggerP
> (
new Logger);
107 this->migration_ =
static_cast<MigrationP
> (
new Migration);
115void State::registerParameters()
118 registry_->registerEntry(
"milestone.interval", (voidP) (
new uint(0)), ECF::UINT,
119 "milestone saving interval in generations; 0: save only at the end (default: 0)");
120 registry_->registerEntry(
"milestone.filename", (voidP) (
new std::string(
"milestone.txt")), ECF::STRING,
121 "milestone file (if stated) stores all the population (default: none)");
122 registry_->registerEntry(
"batch.repeats", (voidP) (
new uint(0)), ECF::UINT,
123 "number of independent runs to perform (default: 1)");
124 registry_->registerEntry(
"batch.singlemilestone", (voidP) (
new uint(0)), ECF::UINT,
125 "use only one milestone file for all the batch runs (1) or one for each run (0) (default: 0)");
126 registry_->registerEntry(
"batch.statsfile", (voidP) (
new std::string(
"")), ECF::STRING,
127 "output batch end of run stats in a single file (default: none)");
130 registry_->registerEntry(
"milestone.generation_", (voidP) (
new uint(0)), ECF::UINT);
131 registry_->registerEntry(
"milestone.elapsedtime_", (voidP) (
new uint(0)), ECF::UINT);
132 registry_->registerEntry(
"batch.remaining_", (voidP) (
new uint(0)), ECF::UINT);
133 registry_->registerEntry(
"batch.logfile_", (voidP) (
new std::string(
"")), ECF::STRING);
135 ECF_LOG(
this, 4,
"Registering parameters: algorithms, operators");
139 for(itAlg = mAlgorithms_.begin(); itAlg != mAlgorithms_.end(); ++itAlg)
140 itAlg->second->registerParameters(state_);
143 mAlgorithms_.begin()->second->registerParallelParameters(state_);
146 for(uint i = 0; i < allTerminationOps_.size(); i++)
147 allTerminationOps_[i]->registerParameters(state_);
150 for(uint i = 0; i < allUserOps_.size(); i++)
151 allUserOps_[i]->registerParameters(state_);
153 mutation_->registerParameters(state_);
154 crossover_->registerParameters(state_);
155 randomizer_->registerParameters(state_);
156 population_->registerParameters(state_);
157 logger_->registerParameters(state_);
158 migration_->registerParameters(state_);
159 evalOp_->registerParameters(state_);
167void State::readParameters()
169 ECF_LOG(
this, 4,
"Rading parameters from the Registry");
172 if(registry_->isModified(
"milestone.filename"))
173 bSaveMilestone_ =
true;
175 bSaveMilestone_ =
false;
177 voidP sptr = registry_->getEntry(
"milestone.interval");
178 milestoneInterval_ = *((uint*) sptr.get());
180 sptr = registry_->getEntry(
"milestone.filename");
181 milestoneFilename_ = *((std::string*) sptr.get());
184 if(registry_->isModified(
"milestone.generation_"))
185 bLoadMilestone_ =
true;
187 bLoadMilestone_ =
false;
189 sptr = registry_->getEntry(
"milestone.generation_");
190 milestoneGeneration_ = *((uint*) sptr.get());
192 sptr = registry_->getEntry(
"milestone.elapsedtime_");
193 milestoneElapsedTime_ = *((uint*) sptr.get());
196 sptr = registry_->getEntry(
"batch.repeats");
197 batchRepeats_ = *((uint*) sptr.get());
199 sptr = registry_->getEntry(
"batch.remaining_");
200 batchRemaining_ = *((uint*) sptr.get());
202 sptr = registry_->getEntry(
"batch.statsfile");
203 batchStatsFile_ = *((std::string*) sptr.get());
205 sptr = registry_->getEntry(
"batch.logfile_");
206 batchLogFile_ = *((std::string*) sptr.get());
208 sptr = registry_->getEntry(
"batch.singlemilestone");
209 bBatchSingleMilestone_ = (*((uint*) sptr.get()) % 2) ?
true:
false;
211 if(registry_->isModified(
"batch.repeats") && batchRepeats_ > 1)
214 bBatchStart_ =
false;
221void State::dumpParameters(std::string fileName,
bool addClear)
223 XMLNode xMainNode = XMLNode::createXMLTopNode(
"ECF");
224 xMainNode.addAttribute(
"version", ECF_VERSION.c_str());
225 xMainNode.addClear(
"ECF parameter dump - list of all parameters",
"<!-- ",
" -->");
227 this->state_ = this->getState();
229 this->registerParameters();
232 for(gen_iter itGen = mGenotypes_.begin(); itGen != mGenotypes_.end(); ++itGen) {
233 itGen->second->setGenotypeId(0);
234 itGen->second->registerParameters(state_);
237 std::vector<CrossoverOpP> crx = itGen->second->getCrossoverOp();
238 for(uint iOp = 0; iOp < crx.size(); iOp++) {
239 crx[iOp]->myGenotype_ = itGen->second;
240 crx[iOp]->registerParameters(state_);
244 std::vector<MutationOpP> mut = itGen->second->getMutationOp();
245 for(uint iOp = 0; iOp < mut.size(); iOp++) {
246 mut[iOp]->myGenotype_ = itGen->second;
247 mut[iOp]->registerParameters(state_);
253 this->registry_->dumpEntries(xRegistry);
256 XMLNode xAlgorithms = XMLNode::createXMLTopNode(NODE_ALGORITHM);
257 xMainNode.addChild(xAlgorithms);
261 for(itAlg = mAlgorithms_.begin(); itAlg != mAlgorithms_.end(); ++itAlg) {
264 std::string algorithmName = itAlg->first;
265 XMLNode xAlgorithm = XMLNode::createXMLTopNode(algorithmName.c_str());
266 xAlgorithms.addChild(xAlgorithm);
269 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
270 XMLNode child = xRegistry.getChildNode(i);
271 std::string key = child.getAttributeValue();
273 if(key.compare(0, algorithmName.size(), algorithmName) == 0) {
275 XMLNode xEntry = child.deepCopy();
276 child.deleteNodeContent();
280 std::string key = xEntry.getAttribute(
"key");
281 key.erase(0, algorithmName.length() + 1);
282 xEntry.updateAttribute(key.c_str(), NULL,
"key");
283 xAlgorithm.addChild(xEntry);
286 std::string desc = xEntry.getAttribute(
"desc");
287 if(addClear && desc !=
"") {
288 xAlgorithm.addClear(desc.c_str(),
"<!-- ",
" -->");
291 xEntry.deleteAttribute(
"desc");
297 XMLNode xGenotypes = XMLNode::createXMLTopNode(NODE_GENOTYPE);
298 xMainNode.addChild(xGenotypes);
301 for(gen_iter itGen = mGenotypes_.begin(); itGen != mGenotypes_.end(); ++itGen) {
304 std::string genotypeName = itGen->first;
305 XMLNode xGenotype = XMLNode::createXMLTopNode(genotypeName.c_str());
306 xGenotypes.addChild(xGenotype);
309 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
310 XMLNode child = xRegistry.getChildNode(i);
311 std::string key = child.getAttributeValue();
313 if(key.compare(0, genotypeName.size(), genotypeName) == 0) {
315 XMLNode xEntry = child.deepCopy();
316 child.deleteNodeContent();
320 std::string key = xEntry.getAttribute(
"key");
321 key.erase(0, genotypeName.length() + 1);
322 xEntry.updateAttribute(key.c_str(), NULL,
"key");
323 xGenotype.addChild(xEntry);
326 std::string desc = xEntry.getAttribute(
"desc");
327 if(addClear && desc !=
"") {
328 xGenotype.addClear(desc.c_str(),
"<!-- ",
" -->");
331 xEntry.deleteAttribute(
"desc");
338 XMLNode xRegClear = XMLNode::createXMLTopNode(NODE_REGISTRY);
339 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
340 XMLNode xEntry = xRegistry.getChildNode(i);
341 XMLNode xE2 = xEntry.deepCopy();
342 std::string desc = xEntry.getAttribute(
"desc");
343 xE2.deleteAttribute(
"desc");
344 xRegClear.addChild(xE2);
346 xRegClear.addClear(desc.c_str(),
"<!-- ",
" -->");
348 xMainNode.addChild(xRegClear);
352 xMainNode.addChild(xRegistry);
356 xMainNode.writeToFile(fileName.c_str());
359 std::string output(xMainNode.createXMLString());
371bool State::parseConfig(std::string filename)
373 std::ifstream fin(filename.c_str());
375 throw std::string(
"Error opening file " + filename);
377 std::cout <<
"Parsing configuration file: " << filename << std::endl;
379 std::string xmlFile, temp;
382 xmlFile +=
"\n" + temp;
386 xConfig_ = XMLNode::parseString(xmlFile.c_str(),
"ECF", &results);
387 if (results.error != eXMLErrorNone) {
388 std::cout <<
"Configuration file: " << XMLNode::getError(results.error);
389 std::cout <<
" (line " << results.nLine <<
", col " << results.nColumn <<
")" << std::endl;
393 if (xConfig_.isEmpty())
396 int n = xConfig_.nChildNode();
397 for (
int i = 0; i < n; ++i) {
398 XMLNode child = xConfig_.getChildNode(i);
399 std::string name = child.getName();
402 if (name == NODE_REGISTRY)
403 ok &= registry_->readEntries(child);
404 else if (name == NODE_ALGORITHM)
405 ok &= parseAlgorithmNode(child);
406 else if (name == NODE_GENOTYPE)
407 ok &= parseGenotypeNode(child);
408 else if (name == NODE_POPULATION)
411 std::cout <<
"Unknown node: " << name << std::endl;
426bool State::parseAlgorithmNode(XMLNode
node)
428 int n =
node.nChildNode();
430 std::cout <<
"Warning: multiple Algorithm nodes found! (using the first one)" << std::endl;
432 XMLNode child =
node.getChildNode(0);
433 alg_iter alg = mAlgorithms_.find(child.getName());
434 if (alg == mAlgorithms_.end()) {
435 throw std::string(
"Error: unknown Algorithm : ") + child.getName();
438 algorithm_ = alg->second;
439 bAlgorithmSet_ =
true;
441 if (! registry_->readEntries(child, child.getName()))
453bool State::parseGenotypeNode(XMLNode
node)
455 int n =
node.nChildNode();
456 for (
int i = 0; i < n; ++i) {
457 XMLNode child =
node.getChildNode(i);
458 gen_iter gen = mGenotypes_.find(child.getName());
459 if (gen == mGenotypes_.end()) {
460 throw std::string(
"Error: unknown Genotype : ") + child.getName();
463 uint genotypeId = (uint)genotype_.size();
464 gen->second->setGenotypeId(genotypeId);
465 gen->second->registerParameters(state_);
468 if (!registry_->readEntries(child, child.getName(), genotypeId))
482bool State::initializeComponents(
int argc,
char **argv)
486 context_->initialize();
489 logger_->initialize(state_);
490 randomizer_->initialize(state_);
495 ECF_LOG(
this, 4,
"Initializing active genotypes...");
496 for(uint i = 0; i < genotype_.size(); i++) {
497 GenotypeP copy = (GenotypeP) genotype_[i]->copy();
498 bInitialized_ &= copy->initialize(state_);
501 throw "Error: Genotype initialization failed!";
505 bInitialized_ &= comm_->initialize(state_, argc, argv);
508 ECF_LOG(
this, 4,
"Initializing population and algorithm...");
509 algorithm_->evalOp_ = this->evalOp_;
510 algorithm_->crossover_ = crossover_;
511 algorithm_->mutation_ = mutation_;
512 algorithm_->state_ = state_;
513 algorithm_->initialize(state_);
515 population_->initialize(state_);
516 algorithm_->initializeParallel(state_);
518 ECF_LOG(
this, 4,
"Initializing genetic operators...");
519 mutation_->initialize(state_);
520 crossover_->initialize(state_);
521 migration_->initialize(state_);
524 ECF_LOG(
this, 4,
"Initializing termination operators...");
525 activeTerminationOps_.clear();
526 for(uint i = 0; i < allTerminationOps_.size(); i++)
528 activeTerminationOps_.push_back(allTerminationOps_[i]);
530 if(activeTerminationOps_.empty())
531 activeTerminationOps_.push_back(allTerminationOps_[0]);
534 ECF_LOG(
this, 4,
"Initializing user defined operators...");
535 activeUserOps_.clear();
536 for(uint i = 0; i < allUserOps_.size(); i++)
538 activeUserOps_.push_back(allUserOps_[i]);
541 ECF_LOG(
this, 4,
"Initializing evaluation operator...");
542 if(!evalOp_->initialize(state_))
543 throw "Error: Evaluation operator initialization failed!";
546 ECF_LOG(
this, 4,
"Generating test individual...");
547 individual_ = (IndividualP) (
new Individual(state_));
551 catch(std::string& msg) {
552 std::cout << msg << std::endl;
553 bInitialized_ =
false;
555 catch(
const char* msg) {
556 std::cout << msg << std::endl;
557 bInitialized_ =
false;
560 std::cout <<
"Unknown error in initialization!" << std::endl;
561 bInitialized_ =
false;
564 return bInitialized_;
573 context_->evaluatedIndividual = individual_;
574 fitness_ = evalOp_->evaluate(individual_);
579 std::cout <<
"Error in evaluation operator!" << std::endl;
592bool State::runBatch()
594 bBatchStart_ =
false;
597 bool bUseLog = registry_->isModified(
"log.filename");
601 batchRemaining_ = batchRepeats_;
602 uint numerals = 1 + (uint) (log((
double) batchRepeats_) / log((
double) 10.));
605 std::string logFileName = *(std::string*) registry_->getEntry(
"log.filename").get();
606 std::string logFileExt =
"";
608 logFileName = batchLogFile_;
610 batchLogFile_ = logFileName;
612 if(logFileName.find_last_of(
".") != std::string::npos) {
613 logFileExt = logFileName.substr(logFileName.find_last_of(
"."));
614 logFileName = logFileName.substr(0, logFileName.find_last_of(
"."));
618 std::string milestoneName = *(std::string*) registry_->getEntry(
"milestone.filename").get();
619 std::string milestoneExt =
"";
620 if(milestoneName.find_last_of(
".") != std::string::npos) {
621 milestoneExt = milestoneName.substr(milestoneName.find_last_of(
"."));
622 milestoneName = milestoneName.substr(0, milestoneName.find_last_of(
"."));
626 std::ofstream statsFile;
627 if(registry_->isModified(
"batch.statsfile")) {
629 statsFile.open(batchStatsFile_.c_str(), std::ios_base::app);
631 statsFile.open(batchStatsFile_.c_str());
632 statsFile <<
"runId\tfit_min\tfit_max\tfit_avg\tfit_std\t#evals\ttime\tgen\n";
635 ECF_LOG_ERROR(
this,
"Error: can't open batch statsfile (" + batchStatsFile_ +
")!");
639 bBatchWriteStats_ =
true;
642 uint runId = batchRepeats_ - batchRemaining_ + 1;
645 for(; runId <= batchRepeats_; runId++) {
648 std::stringstream ss;
649 ss << std::setw(numerals) << std::setfill(
'0') << runId;
650 std::string currentLogName = logFileName +
"_" + ss.str() + logFileExt;
651 registry_->modifyEntry(
"log.filename", (voidP)
new std::string(currentLogName));
655 if(!bBatchSingleMilestone_) {
656 std::stringstream ss;
657 ss << std::setw(numerals) << std::setfill(
'0') << runId;
658 milestoneFilename_ = milestoneName +
"_" + ss.str() + milestoneExt;
662 bInitialized_ =
true;
663 if(!initializeComponents(argc_, argv_))
665 ECF_LOG(
this, 1,
"Running in batch mode: run " + uint2str(runId) +
"/" + uint2str(batchRepeats_));
670 if(comm_->getCommGlobalRank() == 0) {
671 if(bBatchWriteStats_) {
672 statsFile.open(batchStatsFile_.c_str(), std::ios_base::app);
673 std::vector<double> stats = population_->getStats()->getStats();
674 statsFile << runId <<
'\t';
675 statsFile << stats[ECF::FIT_LOW] <<
'\t' << stats[ECF::FIT_HIGH] <<
'\t' << stats[ECF::FIT_AVG] <<
'\t' << stats[ECF::FIT_DEV] <<
'\t';
676 statsFile << stats[ECF::STAT_EVAL] <<
'\t' << stats[ECF::STAT_TIME] <<
'\t' <<
getGenerationNo() <<
'\n';
688 std::cout <<
"Batch mode end (" << batchRepeats_ <<
" runs concluded)." << std::endl;
709 this->state_ = this->getState();
712 mutation_->operators.clear();
713 crossover_->operators.clear();
715 bInitialized_ =
false;
718 std::string config_file;
722 std::cout <<
"-- ECF, version " << ECF_VERSION <<
" --" << std::endl;
724 if(!bEvaluatorSet_) {
725 throw "Error: no EvaluateOp defined!";
730 if(parseCommandLine(argc, argv))
734 config_file = argv[1];
737 registerParameters();
739 if (config_file !=
"") {
740 parseConfig(config_file);
743 std::cout <<
"Warning: no configuration file given." << std::endl;
744 std::cout <<
"Example usage: <ECF_executable> <parameter_file>" << std::endl;
749 algorithm_ = mAlgorithms_.find(
"SteadyStateTournament")->second;
752 throw "Error: no Genotype defined!";
758 bInitialized_ =
true;
760 initializeComponents(argc, argv);
769 ECF_LOG(
this, 4,
"Initialization complete.");
773 catch(std::string& msg) {
774 std::cout << msg << std::endl;
776 catch(
const char* msg) {
777 std::cout << msg << std::endl;
780 std::cout <<
"Unknown error in initialization!" << std::endl;
783 return bInitialized_;
793bool State::parseCommandLine(
int argc,
char** argv)
796 std::vector< std::string > arg;
797 for(
int i = 0; i < argc; i++)
798 arg.push_back(argv[i]);
800 bCommandLine_ =
true;
803 if(arg[1] ==
"-gui") {
804 if(argc > 3 && arg[2] ==
"-pardump")
805 dumpParameters(arg[3],
false);
809 else if(arg[1] ==
"-pardump") {
811 std::cout <<
"No output file given for parameter dump! (usage: <executable> -pardump <filename>)" << std::endl;
814 std::cout <<
"Exporting complete parameter list to \'" << arg[2] <<
"\'...\n";
815 dumpParameters(arg[2]);
820 else if(arg[1].substr(0,2) ==
"-h" || arg[1].substr(0,3) ==
"--h") {
821 std::cout <<
"Current command line arguments:\n";
822 std::cout <<
"\t<parameter_file> run ECF with given parameter file\n";
823 std::cout <<
"\t-pardump <file> dump all parameters in a given file\n";
824 std::cout <<
"\t-h, -help display this help\n";
828 bCommandLine_ =
false;
830 return bCommandLine_;
839{
return algorithm_->isImplicitParallel(); }
846{
return algorithm_->isParallel(); }
853void State::write(XMLNode& xState)
855 registry_->modifyEntry(
"milestone.generation_", (voidP) (
new uint(
getGenerationNo())));
856 registry_->modifyEntry(
"milestone.elapsedtime_", (voidP) (
new time_t(elapsedTime_)));
857 registry_->modifyEntry(
"batch.remaining_", (voidP) (
new uint(batchRemaining_)));
858 if(batchLogFile_ !=
"")
859 registry_->modifyEntry(
"batch.logfile_", (voidP) (
new std::string(batchLogFile_)));
861 registry_->modifyEntry(
"batch.logfile_", (voidP) (
new std::string(
"*")));
869void State::saveMilestone()
871 XMLNode xMainNode = XMLNode::createXMLTopNode(
"ECF");
872 xMainNode.addAttribute(
"milestone", ctime(¤tTime_));
875 this->write(xMilestone);
876 xMainNode.addChild(xMilestone);
878 XMLNode xNode = this->xConfig_.getChildNode(NODE_ALGORITHM);
879 xNode = xNode.deepCopy();
880 xMainNode.addChild(xNode);
881 xNode = this->xConfig_.getChildNode(NODE_GENOTYPE);
882 xNode = xNode.deepCopy();
883 xMainNode.addChild(xNode);
885 this->registry_->write(xNode);
886 xMainNode.addChild(xNode);
890 population_->write(xPopulation);
891 xMainNode.addChild(xPopulation);
894 if(comm_->getCommGlobalRank() != 0)
898 xMainNode.writeToFile(milestoneFilename_.c_str());
906void State::loadMilestone()
908 ECF_LOG(
this, 4,
"Loading population and evolutionary context from milestone...");
909 XMLNode xPopulation = xConfig_.getChildNode(
"Population");
910 population_->read(xPopulation);
912 context_->generationNo_ = milestoneGeneration_;
932 genotype_.push_back((GenotypeP) genotype->copy());
933 uint index = (uint) genotype_.size() - 1;
934 genotype_[index]->setGenotypeId(index);
935 genotype->setGenotypeId(index);
937 genotype_[index]->registerParameters(state_);
940 crossover_->operators.push_back(genotype_[index]->getCrossoverOp());
941 for(uint iOp = 0; iOp < crossover_->operators[index].size(); iOp++) {
942 crossover_->operators[index][iOp]->myGenotype_ = genotype_[index];
943 crossover_->operators[index][iOp]->registerParameters(state_);
946 mutation_->operators.push_back(genotype_[index]->getMutationOp());
947 for(uint iOp = 0; iOp < mutation_->operators[index].size(); iOp++) {
948 mutation_->operators[index][iOp]->myGenotype_ = genotype_[index];
949 mutation_->operators[index][iOp]->registerParameters(state_);
952 bGenotypeSet_ =
true;
959 algorithm_ = algorithm;
960 bAlgorithmSet_ =
true;
972 bEvaluatorSet_ =
true;
983 evalOp_ = (EvaluateOpP) eval;
984 bEvaluatorSet_ =
true;
1002 mGenotypes_[gen->getName()] = gen;
1017 mAlgorithms_[alg->getName()] = alg;
1030 allUserOps_.push_back(op);
1053 if(!bInitialized_) {
1054 std::cout <<
"Error: Initialization failed!" << std::endl;
1059 ECF_LOG(
this, 5,
"Batch mode detected: running batch");
1064 startTime_ = time(NULL);
1065 std::string stime = ctime(&startTime_);
1066 ECF_LOG(
this, 3,
"Start time: " + stime);
1068 startTime_ -= milestoneElapsedTime_;
1071 ECF_LOG(
this, 2,
"Evaluating initial population...");
1072 algorithm_->initializePopulation(state_);
1074 currentTime_ = time(NULL);
1075 elapsedTime_ = currentTime_ - startTime_;
1076 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1077 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint) elapsedTime_));
1078 population_->updateDemeStats();
1081 ECF_LOG(
this, 4,
"Calling user defined operators...");
1082 for(uint i = 0; i < activeUserOps_.size(); i++)
1083 activeUserOps_[i]->operate(state_);
1086 ECF_LOG(
this, 4,
"Checking termination conditions...");
1087 for(uint i = 0; i < activeTerminationOps_.size(); i++)
1088 activeTerminationOps_[i]->operate(state_);
1091 while(context_->bTerminate_ ==
false) {
1092 context_->generationNo_++;
1093 ECF_LOG(
this, 5,
"Calling the active algorithm");
1094 algorithm_->advanceGeneration(state_);
1096 currentTime_ = time(NULL);
1097 elapsedTime_ = currentTime_ - startTime_;
1098 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1099 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint) elapsedTime_));
1101 population_->updateDemeStats();
1104 ECF_LOG(
this, 4,
"Calling user defined operators...");
1105 for(uint i = 0; i < activeUserOps_.size(); i++)
1106 activeUserOps_[i]->operate(state_);
1109 ECF_LOG(
this, 4,
"Checking termination conditions...");
1110 for(uint i = 0; i < activeTerminationOps_.size(); i++)
1111 activeTerminationOps_[i]->operate(state_);
1113 if(context_->bTerminate_)
1114 logger_->saveTo(
true);
1118 if(bSaveMilestone_ &&
1119 milestoneInterval_ > 0 && context_->generationNo_ % milestoneInterval_ == 0)
1122 migration_->operate(state_);
1127 population_->getHof()->write(xHoF);
1128 char *out = xHoF.createXMLString(
true);
1129 ECF_LOG(
this, 1,
"\nBest of run: \n" + std::string(out));
1132 logger_->saveTo(
true);
1136 logger_->closeLog();
1161 if(!bInitialized_) {
1162 std::cout <<
"Error: Initialization failed!" << std::endl;
1164 if(comm_->isInitialized())
1169 startTime_ = time(NULL);
1170 std::string stime = ctime(&startTime_);
1171 ECF_LOG(
this, 3,
"Start time: " + stime);
1173 startTime_ -= milestoneElapsedTime_;
1178 if(comm_->getCommRank() == 0) {
1179 ECF_LOG(
this, 2,
"Evaluating initial population...");
1183 algorithm_->initializePopulation(state_);
1184 comm_->synchronize();
1186 if(comm_->getCommRank() == 0) {
1188 if(isImplicitParallel())
1189 algorithm_->initializeImplicit(state_);
1193 while(context_->bTerminate_ ==
false) {
1195 currentTime_ = time(NULL);
1196 elapsedTime_ = currentTime_ - startTime_;
1197 if(comm_->getCommGlobalRank() == 0) {
1198 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1199 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint) elapsedTime_));
1203 if(comm_->getCommRank() == 0) {
1204 population_->updateDemeStats();
1206 if(bSaveMilestone_ && milestoneInterval_ > 0 && context_->generationNo_ % milestoneInterval_ == 0)
1211 if(comm_->getCommGlobalRank() == 0) {
1212 ECF_LOG(
this, 4,
"Checking termination conditions...");
1213 for(uint i = 0; i < activeTerminationOps_.size(); i++)
1214 activeTerminationOps_[i]->operate(state_);
1216 for(uint i = 1; i < population_->getNoDemes(); i++)
1217 comm_->sendTerminateMessage(comm_->getDemeMaster(i), context_->bTerminate_);
1220 else if(comm_->getCommRank() == 0)
1221 context_->bTerminate_ = comm_->recvTerminateMessage(0);
1224 algorithm_->bcastTermination(state_);
1227 if(comm_->getCommRank() == 0) {
1228 migration_->operate(state_);
1232 ECF_LOG(
this, 4,
"Calling user defined operators...");
1233 for(uint i = 0; i < activeUserOps_.size(); i++)
1234 activeUserOps_[i]->operate(state_);
1236 if(context_->bTerminate_ ==
true) {
1237 logger_->saveTo(
true);
1243 context_->generationNo_++;
1244 ECF_LOG(
this, 5,
"Calling the active algorithm");
1245 algorithm_->advanceGeneration(state_);
1248 logger_->setLogFrequency(1);
1249 if(comm_->getCommGlobalRank() == 0) {
1252 population_->getHof()->write(xHoF);
1253 std::string out = xHoF.createXMLString(
true);
1254 ECF_LOG(
this, 1,
"\nBest of run: \n" + out);
1256 logger_->saveTo(
true);
1259 if(comm_->getCommRank() == 0 && bSaveMilestone_)
1262 logger_->saveTo(
true);
Asynchronous elimination global parallel algorithm (outdated version)
Asynchronous elimination global parallel algorithm.
Synchronous generational global parallel algorithm.
Artificial Bee Colony algorithm (see e.g. http://www.scholarpedia.org/article/Artificial_bee_colony_a...
Binary class - implements genotype as a vector of binary coded real values with variable interval and...
BitString class - implements genotype as a series of bits.
Clonal Selection Algorithm (see e.g. http://en.wikipedia.org/wiki/Clonal_Selection_Algorithm).
Communicator class for interprocess communication.
Crossover class - handles crossover of individuals (as opposed to CrossoverOp class that crosses geno...
Cuckoo search (CS) optimization algorithm (see http://en.wikipedia.org/wiki/Cuckoo_search)
Differential evolution (DE) optimization algorithm (see e.g. http://en.wikipedia.org/wiki/Differentia...
Elimination (generation gap) algorithm with roulette wheel elimination selection operator.
Evolutionary context class.
(mu/rho +/, lambda) - Evolution Strategy (ES) algorithm.
FloatingPoint class - implements genotype as a vector of floating point values.
new algorithm, in development
Genetic annealing algorithm (see e.g. http://citeseerx.ist.psu.edu/viewdoc/summary?...
Individual class - inherits a vector of Genotype objects.
Logging class - handles screen output and file logging.
Migration class - handles individual migration between demes.
Mutation class - handles mutation of individuals (as opposed to MutationOp class that mutates genotyp...
Optimization Immune Algorithm (opt-IA, see e.g. http://www.artificial-immune-systems....
Particle swarm optimization algorithm (see e.g. http://en.wikipedia.org/wiki/Particle_swarm_optimizat...
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Population class - inherits a vector of Deme objects.
Repository for all the system parameters.
Generational algorithm with roulette wheel selection operator.
A simple randomizer that uses in-built random number generator.
void setAlgorithm(AlgorithmP)
Set the desired algorithm (overrides the current choice)
FitnessP getFitnessObject()
get one initial Fitness object (create on demand)
bool addGenotype(GenotypeP gen)
Add user-defined or user customized genotype. (The genotype can then be specified and used in config ...
void setEvalOp(EvaluateOpP)
Set user defined evaluation operator.
bool addAlgorithm(AlgorithmP alg)
Add user-defined or user customized algorithm. (It can then be specified and used in config file....
bool run()
Driver of the evolution process - serial version.
bool isImplicitParallel()
Is the algorithm executed implicitly parallel.
bool initialize(int, char **)
Initialize the whole system.
uint setGenotype(GenotypeP)
Set a genotype to be used in individuals.
bool isAlgorithmParallel()
Is current algorithm parallel.
State()
Construct the one and only State object.
bool addOperator(OperatorP op)
Add user-defined operator. (Its parameters can now be specified and used in config file....
uint getGenerationNo()
get current generation number
void setRandomizer(RandomizerP randomizer)
set Randomizer to be used
Steady state algorithm with tournament elimination operator.
Termination operator: terminates on a given fitness value.
Termination operator: terminates on a given number of fitness evaluations.
Termination operator: terminates on a given number of generations.
Termination operator: terminates on a given elapsed time.
Termination operator: terminates when no improvement occurs in best individual for a given number of ...
Analytical Programing genotype class - implements genotype as a vector of floating point values that ...
Tree class - implements genotype as a tree.