fix back some errors inserted by previous refactoring

- move PBIL classes in deprecated/, superseeded by the EDO module
This commit is contained in:
Johann Dreo 2019-12-06 15:26:21 +01:00
commit 646f20934e
17 changed files with 30 additions and 30 deletions

View file

@ -0,0 +1,119 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoPBILAdditive.h
// (c) Marc Schoenauer, Maarten Keijzer, 2001
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoPBILAdditive_H
#define _eoPBILAdditive_H
#include "../eoDistribUpdater.h"
#include "eoPBILDistrib.h"
/**
* Distribution Class for PBIL algorithm
* (Population-Based Incremental Learning, Baluja and Caruana 96)
*
* This class implements an extended update rule:
* in the original paper, the authors used
*
* p(i)(t+1) = (1-LR)*p(i)(t) + LR*best(i)
*
* here the same formula is applied, with some of the best individuals
* and for some of the worst individuals (with different learning rates)
*/
template <class EOT>
class eoPBILAdditive : public eoDistribUpdater<EOT>
{
public:
/** Ctor with parameters
* using the default values is equivalent to using eoPBILOrg
*/
eoPBILAdditive(double _LRBest, unsigned _nbBest = 1,
double _tolerance=0.0,
double _LRWorst = 0.0, unsigned _nbWorst = 0 ) :
maxBound(1.0-_tolerance), minBound(_tolerance),
LR(0.0), nbBest(_nbBest), nbWorst(_nbWorst)
{
if (nbBest+nbWorst == 0)
throw std::runtime_error("Must update either from best or from worst in eoPBILAdditive");
if (_nbBest)
{
lrb = _LRBest/_nbBest;
LR += _LRBest;
}
else
lrb=0.0; // just in case
if (_nbWorst)
{
lrw = _LRWorst/_nbWorst;
LR += _LRWorst;
}
else
lrw=0.0; // just in case
}
/** Update the distribution from the current population */
virtual void operator()(eoDistribution<EOT> & _distrib, eoPop<EOT>& _pop)
{
eoPBILDistrib<EOT>& distrib = dynamic_cast<eoPBILDistrib<EOT>&>(_distrib);
std::vector<double> & p = distrib.value();
unsigned i, popSize=_pop.size();
std::vector<const EOT*> result;
_pop.sort(result); // is it necessary to sort the whole population?
// but I'm soooooooo lazy !!!
for (unsigned g=0; g<distrib.size(); g++)
{
p[g] *= (1-LR); // relaxation
if (nbBest) // update from some of the best
for (i=0; i<nbBest; i++)
{
const EOT & best = (*result[i]);
if ( best[g] ) // if 1, increase proba
p[g] += lrb;
}
if (nbWorst)
for (i=popSize-1; i>=popSize-nbWorst; i--)
{
const EOT & best = (*result[i]);
if ( !best[g] ) // if 0, increase proba
p[g] += lrw;
}
// stay in [0,1] (possibly strictly due to tolerance)
p[g] = std::min(maxBound, p[g]);
p[g] = std::max(minBound, p[g]);
}
}
private:
double maxBound, minBound; // proba stay away from 0 and 1 by at least tolerance
double LR; // learning rate
unsigned nbBest; // number of Best individuals used for update
unsigned nbWorst; // number of Worse individuals used for update
double lrb, lrw; // "local" learning rates (see operator())
};
#endif

View file

