# t-eoPBIL.cpp

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // t-eoPBIL.cpp
    00005 // (c) Marc Schoenauer, 2001
    00006 /*
    00007     This library is free software; you can redistribute it and/or
    00008     modify it under the terms of the GNU Lesser General Public
    00009     License as published by the Free Software Foundation; either
    00010     version 2 of the License, or (at your option) any later version.
    00011
    00012     This library is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015     Lesser General Public License for more details.
    00016
    00017     You should have received a copy of the GNU Lesser General Public
    00018     License along with this library; if not, write to the Free Software
    00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00020
    00021     Contact: Marc.Schoenauer@inria.fr
    00022  */
    00023 //-----------------------------------------------------------------------------
    00024
    00027 #include <iostream>
    00028 #include <eo>
    00029 #include <ga/make_ga.h>
    00030 #include "binary_value.h"
    00031 #include <apply.h>
    00032 #include <ga/eoPBILDistrib.h>
    00033 #include <ga/eoPBILOrg.h>
    00034 #include <ga/eoPBILAdditive.h>
    00035 #include <eoSimpleEDA.h>
    00036
    00037 using namespace std;
    00038
    00039 typedef eoBit<double> Indi;
    00040
    00041 // instanciating the outside subroutine that creates the distribution
    00042 #include "ga/make_PBILdistrib.h"
    00043 eoPBILDistrib<Indi> & make_PBILdistrib(eoParser& _parser, eoState&_state, Indi _eo)
    00044 {
    00045   return do_make_PBILdistrib(_parser, _state, _eo);
    00046 }
    00047
    00048 // instanciating the outside subroutine that creates the update rule
    00049 #include "ga/make_PBILupdate.h"
    00050 eoDistribUpdater<Indi> & make_PBILupdate(eoParser& _parser, eoState&_state, Indi _eo)
    00051 {
    00052   return do_make_PBILupdate(_parser, _state, _eo);
    00053 }
    00054
    00055
    00056 int main(int argc, char* argv[])
    00057 {
    00058
    00059   try
    00060   {
    00061   eoParser parser(argc, argv);  // for user-parameter reading
    00062
    00063   eoState state;    // keeps all things allocated
    00064
    00067
    00068   // The evaluation fn - encapsulated into an eval counter for output
    00069   eoEvalFuncPtr<Indi, double> mainEval( binary_value<Indi>);
    00070   eoEvalFuncCounter<Indi> eval(mainEval);
    00071
    00072   // COnstruction of the distribution
    00073   eoPBILDistrib<Indi> & distrib = make_PBILdistrib(parser, state, Indi());
    00074   // and the update rule
    00075   eoDistribUpdater<Indi> & update = make_PBILupdate(parser, state, Indi());
    00076
    00079
    00080   // stopping criteria
    00081   eoContinue<Indi> & term = make_continue(parser, state, eval);
    00082   // output
    00083   eoCheckPoint<Indi> & checkpoint = make_checkpoint(parser, state, eval, term);
    00084
    00085   // add a graphical output for the distribution
    00086   // first, get the direname from the parser
    00087   //    it has been enetered in make_checkoint
    00088
    00089   eoParam* ptParam = parser.getParamWithLongName(string("resDir"));
    00090   eoValueParam<string>* ptDirNameParam = dynamic_cast<eoValueParam<string>*>(ptParam);
    00091   if (!ptDirNameParam)  // not found
    00092     throw runtime_error("Parameter resDir not found where it was supposed to be");
    00093
    00094   // now create the snapshot monitor
    00095   eoValueParam<bool>& plotDistribParam = parser.getORcreateParam(false, "plotDistrib",
    00096                                                                  "Plot Distribution", '\0',
    00097                                                                  "Output - Graphical");
    00098     if (plotDistribParam.value())
    00099       {
    00100         unsigned frequency=1;           // frequency of plots updates
    00101         eoGnuplot1DSnapshot *distribSnapshot = new eoGnuplot1DSnapshot(ptDirNameParam->value(),
    00102                                                                        frequency, "distrib");
    00103         state.storeFunctor(distribSnapshot);
    00104         // add the distribution (it is an eoValueParam<vector<double> >)
    00105         distribSnapshot->add(distrib);
    00106         // and of course add it to the checkpoint
    00107         checkpoint.add(*distribSnapshot);
    00108       }
    00109
    00110   // the algorithm: EDA
    00111     // don't know where else to put the population size!
    00112   unsigned popSize = parser.getORcreateParam(unsigned(100), "popSize",
    00113                                              "Population Size", 'P', "Algorithm").value();
    00114   eoSimpleEDA<Indi> eda(update, eval, popSize, checkpoint);
    00115
    00118   // to be called AFTER all parameters have been read!!!
    00119   make_help(parser);
    00120
    00123
    00124   eda(distrib); // run the eda
    00125
    00126   std::cout << "Final Distribution\n";
    00127   distrib.printOn(std::cout);
    00128   std::cout << std::endl;
    00129
    00130   // wait - for graphical output
    00131     if (plotDistribParam.value())
    00132       {
    00133         string foo;
    00134         cin >> foo;
    00135       }
    00136   }
    00137   catch(std::exception& e)
    00138   {
    00139     std::cout << e.what() << std::endl;
    00140   }
    00141 }