From 1eee26598b7a43a62272a4cc92d263f5c4cdb5f6 Mon Sep 17 00:00:00 2001 From: evomarc Date: Thu, 3 May 2001 13:06:34 +0000 Subject: [PATCH] Adding code to Lesson4 --- eo/tutorial/Lesson4/BitEA.cpp | 71 ++++++++++++++ eo/tutorial/Lesson4/ESEA.cpp | 145 +++++++++++++++++++++++++++++ eo/tutorial/Lesson4/Makefile | 19 ++++ eo/tutorial/Lesson4/RealEA.cpp | 72 ++++++++++++++ eo/tutorial/Lesson4/binary_value.h | 25 +++++ eo/tutorial/Lesson4/real_value.h | 19 ++++ 6 files changed, 351 insertions(+) create mode 100644 eo/tutorial/Lesson4/BitEA.cpp create mode 100644 eo/tutorial/Lesson4/ESEA.cpp create mode 100644 eo/tutorial/Lesson4/Makefile create mode 100644 eo/tutorial/Lesson4/RealEA.cpp create mode 100644 eo/tutorial/Lesson4/binary_value.h create mode 100644 eo/tutorial/Lesson4/real_value.h diff --git a/eo/tutorial/Lesson4/BitEA.cpp b/eo/tutorial/Lesson4/BitEA.cpp new file mode 100644 index 00000000..9fa1fd66 --- /dev/null +++ b/eo/tutorial/Lesson4/BitEA.cpp @@ -0,0 +1,71 @@ +#include + +#include +#include "binary_value.h" +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + + try + { + typedef eoBit EOT; + + eoParser parser(argc, argv); // for user-parameter reading + + eoState state; // keeps all things allocated + + ///// FIRST, problem or representation dependent stuff + ////////////////////////////////////////////////////// + + // The evaluation fn - encapsulated into an eval counter for output + eoEvalFuncPtr mainEval( binary_value ); + eoEvalFuncCounter eval(mainEval); + + // the genotype - through a genotype initializer + eoInit& init = make_genotype(parser, state, EOT()); + + // Build the variation operator (any seq/prop construct) + eoGenOp& op = make_op(parser, state, init); + + //// Now the representation-independent things + ////////////////////////////////////////////// + + // initialize the population - and evaluate + // yes, this is representation indepedent once you have an eoInit + eoPop& pop = make_pop(parser, state, init); + + // stopping criteria + eoContinue & term = make_continue(parser, state, eval); + // output + eoCheckPoint & checkpoint = make_checkpoint(parser, state, eval, term); + // algorithm (need the operator!) + eoAlgo& ga = make_algo_scalar(parser, state, eval, checkpoint, op); + + ///// End of construction of the algorith + ///////////////////////////////////////// + // to be called AFTER all parameters have been read!!! + make_help(parser); + + //// GO + /////// + // evaluate intial population AFTER help and status in case it takes time + apply(eval, pop); + // print it out + cout << "Initial Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + + run_ea(ga, pop); // run the ga + + cout << "Final Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + } + catch(exception& e) + { + cout << e.what() << endl; + } +} diff --git a/eo/tutorial/Lesson4/ESEA.cpp b/eo/tutorial/Lesson4/ESEA.cpp new file mode 100644 index 00000000..f673eaf7 --- /dev/null +++ b/eo/tutorial/Lesson4/ESEA.cpp @@ -0,0 +1,145 @@ +// Program to test several EO-ES features + +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#include + +// representation specific +#include + +#include "real_value.h" // the sphere fitness + +// Now the main +/////////////// +typedef eoMinimizingFitness FitT; + +template +void runAlgorithm(EOT, eoParser& _parser, eoState& _state); + +int main_function(int argc, char *argv[]) +{ + // Create the command-line parser + eoParser parser(argc, argv); // for user-parameter reading + + eoState state; // keeps all things allocated + + + eoValueParam& simpleParam = parser.createParam(true, "Isotropic", "Isotropic self-adaptive mutation", 'i', "ES mutation"); + eoValueParam& stdevsParam = parser.createParam(false, "Stdev", "One self-adaptive stDev per variable", 's', "ES mutation"); + eoValueParam& corrParam = parser.createParam(false, "Correl", "Use correlated mutations", 'c', "ES mutation"); + + // Run the appropriate algorithm + if (simpleParam.value() == false) + { + cout << "Using eoReal" << endl; + runAlgorithm(eoReal(), parser, state); + } + else if (stdevsParam.value() == false) + { + cout << "Using eoEsSimple" << endl; + runAlgorithm(eoEsSimple(), parser, state); + } + else if (corrParam.value() == false) + { + cout << "Using eoEsStdev" << endl; + runAlgorithm(eoEsStdev(), parser, state); + } + else + { + cout << "Using eoEsFull" << endl; + runAlgorithm(eoEsFull(), parser, state); + } + + return 0; +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} + +/** The templatized main (sort of) + * quite similar to the main of other genotypes + * (e.g. t-eoReal and t-eoGA in test dir) + */ +template +void runAlgorithm(EOT, eoParser& _parser, eoState& _state) +{ + typedef typename EOT::Fitness FitT; + + ///// FIRST, problem or representation dependent stuff + ////////////////////////////////////////////////////// + + // The evaluation fn - encapsulated into an eval counter for output + eoEvalFuncPtr&> + mainEval( real_value ); + eoEvalFuncCounter eval(mainEval); + + // the genotype - through a genotype initializer + eoRealInitBounded& init = make_genotype(_parser, _state, EOT()); + + // Build the variation operator (any seq/prop construct) + eoGenOp& op = make_op(_parser, _state, init); + + //// Now the representation-independent things + ////////////////////////////////////////////// + + // initialize the population - and evaluate + // yes, this is representation indepedent once you have an eoInit + eoPop& pop = make_pop(_parser, _state, init); + apply(eval, pop); + + // stopping criteria + eoContinue & term = make_continue(_parser, _state, eval); + // output + eoCheckPoint & checkpoint = make_checkpoint(_parser, _state, eval, term); + // algorithm (need the operator!) + eoAlgo& ga = make_algo_scalar(_parser, _state, eval, checkpoint, op); + + ///// End of construction of the algorith + ///////////////////////////////////////// + // to be called AFTER all parameters have been read!!! + make_help(_parser); + + //// GO + /////// + cout << "Initial Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + + run_ea(ga, pop); // run the ga + + cout << "Final Population\n"; + pop.sortedPrintOn(cout); + cout << endl; +} diff --git a/eo/tutorial/Lesson4/Makefile b/eo/tutorial/Lesson4/Makefile new file mode 100644 index 00000000..628c7222 --- /dev/null +++ b/eo/tutorial/Lesson4/Makefile @@ -0,0 +1,19 @@ +.cpp: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/utils/libeoutils.a ../../src/libeo.a + +.cpp.o: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp + + +ALL = BitEA RealEA + +lesson3 : $(ALL) + +all : $(ALL) + +BitEA : BitEA.o ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.2\" -Wall -g -o $@ $< ../../src/ga/libga.a ../../src/utils/libeoutils.a ../../src/libeo.a + +RealEA : RealEA.o ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.2\" -Wall -g -o $@ $< ../../src/es/libes.a ../../src/utils/libeoutils.a ../../src/libeo.a + +ESEA : ESEA.o ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.2\" -Wall -g -o $@ $< ../../src/es/libes.a ../../src/utils/libeoutils.a ../../src/libeo.a + +clean : + @/bin/rm $(ALL) *.o *.sav *.xg *.status *~ diff --git a/eo/tutorial/Lesson4/RealEA.cpp b/eo/tutorial/Lesson4/RealEA.cpp new file mode 100644 index 00000000..09e7ced5 --- /dev/null +++ b/eo/tutorial/Lesson4/RealEA.cpp @@ -0,0 +1,72 @@ +#include + +#include +#include "real_value.h" +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + + try + { + typedef eoReal EOT; + + eoParser parser(argc, argv); // for user-parameter reading + + eoState state; // keeps all things allocated + + ///// FIRST, problem or representation dependent stuff + ////////////////////////////////////////////////////// + + // The evaluation fn - encapsulated into an eval counter for output + eoEvalFuncPtr&> + mainEval( real_value ); + eoEvalFuncCounter eval(mainEval); + + // the genotype - through a genotype initializer + eoRealInitBounded& init = make_genotype(parser, state, EOT()); + + // Build the variation operator (any seq/prop construct) + eoGenOp& op = make_op(parser, state, init); + + //// Now the representation-independent things + ////////////////////////////////////////////// + + // initialize the population - and evaluate + // yes, this is representation indepedent once you have an eoInit + eoPop& pop = make_pop(parser, state, init); + + // stopping criteria + eoContinue & term = make_continue(parser, state, eval); + // output + eoCheckPoint & checkpoint = make_checkpoint(parser, state, eval, term); + // algorithm (need the operator!) + eoAlgo& ea = make_algo_scalar(parser, state, eval, checkpoint, op); + + ///// End of construction of the algorith + ///////////////////////////////////////// + // to be called AFTER all parameters have been read!!! + make_help(parser); + + //// GO + /////// + // evaluate intial population AFTER help and status in case it takes time + apply(eval, pop); + // print it out + cout << "Initial Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + + run_ea(ea, pop); // run the ea + + cout << "Final Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + } + catch(exception& e) + { + cout << e.what() << endl; + } +} diff --git a/eo/tutorial/Lesson4/binary_value.h b/eo/tutorial/Lesson4/binary_value.h new file mode 100644 index 00000000..e36ad50c --- /dev/null +++ b/eo/tutorial/Lesson4/binary_value.h @@ -0,0 +1,25 @@ +#include + +//----------------------------------------------------------------------------- + +/** Just a simple function that takes binary value of a chromosome and sets + the fitnes. + @param _chrom A binary chromosome +*/ + +template float binary_value(const Chrom& _chrom) +{ + float sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + if (_chrom[i]) + sum += pow(2, _chrom.size() - i - 1); + return sum; +} + +struct BinaryValue +{ + template void operator()(Chrom& _chrom) + { + _chrom.fitness(binary_value(_chrom)); + } +}; diff --git a/eo/tutorial/Lesson4/real_value.h b/eo/tutorial/Lesson4/real_value.h new file mode 100644 index 00000000..8f3a0df5 --- /dev/null +++ b/eo/tutorial/Lesson4/real_value.h @@ -0,0 +1,19 @@ +#include +//----------------------------------------------------------------------------- + + +/** Just a simple function that takes an eoEsBase and sets the fitnes + to sphere + @param _ind vector +*/ + +double real_value(const std::vector& _ind) +{ + double sum = 0; + for (unsigned i = 0; i < _ind.size(); i++) + sum += _ind[i] * _ind[i]; + return sqrt(sum); +} + + +