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;
64 alg =
static_cast<AlgorithmP
> (
new AlgGEP);
65 this->mAlgorithms_[alg->getName()] = alg;
70 this->mAlgorithms_[alg->getName()] = alg;
72 this->mAlgorithms_[alg->getName()] = alg;
74 this->mAlgorithms_[alg->getName()] = alg;
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;
89 this->mGenotypes_[gen->getName()] = gen;
91 this->mGenotypes_[gen->getName()] = gen;
93 this->mGenotypes_[gen->getName()] = gen;
95 this->mGenotypes_[gen->getName()] = gen;
99 this->allTerminationOps_.push_back(op);
101 this->allTerminationOps_.push_back(op);
103 this->allTerminationOps_.push_back(op);
105 this->allTerminationOps_.push_back(op);
107 this->allTerminationOps_.push_back(op);
110 this->registry_ =
static_cast<RegistryP
> (
new Registry);
111 this->logger_ =
static_cast<LoggerP
> (
new Logger);
113 this->migration_ =
static_cast<MigrationP
> (
new Migration);
121void State::registerParameters()
124 registry_->registerEntry(
"milestone.interval", (voidP) (
new uint(0)), ECF::UINT,
125 "milestone saving interval in generations; 0: save only at the end (default: 0)");
126 registry_->registerEntry(
"milestone.filename", (voidP) (
new std::string(
"milestone.txt")), ECF::STRING,
127 "milestone file (if stated) stores all the population (default: none)");
128 registry_->registerEntry(
"batch.repeats", (voidP) (
new uint(0)), ECF::UINT,
129 "number of independent runs to perform (default: 1)");
130 registry_->registerEntry(
"batch.singlemilestone", (voidP) (
new uint(0)), ECF::UINT,
131 "use only one milestone file for all the batch runs (1) or one for each run (0) (default: 0)");
132 registry_->registerEntry(
"batch.statsfile", (voidP) (
new std::string(
"")), ECF::STRING,
133 "output batch end of run stats in a single file (default: none)");
136 registry_->registerEntry(
"milestone.generation_", (voidP) (
new uint(0)), ECF::UINT);
137 registry_->registerEntry(
"milestone.elapsedtime_", (voidP) (
new uint(0)), ECF::UINT);
138 registry_->registerEntry(
"batch.remaining_", (voidP) (
new uint(0)), ECF::UINT);
139 registry_->registerEntry(
"batch.logfile_", (voidP) (
new std::string(
"")), ECF::STRING);
141 ECF_LOG(
this, 4,
"Registering parameters: algorithms, operators");
145 for(itAlg = mAlgorithms_.begin(); itAlg != mAlgorithms_.end(); ++itAlg)
146 itAlg->second->registerParameters(state_);
149 mAlgorithms_.begin()->second->registerParallelParameters(state_);
152 for(uint i = 0; i < allTerminationOps_.size(); i++)
153 allTerminationOps_[i]->registerParameters(state_);
156 for(uint i = 0; i < allUserOps_.size(); i++)
157 allUserOps_[i]->registerParameters(state_);
159 mutation_->registerParameters(state_);
160 crossover_->registerParameters(state_);
161 randomizer_->registerParameters(state_);
162 population_->registerParameters(state_);
163 logger_->registerParameters(state_);
164 migration_->registerParameters(state_);
165 evalOp_->registerParameters(state_);
173void State::readParameters()
175 ECF_LOG(
this, 4,
"Rading parameters from the Registry");
178 if(registry_->isModified(
"milestone.filename"))
179 bSaveMilestone_ =
true;
181 bSaveMilestone_ =
false;
183 voidP sptr = registry_->getEntry(
"milestone.interval");
184 milestoneInterval_ = *((uint*) sptr.get());
186 sptr = registry_->getEntry(
"milestone.filename");
187 milestoneFilename_ = *((std::string*) sptr.get());
190 if(registry_->isModified(
"milestone.generation_"))
191 bLoadMilestone_ =
true;
193 bLoadMilestone_ =
false;
195 sptr = registry_->getEntry(
"milestone.generation_");
196 milestoneGeneration_ = *((uint*) sptr.get());
198 sptr = registry_->getEntry(
"milestone.elapsedtime_");
199 milestoneElapsedTime_ = *((uint*) sptr.get());
202 sptr = registry_->getEntry(
"batch.repeats");
203 batchRepeats_ = *((uint*) sptr.get());
205 sptr = registry_->getEntry(
"batch.remaining_");
206 batchRemaining_ = *((uint*) sptr.get());
208 sptr = registry_->getEntry(
"batch.statsfile");
209 batchStatsFile_ = *((std::string*) sptr.get());
211 sptr = registry_->getEntry(
"batch.logfile_");
212 batchLogFile_ = *((std::string*) sptr.get());
214 sptr = registry_->getEntry(
"batch.singlemilestone");
215 bBatchSingleMilestone_ = (*((uint*) sptr.get()) % 2) ?
true:
false;
217 if(registry_->isModified(
"batch.repeats") && batchRepeats_ > 1)
220 bBatchStart_ =
false;
227void State::dumpParameters(std::string fileName,
bool addClear)
229 XMLNode xMainNode = XMLNode::createXMLTopNode(
"ECF");
230 xMainNode.addAttribute(
"version", ECF_VERSION.c_str());
231 xMainNode.addClear(
"ECF parameter dump - list of all parameters",
"<!-- ",
" -->");
233 this->state_ = this->getState();
235 this->registerParameters();
238 for(gen_iter itGen = mGenotypes_.begin(); itGen != mGenotypes_.end(); ++itGen) {
239 itGen->second->setGenotypeId(0);
240 itGen->second->registerParameters(state_);
243 std::vector<CrossoverOpP> crx = itGen->second->getCrossoverOp();
244 for(uint iOp = 0; iOp < crx.size(); iOp++) {
245 crx[iOp]->myGenotype_ = itGen->second;
246 crx[iOp]->registerParameters(state_);
250 std::vector<MutationOpP> mut = itGen->second->getMutationOp();
251 for(uint iOp = 0; iOp < mut.size(); iOp++) {
252 mut[iOp]->myGenotype_ = itGen->second;
253 mut[iOp]->registerParameters(state_);
259 this->registry_->dumpEntries(xRegistry);
262 XMLNode xAlgorithms = XMLNode::createXMLTopNode(NODE_ALGORITHM);
263 xMainNode.addChild(xAlgorithms);
267 for(itAlg = mAlgorithms_.begin(); itAlg != mAlgorithms_.end(); ++itAlg) {
270 std::string algorithmName = itAlg->first;
271 XMLNode xAlgorithm = XMLNode::createXMLTopNode(algorithmName.c_str());
272 xAlgorithms.addChild(xAlgorithm);
275 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
276 XMLNode child = xRegistry.getChildNode(i);
277 std::string key = child.getAttributeValue();
279 if(key.compare(0, algorithmName.size(), algorithmName) == 0) {
281 XMLNode xEntry = child.deepCopy();
282 child.deleteNodeContent();
286 std::string key = xEntry.getAttribute(
"key");
287 key.erase(0, algorithmName.length() + 1);
288 xEntry.updateAttribute(key.c_str(), NULL,
"key");
289 xAlgorithm.addChild(xEntry);
292 std::string desc = xEntry.getAttribute(
"desc");
293 if(addClear && desc !=
"") {
294 xAlgorithm.addClear(desc.c_str(),
"<!-- ",
" -->");
297 xEntry.deleteAttribute(
"desc");
303 XMLNode xGenotypes = XMLNode::createXMLTopNode(NODE_GENOTYPE);
304 xMainNode.addChild(xGenotypes);
307 for(gen_iter itGen = mGenotypes_.begin(); itGen != mGenotypes_.end(); ++itGen) {
310 std::string genotypeName = itGen->first;
311 XMLNode xGenotype = XMLNode::createXMLTopNode(genotypeName.c_str());
312 xGenotypes.addChild(xGenotype);
315 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
316 XMLNode child = xRegistry.getChildNode(i);
317 std::string key = child.getAttributeValue();
319 if(key.compare(0, genotypeName.size(), genotypeName) == 0) {
321 XMLNode xEntry = child.deepCopy();
322 child.deleteNodeContent();
326 std::string key = xEntry.getAttribute(
"key");
327 key.erase(0, genotypeName.length() + 1);
328 xEntry.updateAttribute(key.c_str(), NULL,
"key");
329 xGenotype.addChild(xEntry);
332 std::string desc = xEntry.getAttribute(
"desc");
333 if(addClear && desc !=
"") {
334 xGenotype.addClear(desc.c_str(),
"<!-- ",
" -->");
337 xEntry.deleteAttribute(
"desc");
344 XMLNode xRegClear = XMLNode::createXMLTopNode(NODE_REGISTRY);
345 for(
int i = 0; i < xRegistry.nChildNode(); i++) {
346 XMLNode xEntry = xRegistry.getChildNode(i);
347 XMLNode xE2 = xEntry.deepCopy();
348 std::string desc = xEntry.getAttribute(
"desc");
349 xE2.deleteAttribute(
"desc");
350 xRegClear.addChild(xE2);
352 xRegClear.addClear(desc.c_str(),
"<!-- ",
" -->");
354 xMainNode.addChild(xRegClear);
358 xMainNode.addChild(xRegistry);
362 xMainNode.writeToFile(fileName.c_str());
365 std::string output(xMainNode.createXMLString());
377bool State::parseConfig(std::string filename)
379 std::ifstream fin(filename.c_str());
381 throw std::string(
"Error opening file " + filename);
383 std::cout <<
"Parsing configuration file: " << filename << std::endl;
385 std::string xmlFile, temp;
388 xmlFile +=
"\n" + temp;
392 xConfig_ = XMLNode::parseString(xmlFile.c_str(),
"ECF", &results);
393 if (results.error != eXMLErrorNone) {
394 std::cout <<
"Configuration file: " << XMLNode::getError(results.error);
395 std::cout <<
" (line " << results.nLine <<
", col " << results.nColumn <<
")" << std::endl;
399 if (xConfig_.isEmpty())
402 int n = xConfig_.nChildNode();
403 for (
int i = 0; i < n; ++i) {
404 XMLNode child = xConfig_.getChildNode(i);
405 std::string name = child.getName();
408 if (name == NODE_REGISTRY)
409 ok &= registry_->readEntries(child);
410 else if (name == NODE_ALGORITHM)
411 ok &= parseAlgorithmNode(child);
412 else if (name == NODE_GENOTYPE)
413 ok &= parseGenotypeNode(child);
414 else if (name == NODE_POPULATION)
417 std::cout <<
"Unknown node: " << name << std::endl;
432bool State::parseAlgorithmNode(XMLNode node)
434 int n = node.nChildNode();
436 std::cout <<
"Warning: multiple Algorithm nodes found! (using the first one)" << std::endl;
438 XMLNode child = node.getChildNode(0);
439 alg_iter alg = mAlgorithms_.find(child.getName());
440 if (alg == mAlgorithms_.end()) {
441 throw std::string(
"Error: unknown Algorithm : ") + child.getName();
444 algorithm_ = alg->second;
445 bAlgorithmSet_ =
true;
447 if (! registry_->readEntries(child, child.getName()))
459bool State::parseGenotypeNode(XMLNode node)
461 int n = node.nChildNode();
462 for (
int i = 0; i < n; ++i) {
463 XMLNode child = node.getChildNode(i);
464 gen_iter gen = mGenotypes_.find(child.getName());
465 if (gen == mGenotypes_.end()) {
466 throw std::string(
"Error: unknown Genotype : ") + child.getName();
469 uint genotypeId = (uint)genotype_.size();
470 gen->second->setGenotypeId(genotypeId);
471 gen->second->registerParameters(state_);
474 if (!registry_->readEntries(child, child.getName(), genotypeId))
488bool State::initializeComponents(
int argc,
char **argv)
492 context_->initialize();
495 logger_->initialize(state_);
496 randomizer_->initialize(state_);
501 ECF_LOG(
this, 4,
"Initializing active genotypes...");
502 for(uint i = 0; i < genotype_.size(); i++) {
503 GenotypeP copy = (GenotypeP) genotype_[i]->copy();
504 bInitialized_ &= copy->initialize(state_);
507 throw "Error: Genotype initialization failed!";
511 bInitialized_ &= comm_->initialize(state_, argc, argv);
514 ECF_LOG(
this, 4,
"Initializing population and algorithm...");
515 algorithm_->evalOp_ = this->evalOp_;
516 algorithm_->crossover_ = crossover_;
517 algorithm_->mutation_ = mutation_;
518 algorithm_->state_ = state_;
519 algorithm_->initialize(state_);
521 population_->initialize(state_);
522 algorithm_->initializeParallel(state_);
524 ECF_LOG(
this, 4,
"Initializing genetic operators...");
525 mutation_->initialize(state_);
526 crossover_->initialize(state_);
527 migration_->initialize(state_);
530 ECF_LOG(
this, 4,
"Initializing termination operators...");
531 activeTerminationOps_.clear();
532 for(uint i = 0; i < allTerminationOps_.size(); i++)
534 activeTerminationOps_.push_back(allTerminationOps_[i]);
536 if(activeTerminationOps_.empty())
537 activeTerminationOps_.push_back(allTerminationOps_[0]);
540 ECF_LOG(
this, 4,
"Initializing user defined operators...");
541 activeUserOps_.clear();
542 for(uint i = 0; i < allUserOps_.size(); i++)
544 activeUserOps_.push_back(allUserOps_[i]);
547 ECF_LOG(
this, 4,
"Initializing evaluation operator...");
548 if(!evalOp_->initialize(state_))
549 throw "Error: Evaluation operator initialization failed!";
552 ECF_LOG(
this, 4,
"Generating test individual...");
553 individual_ = (IndividualP) (
new Individual(state_));
557 catch(std::string& msg) {
558 std::cout << msg << std::endl;
559 bInitialized_ =
false;
561 catch(
const char* msg) {
562 std::cout << msg << std::endl;
563 bInitialized_ =
false;
566 std::cout <<
"Unknown error in initialization!" << std::endl;
567 bInitialized_ =
false;
570 return bInitialized_;
579 context_->evaluatedIndividual = individual_;
580 fitness_ = evalOp_->evaluate(individual_);
585 std::cout <<
"Error in evaluation operator!" << std::endl;
598bool State::runBatch()
600 bBatchStart_ =
false;
603 bool bUseLog = registry_->isModified(
"log.filename");
607 batchRemaining_ = batchRepeats_;
608 uint numerals = 1 + (uint) (log((
double) batchRepeats_) / log((
double) 10.));
611 std::string logFileName = *(std::string*) registry_->getEntry(
"log.filename").get();
612 std::string logFileExt =
"";
614 logFileName = batchLogFile_;
616 batchLogFile_ = logFileName;
618 if(logFileName.find_last_of(
".") != std::string::npos) {
619 logFileExt = logFileName.substr(logFileName.find_last_of(
"."));
620 logFileName = logFileName.substr(0, logFileName.find_last_of(
"."));
624 std::string milestoneName = *(std::string*) registry_->getEntry(
"milestone.filename").get();
625 std::string milestoneExt =
"";
626 if(milestoneName.find_last_of(
".") != std::string::npos) {
627 milestoneExt = milestoneName.substr(milestoneName.find_last_of(
"."));
628 milestoneName = milestoneName.substr(0, milestoneName.find_last_of(
"."));
632 std::ofstream statsFile;
633 if(registry_->isModified(
"batch.statsfile")) {
635 statsFile.open(batchStatsFile_.c_str(), std::ios_base::app);
637 statsFile.open(batchStatsFile_.c_str());
638 statsFile <<
"runId\tfit_min\tfit_max\tfit_avg\tfit_std\t#evals\ttime\tgen\n";
641 ECF_LOG_ERROR(
this,
"Error: can't open batch statsfile (" + batchStatsFile_ +
")!");
645 bBatchWriteStats_ =
true;
648 uint runId = batchRepeats_ - batchRemaining_ + 1;
651 for(; runId <= batchRepeats_; runId++) {
654 std::stringstream ss;
655 ss << std::setw(numerals) << std::setfill(
'0') << runId;
656 std::string currentLogName = logFileName +
"_" + ss.str() + logFileExt;
657 registry_->modifyEntry(
"log.filename", (voidP)
new std::string(currentLogName));
661 if(!bBatchSingleMilestone_) {
662 std::stringstream ss;
663 ss << std::setw(numerals) << std::setfill(
'0') << runId;
664 milestoneFilename_ = milestoneName +
"_" + ss.str() + milestoneExt;
668 bInitialized_ =
true;
669 if(!initializeComponents(argc_, argv_))
671 ECF_LOG(
this, 1,
"Running in batch mode: run " + uint2str(runId) +
"/" + uint2str(batchRepeats_));
676 if(comm_->getCommGlobalRank() == 0) {
677 if(bBatchWriteStats_) {
678 statsFile.open(batchStatsFile_.c_str(), std::ios_base::app);
679 std::vector<double> stats = population_->getStats()->getStats();
680 statsFile << runId <<
'\t';
681 statsFile << stats[ECF::FIT_LOW] <<
'\t' << stats[ECF::FIT_HIGH] <<
'\t' << stats[ECF::FIT_AVG] <<
'\t' << stats[ECF::FIT_DEV] <<
'\t';
682 statsFile << stats[ECF::STAT_EVAL] <<
'\t' << stats[ECF::STAT_TIME] <<
'\t' <<
getGenerationNo() <<
'\n';
694 std::cout <<
"Batch mode end (" << batchRepeats_ <<
" runs concluded)." << std::endl;
715 this->state_ = this->getState();
718 mutation_->operators.clear();
719 crossover_->operators.clear();
721 bInitialized_ =
false;
724 std::string config_file;
728 std::cout <<
"-- ECF, version " << ECF_VERSION <<
" --" << std::endl;
730 if(!bEvaluatorSet_) {
731 throw "Error: no EvaluateOp defined!";
736 if(parseCommandLine(argc, argv))
740 config_file = argv[1];
743 registerParameters();
745 if (config_file !=
"") {
746 parseConfig(config_file);
749 std::cout <<
"Warning: no configuration file given." << std::endl;
750 std::cout <<
"Example usage: <ECF_executable> <parameter_file>" << std::endl;
755 algorithm_ = mAlgorithms_.find(
"SteadyStateTournament")->second;
758 throw "Error: no Genotype defined!";
764 bInitialized_ =
true;
766 initializeComponents(argc, argv);
775 ECF_LOG(
this, 4,
"Initialization complete.");
779 catch (
const std::exception& e)
781 std::cout <<
"Initialization exception: " << e.what();
783 catch(
const std::string& msg) {
784 std::cout << msg << std::endl;
786 catch(
const char* msg) {
787 std::cout << msg << std::endl;
790 std::cout <<
"Unknown error in initialization!" << std::endl;
793 return bInitialized_;
803bool State::parseCommandLine(
int argc,
char** argv)
806 std::vector< std::string > arg;
807 for(
int i = 0; i < argc; i++)
808 arg.push_back(argv[i]);
810 bCommandLine_ =
true;
813 if(arg[1] ==
"-gui") {
814 if(argc > 3 && arg[2] ==
"-pardump")
815 dumpParameters(arg[3],
false);
819 else if(arg[1] ==
"-pardump") {
821 std::cout <<
"No output file given for parameter dump! (usage: <executable> -pardump <filename>)" << std::endl;
824 std::cout <<
"Exporting complete parameter list to \'" << arg[2] <<
"\'...\n";
825 dumpParameters(arg[2]);
830 else if(arg[1].substr(0,2) ==
"-h" || arg[1].substr(0,3) ==
"--h") {
831 std::cout <<
"Current command line arguments:\n";
832 std::cout <<
"\t<parameter_file> run ECF with given parameter file\n";
833 std::cout <<
"\t-pardump <file> dump all parameters in a given file\n";
834 std::cout <<
"\t-h, -help display this help\n";
838 bCommandLine_ =
false;
840 return bCommandLine_;
849{
return algorithm_->isImplicitParallel(); }
856{
return algorithm_->isParallel(); }
863void State::write(XMLNode& xState)
865 registry_->modifyEntry(
"milestone.generation_", (voidP) (
new uint(
getGenerationNo())));
866 registry_->modifyEntry(
"milestone.elapsedtime_", (voidP) (
new time_t(elapsedTime_)));
867 registry_->modifyEntry(
"batch.remaining_", (voidP) (
new uint(batchRemaining_)));
868 if(batchLogFile_ !=
"")
869 registry_->modifyEntry(
"batch.logfile_", (voidP) (
new std::string(batchLogFile_)));
871 registry_->modifyEntry(
"batch.logfile_", (voidP) (
new std::string(
"*")));
879void State::saveMilestone()
881 XMLNode xMainNode = XMLNode::createXMLTopNode(
"ECF");
882 xMainNode.addAttribute(
"milestone", ctime(¤tTime_));
885 this->write(xMilestone);
886 xMainNode.addChild(xMilestone);
888 XMLNode xNode = this->xConfig_.getChildNode(NODE_ALGORITHM);
889 xNode = xNode.deepCopy();
890 xMainNode.addChild(xNode);
891 xNode = this->xConfig_.getChildNode(NODE_GENOTYPE);
892 xNode = xNode.deepCopy();
893 xMainNode.addChild(xNode);
895 this->registry_->write(xNode);
896 xMainNode.addChild(xNode);
900 population_->write(xPopulation);
901 xMainNode.addChild(xPopulation);
904 if(comm_->getCommGlobalRank() != 0)
908 xMainNode.writeToFile(milestoneFilename_.c_str());
916void State::loadMilestone()
918 ECF_LOG(
this, 4,
"Loading population and evolutionary context from milestone...");
919 XMLNode xPopulation = xConfig_.getChildNode(
"Population");
920 population_->read(xPopulation);
922 context_->generationNo_ = milestoneGeneration_;
942 genotype_.push_back((GenotypeP) genotype->copy());
943 uint index = (uint) genotype_.size() - 1;
944 genotype_[index]->setGenotypeId(index);
945 genotype->setGenotypeId(index);
947 genotype_[index]->registerParameters(state_);
950 crossover_->operators.push_back(genotype_[index]->getCrossoverOp());
951 for(uint iOp = 0; iOp < crossover_->operators[index].size(); iOp++) {
952 crossover_->operators[index][iOp]->myGenotype_ = genotype_[index];
953 crossover_->operators[index][iOp]->registerParameters(state_);
956 mutation_->operators.push_back(genotype_[index]->getMutationOp());
957 for(uint iOp = 0; iOp < mutation_->operators[index].size(); iOp++) {
958 mutation_->operators[index][iOp]->myGenotype_ = genotype_[index];
959 mutation_->operators[index][iOp]->registerParameters(state_);
962 bGenotypeSet_ =
true;
969 algorithm_ = algorithm;
970 bAlgorithmSet_ =
true;
982 bEvaluatorSet_ =
true;
993 evalOp_ = (EvaluateOpP) eval;
994 bEvaluatorSet_ =
true;
1012 mGenotypes_[gen->getName()] = gen;
1027 mAlgorithms_[alg->getName()] = alg;
1040 allUserOps_.push_back(op);
1063 if(!bInitialized_) {
1064 std::cout <<
"Error: Initialization failed!" << std::endl;
1069 ECF_LOG(
this, 5,
"Batch mode detected: running batch");
1075 startTime_ = time(NULL);
1076 std::string stime = ctime(&startTime_);
1077 ECF_LOG(
this, 3,
"Start time: " + stime);
1079 startTime_ -= milestoneElapsedTime_;
1082 ECF_LOG(
this, 2,
"Evaluating initial population...");
1083 algorithm_->initializePopulation(state_);
1085 currentTime_ = time(NULL);
1086 elapsedTime_ = currentTime_ - startTime_;
1087 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1088 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint)elapsedTime_));
1089 population_->updateDemeStats();
1092 ECF_LOG(
this, 5,
"Calling user defined operators...");
1093 for (uint i = 0; i < activeUserOps_.size(); i++)
1094 activeUserOps_[i]->operate(state_);
1097 ECF_LOG(
this, 5,
"Checking termination conditions...");
1098 for (uint i = 0; i < activeTerminationOps_.size(); i++)
1099 activeTerminationOps_[i]->operate(state_);
1102 while (context_->bTerminate_ ==
false) {
1103 context_->generationNo_++;
1104 ECF_LOG(
this, 5,
"Calling the active algorithm");
1105 algorithm_->advanceGeneration(state_);
1107 currentTime_ = time(NULL);
1108 elapsedTime_ = currentTime_ - startTime_;
1109 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1110 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint)elapsedTime_));
1112 population_->updateDemeStats();
1114 IndividualP bestInd = this->
getPopulation()->getHof()->getBest().at(0);
1115 ECF_LOG(
this, 4,
"Current best:\n" + bestInd->toString());
1118 ECF_LOG(
this, 5,
"Calling user defined operators...");
1119 for (uint i = 0; i < activeUserOps_.size(); i++)
1120 activeUserOps_[i]->operate(state_);
1123 ECF_LOG(
this, 5,
"Checking termination conditions...");
1124 for (uint i = 0; i < activeTerminationOps_.size(); i++)
1125 activeTerminationOps_[i]->operate(state_);
1127 if (context_->bTerminate_)
1128 logger_->saveTo(
true);
1132 if (bSaveMilestone_ &&
1133 milestoneInterval_ > 0 && context_->generationNo_ % milestoneInterval_ == 0)
1136 migration_->operate(state_);
1141 population_->getHof()->write(xHoF);
1142 char* out = xHoF.createXMLString(
true);
1143 ECF_LOG(
this, 1,
"\nBest of run: \n" + std::string(out));
1146 logger_->saveTo(
true);
1147 if (bSaveMilestone_)
1150 logger_->closeLog();
1152 catch (
const std::exception& e)
1154 std::cout <<
"Runtime exception: " << e.what();
1158 cout <<
"Unkown error in algorithm run!";
1184 if(!bInitialized_) {
1185 std::cout <<
"Error: Initialization failed!" << std::endl;
1187 if(comm_->isInitialized())
1192 startTime_ = time(NULL);
1193 std::string stime = ctime(&startTime_);
1194 ECF_LOG(
this, 3,
"Start time: " + stime);
1196 startTime_ -= milestoneElapsedTime_;
1201 if(comm_->getCommRank() == 0) {
1202 ECF_LOG(
this, 2,
"Evaluating initial population...");
1206 algorithm_->initializePopulation(state_);
1207 comm_->synchronize();
1209 if(comm_->getCommRank() == 0) {
1212 algorithm_->initializeImplicit(state_);
1216 while(context_->bTerminate_ ==
false) {
1218 currentTime_ = time(NULL);
1219 elapsedTime_ = currentTime_ - startTime_;
1220 if(comm_->getCommGlobalRank() == 0) {
1221 ECF_LOG(
this, 2,
"Generation: " + uint2str(context_->generationNo_));
1222 ECF_LOG(
this, 2,
"Elapsed time: " + uint2str((uint) elapsedTime_));
1226 if(comm_->getCommRank() == 0) {
1227 population_->updateDemeStats();
1229 if(bSaveMilestone_ && milestoneInterval_ > 0 && context_->generationNo_ % milestoneInterval_ == 0)
1234 if(comm_->getCommGlobalRank() == 0) {
1235 ECF_LOG(
this, 4,
"Checking termination conditions...");
1236 for(uint i = 0; i < activeTerminationOps_.size(); i++)
1237 activeTerminationOps_[i]->operate(state_);
1239 for(uint i = 1; i < population_->getNoDemes(); i++)
1240 comm_->sendTerminateMessage(comm_->getDemeMaster(i), context_->bTerminate_);
1243 else if(comm_->getCommRank() == 0)
1244 context_->bTerminate_ = comm_->recvTerminateMessage(0);
1247 algorithm_->bcastTermination(state_);
1250 if(comm_->getCommRank() == 0) {
1251 migration_->operate(state_);
1255 ECF_LOG(
this, 4,
"Calling user defined operators...");
1256 for(uint i = 0; i < activeUserOps_.size(); i++)
1257 activeUserOps_[i]->operate(state_);
1259 if(context_->bTerminate_ ==
true) {
1260 logger_->saveTo(
true);
1266 context_->generationNo_++;
1267 ECF_LOG(
this, 5,
"Calling the active algorithm");
1268 algorithm_->advanceGeneration(state_);
1271 logger_->setLogFrequency(1);
1272 if(comm_->getCommGlobalRank() == 0) {
1275 population_->getHof()->write(xHoF);
1276 std::string out = xHoF.createXMLString(
true);
1277 ECF_LOG(
this, 1,
"\nBest of run: \n" + out);
1279 logger_->saveTo(
true);
1282 if(comm_->getCommRank() == 0 && bSaveMilestone_)
1285 logger_->saveTo(
true);
Asynchronous elimination global parallel algorithm (outdated version).
Asynchronous elimination global parallel algorithm.
Generational algorithm with roulette wheel selection operator and unique operators and chromosome rep...
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.
GEPChromosome class - implements genotype as a Gene Expression Programming chromosome.
new algorithm, in development
Genetic annealing algorithm (see e.g. http://citeseerx.ist.psu.edu/viewdoc/summary?...
IntGenotype class - implements genotype as a vector of int values.
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.
PopulationP getPopulation()
get Population
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.