Moved the es files to an es directory

This commit is contained in:
mac 2000-03-22 14:18:39 +00:00
commit ddb4bfb00b
3 changed files with 650 additions and 0 deletions

126
eo/src/es/eoESChrom.h Normal file
View file

@ -0,0 +1,126 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoESChrom.h
// (c) GeNeura Team, 1998
/*
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: todos@geneura.ugr.es, http://geneura.ugr.es
*/
//-----------------------------------------------------------------------------
#ifndef _eoESCHROM_H
#define _eoESCHROM_H
// STL libraries
#include <vector> // For vector<>
#include <stdexcept>
#include <strstream>
#include <iostream> // for ostream
// EO includes
#include <eoVector.h>
/**@name Chromosomes for evolution strategies
Each chromosome in an evolution strategies is composed of a vector of floating point
values plus a vector of sigmas, that are added to them during mutation
*/
//@{
/** Each gene in an Evolution Strategies is composed of a value plus an standard
deviation, sigma, used for mutation*/
struct eoESGene {
double val, sigma;
eoESGene( double _val = 0, double _sigma = 0 ): val( _val ), sigma( _sigma ) {};
};
/// Tricky operator to avoid errors in some VC++ systems, namely VC 5.0 SP3
bool operator < ( eoESGene _e1, eoESGene _e2 ) {
return _e1.val < _e2.val;
}
/// Tricky operator to avoid errors in some VC++ systems
bool operator == ( eoESGene _e1, eoESGene _e2 ) {
return ( _e1.val == _e2.val ) && ( _e1.sigma == _e2.sigma ) ;
}
///
ostream & operator << ( ostream& _s, const eoESGene& _e ) {
_s << _e.val << ", " << _e.sigma << " | ";
return _s;
}
/// Dummy >>
istream & operator >> ( istream& _s, const eoESGene& _e ) {
_s >> _e.val;
_s >> _e.sigma;
return _s;
}
/** Basic chromosome for evolution strategies (ES), as defined by Rechenberg and
Schwefel. Each chromosomes is composed of "genes"; each one of then is an eoESGene
@see eoESGene
*/
template <typename fitT = float >
class eoESChrom: public eoVector<eoESGene, fitT> {
public:
/// Basic ctor
eoESChrom( ):eoVector<eoESGene, fitT>() {};
/** Ctor using a couple of random number generators
@param _size Lineal length of the object
@param _rnd a random number generator, which returns a random value each time it´s called.
@param _rndS another one, for the sigma
*/
eoESChrom( unsigned _size, eoRnd<double>& _rnd, eoRnd<double>& _rndS )
: eoVector<eoESGene, fitT>( _size ){
for ( iterator i = begin(); i != end(); i ++ ) {
i->val = _rnd();
i->sigma = _rndS();
}
};
/// Copy ctor
eoESChrom( const eoESChrom& _eoes): eoVector<eoESGene, fitT>( _eoes ) {};
/// Assignment operator
const eoESChrom& operator =( const eoESChrom & _eoes ) {
if ( this != &_eoes ){
eoVector<eoESGene, fitT>::operator=( _eoes );
}
return *this;
}
///
~eoESChrom() {};
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eo1d
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoESChrom";};
//@}
};
//@}
#endif

271
eo/src/es/eoESFullChrom.h Normal file
View file