@ -0,0 +1,100 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoPBILDistrib.h
// (c) Marc Schoenauer, 2001
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: Marc.Schoenauer@inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef _eoPBILDistrib_H
#define _eoPBILDistrib_H
#include "../eoDistribution.h"
/**
* Distribution Class for PBIL algorithm
* (Population-Based Incremental Learning, Baluja and Caruana 96)
*
* It encodes a univariate distribution on the space of bitstrings,
* i.e. one probability for each bit to be one
*
* It is an eoValueParam<std::vector<double> > :
* the std::vector<double> stores the probabilities that each bit is 1
*
* It is still pure virtual, as the update method needs to be specified
*/
template <class EOT>
class eoPBILDistrib : public eoDistribution<EOT>,
public eoValueParam<std::vector<double> >
{
public:
/** Ctor with size of genomes, and update parameters */
eoPBILDistrib(unsigned _genomeSize) :
eoDistribution<EOT>(),
eoValueParam<std::vector<double> >(std::vector<double>(_genomeSize, 0.5), "Distribution"),
genomeSize(_genomeSize)
{}
/** the randomizer of indis */
virtual void operator()(EOT & _eo)
{
_eo.resize(genomeSize); // just in case
for (unsigned i=0; i<genomeSize; i++)
_eo[i] = eo::rng.flip(value()[i]);
_eo.invalidate(); // DO NOT FORGET!!!
}
/** Accessor to the genome size */
unsigned Size() {return genomeSize;}
/** printing... */
virtual void printOn(std::ostream& os) const
{
os << value().size() << ' ';
for (unsigned i=0; i<value().size(); i++)
os << value()[i] << ' ';
}
/** reading...*/
virtual void readFrom(std::istream& is)
{
unsigned sz;
is >> sz;
value().resize(sz);
unsigned i;
for (i = 0; i < sz; ++i)
{
double atom;
is >> atom;
value()[i] = atom;
}
}
unsigned int size() {return genomeSize;}
virtual std::string className() const {return "eoPBILDistrib";};
private:
unsigned genomeSize; // size of indis
};
#endif

View file

@ -0,0 +1,78 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoPBILOrg.h
// (c) Marc Schoenauer, Maarten Keijzer, 2001
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef _eoPBILOrg_H
#define _eoPBILOrg_H
#include "eoDistribUpdater.h"
#include "../eoPBILDistrib.h"
/**
* Distribution Class for PBIL algorithm
* (Population-Based Incremental Learning, Baluja and Caruana 95)
*
* This class implements the update rule from the original paper:
*
* p(i)(t+1) = (1-LR)*p(i)(t) + LR*best(i)
*/
template <class EOT>
class eoPBILOrg : public eoDistribUpdater<EOT>
{
public:
/** Ctor with size of genomes, and update parameters */
eoPBILOrg(double _LR, double _tolerance=0.0 ) :
LR(_LR), maxBound(1.0-_tolerance), minBound(_tolerance)
{}
/** Update the distribution from the current population */
virtual void operator()(eoDistribution<EOT> & _distrib, eoPop<EOT>& _pop)
{
const EOT & best = _pop.best_element();
eoPBILDistrib<EOT>& distrib = dynamic_cast<eoPBILDistrib<EOT>&>(_distrib);
std::vector<double> & p = distrib.value();
for (unsigned g=0; g<distrib.size(); g++)
{
// double & r = value()[g];
p[g] *= (1-LR);
if ( best[g] )
p[g] += LR;
// else nothing
// stay away from 0 and 1
p[g] = std::min(maxBound, p[g]);
p[g] = std::max(minBound, p[g]);
}
}
private:
double LR; // learning rate for best guys
double maxBound, minBound; // proba stay away from 0 and 1 by at least tolerance
};
#endif

View file

@ -0,0 +1,148 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// t-eoPBIL.cpp
// (c) Marc Schoenauer, 2001
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: Marc.Schoenauer@inria.fr
*/
//-----------------------------------------------------------------------------
/** test program for PBIL algorithm */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <eo>
#include <ga/make_ga.h>
#include "binary_value.h"
#include <apply.h>
#include <ga/eoPBILDistrib.h>
#include <ga/eoPBILOrg.h>
#include <ga/eoPBILAdditive.h>
#include <eoSimpleEDA.h>
using namespace std;
typedef eoBit<double> Indi;
// instanciating the outside subroutine that creates the distribution
#include "ga/make_PBILdistrib.h"
eoPBILDistrib<Indi> & 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<Indi> & 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<Indi, double> mainEval( binary_value<Indi>);
eoEvalFuncCounter<Indi> eval(mainEval);
// Construction of the distribution
eoPBILDistrib<Indi> & distrib = make_PBILdistrib(parser, state, Indi());
// and the update rule
eoDistribUpdater<Indi> & update = make_PBILupdate(parser, state, Indi());
//// Now the representation-independent things
//////////////////////////////////////////////
// stopping criteria
eoContinue<Indi> & term = make_continue(parser, state, eval);
// output
eoCheckPoint<Indi> & 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<string>* ptDirNameParam = dynamic_cast<eoValueParam<string>*>(ptParam);
if (!ptDirNameParam) // not found
throw runtime_error("Parameter resDir not found where it was supposed to be");
// now create the snapshot monitor
eoValueParam<bool>& plotDistribParam = parser.getORcreateParam(false, "plotDistrib",
"Plot Distribution", '\0',
"Output - Graphical");
if (plotDistribParam.value())
{
#ifdef HAVE_GNUPLOT
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<vector<double> >)
distribSnapshot->add(distrib);
// and of course add it to the checkpoint
checkpoint.add(*distribSnapshot);
#endif
}
// the algorithm: EDA
// don't know where else to put the population size!
unsigned popSize = parser.getORcreateParam(unsigned(100), "popSize",
"Population Size", 'P', "Algorithm").value();
eoSimpleEDA<Indi> eda(update, eval, popSize, checkpoint);
///// End of construction of the algorith
/////////////////////////////////////////
// to be called AFTER all parameters have been read!!!
make_help(parser);
//// GO
///////
eda(distrib); // run the eda
std::cout << "Final Distribution\n";
distrib.printOn(std::cout);
std::cout << std::endl;
// wait - for graphical output
if (plotDistribParam.value())
{
string foo;
cin >> foo;
}
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}
}

