From e0ae220420f5c4734718ef2504f164d866034b06 Mon Sep 17 00:00:00 2001 From: evomarc Date: Thu, 8 Nov 2001 06:53:55 +0000 Subject: [PATCH] Adding PBIL --- eo/test/Makefile.am | 12 ++++- eo/test/t-eoPBIL.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 eo/test/t-eoPBIL.cpp diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 8cc72c5e..037e7742 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -15,7 +15,7 @@ CXXFLAGS = -g -Wall check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoVirus\ t-MGE t-MGE1bit t-MGE-control t-eoStateAndParser t-eoCheckpointing t-eoSSGA \ - t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll + t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll t-eoPBIL TESTS=$(check_PROGRAMS) run_tests @@ -151,6 +151,7 @@ t_eoESAll_SOURCES = t-eoESAll.cpp real_value.h t_eoESAll_DEPENDENCIES = $(DEPS) $(top_builddir)/src/es/libes.a t_eoESAll_LDFLAGS = -lm t_eoESAll_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS) + ############################################################################### t_eoSSGA_SOURCES = t-eoSSGA.cpp binary_value.h @@ -164,10 +165,19 @@ t_eoPareto_SOURCES = t-eoPareto.cpp t_eoPareto_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a t_eoPareto_LDFLAGS = -lm t_eoPareto_LDADD = $(LDADDS) + ############################################################################### t_eoParetoFitness_SOURCES = t-eoParetoFitness.cpp t_eoParetoFitness_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a t_eoParetoFitness_LDFLAGS = -lm t_eoParetoFitness_LDADD = $(LDADDS) + +############################################################################### + +t_eoPBIL_SOURCES = t-eoPBIL.cpp +t_eoPBIL_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a +t_eoPBIL_LDFLAGS = -lm +t_eoPBIL_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS) + ############################################################################### diff --git a/eo/test/t-eoPBIL.cpp b/eo/test/t-eoPBIL.cpp new file mode 100644 index 00000000..8863bde0 --- /dev/null +++ b/eo/test/t-eoPBIL.cpp @@ -0,0 +1,112 @@ +#include + +#include +#include +#include "binary_value.h" +#include +#include +#include +#include +#include + +using namespace std; + +typedef eoBit Indi; + +// instanciating the outside subroutine that creates the distribution +#include "ga/make_PBILdistrib.h" +eoPBILDistrib & make_PBILdistrib(eoParser& _parser, eoState&_state, Indi _eo) +{ + return do_make_PBILdistrib(_parser, _state, _eo); +} + +// instanciating the outside subroutine that creates the update rule +#include "ga/make_PBILupdate.h" +eoDistribUpdater & make_PBILupdate(eoParser& _parser, eoState&_state, Indi _eo) +{ + return do_make_PBILupdate(_parser, _state, _eo); +} + + +int main(int argc, char* argv[]) +{ + + try + { + 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); + + // COnstruction of the distribution + eoPBILDistrib & distrib = make_PBILdistrib(parser, state, Indi()); + // and the update rule + eoDistribUpdater & update = make_PBILupdate(parser, state, Indi()); + + //// Now the representation-independent things + ////////////////////////////////////////////// + + // stopping criteria + eoContinue & term = make_continue(parser, state, eval); + // output + eoCheckPoint & checkpoint = make_checkpoint(parser, state, eval, term); + + // add a graphical output for the distribution + // first, get the direname from the parser + // it has been enetered in make_checkoint + + eoParam* ptParam = parser.getParamWithLongName(string("resDir")); + eoValueParam* ptDirNameParam = dynamic_cast*>(ptParam); + if (!ptDirNameParam) // not found + throw runtime_error("Parameter resDir not found where it was supposed to be"); + + // now create the snapshot monitor + eoValueParam& plotDistribParam = parser.createParam(false, "plotDistrib", "Plot Distribution", '\0', "Output - Graphical"); + if (plotDistribParam.value()) + { + unsigned frequency=1; // frequency of plots updates + eoGnuplot1DSnapshot *distribSnapshot = new eoGnuplot1DSnapshot(ptDirNameParam->value(), frequency, "distrib"); + state.storeFunctor(distribSnapshot); + // add the distribution (it is an eoValueParam >) + distribSnapshot->add(distrib); + // and of course add it to the checkpoint + checkpoint.add(*distribSnapshot); + } + + // the algorithm: DEA + // don't know where else to put the population size! + unsigned popSize = parser.createParam(unsigned(100), "popSize", "Population Size", 'P', "Algorithm").value(); + eoSimpleDEA dea(update, eval, popSize, checkpoint); + + ///// End of construction of the algorith + ///////////////////////////////////////// + // to be called AFTER all parameters have been read!!! + make_help(parser); + + //// GO + /////// + + dea(distrib); // run the dea + + cout << "Final Distribution\n"; + distrib.printOn(cout); + cout << endl; + + // wait - for graphical output + if (plotDistribParam.value()) + { + string foo; + cin >> foo; + } + } + catch(exception& e) + { + cout << e.what() << endl; + } +}