@ -0,0 +1,271 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoESInd.h
// (c) GeNeura Team, 1998 - EEAAX 1999
/*
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: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
*/
//-----------------------------------------------------------------------------
#ifndef _EOESFULLCHROM_H
#define _EOESFULLCHROM_H
// STL libraries
#include <vector> // For vector<>
#include <stdexcept>
#include <strstream>
#include <iostream> // for ostream
// EO includes
#include <eoVector.h>
#include <eoRNG.h>
/**@name Chromosomes for evolution strategies
Each chromosome in an evolution strategies is composed of a vector of floating point
values plus a vector of sigmas, that are added to them during mutation and a vector of correlations
*/
//@{
/**@name individuals for evolution strategies -MS- 22/10/99
Each individual in an evolution strategy is composed of
a vector of floating point values
a vector of std deviations
a vector of rotation angles (for correlated mutations)
THese individuals CANNOT BE IMPLEMENTED as vectors of anything
at least in the case of correlated mutations
*/
//@{
template <typename fitT = float >
class eoESFullChrom : public eoVector<double, fitT> {
public:
/// constructor
eoESFullChrom( unsigned _num_genes = 1,
unsigned _num_sigma = 1, unsigned _num_correl = 0,
bool _verbose = false,
double _ObjMin = 0, double _ObjMax = 1,
double _StdDevInit = 0.3 ):
eoVector<double, fitT>(_num_genes),
// ObjVar( _num_genes ), now an eoVector<double>
StdDev( _num_sigma ),
CorCff( _num_correl ),
verbose( _verbose ),
ObjMin( _ObjMin ),
ObjMax(_ObjMax ),
StdDevInit( _StdDevInit ) {}
/// copy constructor
eoESFullChrom( const eoESFullChrom& _eo ):
eoVector<double, fitT> ( _eo ), // ObjVar ( _eo.ObjVar ),
StdDev ( _eo.StdDev ), CorCff( _eo.CorCff ), verbose( _eo.verbose ),
ObjMin( _eo.ObjMin ), ObjMax(_eo.ObjMax ), StdDevInit( _eo.StdDevInit ) {}
/* another constructor, for compatibility reasons */
eoESFullChrom(istream& _s) { cout << "Not Yet implemented\n";exit(1);};
/* And now the useful constructor: from a parser (should be in the
factory, if such a thing exists one day for eoESFullChrom
*/
eoESFullChrom(Parser & parser) : StdDev(0), CorCff(0) {
parser.AddTitle("Description of ES individuals");
int num_genes, num_sigma;
bool correlated_mutations;
try {
num_genes = parser.getInt("-Io", "--NbObjVar", "2",
"Number of Object Variables" );
num_sigma = parser.getInt("-Is", "--NbSigma", "1",
"Number of Standard Deviations" );
correlated_mutations = parser.getBool("-Ic", "--Correlated",
"Correlated mutation?" );
ObjMin = parser.getFloat("-Im", "--min", "0",
"Minimum value for object variables" );
ObjMax = parser.getFloat("-IM", "--max", "1",
"Maximum value for object variables" );
StdDevInit = parser.getFloat("-II", "--SigmaInit", "0.3",
"Initial value for std. dev. (scaled by range)" );
verbose = parser.getBool("-Iv", "--verbose",
"Verbose listing of ES individuals (mutation parameters");
}
catch (exception & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
// consistency tests
if (! num_sigma) { // no std dev??? EXCEPTION
throw invalid_argument( "No standard deviation: choose another representation please" );
}
if (num_sigma > num_genes) {
cout << "WARNING, Number of Standard Deviations > Number of Object Variables\nAdjusted!\n";
num_sigma = num_genes;
// modify the Param value - so .status is OK
ostrstream sloc;
sloc << num_genes;
parser.setParamValue("--NbSigma", sloc.str());
}
// adjust the sizes!!!
resize(num_genes);
if (num_sigma)
StdDev.resize(num_sigma);
if (correlated_mutations) {
if (num_sigma < num_genes) {
cout << "WARNING less Std Dev. than number of variables + Correlated mutations\n";
cout << "Though possible, this is a strange setting" << endl;
}
// nb of rotation angles: N*(N-1)/2 (in general!)
CorCff.resize ( (2*num_genes - num_sigma)*(num_sigma - 1) / 2 );
}
};
/// Operator =
const eoESFullChrom& operator = ( const eoESFullChrom& _eo ) {
if ( this != &_eo ) {
// Change EO part
eoVector<double, fitT>::operator = (_eo);
// Change this part
// ObjVar = _eo.ObjVar;
StdDev = _eo.StdDev;
CorCff = _eo.CorCff;
verbose = _eo.verbose;
ObjMin = _eo.ObjMin;
ObjMax = _eo.ObjMax;
StdDevInit = _eo.StdDevInit;
}
return *this;
}
/// destructor
virtual ~eoESFullChrom() {}
///
double getStdDev( unsigned _i ) const {
if ( _i >= length() )
throw out_of_range( "out_of_range when reading StdDev");
return StdDev[ _i ];
}
///
void setStdDev( unsigned _i, double _val ) {
if ( _i < length() ) {
StdDev[_i] = _val;
} else
throw out_of_range( "out_of_range when writing StdDev");
}
///
double getCorCff( unsigned _i ) const {
if ( _i >= length() )
throw out_of_range( "out_of_range when reading CorCff");
return CorCff[ _i ];
}
///
void setCorCff( unsigned _i, double _val ) {
if ( _i < length() ) {
CorCff[_i] = _val;
} else
throw out_of_range( "out_of_range when writing CorCff");
}
///
void insertGene( unsigned _i, double _val ) {
throw FixedLengthChromosome();
};
///
void deleteGene( unsigned _i ) {
throw FixedLengthChromosome();
};
///
unsigned length() const { return size();}/* formerly ObjVar.size() */
unsigned StdDevLength() const { return StdDev.size();}
unsigned CorCffLength() const { return CorCff.size();}
/** Print itself: inherited from eoObject implementation.
Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{
copy( begin(), end(), ostream_iterator<double>( _s, " ") );
// The formatting instructinos shoudl be left to the caller
// _s << "\n";
if (verbose) {
_s << "\n\tStd Dev. " ;
copy( StdDev.begin(), StdDev.end(), ostream_iterator<double>( _s, " ") );
if (CorCff.size()) {
_s << "\n\t";
copy( CorCff.begin(), CorCff.end(), ostream_iterator<double>( _s, " ") );
}
}
};
/** This exception should be thrown when trying to insert or delete a gene
in a fixed length chromosome
*/
class FixedLengthChromosome : public exception {
public:
/**
* Constructor
*/
FixedLengthChromosome()
: exception() { };
~FixedLengthChromosome() {};
};
// accessors
double getObjMin() const {return ObjMin;}
double getObjMax() const {return ObjMax;}
double getStdDevInit () const {return StdDevInit;}
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoESFullChrom";};
private:
// vector<double> ObjVar; /* object variable vector */
// or shoudl the class be subclass of EOVector<double> ???
vector<double> StdDev; /* standard deviation vector */
vector<double> CorCff; /* correlation coefficient vector */
bool verbose; /* Print std deviations or not */
/** the range is used for mutation AND random initialization,
* while the StdDevInit is used only for random initialization
* this in a little inconsistent!
*/
double ObjMin, ObjMax; /* Range for Object variables */
double StdDevInit; /* Initial value of Standard Deviations */
};
#endif

253
eo/src/es/eoESFullMut.h Normal file
View file

@ -0,0 +1,253 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoESMute.h : ES mutation
// (c) GeNeura Team, 1998 for the EO part
// Th. Baeck 1994 and EEAAX 1999 for the ES part
/*
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: todos@geneura.ugr.es, http://geneura.ugr.es
marc.schoenauer@polytechnique.fr
http://eeaax.cmap.polytchnique.fr/
*/
//-----------------------------------------------------------------------------
#ifndef _EOESMUT_H
#define _EOESMUT_H
#include <eoParser.h>
#include <eoRNG.h>
#include <cmath> // for exp
#include <eoESFullChrom.h>
#include <eoOp.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
const double ES_SIGEPS = 1.0e-40; /* ES lower bound for sigma values */
// should not be a parameter ...
/** ES-style mutation in the large: Obviously, valid only for eoESInd
*/
template <class fitT>
class eoESMutate: public eoMonOp< eoESFullChrom<fitT> > {
public:
eoESMutate(double n)
{
TauLcl = 1/sqrt(2*sqrt(n));
TauGlb= 1 / sqrt(2 * n);
TauBeta = 0.0873;
}///
eoESMutate( double _TauLcl, double _TauGlb, double _TauBeta )
: eoMonOp< eoESFullChrom<fitT> >( ), TauLcl(_TauLcl), TauGlb(_TauGlb),
TauBeta(_TauBeta) {};
/* The parser constructor
*/
eoESMutate(Parser & parser, unsigned _stdDevLength, unsigned _size, bool _correlated ):
eoMonOp< eoESFullChrom<fitT> >( ) {
parser.AddTitle("Parameters of ES mutation (before renormalization)");
try { // we know that there is at least 1 std dev.
if (_stdDevLength == 1) {
TauLcl = parser.getInt("-Ml", "--TauLcl", "1",
"TauLcl, Mutation rate for the only Std Dev." );
// different normalization in that case -- Thomas Baeck
TauLcl /= sqrt((double) _size);
}
else { /* more than 1 std dev */
TauLcl = parser.getFloat("-Ml", "--TauLcl", "1",
"Local mutation rate for Std Dev." );
TauGlb = parser.getFloat("-Mg", "--TauGlb", "1",
"Global mutation rate for Std Dev." );
// renormalization
TauLcl /= sqrt( 2.0 * sqrt( (double)_size ) );
TauGlb /= sqrt( 2.0 * ( (double) _size ) );
if ( _correlated ) { // Correlated Mutations
TauBeta = parser.getFloat("-Mb", "--TauBeta", "0.0873",
"Mutation rate for corr. coeff." );
// rotation angles: no normalization
}
}
}
catch (exception & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
};
/// needed virtual dtor
virtual ~eoESMutate() {};
// virtual separation depending wether correlated mutations are present
virtual void operator() ( eoESFullChrom<fitT> & _eo ) const {
if (_eo.CorCffLength())
CorrelatedMutation(_eo);
else
StandardMutation(_eo);
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoESMutate";};
private:
/// mutations - standard et correlated
// =========
/*
* Standard mutation of object variables and standard
* deviations in ESs.
* If there are fewer different standard deviations available
* than the dimension of the objective function requires, the
* last standard deviation is responsible for ALL remaining
* object variables.
* Schwefel 1977: Numerische Optimierung von Computer-Modellen
* mittels der Evolutionsstrategie, pp. 165 ff.
*/
virtual void StandardMutation( eoESFullChrom<fitT> & _eo ) const {
unsigned i,k;
double Glb, StdLoc;
if (_eo.StdDevLength() == 1) { /* single StdDev -> No global factor */
StdLoc = _eo.getStdDev(0);
StdLoc *= exp(TauLcl*rng.normal());
if (StdLoc < ES_SIGEPS)
StdLoc = ES_SIGEPS;
_eo.setStdDev(0, StdLoc);
_eo.setGene( 0, _eo.getGene(0) + StdLoc*rng.normal());
i = 1;
}
else { /* more than one std dev. */
Glb = exp(TauGlb*rng.normal());
for (i = 0; i < _eo.length() && i < _eo.StdDevLength(); i++) {
StdLoc = _eo.getStdDev(i);
StdLoc *= Glb * exp(TauLcl*rng.normal());
if (StdLoc < ES_SIGEPS)
StdLoc = ES_SIGEPS;
_eo.setStdDev(i, StdLoc);
_eo.setGene( i, _eo.getGene(i) + StdLoc*rng.normal());
}
}
// last object variables: same STdDev than the preceding one
for (k = i; k < _eo.length(); k++) {
_eo.setGene( k, _eo.getGene(k) + StdLoc*rng.normal() );
}
}
/*
* Correlated mutations in ESs, according to the following
* sources:
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
* p. 43, 1980
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
* strategien, Diploma Thesis, University of Dortmund, 1990
*/
// Code from Thomas Baeck
virtual void CorrelatedMutation( eoESFullChrom<fitT> & _eo ) const {
int i, k, n1, n2, nq;
double d1, d2, S, C, Glb;
double tmp;
/*
* First: mutate standard deviations (as above).
*/
Glb = exp(TauGlb*rng.normal());
for (i = 0; i < _eo.StdDevLength(); i++) {
tmp = _eo.getStdDev(i);
_eo.setStdDev( i, tmp*Glb*exp(TauLcl*rng.normal()) );
}
/*
* Mutate rotation angles.
*/
for (i = 0; i < _eo.CorCffLength(); i++) {
tmp = _eo.getCorCff(i);
tmp += TauBeta*rng.normal();
// danger of VERY long loops --MS--
// while (CorCff[i] > M_PI)
// CorCff[i] -= 2.0 * M_PI;
// while (CorCff[i] < - M_PI)
// CorCff[i] += 2.0 * M_PI;
if ( fabs(tmp) > M_PI ) {
tmp -= M_PI * (int) (tmp/M_PI) ;
}
_eo.setCorCff(i, tmp);
}
/*
* Perform correlated mutations.
*/
vector<double> VarStp(_eo.size());
for (i = 0; i < _eo.size() && i < _eo.StdDevLength(); i++)
VarStp[i] = _eo.getStdDev(i)*rng.normal();
for (k = i; k < _eo.size(); k++)
VarStp[k] = _eo.getStdDev(i-1)*rng.normal();
nq = _eo.CorCffLength() - 1;
for (k = _eo.size()-_eo.StdDevLength(); k < _eo.size()-1; k++) {
n1 = _eo.size() - k - 1;
n2 = _eo.size() - 1;
for (i = 0; i < k; i++) {
d1 = VarStp[n1];
d2 = VarStp[n2];
S = sin( _eo.getCorCff(nq) );
C = cos( _eo.getCorCff(nq) );
VarStp[n2] = d1 * S + d2 * C;
VarStp[n1] = d1 * C - d2 * S;
n2--;
nq--;
}
}
for (i = 0; i < _eo.size(); i++)
_eo[i] += VarStp[i];
}
// the data
//=========
double TauLcl; /* Local factor for mutation of std deviations */
double TauGlb; /* Global factor for mutation of std deviations */
double TauBeta; /* Factor for mutation of correlation parameters */
};
/*
* Correlated mutations in ESs, according to the following
* sources:
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
* p. 43, 1980
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
* strategien, Diploma Thesis, University of Dortmund, 1990
*/
// Not yet implemented!
#endif