View file

@ -28,7 +28,7 @@ Authors:
#include <eo> #include <eo>
#include <edo> #include <edo>
#include "Rosenbrock.h" #include "../application/common/Rosenbrock.h"
typedef eoReal< eoMinimizingFitness > EOT; typedef eoReal< eoMinimizingFitness > EOT;

View file

@ -28,7 +28,7 @@ Authors:
#include <eo> #include <eo>
#include <edo> #include <edo>
#include "Rosenbrock.h" #include "../application/common/Rosenbrock.h"
typedef eoReal< eoMinimizingFitness > EOT; typedef eoReal< eoMinimizingFitness > EOT;
typedef edoUniform< EOT > Distrib; typedef edoUniform< EOT > Distrib;

View file

@ -33,8 +33,8 @@ Authors:
#include <edo> #include <edo>
#include "Rosenbrock.h" #include "../application/common/Rosenbrock.h"
#include "Sphere.h" #include "../application/common/Sphere.h"
typedef eoReal< eoMinimizingFitness > EOT; typedef eoReal< eoMinimizingFitness > EOT;
typedef edoNormalMulti< EOT > Distrib; typedef edoNormalMulti< EOT > Distrib;

View file

@ -38,8 +38,8 @@ Authors:
#include <edo> #include <edo>
#include "Rosenbrock.h" #include "../application/common/Rosenbrock.h"
#include "Sphere.h" #include "../application/common/Sphere.h"
typedef eoReal< eoMinimizingFitness > EOT; typedef eoReal< eoMinimizingFitness > EOT;
typedef edoNormalMulti< EOT > Distrib; typedef edoNormalMulti< EOT > Distrib;

View file

@ -28,7 +28,7 @@ Authors:
#include <eo> #include <eo>
#include <edo> #include <edo>
#include "Rosenbrock.h" #include "../application/common/Rosenbrock.h"
typedef eoReal< eoMinimizingFitness > EOT; typedef eoReal< eoMinimizingFitness > EOT;

View file

@ -22,8 +22,8 @@
Authors : Authors :
todos@geneura.ugr.es todos@geneura.ugr.es
Marc Schoenauer Marc Schoenauer
Ram<EFBFBD>n Casero Ca<EFBFBD>as Ramón Casero Cañas
Johann Dr<EFBFBD>o Johann Dreo
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -40,7 +40,7 @@ Authors :
to be consistent with other Combined constructs to be consistent with other Combined constructs
and allow to easily handle more than 2 continuators and allow to easily handle more than 2 continuators
02/2003 Ram<EFBFBD>n Casero Ca<EFBFBD>as - added the removeLast() method 02/2003 Ramón Casero Cañas - added the removeLast() method
@ingroup Combination @ingroup Combination
*/ */

View file

