This commit is contained in:
commit
3fe0218a72
47 changed files with 12547 additions and 0 deletions
6
eo/src/.cvsignore
Normal file
6
eo/src/.cvsignore
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
*.lo
|
||||
*.la
|
||||
.deps
|
||||
.libs
|
||||
Makefile
|
||||
Makefile.in
|
||||
123
eo/src/EO.h
Normal file
123
eo/src/EO.h
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// EO.h
|
||||
// (c) GeNeura Team 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EO_H
|
||||
#define EO_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
#include <eoObject.h>
|
||||
#include <eoPersistent.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** EO is a base class for evolvable objects, that is, the subjects of evolution.
|
||||
EOs have only got a fitness, which at the same time needs to be only an object with the
|
||||
operation less than (<) defined. Fitness says how good is the object; evolution or change
|
||||
of these objects is left to the genetic operators. A fitness less than another means a
|
||||
worse fitness, in whatever the context; thus, fitness is always maximized; although
|
||||
it can be minimized with a proper definition of the < operator.\\
|
||||
The fitness object must have, besides an void ctor, a copy ctor.
|
||||
*/
|
||||
template<class F> class EO: public eoObject, public eoPersistent
|
||||
{
|
||||
public:
|
||||
typedef F Fitness;
|
||||
|
||||
/** Default constructor.
|
||||
Fitness must have a ctor which takes 0 as a value; we can not use void ctors here
|
||||
since default types like float have no void initializer. VC++ allows it, but gcc does not
|
||||
*/
|
||||
EO(): repFitness(0), invalidFitness(true) {}
|
||||
|
||||
/** Ctor from stream.
|
||||
Fitness must have defined the lecture from an istream.
|
||||
*/
|
||||
EO( istream& _is ) {
|
||||
_is >> repFitness;
|
||||
validFitness = true;
|
||||
};
|
||||
|
||||
/// Copy ctor
|
||||
EO( const EO& _eo ): repFitness( _eo.repFitness ), invalidFitness( _eo.invalidFitness ) {};
|
||||
|
||||
/// Virtual dtor
|
||||
virtual ~EO() {};
|
||||
|
||||
/// Return fitness value.
|
||||
Fitness fitness() const
|
||||
{
|
||||
if (invalid())
|
||||
throw runtime_error("invalid fitness");
|
||||
return repFitness;
|
||||
}
|
||||
|
||||
// Set fitness as invalid.
|
||||
void invalidate() { invalidFitness = true; }
|
||||
|
||||
/** Set fitness. At the same time, validates it.
|
||||
* @param _fitness New fitness value.
|
||||
*/
|
||||
void fitness(const Fitness& _fitness)
|
||||
{
|
||||
repFitness = _fitness;
|
||||
invalidFitness = false;
|
||||
}
|
||||
|
||||
/** Return true If fitness value is invalid, false otherwise.
|
||||
* @return true If fitness is invalid.
|
||||
*/
|
||||
bool invalid() const { return invalidFitness; }
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
//@{
|
||||
|
||||
/** Return the class id.
|
||||
* @return the class name as a string
|
||||
*/
|
||||
virtual string className() const { return "EO"; };
|
||||
|
||||
/**
|
||||
* Read object.\\
|
||||
* Calls base class, just in case that one had something to do. The read and print
|
||||
* methods should be compatible and have the same format. In principle, format is
|
||||
* "plain": they just print a number
|
||||
* @param _is a istream.
|
||||
* @throw runtime_exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
_is >> repFitness;
|
||||
invalidFitness = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << repFitness << endl;
|
||||
}
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
Fitness repFitness; // value of fitness for this chromosome
|
||||
bool invalidFitness; // true if the value of fitness is invalid
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> bool operator<(const EO<T>& eo1, const EO<T>& eo2)
|
||||
{
|
||||
return eo1.fitness() < eo2.fitness();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#endif EO_H
|
||||
10
eo/src/Makefile.am
Normal file
10
eo/src/Makefile.am
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
###############################################################################
|
||||
##
|
||||
## Makefile.am for eo/src
|
||||
##
|
||||
###############################################################################
|
||||
|
||||
lib_LTLIBRARIES = libeo.la
|
||||
libeo_la_SOURCES = eoObject.cpp eoPersistent.cpp eoPrintable.cpp
|
||||
libeoincdir = $(includedir)/eo
|
||||
libeoinc_HEADERS = eo EO.h eoDup.h eoMultiMonOp.h eoPop.h eoUniform.h eoESChrom.h eoNegExp.h eoProblem.h eoVector.h eoFitness.h eoNormal.h eoRnd.h eoXOver2.h eo1d.h eoID.h eoObject.h eoString.h eoAged.h eoKill.h eoOp.h eoTranspose.h eoBin.h
|
||||
50
eo/src/eo
Normal file
50
eo/src/eo
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eo
|
||||
// (c) GeNeura Team 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eo_
|
||||
#define _eo_
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoObject.h>
|
||||
#include <eoPrintable.h>
|
||||
#include <eoPersistent.h>
|
||||
#include <EO.h>
|
||||
|
||||
#include <eoID.h>
|
||||
#include <eoAged.h>
|
||||
|
||||
#include <eoVector.h>
|
||||
#include <eo1d.h>
|
||||
#include <eoString.h>
|
||||
#include <eoESChrom.h>
|
||||
#include <eoBin.h>
|
||||
|
||||
#include <eoRnd.h>
|
||||
#include <eoUniform.h>
|
||||
#include <eoNormal.h>
|
||||
#include <eoNegExp.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
#include <eoMultiMonOp.h>
|
||||
|
||||
#include <eoDup.h>
|
||||
#include <eoKill.h>
|
||||
#include <eoTranspose.h>
|
||||
#include <eoXOver2.h>
|
||||
#include <eoMutation.h>
|
||||
|
||||
#include <eoPop.h>
|
||||
#include <eoPopOps.h>
|
||||
|
||||
#include <eoFitness.h> // what's the matter with you?
|
||||
#include <eoProblem.h> // what's the matter with you?
|
||||
|
||||
//#include <eoGA.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif _eo_
|
||||
|
||||
164
eo/src/eo1d.h
Normal file
164
eo/src/eo1d.h
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eo1d.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EO1D_H
|
||||
#define _EO1D_H
|
||||
|
||||
#include <iostream> // for ostream
|
||||
|
||||
// EO Includes
|
||||
#include <EO.h>
|
||||
#include <eoRnd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** @name EO1d class
|
||||
* Randomly accesible evolvable object with one dimension, with
|
||||
variable length.
|
||||
* Use this if you want to evolve "linear" things, like bitstrings, or
|
||||
floating-point arrays. If you don't, subclass directly from EO
|
||||
* @see EO
|
||||
* @author GeNeura
|
||||
* @version 0.2
|
||||
*/
|
||||
|
||||
//@{
|
||||
|
||||
/** eo1d: Base class for "chromosomes" with a single dimension
|
||||
#T# is the type it will be instantiated with; this type must have, at
|
||||
least, a copy ctor, assignment operators,
|
||||
*/
|
||||
template<class T, class fitnessT = float>
|
||||
class eo1d: public EO< fitnessT > {
|
||||
public:
|
||||
|
||||
/// Declaration to make it accessible from subclasses
|
||||
typedef T Type;
|
||||
|
||||
/** Can be used as default ctor; should be called from derived
|
||||
classes. Fitness should be at birth
|
||||
*/
|
||||
eo1d()
|
||||
:EO<fitnessT> ( ) {};
|
||||
|
||||
/** Ctor using a random number generator and with an specified size
|
||||
@param _rndGen Random number generator
|
||||
@param _size lineal dimension of the eo1d
|
||||
@param _ID An ID string, preferably unique
|
||||
*/
|
||||
eo1d( unsigned _size, eoRnd<T>& _rndGen, const string& _ID = "");
|
||||
|
||||
/** Ctor from a istream. It just passes the stream to EO.
|
||||
@param _is the input stream; eo1d just cares about the fitness
|
||||
*/
|
||||
eo1d( istream& _is): EO<fitnessT>( _is ){};
|
||||
|
||||
/// Copy ctor
|
||||
eo1d( const eo1d& _eo )
|
||||
:EO<fitnessT> ( _eo ) {};
|
||||
|
||||
/// Assignment operator
|
||||
const eo1d& operator= ( const eo1d& _eo ) {
|
||||
EO<fitnessT>::operator = ( _eo );
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Needed virtual dtor
|
||||
virtual ~eo1d(){};
|
||||
|
||||
/** Reads and returns a copy of the gene in position _i
|
||||
This implies that T must have a copy ctor .
|
||||
@param _i index of the gene, which is the minimal unit. Must be
|
||||
an unsigned less than #length()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@exception out_of_range if _i > size()
|
||||
*/
|
||||
virtual T gene( unsigned _i ) const = 0;
|
||||
|
||||
/** Overwrites the gene placed in position _i with a
|
||||
* new value. This means that the assignment operator
|
||||
* for T must be defined .
|
||||
@param _i index
|
||||
@return what's inside the gene, with the correct type
|
||||
@exception out_of_range if _i > size()
|
||||
*/
|
||||
virtual T& gene( unsigned _i ) = 0;
|
||||
|
||||
/** Inserts a gene, moving the rest to the right. If
|
||||
* _i = length(), it should insert it at the end.
|
||||
* Obviously, changes length
|
||||
@param _i index
|
||||
@param _val new value
|
||||
*/
|
||||
virtual void insertGene( unsigned _i, T _val ) = 0;
|
||||
|
||||
/** Eliminates the gene at position _i; all the other genes will
|
||||
be shifted left
|
||||
@param _i index of the gene that is going to be modified.
|
||||
*/
|
||||
virtual void deleteGene( unsigned _i ) = 0;
|
||||
|
||||
/// Returns the number of genes in the eo1d
|
||||
virtual unsigned length() const = 0;
|
||||
|
||||
/// @name Methods from eoObject
|
||||
//@{
|
||||
/**
|
||||
* Read object. Theoretically, the length is known in advance. All objects
|
||||
* Should call base class
|
||||
* @param _s A istream.
|
||||
* @throw runtime_exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(istream& _s) {
|
||||
|
||||
for ( unsigned i = 0; i < length(); i ++ ) {
|
||||
_s >> gene( i );
|
||||
}
|
||||
// there is no way of distinguishing fitness from the object, so
|
||||
// it's skipped
|
||||
}
|
||||
|
||||
/** Print itself: inherited from eoObject implementation. Declared virtual so that
|
||||
it can be reimplemented anywhere. 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{
|
||||
for ( unsigned i = 0; i < length(); i ++ ) {
|
||||
_s << gene( i ) << " ";
|
||||
}
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eo1d";};
|
||||
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
// --------------- Implementations --------------------------
|
||||
|
||||
/* Ctor using a random number generator and with an specified size
|
||||
@param _rndGen Random number generator
|
||||
@param _size lineal dimension of the eo1d
|
||||
@param _ID An ID string, preferably unique
|
||||
*/
|
||||
template< class T, class fitnessT>
|
||||
eo1d<T,fitnessT>::eo1d<T,fitnessT>( unsigned _size, eoRnd<T>& _rndGen,
|
||||
const string& _ID )
|
||||
:EO<fitnessT> ( _ID ) {
|
||||
for ( unsigned i = 0; i < size; i ++ ) {
|
||||
insertGene( i, _rndGen() );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
90
eo/src/eoAged.h
Normal file
90
eo/src/eoAged.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAge.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOAGED_H
|
||||
#define EOAGED_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // istream, ostream
|
||||
#include <string> // para string
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAge
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoAge is a template class that adds an age to an object.\\
|
||||
Requisites for template instantiation are that the object must admit a default ctor
|
||||
and a copy ctor. The Object must be an eoObject, thus, it must have its methods: className,
|
||||
printOn, readFrom.
|
||||
@see eoObject
|
||||
*/
|
||||
template <class Object>
|
||||
class eoAged: public Object
|
||||
{
|
||||
public:
|
||||
/// Main ctor from an already built Object.
|
||||
eoAged( const Object& _o): Object( _o ), age(0) {};
|
||||
|
||||
/// Copy constructor.
|
||||
eoAged( const eoAged& _a): Object( _a ), age( _a.age ) {};
|
||||
|
||||
/// Virtual dtor. They are needed in virtual class hierarchies
|
||||
virtual ~eoAged() {};
|
||||
|
||||
|
||||
///returns the age of the object
|
||||
unsigned long Age() const {return age;}
|
||||
|
||||
/// Increments age
|
||||
const eoAged& operator ++ () { age++; return *this;}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Return the class id. This should be redefined in each class; but
|
||||
it's got code as an example of implementation. Only "leaf" classes
|
||||
can be non-virtual.
|
||||
*/
|
||||
virtual string className() const { return string("eoAged")+Object::className(); };
|
||||
|
||||
/**
|
||||
* Read object.
|
||||
* @param _is A istream.
|
||||
* @throw runtime_exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
Object::readFrom( _is );
|
||||
_is >> age;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const{
|
||||
Object::printOn( _os );
|
||||
_os << age;
|
||||
}
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
||||
/** Default Constructor. \\
|
||||
It´s private so that it is not used anywhere; the right way of using this object
|
||||
is to create an Object and passing it to an aged by means of the copy ctor; that way
|
||||
it´s turned into an Aged object*/
|
||||
eoAged(): Object(), age(0) {};
|
||||
|
||||
unsigned long age;
|
||||
};
|
||||
|
||||
#endif EOAGE_H
|
||||
387
eo/src/eoBin.h
Normal file
387
eo/src/eoBin.h
Normal file
|
|
@ -0,0 +1,387 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoBin.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoBin_h
|
||||
#define eoBin_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // ostream, istream
|
||||
#include <bvector.h> // bit_vector
|
||||
#include <string> // string
|
||||
#include <EO.h> // EO
|
||||
#include <eoOp.h> // eoMonOp
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <eoRnd.h> // eoRnd
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class F> class eoBin: public EO<F>, public bit_vector
|
||||
{
|
||||
public:
|
||||
typedef bool Type;
|
||||
|
||||
/// (Default) Constructor.
|
||||
/// @param size Size of the binary string.
|
||||
eoBin(const unsigned& size = 0, const bool& value = false):
|
||||
bit_vector(size, value) {}
|
||||
|
||||
/// (Default) Constructor.
|
||||
/// @param size Size of the binary string.
|
||||
eoBin(const unsigned& size, const eoRnd<Type>& rnd): bit_vector(size)
|
||||
{
|
||||
generate(begin(), end(), rnd);
|
||||
}
|
||||
|
||||
/// Constructor from istream.
|
||||
/// @param is The istream to read from.
|
||||
eoBin(istrstream& is)
|
||||
{
|
||||
readFrom(is);
|
||||
}
|
||||
|
||||
/// My class name.
|
||||
string className() const
|
||||
{
|
||||
return "eoBin";
|
||||
}
|
||||
|
||||
/// To print me on a stream.
|
||||
/// @param os The ostream.
|
||||
void printOn(ostream& os) const
|
||||
{
|
||||
copy(begin(), end(), ostream_iterator<bool>(os));
|
||||
}
|
||||
|
||||
/// To read me from a stream.
|
||||
/// @param is The istream.
|
||||
void readFrom(istream& is)
|
||||
{
|
||||
string bits;
|
||||
is >> bits;
|
||||
if (is)
|
||||
{
|
||||
resize(bits.size());
|
||||
transform(bits.begin(), bits.end(), begin(),
|
||||
bind2nd(equal_to<char>(), '1'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinRandom --> mofify a chromosome in a random way
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinRandom"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
chrom[i] = (uniform() < 0.5) ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinBitFlip --> chages a bit
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinBitFlip"; }
|
||||
|
||||
///
|
||||
void eoBinBitFlip::operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size());
|
||||
unsigned pos = uniform();
|
||||
chrom[pos] = !chrom[pos];
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinMutation --> classical mutation
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinMutation"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
if (uniform() < rate)
|
||||
chrom[i] = !chrom[i];
|
||||
}
|
||||
|
||||
private:
|
||||
double rate;
|
||||
mutable eoUniform<float> uniform;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinInversion --> inverts the bits of the chromosome between an interval
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinInversion"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size() + 1);
|
||||
|
||||
unsigned u1 = uniform(), u2;
|
||||
do u2 = uniform(); while (u1 == u2);
|
||||
unsigned r1 = min(u1, u2), r2 = max(u1, u2);
|
||||
|
||||
reverse(chrom.begin() + r1, chrom.begin() + r2);
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinNext --> next binary value
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinNext: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinNext"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinPrev --> previos binary value
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinPrev"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (int i = chrom.size() - 1; i >= 0; i--)
|
||||
if (chrom[i])
|
||||
{
|
||||
chrom[i] = 0;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
chrom[i] = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinCrossover --> classic crossover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
string className() const { return "eoBinCrossover"; }
|
||||
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size()));
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin());
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinNxOver --> n-point crossover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Defualt) Constructor.
|
||||
eoBinNxOver(const unsigned& _num_points = 2): num_points(_num_points)
|
||||
{
|
||||
if (num_points < 1)
|
||||
{
|
||||
cerr << "NxOver --> invalid number of points " << num_points << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinNxOver"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_size = min(chrom1.size(), chrom2.size());
|
||||
unsigned max_points = min(max_size - 1, num_points);
|
||||
|
||||
vector<bool> points(max_size, false);
|
||||
eoUniform<unsigned> uniform(1, max_size);
|
||||
|
||||
// select ranges of bits to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
max_points--;
|
||||
}
|
||||
} while (max_points);
|
||||
|
||||
|
||||
// swap bits between chromosomes
|
||||
bool change = false;
|
||||
for (unsigned bit = 1; bit < points.size(); bit++)
|
||||
{
|
||||
if (points[bit])
|
||||
change = !change;
|
||||
|
||||
if (change)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinGxOver --> gene crossover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoBinGxOver(const unsigned _gene_size, const unsigned _num_points = 2):
|
||||
gene_size(_gene_size), num_points(_num_points)
|
||||
{
|
||||
if (gene_size < 1)
|
||||
{
|
||||
cerr << "GxOver --> invalid gene size " << gene_size << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (num_points < 1)
|
||||
{
|
||||
cerr << "GxOver --> invalid number of points " << num_points << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/// The class name
|
||||
string className() const { return "eoBinGxOver"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size;
|
||||
unsigned cut_genes = min(max_genes, num_points);
|
||||
|
||||
vector<bool> points(max_genes, false);
|
||||
eoUniform<unsigned> uniform(0, max_genes);
|
||||
|
||||
// selects genes to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
{
|
||||
points[bit] = true;
|
||||
cut_genes--;
|
||||
}
|
||||
} while (cut_genes);
|
||||
|
||||
// swaps genes
|
||||
for (unsigned i = 0; i < points.size(); i++)
|
||||
if (points[i])
|
||||
swap_ranges(chrom1.begin() + i * gene_size,
|
||||
chrom1.begin() + i * gene_size + gene_size,
|
||||
chrom2.begin() + i * gene_size);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned gene_size;
|
||||
unsigned num_points;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBinUxOver --> uniform crossover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoBinUxOver(const float _rate): rate(_rate)
|
||||
{
|
||||
if (rate < 0 || rate > 1)
|
||||
{
|
||||
cerr << "UxOver --> invalid rate " << rate << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinUxOver"; }
|
||||
|
||||
///
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned min_size = min(chrom1.size(), chrom2.size());
|
||||
eoUniform<float> uniform(0, 1);
|
||||
|
||||
for (unsigned bit = 0; bit < min_size; bit++)
|
||||
if (uniform() < rate)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
|
||||
public:
|
||||
float rate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#endif eoBin_h
|
||||
45
eo/src/eoDup.h
Normal file
45
eo/src/eoDup.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoKill.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EODUP_h
|
||||
#define _EODUP_h
|
||||
|
||||
#include <eoUniform.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/// Dup or duplicate: duplicates a gene in a chromosome
|
||||
template <class EOT>
|
||||
class eoDup: public eoMonOp<EOT> {
|
||||
public:
|
||||
///
|
||||
eoDup( )
|
||||
: eoMonOp< EOT >( ){};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoDup() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
eoUniform<unsigned> uniform( 0, _eo.length() );
|
||||
unsigned pos = uniform();
|
||||
_eo.insertGene( pos, _eo.gene(pos) );
|
||||
}
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoDup";};
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
108
eo/src/eoESChrom.h
Normal file
108
eo/src/eoESChrom.h
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESChrom.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#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
|
||||
34
eo/src/eoEvalFunc.h
Normal file
34
eo/src/eoEvalFunc.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvalFunc.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoEvalFunc_H
|
||||
#define eoEvalFunc_H
|
||||
|
||||
/** Evaluate: takes one EO and sets its "fitness" property
|
||||
* returning this fitness also. That is why EOT is passed by
|
||||
* non-const reference: it must be altered within evaluate.\\
|
||||
|
||||
The requirements on the types with which this class is to be
|
||||
instantiated with are null, or else, they depend on the particular
|
||||
class it's going to be applied to; EO does not impose any requirement
|
||||
on it. If you subclass this abstract class, and use it to evaluate an
|
||||
EO, the requirements on this EO will depend on the evaluator.
|
||||
*/
|
||||
template< class EOT >
|
||||
struct eoEvalFunc {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Fitness EOFitT;
|
||||
#else
|
||||
typedef typename Fitness::EOFitT EOFitT;
|
||||
#endif
|
||||
|
||||
/// Effectively applies the evaluation function to an EO or urEO
|
||||
virtual EOFitT evaluate( EOT & _eo ) const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
53
eo/src/eoEvaluator.h
Normal file
53
eo/src/eoEvaluator.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvaluator.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOEVALUATOR_H
|
||||
#define _EOEVALUATOR_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoPopOps.h>
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Evaluator takes a vector of EOs and evaluates its fitness
|
||||
* returning void. Template instances must be of fitness and EO type
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoEvaluator: public eoTransform<EOT>{
|
||||
public:
|
||||
/// ctor
|
||||
eoEvaluator( const eoEvalFunc< EOT> & _ef )
|
||||
: eoTransform<EOT>(), repEF( _ef ){};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoEvaluator() {};
|
||||
|
||||
/* Sets the evaluation function
|
||||
virtual void EF( const eoEvalFunc< EOT> & _ef ) { repEF= _ef;};*/
|
||||
|
||||
/// Gets the evaluation function
|
||||
virtual const eoEvalFunc< EOT>& EF() { return repEF;};
|
||||
|
||||
/** This is the actual function operator(); it is left without implementation.
|
||||
It takes a vector of pointers to eo
|
||||
* @param _vEO is a vector of pointers to eo, that will be evaluated
|
||||
*/
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
/** Return the class id */
|
||||
virtual string className() const { return "eoEvaluator"; }
|
||||
|
||||
/** Read and print are left without implementation */
|
||||
//@}
|
||||
|
||||
private:
|
||||
const eoEvalFunc< EOT> & repEF;
|
||||
};
|
||||
//@}
|
||||
|
||||
#endif
|
||||
55
eo/src/eoFactory.h
Normal file
55
eo/src/eoFactory.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoFactory.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOFACTORY_H
|
||||
#define _EOFACTORY_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoObject.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** EO Factory. A factory is used to create other objects. In particular,
|
||||
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
|
||||
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,
|
||||
as usual. The base factory class for each hierarchy should be redefined every time a new
|
||||
object is added to the hierarchy, which is not too good, but in any case, some code would
|
||||
have to be modified*/
|
||||
template<class EOClass>
|
||||
class eoFactory: public eoObject {
|
||||
|
||||
public:
|
||||
|
||||
/// @name ctors and dtors
|
||||
//{@
|
||||
/// constructor
|
||||
eoFactory( ) {}
|
||||
|
||||
/// destructor
|
||||
virtual ~eoFactory() {}
|
||||
//@}
|
||||
|
||||
/** Another factory methods: creates an object from an istream, reading from
|
||||
it whatever is needed to create the object. Usually, the format for the istream will be\\
|
||||
objectType parameter1 parameter2 ... parametern\\
|
||||
*/
|
||||
virtual EOClass* make(istream& _is) = 0;
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
/** Return the class id */
|
||||
virtual string className() const { return "eoFactory"; }
|
||||
|
||||
/** Read and print are left without implementation */
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif _EOFACTORY_H
|
||||
36
eo/src/eoFitness.h
Normal file
36
eo/src/eoFitness.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoFitness.cpp
|
||||
// (c) GeNeura Team 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOFITNESS_H
|
||||
#define EOFITNESS_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoFitness: public eoPersistent
|
||||
{
|
||||
public:
|
||||
virtual bool operator<(const eoFitness& other) const = 0;
|
||||
|
||||
bool operator>(const eoFitness& other) const
|
||||
{
|
||||
return !(*this < other || *this == other);
|
||||
}
|
||||
|
||||
bool operator==(const eoFitness& other) const
|
||||
{
|
||||
return !(other < *this || *this < other);
|
||||
}
|
||||
|
||||
bool operator!=(const eoFitness& other) const
|
||||
{
|
||||
return other < *this || *this < other;
|
||||
}
|
||||
|
||||
virtual operator float() const = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EOFITNESS_H
|
||||
43
eo/src/eoGA.h
Normal file
43
eo/src/eoGA.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoGA.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoGA_h
|
||||
#define eoGA_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGA
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoGA
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGA(eoSelect& _select, eoTranform& _transform, eoMerge& _replace)
|
||||
{
|
||||
}
|
||||
|
||||
///
|
||||
void operator()(eoPop& pop)
|
||||
{
|
||||
eoPop breeders;
|
||||
|
||||
select(pop, breeders);
|
||||
transform(breeders);
|
||||
replace(breeders, pop);
|
||||
}
|
||||
|
||||
private:
|
||||
eoSelect& select;
|
||||
eoTranform& transform;
|
||||
eoMerge& replace;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoGA_h
|
||||
92
eo/src/eoID.h
Normal file
92
eo/src/eoID.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoID.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOID_H
|
||||
#define EOID_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // istream, ostream
|
||||
#include <string> // for string
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoID
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoID is a template class that adds an ID to an object.\\
|
||||
Requisites for template instantiation are that the object must admit a default ctor
|
||||
and a copy ctor. The Object must be an eoObject, thus, it must have its methods: className,
|
||||
printOn, readFrom, that is why we don´t subclass eoObject to avoid multiple inheritance.\\
|
||||
IDs can be used to count objects of a a kind, or track them, or whatever.
|
||||
@see eoObject
|
||||
*/
|
||||
template <class Object>
|
||||
class eoID: public Object
|
||||
{
|
||||
public:
|
||||
/// Main ctor from an already built Object.
|
||||
eoID( const Object& _o): Object( _o ), thisID(globalID++) {};
|
||||
|
||||
/// Copy constructor.
|
||||
eoID( const eoID& _id): Object( _id ), thisID(globalID++ ) {};
|
||||
|
||||
/// Virtual dtor. They are needed in virtual class hierarchies
|
||||
virtual ~eoID() {};
|
||||
|
||||
|
||||
///returns the age of the object
|
||||
unsigned long ID() const {return thisID;}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Return the class id. This should be redefined in each class; but
|
||||
it's got code as an example of implementation. Only "leaf" classes
|
||||
can be non-virtual.
|
||||
*/
|
||||
virtual string className() const { return string("[eoID]")+Object::className(); };
|
||||
|
||||
/**
|
||||
* Read object.
|
||||
* @param _is A istream.
|
||||
* @throw runtime_exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
Object::readFrom( _is );
|
||||
_is >> thisID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A ostream.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const{
|
||||
Object::printOn( _os );
|
||||
_os << thisID;
|
||||
}
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
||||
/** Default Constructor. \\
|
||||
It´s private so that it is not used anywhere; the right way of using this object
|
||||
is to create an Object and passing it to an aged by means of the copy ctor; that way
|
||||
it´s turned into an Aged object*/
|
||||
eoID(): Object(), thisID( globalID++ ) {};
|
||||
|
||||
unsigned long thisID;
|
||||
static unsigned long globalID;
|
||||
};
|
||||
|
||||
template< class Object>
|
||||
unsigned long eoID< Object >::globalID = 0;
|
||||
|
||||
#endif EOID_H
|
||||
44
eo/src/eoKill.h
Normal file
44
eo/src/eoKill.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoKill.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOKILL_h
|
||||
#define _EOKILL_h
|
||||
|
||||
#include <eoUniform.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/// Kill eliminates a gen in a chromosome
|
||||
template <class EOT >
|
||||
class eoKill: public eoMonOp<EOT> {
|
||||
public:
|
||||
///
|
||||
eoKill( )
|
||||
: eoMonOp< EOT >(){};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoKill() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
eoUniform<unsigned> uniform( 0, _eo.length() );
|
||||
unsigned pos = uniform( );
|
||||
_eo.deleteGene( pos );
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoKill";};
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
60
eo/src/eoLottery.h
Normal file
60
eo/src/eoLottery.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoLottery.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoLottery_h
|
||||
#define eoLottery_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eo>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoLottery
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
||||
{
|
||||
public:
|
||||
eoLottery(const float& rate = 1.0): eoLottery(rate) {}
|
||||
|
||||
void operator()(const eoPop<Chrom>& pop, eoPop<Chrom>& breeders)
|
||||
{
|
||||
// scores of chromosomes
|
||||
vector<float> score(pop.size());
|
||||
|
||||
// calculates accumulated scores for chromosomes
|
||||
transform(pop.begin(), pop.end(), score.begin(), fitness);
|
||||
float sum = accumulate(score.begin(), score.end(), MINFLOAT);
|
||||
transform(score.begin(), score.end(), score.begin(),
|
||||
bind2nd(divides<float>(), sum));
|
||||
partial_sum(score.begin(), score.end(), score.begin());
|
||||
|
||||
// generates random numbers
|
||||
vector<float> random(pop.size());
|
||||
generate(random.begin(), random.end(), Uniform<float>(0,1));
|
||||
sort(random.begin(), random.end(), less<float>());
|
||||
|
||||
// selection of chromosomes
|
||||
unsigned score_index = 0; // position in score vector
|
||||
unsigned random_index = 0; // position in random vector
|
||||
unsigned num_chroms = (unsigned)(rate * pop.size());
|
||||
do {
|
||||
if(random[random_index] < score[score_index])
|
||||
{
|
||||
breeders.push_back(pop[score_index]);
|
||||
random_index++;
|
||||
}
|
||||
else
|
||||
if (score_index < pop.size())
|
||||
score_index++;
|
||||
else
|
||||
fill_n(back_insert_iterator<Pop>(breeders),
|
||||
num_chroms - breeders.size(), pop.back());
|
||||
} while (breeders.size() < num_chroms);
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoLottery_h
|
||||
63
eo/src/eoMerge.h
Normal file
63
eo/src/eoMerge.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoMerge.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoMerge_h
|
||||
#define eoMerge_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class Insertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
eoInsertion(const float& _rate = 1): eoMerge<Chrom>(rate) {}
|
||||
|
||||
bool compare(const Chrom& chrom1, const Chrom& chrom2)
|
||||
{
|
||||
return chrom1.fitness() < chrom2.fitness();
|
||||
}
|
||||
|
||||
void operator()(const Pop& breeders, Pop& pop)
|
||||
{
|
||||
sort(pop.begin(), pop.end() compare);
|
||||
|
||||
pop.erase(pop.end() + (int)(pop.size() * (rate - 1) - breeders.size()),
|
||||
pop.end());
|
||||
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<Pop>(pop));
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Fitness> class Inclusion: public Replace<Fitness>
|
||||
{
|
||||
public:
|
||||
Inclusion(const float& rate = 1): Replace<Fitness>(rate) {}
|
||||
|
||||
void operator()(Pop& breeders, Pop& pop)
|
||||
{
|
||||
Pop temp;
|
||||
|
||||
sort(breeders.begin(), breeders.end(), compare);
|
||||
sort(pop.begin(), pop.end(), compare);
|
||||
|
||||
merge(breeders.begin(), breeders.end(),
|
||||
pop.begin(), pop.end(),
|
||||
back_insert_iterator<Pop>(temp), compare);
|
||||
|
||||
temp.erase(temp.begin() + (unsigned)(rate * pop.size()), temp.end());
|
||||
pop.swap(temp);
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoMerge_h
|
||||
68
eo/src/eoMonOpFactory.h
Normal file
68
eo/src/eoMonOpFactory.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMonOpFactory.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOMONOPFACTORY_H
|
||||
#define _EOMONOPFACTORY_H
|
||||
|
||||
#include <eoFactory.h>
|
||||
#include <eoDup.h>
|
||||
#include <eoKill.h>
|
||||
#include <eoTranspose.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** EO Factory. An instance of the factory class to create monary operators.
|
||||
@see eoSelect*/
|
||||
template< class EOT>
|
||||
class eoMonOpFactory: public eoFactory< eoMonOp<EOT> > {
|
||||
|
||||
public:
|
||||
|
||||
/// @name ctors and dtors
|
||||
//{@
|
||||
/// constructor
|
||||
eoMonOpFactory( ) {}
|
||||
|
||||
/// destructor
|
||||
virtual ~eoMonOpFactory() {}
|
||||
//@}
|
||||
|
||||
/** Another factory method: creates an object from an istream, reading from
|
||||
it whatever is needed to create the object. Usually, the format for the istream will be\\
|
||||
objectType parameter1 parameter2 ... parametern\\
|
||||
*/
|
||||
virtual eoMonOp<EOT>* make(istream& _is) {
|
||||
eoMonOp<EOT> * opPtr;
|
||||
string objectTypeStr;
|
||||
_is >> objectTypeStr;
|
||||
if ( objectTypeStr == "eoDup") {
|
||||
opPtr = new eoDup<EOT>();
|
||||
}
|
||||
if ( objectTypeStr == "eoKill" ) {
|
||||
opPtr = new eoKill<EOT>( );
|
||||
}
|
||||
if ( objectTypeStr == "eoTranspose" ) {
|
||||
opPtr = new eoTranspose<EOT>( );
|
||||
}
|
||||
if ( !opPtr ) {
|
||||
throw runtime_error( "Incorrect selector type" );
|
||||
}
|
||||
return opPtr;
|
||||
}
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
void printOn( ostream& _os ) const {};
|
||||
void readFrom( istream& _is ){};
|
||||
|
||||
/** className is inherited */
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif _EOFACTORY_H
|
||||
63
eo/src/eoMultiMonOp.h
Normal file
63
eo/src/eoMultiMonOp.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMultiMonOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOMULTIMONOP_h
|
||||
#define _EOMULTIMONOP_h
|
||||
|
||||
#include <iterator>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/** MultiMonOp combines several monary operators. By itself, it does nothing to the
|
||||
EO it´s handled*/
|
||||
template <class EOT>
|
||||
class eoMultiMonOp: public eoMonOp<EOT> {
|
||||
public:
|
||||
/// Ctor from an already existing op
|
||||
eoMultiMonOp( const eoMonOp<EOT>* _op )
|
||||
: eoMonOp< EOT >( ), vOp(){
|
||||
vOp.push_back( _op );
|
||||
};
|
||||
|
||||
///
|
||||
eoMultiMonOp( )
|
||||
: eoMonOp< EOT >( ), vOp(){};
|
||||
|
||||
/// Ctor from an already existing op
|
||||
void adOp( const eoMonOp<EOT>* _op ){
|
||||
vOp.push_back( _op );
|
||||
};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoMultiMonOp() {};
|
||||
|
||||
///
|
||||
/// Applies all operators to the EO
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
if ( vOp.begin() != vOp.end() ) {
|
||||
for ( vector<const eoMonOp<EOT>* >::const_iterator i = vOp.begin();
|
||||
i != vOp.end(); i++ ) {
|
||||
(*i)->operator () ( _eo );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoMonOp";};
|
||||
//@}
|
||||
private:
|
||||
vector< const eoMonOp<EOT>* > vOp;
|
||||
};
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show more
Reference in a new issue