@ -31,7 +31,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** EO Factory. A factory is used to create other objects. In particular, /** EO Factory. A factory is used to create other objects. In particular,
it can be used so that objects of that kind can<EFBFBD>t be created in any other it can be used so that objects of that kind can't be created in any other
way. It should be instantiated with anything that needs a factory, like selectors way. It should be instantiated with anything that needs a factory, like selectors
or whatever; but the instance class should be the parent class from which all the or whatever; but the instance class should be the parent class from which all the
object that are going to be created descend. This class basically defines an interface, object that are going to be created descend. This class basically defines an interface,

View file

@ -39,7 +39,7 @@ changed. eoObject is used to define a name (#className#)
that is used when loading or saving the state. that is used when loading or saving the state.
Previously, this object also defined a print and read Previously, this object also defined a print and read
interface, but it<EFBFBD>s been moved to eoPrintable and eoPersistent. interface, but it's been moved to eoPrintable and eoPersistent.
*/ */
/** Defines a name (#className#), used when loading or saving a state. /** Defines a name (#className#), used when loading or saving a state.

View file

@ -79,7 +79,7 @@ public:
_is >> rate; _is >> rate;
if ( _is ) { if ( _is ) {
eoOp<eoClass>* op = operatorFactory.make( _is ); // This reads the rest of the line eoOp<eoClass>* op = operatorFactory.make( _is ); // This reads the rest of the line
// Add the operators to the selector, don<EFBFBD>t pay attention to the IDs // Add the operators to the selector, don't pay attention to the IDs
opSelectorP->addOp( *op, rate ); opSelectorP->addOp( *op, rate );
// Keep it in the store, to destroy later // Keep it in the store, to destroy later
tmpPVec.push_back( op ); tmpPVec.push_back( op );

View file

@ -27,8 +27,8 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include "iostream" // std::istream, std::ostream #include <iostream> // std::istream, std::ostream
#include "string" // para std::string #include <string> // para std::string
/* /*
This functionality was separated from eoObject, since it makes no sense to print This functionality was separated from eoObject, since it makes no sense to print

View file

@ -36,28 +36,28 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// the genotypes - from plain std::vector<double> to full correlated mutation // the genotypes - from plain std::vector<double> to full correlated mutation
#include "eoReal.h" #include "es/eoReal.h"
#include "eoEsSimple.h" #include "es/eoEsSimple.h"
#include "eoEsStdev.h" #include "es/eoEsStdev.h"
#include "eoEsFull.h" #include "es/eoEsFull.h"
// the initialization // the initialization
#include "eoEsChromInit.h" #include "es/eoEsChromInit.h"
// general operators // general operators
#include "eoRealOp.h" #include "es/eoRealOp.h"
#include "eoNormalMutation.h" #include "es/eoNormalMutation.h"
#include "eoRealAtomXover.h" // for generic operators #include "es/eoRealAtomXover.h" // for generic operators
// SBX crossover (following Deb) // SBX crossover (following Deb)
#include "eoSBXcross.h" #include "es/eoSBXcross.h"
// ES specific operators // ES specific operators
#include "eoEsGlobalXover.h" // Global ES Xover #include "es/eoEsGlobalXover.h" // Global ES Xover
#include "eoEsStandardXover.h" // 2-parents ES Xover #include "es/eoEsStandardXover.h" // 2-parents ES Xover
// the ES-mutations // the ES-mutations
#include "eoEsMutationInit.h" #include "es/eoEsMutationInit.h"
#include "eoEsMutate.h" #include "es/eoEsMutate.h"
#endif #endif

View file

@ -2,7 +2,7 @@
* all files: Change from absolute to relative include. * all files: Change from absolute to relative include.
2007-08-21 Jochen K<EFBFBD>pper <jochen@fhi-berlin.mpg.de> 2007-08-21 Jochen Küpper <jochen@fhi-berlin.mpg.de>
* eoBitOp.h (eoNPtsBitXover::operator()): Make sure bit is within * eoBitOp.h (eoNPtsBitXover::operator()): Make sure bit is within
allocated length of vector points: [0, max_size). allocated length of vector points: [0, max_size).

View file

@ -45,7 +45,7 @@ set (TEST_LIST
t-eoReal t-eoReal
t-eoVector t-eoVector
t-eoESAll t-eoESAll
t-eoPBIL # t-eoPBIL # deprecated, see EDO module.
t-eoFitnessAssembled t-eoFitnessAssembled
t-eoFitnessAssembledEA t-eoFitnessAssembledEA
t-eoRoulette t-eoRoulette