Added new files to the brew, mainly distance and new-op-interface related
This commit is contained in:
parent
472898871c
commit
7db1492943
40 changed files with 2490 additions and 992 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
AC_INIT(src/eo)
|
AC_INIT(src/eo)
|
||||||
|
|
||||||
AM_INIT_AUTOMAKE(eo, 0.0.8)
|
dnl Change the version number here
|
||||||
|
AM_INIT_AUTOMAKE(eo)
|
||||||
|
|
||||||
AC_PROG_CXX
|
AC_PROG_CXX
|
||||||
|
|
||||||
|
|
@ -10,4 +11,5 @@ AM_PROG_LIBTOOL
|
||||||
|
|
||||||
AM_MAINTAINER_MODE
|
AM_MAINTAINER_MODE
|
||||||
|
|
||||||
|
dnl add Makefiles that must be modified by the configuration here
|
||||||
AC_OUTPUT(Makefile src/Makefile test/Makefile win/Makefile)
|
AC_OUTPUT(Makefile src/Makefile test/Makefile win/Makefile)
|
||||||
|
|
|
||||||
|
|
@ -5,22 +5,25 @@
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
lib_LIBRARIES = libeo.a
|
lib_LIBRARIES = libeo.a
|
||||||
libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp eoParserUtils.cpp
|
libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp
|
||||||
|
|
||||||
libeoincdir = $(includedir)/eo
|
libeoincdir = $(includedir)/eo
|
||||||
libeoinc_HEADERS = EO.h eo eo1d.h eo2d.h eo2dVector.h eoAged.h eoAlgo.h\
|
libeoinc_HEADERS = EO.h eo eo1d.h eo1dWDistance.h eo2d.h \
|
||||||
|
eo2dVector.h eoAged.h eoAlgo.h\
|
||||||
|
eoAltBreeder.h eoAltProportionalOpSel.h\
|
||||||
eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \
|
eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \
|
||||||
eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\
|
eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\
|
||||||
eoData.h eoDetTournament.h eoDup.h eoESChrom.h eoESFullChrom.h \
|
eoData.h eoDetTournament.h eoDistance.h eoDup.h eoESChrom.h \
|
||||||
eoESFullMut.h eoEasyEA.h\
|
eoESFullChrom.h eoESFullMut.h eoEasyEA.h\
|
||||||
eoEvalFunc.h eoEvalFuncPtr.h eoFitTerm.h eoFitness.h\
|
eoEvalFunc.h eoEvalFuncPtr.h eoEvalFuncPtrCnt.h eoFitTerm.h eoFitness.h\
|
||||||
eoGenTerm.h eoGeneration.h eoID.h eoInclusion.h eoInsertion.h\
|
eoGenTerm.h eoGeneralOp.h eoGeneration.h eoGOpSelector.h eoID.h\
|
||||||
eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h \
|
eoInclusion.h eoInsertion.h\
|
||||||
eoMutation.h eoNegExp.h\
|
eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h eoMutation.h eoNegExp.h\
|
||||||
eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\
|
eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\
|
||||||
eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\
|
eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\
|
||||||
eoProblem.h eoProportionalOpSel.h eoRNG.h eoRnd.h eoString.h\
|
eoProblem.h eoProportionalGOpSel.h eoProportionalOpSel.h eoRNG.h\
|
||||||
|
eoRnd.h eoString.h\
|
||||||
eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \
|
eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \
|
||||||
eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h \
|
eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h
|
||||||
eoParserUtils.h
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eo1d.h
|
eo1d.h
|
||||||
// (c) GeNeura Team, 1998
|
Serial EO.
|
||||||
/*
|
|
||||||
|
(c) GeNeura Team, 1998
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
|
|
@ -43,7 +45,6 @@ using namespace std;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
/** eo1d: Base class for "chromosomes" with a single dimension
|
/** eo1d: Base class for "chromosomes" with a single dimension
|
||||||
#T# is the type it will be instantiated with; this type must have, at
|
#T# is the type it will be instantiated with; this type must have, at
|
||||||
least, a copy ctor, assignment operators,
|
least, a copy ctor, assignment operators,
|
||||||
|
|
@ -91,7 +92,7 @@ public:
|
||||||
This implies that T must have a copy ctor .
|
This implies that T must have a copy ctor .
|
||||||
@param _i index of the gene, which is the minimal unit. Must be
|
@param _i index of the gene, which is the minimal unit. Must be
|
||||||
an unsigned less than #length()#
|
an unsigned less than #length()#
|
||||||
@return what's inside the gene, with the correct type
|
@return what's inside the gene, with the correct type
|
||||||
@exception out_of_range if _i > size()
|
@exception out_of_range if _i > size()
|
||||||
*/
|
*/
|
||||||
virtual T getGene( unsigned _i ) const = 0;
|
virtual T getGene( unsigned _i ) const = 0;
|
||||||
|
|
@ -99,8 +100,8 @@ public:
|
||||||
/** Overwrites the gene placed in position _i with a
|
/** Overwrites the gene placed in position _i with a
|
||||||
* new value. This means that the assignment operator
|
* new value. This means that the assignment operator
|
||||||
* for T must be defined .
|
* for T must be defined .
|
||||||
@param _i index
|
@param _i index
|
||||||
@return what's inside the gene, with the correct type
|
@return what's inside the gene, with the correct type
|
||||||
@exception out_of_range if _i > size()
|
@exception out_of_range if _i > size()
|
||||||
*/
|
*/
|
||||||
virtual void setGene( unsigned _i, const T& _value ) = 0;
|
virtual void setGene( unsigned _i, const T& _value ) = 0;
|
||||||
|
|
@ -121,16 +122,16 @@ public:
|
||||||
|
|
||||||
/// Returns the number of genes in the eo1d
|
/// Returns the number of genes in the eo1d
|
||||||
virtual unsigned length() const = 0;
|
virtual unsigned length() const = 0;
|
||||||
|
|
||||||
/// @name Methods from eoObject
|
/// @name Methods from eoObject
|
||||||
//@{
|
//@{
|
||||||
/**
|
/**
|
||||||
* Read object. Theoretically, the length is known in advance. All objects
|
* Read object. Theoretically, the length is known in advance. All objects
|
||||||
* Should call base class
|
* Should call base class
|
||||||
* @param _s A istream.
|
* @param _s A istream.
|
||||||
* @throw runtime_exception If a valid object can't be read.
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
*/
|
*/
|
||||||
virtual void readFrom(istream& _s) {
|
virtual void readFrom(istream& _s) {
|
||||||
|
|
||||||
for ( unsigned i = 0; i < length(); i ++ ) {
|
for ( unsigned i = 0; i < length(); i ++ ) {
|
||||||
T tmp;
|
T tmp;
|
||||||
|
|
@ -142,7 +143,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Print itself: inherited from eoObject implementation.
|
/** Print itself: inherited from eoObject implementation.
|
||||||
Instance from base classes are processed in
|
Instance from base classes are processed in
|
||||||
base classes, so you don´t have to worry about, for instance, fitness.
|
base classes, so you don´t have to worry about, for instance, fitness.
|
||||||
@param _s the ostream in which things are written*/
|
@param _s the ostream in which things are written*/
|
||||||
virtual void printOn( ostream& _s ) const{
|
virtual void printOn( ostream& _s ) const{
|
||||||
|
|
@ -155,8 +156,8 @@ public:
|
||||||
@see eoObject
|
@see eoObject
|
||||||
*/
|
*/
|
||||||
string className() const {return "eo1d";};
|
string className() const {return "eo1d";};
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -171,7 +172,7 @@ public:
|
||||||
@param _ID An ID string, preferably unique
|
@param _ID An ID string, preferably unique
|
||||||
*/
|
*/
|
||||||
template< class T, class fitnessT>
|
template< class T, class fitnessT>
|
||||||
eo1d<T,fitnessT>::eo1d<T,fitnessT>( unsigned _size, eoRnd<T>& _rndGen,
|
eo1d<T,fitnessT>::eo1d<T,fitnessT>( unsigned _size, eoRnd<T>& _rndGen,
|
||||||
const string& _ID )
|
const string& _ID )
|
||||||
:EO<fitnessT> ( _ID ) {
|
:EO<fitnessT> ( _ID ) {
|
||||||
for ( unsigned i = 0; i < _size; i ++ ) {
|
for ( unsigned i = 0; i < _size; i ++ ) {
|
||||||
|
|
|
||||||
136
eo/src/eo1dWDistance.h
Normal file
136
eo/src/eo1dWDistance.h
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eo1dWDistance.h
|
||||||
|
Serial EO with distances. Acts as a wrapper for normal eo1ds
|
||||||
|
|
||||||
|
(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 _EO1DWDISTANCE_H
|
||||||
|
#define _EO1DWDISTANCE_H
|
||||||
|
|
||||||
|
#include <iostream> // for ostream
|
||||||
|
|
||||||
|
// EO Includes
|
||||||
|
#include <eo1d.h>
|
||||||
|
#include <eoDistance.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/** eo1dWDistance: wraps around eo1ds and adds the possibility of computing distances
|
||||||
|
around them.
|
||||||
|
*/
|
||||||
|
template<class T, class fitnessT = float>
|
||||||
|
class eo1dWDistance:
|
||||||
|
public eo1d< T,fitnessT >,
|
||||||
|
public eoDistance<eo1d<T,fitnessT> > {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/** Can be used as default ctor; should be called from derived
|
||||||
|
classes. Fitness should be at birth
|
||||||
|
*/
|
||||||
|
eo1dWDistance( eo1d<T,fitnessT>& _eo)
|
||||||
|
:eo1d<T,fitnessT> (), eoDistance< eo1d<T,fitnessT> >(), innereo1d( _eo ) {};
|
||||||
|
|
||||||
|
/// Needed virtual dtor
|
||||||
|
virtual ~eo1dWDistance(){};
|
||||||
|
|
||||||
|
/** 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 getGene( unsigned _i ) const {
|
||||||
|
return innereo1d.getGene( _i );
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 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 void setGene( unsigned _i, const T& _value ) {
|
||||||
|
innereo1d.setGene( _i, _value);
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 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 ) {
|
||||||
|
innereo1d.insertGene( _i, _val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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 ) {
|
||||||
|
innereo1d.deleteGene( _i );
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the number of genes in the eo1d
|
||||||
|
virtual unsigned length() const {
|
||||||
|
return innereo1d.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the distance from this EO to the other
|
||||||
|
virtual double distance( const eo1d<T,fitnessT>& _eo ) const {
|
||||||
|
double tmp = 0;
|
||||||
|
// Which one is shorter
|
||||||
|
unsigned len = (innereo1d.length() < _eo.length()) ? _eo.length():innereo1d.length();
|
||||||
|
|
||||||
|
// Compute over the whole length. If it does not exists, it counts as 0
|
||||||
|
for ( unsigned i = 0; i < len; i ++ ) {
|
||||||
|
T val1, val2;
|
||||||
|
val1 = ( i > innereo1d.length())?0:innereo1d.getGene(i);
|
||||||
|
val2 = ( i > _eo.length() )?0:_eo.getGene(i);
|
||||||
|
double diff = val1 - val2;
|
||||||
|
tmp += diff*diff;
|
||||||
|
}
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Inherited from eoObject
|
||||||
|
@see eoObject
|
||||||
|
*/
|
||||||
|
string className() const {return "eo1dWDistance";};
|
||||||
|
|
||||||
|
//@}
|
||||||
|
|
||||||
|
private:
|
||||||
|
///Private ctor. Do not use
|
||||||
|
eo1dWDistance() {};
|
||||||
|
|
||||||
|
|
||||||
|
eo1d<T,fitnessT>& innereo1d;
|
||||||
|
};
|
||||||
|
|
||||||
|
//@}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
302
eo/src/eoAltBreeder.h
Normal file
302
eo/src/eoAltBreeder.h
Normal file
|
|
@ -0,0 +1,302 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoBreeder.h
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef eoBreeder_h
|
||||||
|
#define eoBreeder_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <vector> // vector
|
||||||
|
#include <iterator>
|
||||||
|
#include <eoUniform.h> // eoUniform
|
||||||
|
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||||
|
#include <eoPop.h> // eoPop
|
||||||
|
#include <eoPopOps.h> // eoTransform
|
||||||
|
#include <eoOpSelector.h> // eoOpSelector
|
||||||
|
#include <list>
|
||||||
|
#include "eoRng.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* eoBreeder: transforms a population using genetic operators. *
|
||||||
|
* For every operator there is a rated to be applyed. *
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
template<class EOT, class OutIt>
|
||||||
|
class eoGeneralOp: public eoOp<EOT>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
eoGeneralOp()
|
||||||
|
:eoOp<EOT>( Nary ) {};
|
||||||
|
virtual ~eoGeneralOp () {};
|
||||||
|
|
||||||
|
virtual void operator()( vector<const EOT*> _in, OutIt _out) const = 0;
|
||||||
|
virtual int nInputs(void) const = 0;
|
||||||
|
virtual int nOutputs(void) const = 0; // no support for 2 -> 2 xover
|
||||||
|
|
||||||
|
virtual string className() const {return "eoGeneralOp";};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT, class OutIt>
|
||||||
|
class eoWrappedMonOp : public eoGeneralOp<EOT, OutIt>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
eoWrappedMonOp(const eoMonOp<EOT>& _op) : eoGeneralOp<EOT, OutIt>(), op(_op) {}
|
||||||
|
virtual ~eoWrappedMonOp() {}
|
||||||
|
|
||||||
|
void operator()( vector<const EOT*> _in, OutIt _out) const
|
||||||
|
{
|
||||||
|
*_out = *_in[0];
|
||||||
|
op(*_out );
|
||||||
|
}
|
||||||
|
|
||||||
|
int nInputs(void) const { return 1; }
|
||||||
|
int nOutputs(void) const { return 1; }
|
||||||
|
|
||||||
|
virtual string className() const {return "eoWrappedOp";};
|
||||||
|
|
||||||
|
|
||||||
|
private :
|
||||||
|
const eoMonOp<EOT>& op;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT, class OutIt>
|
||||||
|
class eoWrappedBinOp : public eoGeneralOp<EOT, OutIt>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
eoWrappedBinOp(const eoBinOp<EOT>& _op) : eoGeneralOp<EOT, OutIt>(), op(_op) {}
|
||||||
|
virtual ~eoWrappedBinOp() {}
|
||||||
|
|
||||||
|
void operator()( vector<const EOT*> _in, OutIt _out) const
|
||||||
|
{
|
||||||
|
*_out = *_in[0];
|
||||||
|
*(_out + 1) = *_in[1];
|
||||||
|
op(*_out, *(_out + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int nInputs(void) const { return 2; }
|
||||||
|
int nOutputs(void) const { return 2; } // Yup, due to the bad design, can't choose between outputting 1 or 2
|
||||||
|
|
||||||
|
virtual string className() const {return "eoWrappedOp";};
|
||||||
|
|
||||||
|
|
||||||
|
private :
|
||||||
|
const eoBinOp<EOT>& op;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT, class OutIt>
|
||||||
|
class eoCombinedOp : public eoGeneralOp<EOT, OutIt>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
eoCombinedOp() : eoGeneralOp<EOT, OutIt>(), arity(0) {}
|
||||||
|
virtual ~eoCombinedOp() {}
|
||||||
|
|
||||||
|
int nInputs(void) const { return arity; }
|
||||||
|
int nOutputs(void) const { return 1; }
|
||||||
|
|
||||||
|
void addOp(eoGeneralOp<EOT, OutIt>* _op)
|
||||||
|
{
|
||||||
|
ops.push_back(_op);
|
||||||
|
arity = arity < _op->nInputs()? _op->nInputs() : arity;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void clear(void)
|
||||||
|
{
|
||||||
|
ops.resize(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void operator()( vector<const EOT*> _in, OutIt _out) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < ops.size(); ++i)
|
||||||
|
{
|
||||||
|
(*ops[i])(_in, _out);
|
||||||
|
_in[0] = &*_out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
vector<eoGeneralOp<EOT, OutIt>* > ops;
|
||||||
|
int arity;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class EOT, class OutIt>
|
||||||
|
class eoAltOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT, OutIt>*>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual ID addOp( eoOp<EOT>& _op, float _arg )
|
||||||
|
{
|
||||||
|
eoGeneralOp<EOT, OutIt>* op = dynamic_cast<eoGeneralOp<EOT, OutIt>*>(&_op);
|
||||||
|
|
||||||
|
|
||||||
|
if (op == 0)
|
||||||
|
{
|
||||||
|
switch(_op.readArity())
|
||||||
|
{
|
||||||
|
case unary :
|
||||||
|
oplist.push_back(auto_ptr<eoGeneralOp<EOT, OutIt> >(new eoWrappedMonOp<EOT, OutIt>(static_cast<eoMonOp<EOT>&>(_op))));
|
||||||
|
|
||||||
|
op = oplist.back().get();
|
||||||
|
break;
|
||||||
|
case binary :
|
||||||
|
oplist.push_back(auto_ptr<eoGeneralOp<EOT, OutIt> >(new eoWrappedBinOp<EOT, OutIt>(static_cast<eoBinOp<EOT>&>(_op))));
|
||||||
|
op = oplist.back().get();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator result = find(begin(), end(), (eoGeneralOp<EOT, OutIt>*) 0); // search for nullpointer
|
||||||
|
|
||||||
|
if (result == end())
|
||||||
|
{
|
||||||
|
push_back(op);
|
||||||
|
rates.push_back(_arg);
|
||||||
|
return size();
|
||||||
|
}
|
||||||
|
// else
|
||||||
|
|
||||||
|
*result = op;
|
||||||
|
ID id = result - begin();
|
||||||
|
rates[id] = _arg;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const eoOp<EOT>& getOp( ID _id )
|
||||||
|
{
|
||||||
|
return *operator[](_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void deleteOp( ID _id )
|
||||||
|
{
|
||||||
|
operator[](_id) = 0; // TODO, check oplist and clear it there too.
|
||||||
|
rates[_id] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual eoOp<EOT>* Op()
|
||||||
|
{
|
||||||
|
return &selectOp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virtual eoGeneralOp<EOT, OutIt>& selectOp() = 0;
|
||||||
|
|
||||||
|
|
||||||
|
virtual string className() const { return "eoAltOpSelector"; };
|
||||||
|
|
||||||
|
void printOn(ostream& _os) const {}
|
||||||
|
|
||||||
|
|
||||||
|
protected :
|
||||||
|
|
||||||
|
vector<float> rates;
|
||||||
|
list<auto_ptr<eoGeneralOp<EOT, OutIt> > > oplist;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT, class OutIt>
|
||||||
|
class eoProportionalOpSelector : public eoAltOpSelector<EOT, OutIt>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
eoProportionalOpSelector() : eoAltOpSelector<EOT, OutIt>() {}
|
||||||
|
|
||||||
|
|
||||||
|
virtual eoGeneralOp<EOT, OutIt>& selectOp()
|
||||||
|
{
|
||||||
|
int what = rng.roulette_wheel(rates);
|
||||||
|
|
||||||
|
return *operator[](what);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT, class OutIt>
|
||||||
|
class eoSequentialOpSelector : public eoAltOpSelector<EOT, OutIt>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
eoSequentialOpSelector() : eoAltOpSelector<EOT, OutIt>() {}
|
||||||
|
|
||||||
|
virtual eoGeneralOp<EOT, OutIt>& selectOp()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < size(); ++i)
|
||||||
|
{
|
||||||
|
if (operator[](i) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (rng.flip(rates[i]))
|
||||||
|
combined.addOp(operator[](i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return combined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
eoCombinedOp<EOT, OutIt> combined;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class EOT>
|
||||||
|
class eoRandomIndy // living in a void right now
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
eoRandomIndy() {}
|
||||||
|
|
||||||
|
vector<const EOT*> operator()(int _n, eoPop<EOT>::iterator _begin, eoPop<EOT>::iterator _end)
|
||||||
|
{
|
||||||
|
vector<const EOT*> result(_n);
|
||||||
|
|
||||||
|
for (int i = 0; i < result.size(); ++i)
|
||||||
|
{
|
||||||
|
result[i] = &*(_begin + rng.random(_end - _begin));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class Chrom> class eoAltBreeder: public eoTransform<Chrom>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef eoPop<Chrom>::reverse_iterator outIt;
|
||||||
|
/// Default constructor.
|
||||||
|
eoAltBreeder( eoAltOpSelector<Chrom, outIt>& _opSel): opSel( _opSel ) {}
|
||||||
|
|
||||||
|
/// Destructor.
|
||||||
|
virtual ~eoAltBreeder() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enlarges the population.
|
||||||
|
* @param pop The population to be transformed.
|
||||||
|
*/
|
||||||
|
void operator()(eoPop<Chrom>& pop)
|
||||||
|
{
|
||||||
|
int size = pop.size();
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
eoGeneralOp<Chrom, outIt>& op = opSel.selectOp();
|
||||||
|
|
||||||
|
pop.resize(pop.size() + op.nOutputs());
|
||||||
|
vector<const Chrom*> indies = indySelector(op.nInputs(), pop.begin(), pop.begin() + size);
|
||||||
|
|
||||||
|
op(indies, pop.rbegin());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The class name.
|
||||||
|
string classname() const { return "eoAltBreeder"; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
eoAltOpSelector<Chrom, outIt >& opSel;
|
||||||
|
eoRandomIndy<Chrom> indySelector;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif eoBreeder_h
|
||||||
|
|
@ -1,63 +1,64 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoAtomRandom.h
|
eoAtomRandom.h
|
||||||
// Increments or decrements by one a single element
|
Increments or decrements by one a single element
|
||||||
// (c) GeNeura Team, 1998
|
(c) GeNeura Team, 1998
|
||||||
/*
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
version 2 of the License, or (at your option) any later version.
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Lesser General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
#ifndef _EOATOMCREEP_H
|
|
||||||
#define _EOATOMCREEP_H
|
#ifndef _EOATOMRANDOM_H
|
||||||
|
#define _EOATOMRANDOM_H
|
||||||
#include <eoRandom.h>
|
|
||||||
|
#include <eoRnd.h>
|
||||||
/** Modifies using a random number generator, that is entered in the ctor.
|
|
||||||
The RNG must return positive or negative numbers, whatever.
|
/** Modifies using a random number generator, that is entered in the ctor.
|
||||||
*/
|
The RNG must return positive or negative numbers, whatever.
|
||||||
template <class T>
|
*/
|
||||||
class eoAtomRandom: public eoAtomMutator<T> {
|
template <class T>
|
||||||
public:
|
class eoAtomRandom: public eoAtomMutator<T> {
|
||||||
|
public:
|
||||||
///
|
|
||||||
eoAtomRandom( eoRandom& _rng): rng( _rng ) {};
|
///
|
||||||
|
eoAtomRandom( eoRnd<T>& _rng): rng( _rng ) {};
|
||||||
///
|
|
||||||
virtual ~eoAtomRandom() {};
|
///
|
||||||
|
virtual ~eoAtomRandom() {};
|
||||||
///
|
|
||||||
virtual void operator()( T& _val ) const {
|
/// Adds the value generated by the RNG to the former value
|
||||||
_val += rng();
|
virtual void operator()( T& _val ) const {
|
||||||
}
|
_val += rng();
|
||||||
|
}
|
||||||
/** @name Methods from eoObject
|
|
||||||
*/
|
/** @name Methods from eoObject
|
||||||
//@{
|
*/
|
||||||
/** Inherited from eoObject
|
//@{
|
||||||
@see eoObject
|
/** Inherited from eoObject
|
||||||
*/
|
@see eoObject
|
||||||
string className() const {return "eoAtomRandom";};
|
*/
|
||||||
//@}
|
string className() const {return "eoAtomRandom";};
|
||||||
|
//@}
|
||||||
private:
|
|
||||||
eoRandom rng;
|
private:
|
||||||
|
eoRnd<T>& rng;
|
||||||
};
|
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,28 @@
|
||||||
//-----------------------------------------------------------------------------
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
// eoData.h
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
eoData.h
|
||||||
|
Some numeric limits and types and things like that; with #ifdefs to keep
|
||||||
|
compatibility
|
||||||
|
|
||||||
|
(c) GeNeura Team & Maarten Keijzer, 1998, 1999, 2000
|
||||||
|
|
||||||
|
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 EODATA_H
|
#ifndef EODATA_H
|
||||||
#define EODATA_H
|
#define EODATA_H
|
||||||
|
|
@ -22,8 +44,10 @@ using namespace std;
|
||||||
#define MAXINT numeric_limits<int>::max()
|
#define MAXINT numeric_limits<int>::max()
|
||||||
#else
|
#else
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#ifndef _WIN32 // should be the define for UN*X flavours: _POSIX??
|
||||||
#include <values.h>
|
#include <values.h>
|
||||||
|
#endif
|
||||||
#ifndef MAXFLOAT
|
#ifndef MAXFLOAT
|
||||||
#define MAXFLOAT (float)1e127
|
#define MAXFLOAT (float)1e127
|
||||||
#define MAXDOUBLE (double)1.79769313486231570e+308
|
#define MAXDOUBLE (double)1.79769313486231570e+308
|
||||||
|
|
|
||||||
63
eo/src/eoDistance.h
Normal file
63
eo/src/eoDistance.h
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoObject.h
|
||||||
|
This is the base class for most objects in EO. It basically defines an interf
|
||||||
|
face for giving names to classes.
|
||||||
|
|
||||||
|
(c) GeNeura Team, 1998, 1999, 2000
|
||||||
|
|
||||||
|
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 EODISTANCE_H
|
||||||
|
#define EODISTANCE_H
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoDistance
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
/** Defines an interface for measuring distances between evolving objects */
|
||||||
|
template <class EOT>
|
||||||
|
class eoDistance {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Default Constructor.
|
||||||
|
eoDistance() {}
|
||||||
|
|
||||||
|
/// Copy constructor.
|
||||||
|
eoDistance( const eoDistance& ) {}
|
||||||
|
|
||||||
|
/// Virtual dtor. They are needed in virtual class hierarchies.
|
||||||
|
virtual ~eoDistance() {}
|
||||||
|
|
||||||
|
|
||||||
|
/** Return the distance from the object with this interface to other
|
||||||
|
object of the same type.
|
||||||
|
*/
|
||||||
|
virtual double distance( const EOT& ) const = 0;
|
||||||
|
|
||||||
|
/// Returns classname
|
||||||
|
virtual string className() const { return "eoDistance"; }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif EOOBJECT_H
|
||||||
|
|
@ -57,7 +57,7 @@ template<class Chrom> class eoEasyEA: public eoAlgo<Chrom>
|
||||||
|
|
||||||
/// Apply one generation of evolution to the population.
|
/// Apply one generation of evolution to the population.
|
||||||
virtual void operator()(eoPop<Chrom>& pop) {
|
virtual void operator()(eoPop<Chrom>& pop) {
|
||||||
while ( terminator( pop ) ){
|
do {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
step(pop);
|
step(pop);
|
||||||
|
|
@ -68,7 +68,7 @@ template<class Chrom> class eoEasyEA: public eoAlgo<Chrom>
|
||||||
s.append( " in eoEasyEA ");
|
s.append( " in eoEasyEA ");
|
||||||
throw runtime_error( s );
|
throw runtime_error( s );
|
||||||
}
|
}
|
||||||
}
|
} while ( terminator( pop ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Class name.
|
/// Class name.
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,59 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoOp.h
|
eoEvalFuncPtr.h
|
||||||
// (c) GeNeura Team, 1998
|
Converts a classical C fitness evaluation function into a fitness
|
||||||
/*
|
evaluation object
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
(c) GeNeura Team, 2000
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2 of the License, or (at your option) any later version.
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
This library is distributed in the hope that it will be useful,
|
License as published by the Free Software Foundation; either
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
version 2 of the License, or (at your option) any later version.
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
You should have received a copy of the GNU Lesser General Public
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
License along with this library; if not, write to the Free Software
|
Lesser General Public License for more details.
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
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 EOEVALFUNCPTR_H
|
*/
|
||||||
#define EOEVALFUNCPTR_H
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eoEvalFunc.h>
|
#ifndef EOEVALFUNCPTR_H
|
||||||
|
#define EOEVALFUNCPTR_H
|
||||||
/** EOEvalFuncPtr: This class
|
|
||||||
* takes an existing function pointer and converts it into a evaluation
|
#include <eoEvalFunc.h>
|
||||||
* function class. That way, old style C or C++ functions can be adapted to EO
|
|
||||||
* function classes.
|
/** EOEvalFuncPtr: This class
|
||||||
*/
|
* takes an existing function pointer and converts it into a evaluation
|
||||||
template< class EOT >
|
* function class. That way, old style C or C++ functions can be adapted to EO
|
||||||
struct eoEvalFuncPtr: public eoEvalFunc<EOT> {
|
* function classes.
|
||||||
|
*/
|
||||||
eoEvalFuncPtr( void (* _eval)( EOT& ) )
|
template< class EOT >
|
||||||
: eoEvalFunc<EOT>(), evalFunc( _eval ) {};
|
struct eoEvalFuncPtr: public eoEvalFunc<EOT> {
|
||||||
|
|
||||||
/// Effectively applies the evaluation function to an EO
|
/** Applies the function to the chromosome and sets the fitness of the
|
||||||
virtual void operator() ( EOT & _eo ) const {
|
Chrom. Thus, the evaluation function need not be worried about that.
|
||||||
(*evalFunc)( _eo );
|
@param _eval pointer to the evaluation function, takes a EOT as an
|
||||||
};
|
argument and returns the fitness
|
||||||
|
@return the evaluated fitness for that object.
|
||||||
private:
|
*/
|
||||||
void (* evalFunc )( EOT& );
|
eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) )
|
||||||
};
|
: eoEvalFunc<EOT>(), evalFunc( _eval ) {};
|
||||||
|
|
||||||
#endif
|
/// Effectively applies the evaluation function to an EO
|
||||||
|
virtual void operator() ( EOT & _eo ) const {
|
||||||
|
_eo.fitness((*evalFunc)( _eo ));
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
EOFitT (* evalFunc )( EOT& );
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
||||||
62
eo/src/eoEvalFuncPtrCnt.h
Normal file
62
eo/src/eoEvalFuncPtrCnt.h
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoEvalFuncPtrCnt.h
|
||||||
|
Same as eoEvalFuncPtr, but counts the number of evaluations
|
||||||
|
|
||||||
|
(c) GeNeura Team, 2000
|
||||||
|
|
||||||
|
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 EOEVALFUNCPTRCNT_H
|
||||||
|
#define EOEVALFUNCPTRCNT_H
|
||||||
|
|
||||||
|
#include <eoEvalFuncPtr.h>
|
||||||
|
|
||||||
|
/** EOEvalFuncCntPtr: This class
|
||||||
|
* takes an existing function pointer and converts it into a evaluation
|
||||||
|
* function class. That way, old style C or C++ functions can be adapted to EO
|
||||||
|
* function classes.
|
||||||
|
*/
|
||||||
|
template< class EOT >
|
||||||
|
struct eoEvalFuncPtrCnt: public eoEvalFuncPtr<EOT> {
|
||||||
|
|
||||||
|
/** Applies the function to the chromosome and sets the fitness of the
|
||||||
|
Chrom. Thus, the evaluation function need not be worried about that.
|
||||||
|
@param _eval pointer to the evaluation function, takes a EOT as an
|
||||||
|
argument and returns the fitness
|
||||||
|
@return the evaluated fitness for that object.
|
||||||
|
*/
|
||||||
|
eoEvalFuncPtrCnt( EOFitT (* _eval)( const EOT& ) )
|
||||||
|
: eoEvalFuncPtr<EOT>( _eval ), numOfEvaluations( 0 ) {};
|
||||||
|
|
||||||
|
/// Effectively applies the evaluation function to an EO
|
||||||
|
virtual void operator() ( EOT & _eo ) const {
|
||||||
|
eoEvalFuncPtr<EOT>::operator()( _eo );
|
||||||
|
numOfEvaluations++;
|
||||||
|
};
|
||||||
|
|
||||||
|
///
|
||||||
|
unsigned getNumOfEvaluations() const { return numOfEvaluations; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable unsigned numOfEvaluations;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
139
eo/src/eoGOpSelector.h
Normal file
139
eo/src/eoGOpSelector.h
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoGOpSelector.h
|
||||||
|
Base class for generalized (n-inputs, n-outputs) operator selectors.
|
||||||
|
Includes code and variables that contain operators and rates
|
||||||
|
|
||||||
|
(c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000
|
||||||
|
|
||||||
|
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 eoGOpSelector_h
|
||||||
|
#define eoGOpSelector_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <vector> // vector
|
||||||
|
#include <iterator>
|
||||||
|
#include <eoUniform.h> // eoUniform
|
||||||
|
#include <eoGeneralOp.h> // eoOp, eoMonOp, eoBinOp
|
||||||
|
#include <eoPop.h> // eoPop
|
||||||
|
#include <eoPopOps.h> // eoTransform
|
||||||
|
#include <eoOpSelector.h> // eoOpSelector
|
||||||
|
#include <list>
|
||||||
|
#include "eoRNG.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/** Base class for alternative selectors, which use the generalized operator
|
||||||
|
interface */
|
||||||
|
|
||||||
|
template<class EOT>
|
||||||
|
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Dtor
|
||||||
|
virtual ~eoGOpSelector() {
|
||||||
|
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
|
||||||
|
i != ownOpList.begin(); i ++ ) {
|
||||||
|
delete *i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add any kind of operator to the operator mix, with an argument
|
||||||
|
virtual ID addOp( eoOp<EOT>& _op, float _arg ) {
|
||||||
|
eoGeneralOp<EOT>* op = dynamic_cast<eoGeneralOp<EOT>*>(&_op);
|
||||||
|
|
||||||
|
// if it's not a general op, it's a "old" op; create a wrapped op from it
|
||||||
|
// and keep it on a list to delete them afterwards
|
||||||
|
// will use auto_ptr when they're readily available
|
||||||
|
if (op == 0) {
|
||||||
|
switch(_op.readArity())
|
||||||
|
{
|
||||||
|
case unary :
|
||||||
|
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
|
||||||
|
break;
|
||||||
|
case binary :
|
||||||
|
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ownOpList.push_back( op );
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer
|
||||||
|
|
||||||
|
if (result == end())
|
||||||
|
{
|
||||||
|
push_back(op);
|
||||||
|
rates.push_back(_arg);
|
||||||
|
return size();
|
||||||
|
}
|
||||||
|
// else
|
||||||
|
|
||||||
|
*result = op;
|
||||||
|
ID id = result - begin();
|
||||||
|
rates[id] = _arg;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Retrieve the operator using its integer handle
|
||||||
|
@param _id The id number. Should be a valid id, or an exception
|
||||||
|
will be thrown
|
||||||
|
@return a reference of the operator corresponding to that id.
|
||||||
|
*/
|
||||||
|
virtual const eoOp<EOT>& getOp( ID _id )
|
||||||
|
{
|
||||||
|
return *operator[](_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual void deleteOp( ID _id )
|
||||||
|
{
|
||||||
|
operator[](_id) = 0; // TODO, check oplist and clear it there too.
|
||||||
|
rates[_id] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual eoOp<EOT>* Op()
|
||||||
|
{
|
||||||
|
return &selectOp();
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual eoGeneralOp<EOT>& selectOp() = 0;
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual string className() const { return "eoGOpSelector"; };
|
||||||
|
|
||||||
|
///
|
||||||
|
void printOn(ostream& _os) const {
|
||||||
|
_os << className() << endl;
|
||||||
|
for ( unsigned i=0; i!= rates.size(); i++ ) {
|
||||||
|
_os << *(operator[](i)) << "\t" << rates[i] << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected :
|
||||||
|
vector<float> rates;
|
||||||
|
list< eoGeneralOp<EOT>* > ownOpList;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif eoGOpSelector_h
|
||||||
200
eo/src/eoGeneralOp.h
Normal file
200
eo/src/eoGeneralOp.h
Normal file
|
|
@ -0,0 +1,200 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoGeneralOp.h
|
||||||
|
General genetic operator, which can be used to wrap any unary or binary
|
||||||
|
operator
|
||||||
|
(c) Maarten Keijzer (mak@dhi.dk) and GeNeura Team, 1999, 2000
|
||||||
|
|
||||||
|
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 eoGeneralOp_h
|
||||||
|
#define eoGeneralOp_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <vector> // vector
|
||||||
|
#include <iterator>
|
||||||
|
#include <eoUniform.h> // eoUniform
|
||||||
|
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||||
|
#include <eoPop.h> // eoPop
|
||||||
|
#include <eoPopOps.h> // eoTransform
|
||||||
|
#include <eoOpSelector.h> // eoOpSelector
|
||||||
|
#include <list>
|
||||||
|
#include "eoRNG.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* eGeneralOp: General genetic operator; for objects used to transform sets
|
||||||
|
of EOs
|
||||||
|
*/
|
||||||
|
template<class EOT>
|
||||||
|
class eoGeneralOp: public eoOp<EOT>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Ctor that honors its superclass
|
||||||
|
eoGeneralOp(): eoOp<EOT>( Nary ) {};
|
||||||
|
|
||||||
|
/// Virtual dtor
|
||||||
|
virtual ~eoGeneralOp () {};
|
||||||
|
|
||||||
|
/** Method that really does the stuff. Applies the genetic operator
|
||||||
|
to a vector of inputs, and puts results in the output vector */
|
||||||
|
virtual void operator()( eoPop<EOT>::iterator _in,
|
||||||
|
insert_iterator< eoPop<EOT> > _out) const = 0;
|
||||||
|
|
||||||
|
/// Number of inputs
|
||||||
|
virtual unsigned nInputs(void) const { return repNInputs;};
|
||||||
|
|
||||||
|
/// Number of output arguments, or arguments that are pushed onto the output vector
|
||||||
|
virtual unsigned nOutputs(void) const { return repNOutputs; };
|
||||||
|
|
||||||
|
virtual string className() const {return "eoGeneralOp";};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// Default ctor; protected so that only derived classes can use it
|
||||||
|
eoGeneralOp( unsigned _nInputs = 0, unsigned _nOutputs = 0 )
|
||||||
|
: repNInputs( _nInputs), repNOutputs( _nOutputs) {};
|
||||||
|
|
||||||
|
/// change number of inputs
|
||||||
|
void setNInputs( unsigned _nInputs) { repNInputs = _nInputs;};
|
||||||
|
|
||||||
|
/// change number of outputs
|
||||||
|
void setNOutputs( unsigned _nOutputs) { repNOutputs = _nOutputs;};
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned repNInputs;
|
||||||
|
unsigned repNOutputs;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// Wraps monary operators
|
||||||
|
template <class EOT>
|
||||||
|
class eoWrappedMonOp : public eoGeneralOp<EOT>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
///
|
||||||
|
eoWrappedMonOp(const eoMonOp<EOT>& _op) : eoGeneralOp<EOT>( 1, 1), op(_op) {};
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual ~eoWrappedMonOp() {}
|
||||||
|
|
||||||
|
/// Instantiates the abstract method
|
||||||
|
void operator()( eoPop<EOT>::iterator _in,
|
||||||
|
insert_iterator< eoPop< EOT> > _out ) const {
|
||||||
|
EOT result = *_in;
|
||||||
|
op( result );
|
||||||
|
*_out = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual string className() const {return "eoWrappedMonOp";};
|
||||||
|
|
||||||
|
|
||||||
|
private :
|
||||||
|
const eoMonOp<EOT>& op;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// Wraps binary operators
|
||||||
|
template <class EOT>
|
||||||
|
class eoWrappedBinOp : public eoGeneralOp<EOT>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
///
|
||||||
|
eoWrappedBinOp(const eoBinOp<EOT>& _op) : eoGeneralOp<EOT>(2, 2), op(_op) {}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual ~eoWrappedBinOp() {}
|
||||||
|
|
||||||
|
/// Instantiates the abstract method. EOT should have copy ctor.
|
||||||
|
void operator()(eoPop<EOT>::iterator _in,
|
||||||
|
insert_iterator< eoPop< EOT> > _out ) const {
|
||||||
|
EOT out1 = *_in;
|
||||||
|
_in++;
|
||||||
|
EOT out2 = *_in;
|
||||||
|
op(out1, out2);
|
||||||
|
*_out++ = out1;
|
||||||
|
*_out = out2;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual string className() const {return "eoWrappedBinOp";};
|
||||||
|
|
||||||
|
private :
|
||||||
|
const eoBinOp<EOT>& op;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Combines several ops
|
||||||
|
template <class EOT>
|
||||||
|
class eoCombinedOp : public eoGeneralOp<EOT>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
///
|
||||||
|
eoCombinedOp() : eoGeneralOp<EOT>() {}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual ~eoCombinedOp() {}
|
||||||
|
|
||||||
|
/// Adds a new operator to the combined Op
|
||||||
|
void addOp(eoGeneralOp<EOT>* _op) {
|
||||||
|
ops.push_back(_op);
|
||||||
|
unsigned nInputs = nInputs() < _op->nInputs()? _op->nInputs() : nInputs;
|
||||||
|
setNInputs( nInputs );
|
||||||
|
unsigned nOutputs = nOutputs() < _op->nOutputs()? _op->nOutputs() : nOutputs;
|
||||||
|
setNOutputs( nInputs );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Erases all operators added so far
|
||||||
|
void clear(void) {
|
||||||
|
ops.resize(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Applies all ops in the combined op
|
||||||
|
void operator()( eoPop<EOT>::iterator _in,
|
||||||
|
insert_iterator< eoPop< EOT> > _out ) const {
|
||||||
|
// used for provisional input and output. Results are put in provOut,
|
||||||
|
// and copied back to provIn.
|
||||||
|
eoPop<EOT> provIn, provOut;
|
||||||
|
insert_iterator< eoPop< EOT> > out = provOut.begin();
|
||||||
|
ops[0]( _in, out );
|
||||||
|
for ( unsigned i = 1; i < ops.size; i ++ ) {
|
||||||
|
copy( provOut.begin(), provOut.end(), provIn.begin() );
|
||||||
|
insert_iterator< eoPop< EOT> > in = provIn.begin();
|
||||||
|
out = provOut.begin();
|
||||||
|
ops[i]( in, out );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy back to output
|
||||||
|
copy( provOut.begin(), provOut.end(), _out );
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
vector<eoGeneralOp<EOT>* > ops;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif eoGeneral_h
|
||||||
|
|
@ -71,9 +71,9 @@ template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||||
string className() const { return "eoGeneration"; }
|
string className() const { return "eoGeneration"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
eoBinPopOp<Chrom>& select;
|
eoBinPopOp<Chrom>& select;
|
||||||
eoMonPopOp<Chrom>& transform;
|
eoMonPopOp<Chrom>& transform;
|
||||||
eoBinPopOp<Chrom>& replace;
|
eoBinPopOp<Chrom>& replace;
|
||||||
eoEvalFunc<Chrom>& evaluator;
|
eoEvalFunc<Chrom>& evaluator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eoLottery.h
|
// eoLottery.h
|
||||||
// Implements the lottery procedure for selection
|
// Implements the lottery procedure for selection
|
||||||
// (c) GeNeura Team, 1998
|
// (c) GeNeura Team, 1998 - Marc Schoenauer, 2000
|
||||||
/*
|
/*
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
|
@ -33,53 +33,49 @@
|
||||||
#include <eo> // eoPop eoSelect MINFLOAT
|
#include <eo> // eoPop eoSelect MINFLOAT
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/// eoLottery: a selection method.
|
/** eoLottery: a selection method. Puts into the output a group of individuals
|
||||||
/// requires Chrom::Fitness to be float castable
|
selected using lottery; individuals with higher probability will have more
|
||||||
|
chances of being selected.
|
||||||
|
Requires EOT::Fitness to be float castable
|
||||||
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
template<class Chrom> class eoLottery: public eoBinPopOp<Chrom>
|
template<class EOT> class eoLottery: public eoBinPopOp<EOT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// (Default) Constructor.
|
/// (Default) Constructor.
|
||||||
eoLottery(const float& _rate = 1.0): rate(_rate) {}
|
eoLottery(const float& _rate = 1.0): rate(_rate) {}
|
||||||
|
|
||||||
///
|
/** actually selects individuals from pop and pushes them back them into breeders
|
||||||
void operator()( eoPop<Chrom>& pop, eoPop<Chrom>& breeders)
|
* until breeders has the right size: rate*pop.size()
|
||||||
|
* BUT what happens if breeders is already too big?
|
||||||
|
* Too big for what?
|
||||||
|
*/
|
||||||
|
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
|
||||||
{
|
{
|
||||||
// scores of chromosomes
|
// scores of chromosomes
|
||||||
vector<float> score(pop.size());
|
vector<float> score(pop.size());
|
||||||
|
|
||||||
// calculates accumulated scores for chromosomes
|
// calculates total scores for chromosomes
|
||||||
for (unsigned i = 0; i < pop.size(); i++)
|
float total = 0;
|
||||||
|
for (unsigned i = 0; i < pop.size(); i++) {
|
||||||
score[i] = static_cast<float>(pop[i].fitness());
|
score[i] = static_cast<float>(pop[i].fitness());
|
||||||
|
total += score[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// number of offspring needed
|
||||||
|
int target = (int)rint(rate * pop.size());
|
||||||
|
|
||||||
|
// test of consistency
|
||||||
|
if (breeders.size() >= target) {
|
||||||
|
throw("Problem in eoLottery: already too many offspring");
|
||||||
|
}
|
||||||
|
|
||||||
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(rint(rate * pop.size()));
|
|
||||||
generate(random.begin(), random.end(), eoUniform<float>(0,1));
|
|
||||||
sort(random.begin(), random.end(), less<float>());
|
|
||||||
|
|
||||||
// selection of chromosomes
|
// selection of chromosomes
|
||||||
unsigned score_index = 0; // position in score vector
|
while (breeders.size() < target) {
|
||||||
unsigned random_index = 0; // position in random vector
|
unsigned indloc = rng.roulette_wheel(score, total);
|
||||||
while (breeders.size() < random.size())
|
breeders.push_back(pop[indloc]);
|
||||||
{
|
}
|
||||||
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<eoPop<Chrom> >(breeders),
|
|
||||||
random.size() - breeders.size(), pop.back());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoNegExp.h
|
eoNegExp.h
|
||||||
// (c) GeNeura Team, 1998
|
(c) GeNeura Team, 1998
|
||||||
/*
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
|
|
@ -43,7 +43,7 @@ class eoNegExp: public eoRnd<T>
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
* @param _mean Dsitribution mean
|
* @param _mean Distribution mean
|
||||||
*/
|
*/
|
||||||
eoNegExp(T _mean): eoRnd<T>(), mean(_mean) {};
|
eoNegExp(T _mean): eoRnd<T>(), mean(_mean) {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,66 +1,69 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoObject.h
|
eoObject.h
|
||||||
// (c) GeNeura Team, 1998
|
This is the base class for most objects in EO. It basically defines an interf
|
||||||
/*
|
face for giving names to classes.
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
(c) GeNeura Team, 1998, 1999, 2000
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2 of the License, or (at your option) any later version.
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
This library is distributed in the hope that it will be useful,
|
License as published by the Free Software Foundation; either
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
version 2 of the License, or (at your option) any later version.
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
You should have received a copy of the GNU Lesser General Public
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
License along with this library; if not, write to the Free Software
|
Lesser General Public License for more details.
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
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 EOOBJECT_H
|
*/
|
||||||
#define EOOBJECT_H
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
#ifndef EOOBJECT_H
|
||||||
|
#define EOOBJECT_H
|
||||||
#include <eoData.h> // For limits definition
|
|
||||||
#include <iostream> // istream, ostream
|
//-----------------------------------------------------------------------------
|
||||||
#include <string> // string
|
|
||||||
|
#include <eoData.h> // For limits definition
|
||||||
using namespace std;
|
#include <iostream> // istream, ostream
|
||||||
|
#include <string> // string
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// eoObject
|
using namespace std;
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
/**
|
//-----------------------------------------------------------------------------
|
||||||
This is the base class for the whole hierarchy; an eoObject defines
|
// eoObject
|
||||||
basically an interface for the whole hierarchy: each object should
|
//-----------------------------------------------------------------------------
|
||||||
know its name (#className#). Previously, this object defined a print and read
|
/**
|
||||||
interface, but it´s been moved to eoPrintable and eoPersistent.
|
This is the base class for the whole hierarchy; an eoObject defines
|
||||||
*/
|
basically an interface for the whole hierarchy: each object should
|
||||||
class eoObject
|
know its name (#className#). Previously, this object defined a print and read
|
||||||
{
|
interface, but it´s been moved to eoPrintable and eoPersistent.
|
||||||
public:
|
*/
|
||||||
|
class eoObject
|
||||||
/// Default Constructor.
|
{
|
||||||
eoObject() {}
|
public:
|
||||||
|
|
||||||
/// Copy constructor.
|
/// Default Constructor.
|
||||||
eoObject( const eoObject& ) {}
|
eoObject() {}
|
||||||
|
|
||||||
/// Virtual dtor. They are needed in virtual class hierarchies.
|
/// Copy constructor.
|
||||||
virtual ~eoObject() {}
|
eoObject( const eoObject& ) {}
|
||||||
|
|
||||||
/** Return the class id. This should be redefined in each class; but
|
/// Virtual dtor. They are needed in virtual class hierarchies.
|
||||||
it's got code as an example of implementation. Only "leaf" classes
|
virtual ~eoObject() {}
|
||||||
can be non-virtual.
|
|
||||||
*/
|
/** Return the class id. This should be redefined in each class; but
|
||||||
virtual string className() const { return "eoObject"; }
|
it's got code as an example of implementation. Only "leaf" classes
|
||||||
|
can be non-virtual.
|
||||||
};
|
*/
|
||||||
|
virtual string className() const { return "eoObject"; }
|
||||||
#endif EOOBJECT_H
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif EOOBJECT_H
|
||||||
|
|
|
||||||
|
|
@ -77,16 +77,15 @@ public:
|
||||||
* @param _os A ostream.
|
* @param _os A ostream.
|
||||||
*/
|
*/
|
||||||
virtual void printOn(ostream& _os) const {
|
virtual void printOn(ostream& _os) const {
|
||||||
_os << className();
|
_os << className();
|
||||||
// _os << arity;
|
// _os << arity;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Inherited from eoObject
|
/** Inherited from eoObject
|
||||||
@see eoObject
|
@see eoObject
|
||||||
*/
|
*/
|
||||||
virtual string className() const {return "eoOp";};
|
virtual string className() const {return "eoOp";};
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// arity is the number of operands it takes
|
/// arity is the number of operands it takes
|
||||||
|
|
|
||||||
|
|
@ -1,85 +1,90 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoOpSelector.h
|
eoOpSelector.h
|
||||||
// (c) GeNeura Team 1998
|
Base class for operator selectors, which return 1 operator according
|
||||||
/*
|
to some criterium
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
(c) GeNeura Team 1998, 1999, 2000
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2 of the License, or (at your option) any later version.
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
This library is distributed in the hope that it will be useful,
|
License as published by the Free Software Foundation; either
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
version 2 of the License, or (at your option) any later version.
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
You should have received a copy of the GNU Lesser General Public
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
License along with this library; if not, write to the Free Software
|
Lesser General Public License for more details.
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
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 EOOPSELECTOR_H
|
*/
|
||||||
#define EOOPSELECTOR_H
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
#ifndef EOOPSELECTOR_H
|
||||||
#include <stdexcept> // runtime_error
|
#define EOOPSELECTOR_H
|
||||||
|
|
||||||
#include <eoObject.h>
|
//-----------------------------------------------------------------------------
|
||||||
#include <eoPrintable.h>
|
|
||||||
#include <eoOp.h>
|
#include <stdexcept> // runtime_error
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
#include <eoObject.h>
|
||||||
/** An operator selector is an object that contains a set of EO operators, and
|
#include <eoPrintable.h>
|
||||||
and selects one based on whatever criteria. It will be used in the breeder objects.\\
|
#include <eoOp.h>
|
||||||
This class is basically a generic interface for operator selection
|
|
||||||
*/
|
//-----------------------------------------------------------------------------
|
||||||
template<class EOT>
|
/** An operator selector is an object that contains a set of EO operators,
|
||||||
class eoOpSelector: public eoObject, public eoPrintable
|
and selects one based on whatever criteria. It will be used in the breeder objects.\\
|
||||||
{
|
This class is basically a generic interface for operator selection
|
||||||
public:
|
*/
|
||||||
typedef unsigned ID;
|
template<class EOT>
|
||||||
|
class eoOpSelector: public eoObject, public eoPrintable
|
||||||
/** add an operator to the operator set
|
{
|
||||||
@param _op a genetic operator, that will be applied in some way
|
public:
|
||||||
@param _arg the operator rate, usually, or any other argument to the operator
|
/// type of IDs assigned to each operators, used to handle them
|
||||||
@return an ID that will be used to identify the operator
|
typedef unsigned ID;
|
||||||
*/
|
|
||||||
virtual ID addOp( eoOp<EOT>& _op, float _arg ) = 0;
|
/** add an operator to the operator set
|
||||||
|
@param _op a genetic operator, that will be applied in some way
|
||||||
/** Gets a non-const reference to an operator, so that it can be changed,
|
@param _arg the operator rate, usually, or any other argument to the operator
|
||||||
modified or whatever
|
@return an ID that will be used to identify the operator
|
||||||
@param _id a previously assigned ID
|
*/
|
||||||
@throw runtime_exception if the ID does not exist*/
|
virtual ID addOp( eoOp<EOT>& _op, float _arg ) = 0;
|
||||||
virtual eoOp<EOT>& getOp( ID _id ) = 0;
|
|
||||||
|
/** Gets a non-const reference to an operator, so that it can be changed,
|
||||||
/** Remove an operator from the operator set
|
modified or whatever
|
||||||
@param _id a previously assigned ID
|
@param _id a previously assigned ID
|
||||||
@throw runtime_exception if the ID does not exist
|
@throw runtime_exception if the ID does not exist*/
|
||||||
*/
|
virtual const eoOp<EOT>& getOp( ID _id ) = 0;
|
||||||
virtual void deleteOp( ID _id ) = 0;
|
|
||||||
|
/** Remove an operator from the operator set
|
||||||
/// Returns a genetic operator according to the established criteria
|
@param _id a previously assigned ID
|
||||||
virtual eoOp<EOT>* Op() = 0;
|
@throw runtime_exception if the ID does not exist
|
||||||
|
*/
|
||||||
/// Methods inherited from eoObject
|
virtual void deleteOp( ID _id ) = 0;
|
||||||
//@{
|
|
||||||
|
/// Returns a genetic operator according to the established criteria
|
||||||
/** Return the class id.
|
virtual eoOp<EOT>* Op() = 0;
|
||||||
@return the class name as a string
|
|
||||||
*/
|
/// Methods inherited from eoObject
|
||||||
virtual string className() const { return "eoOpSelector"; };
|
//@{
|
||||||
|
|
||||||
/**
|
/** Return the class id.
|
||||||
* Read object and print objects are left for subclasses to define.
|
@return the class name as a string
|
||||||
*/
|
*/
|
||||||
//@}
|
virtual string className() const { return "eoOpSelector"; };
|
||||||
};
|
|
||||||
|
/**
|
||||||
//-----------------------------------------------------------------------------
|
* Read object and print objects are left for subclasses to define.
|
||||||
|
*/
|
||||||
#endif EO_H
|
//@}
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif EO_H
|
||||||
|
|
|
||||||
153
eo/src/eoPop.h
153
eo/src/eoPop.h
|
|
@ -49,87 +49,76 @@ to the population.
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
|
class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
|
||||||
|
|
||||||
/*
|
/// Type is the type of each gene in the chromosome
|
||||||
Victor Rivas (vrivas@ujaen.es): 15-Dec-1999
|
#ifdef _MSC_VER
|
||||||
An EO can have NO genes.
|
typedef EOT::Type Type;
|
||||||
/// Type is the type of each gene in the chromosome
|
#else
|
||||||
#ifdef _MSC_VER
|
typedef typename EOT::Type Type;
|
||||||
typedef EOT::Type Type;
|
#endif
|
||||||
#else
|
|
||||||
typedef typename EOT::Type Type;
|
public:
|
||||||
#endif
|
/** Protected ctor. This is intended to avoid creation of void populations, except
|
||||||
*/
|
from sibling classes
|
||||||
|
*/
|
||||||
public:
|
eoPop() :vector<EOT>() {};
|
||||||
/** Protected ctor. This is intended to avoid creation of void populations, except
|
|
||||||
from sibling classes
|
|
||||||
*/
|
/** Ctor for fixed-size chromosomes, with variable content
|
||||||
eoPop():vector<EOT>() {};
|
@param _popSize total population size
|
||||||
|
@param _eoSize chromosome size. EOT should accept a fixed-size ctor
|
||||||
|
@param _geneRdn random number generator for each of the genes
|
||||||
/** Ctor for fixed-size chromosomes, with variable content
|
*/
|
||||||
@param _popSize total population size
|
eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd )
|
||||||
@param _eoSize chromosome size. EOT should accept a fixed-size ctor
|
:vector<EOT>() {
|
||||||
@param _geneRdn random number generator for each of the genes
|
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||||
*/
|
EOT tmpEOT( _eoSize, _geneRnd);
|
||||||
/*
|
push_back( tmpEOT );
|
||||||
Victor Rivas (vrivas@ujaen.es): 15-Dec-1999
|
}
|
||||||
This constructor must be substitued by one using factories.
|
};
|
||||||
eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd )
|
|
||||||
:vector<EOT>() {
|
/** Ctor for variable-size chromosomes, with variable content
|
||||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
@param _popSize total population size
|
||||||
EOT tmpEOT( _eoSize, _geneRnd);
|
@param _sizeRnd RNG for the chromosome size. This will be added 1, just in case.
|
||||||
push_back( tmpEOT );
|
@param _geneRdn random number generator for each of the genes
|
||||||
}
|
*/
|
||||||
};
|
eoPop( unsigned _popSize, eoRnd<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
|
||||||
*/
|
:vector<EOT>() {
|
||||||
|
for ( unsigned i = 0; i < _popSize; i ++ ){
|
||||||
/** Ctor for variable-size chromosomes, with variable content
|
unsigned size = 1 + _sizeRnd();
|
||||||
@param _popSize total population size
|
EOT tmpEOT( size, _geneRnd);
|
||||||
@param _sizeRnd RNG for the chromosome size. This will be added 1, just in case.
|
push_back( tmpEOT );
|
||||||
@param _geneRdn random number generator for each of the genes
|
}
|
||||||
*/
|
};
|
||||||
/*
|
|
||||||
Victor Rivas (vrivas@ujaen.es): 15-Dec-1999
|
/** Ctor from an istream; reads the population from a stream,
|
||||||
This constructor must be substitued by one using factories.
|
each element should be in different lines
|
||||||
eoPop( unsigned _popSize, eoRnd<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
|
@param _is the stream
|
||||||
:vector<EOT>() {
|
*/
|
||||||
for ( unsigned i = 0; i < _popSize; i ++ ){
|
eoPop( istream& _is ):vector<EOT>() {
|
||||||
unsigned size = 1 + _sizeRnd();
|
readFrom( _is );
|
||||||
EOT tmpEOT( size, _geneRnd);
|
}
|
||||||
push_back( tmpEOT );
|
|
||||||
}
|
///
|
||||||
};
|
~eoPop() {};
|
||||||
*/
|
|
||||||
/** Ctor from an istream; reads the population from a stream,
|
/** @name Methods from eoObject */
|
||||||
each element should be in different lines
|
//@{
|
||||||
@param _is the stream
|
/**
|
||||||
*/
|
* Read object. The EOT class must have a ctor from a stream;
|
||||||
eoPop( istream& _is ):vector<EOT>() {
|
in this case, a strstream is used.
|
||||||
readFrom( _is );
|
* @param _is A istream.
|
||||||
}
|
|
||||||
|
*/
|
||||||
///
|
|
||||||
~eoPop() {};
|
|
||||||
|
|
||||||
/** @name Methods from eoObject */
|
|
||||||
//@{
|
|
||||||
/**
|
|
||||||
* Read object. The EOT class must have a ctor from a stream;
|
|
||||||
in this case, a strstream is used.
|
|
||||||
* @param _is A istream.
|
|
||||||
|
|
||||||
*/
|
|
||||||
virtual void readFrom(istream& _is) {
|
virtual void readFrom(istream& _is) {
|
||||||
while( _is ) { // reads line by line, and creates an object per
|
while( _is ) { // reads line by line, and creates an object per
|
||||||
// line
|
// line
|
||||||
char line[MAXLINELENGTH];
|
char line[MAXLINELENGTH];
|
||||||
_is.getline( line, MAXLINELENGTH-1 );
|
_is.getline( line, MAXLINELENGTH-1 );
|
||||||
if (strlen( line ) ) {
|
if (strlen( line ) ) {
|
||||||
istrstream s( line );
|
istrstream s( line );
|
||||||
EOT thisEOT( s );
|
EOT thisEOT( s );
|
||||||
push_back( thisEOT );
|
push_back( thisEOT );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,18 +127,18 @@ public:
|
||||||
* @param _os A ostream. In this case, prints the population to
|
* @param _os A ostream. In this case, prints the population to
|
||||||
standard output. The EOT class must hav standard output with cout,
|
standard output. The EOT class must hav standard output with cout,
|
||||||
but since it should be an eoObject anyways, it's no big deal.
|
but since it should be an eoObject anyways, it's no big deal.
|
||||||
*/
|
*/
|
||||||
virtual void printOn(ostream& _os) const {
|
virtual void printOn(ostream& _os) const {
|
||||||
copy( begin(), end(), ostream_iterator<EOT>( _os, "\n") );
|
copy( begin(), end(), ostream_iterator<EOT>( _os, "\n") );
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Inherited from eoObject. Returns the class name.
|
/** Inherited from eoObject. Returns the class name.
|
||||||
@see eoObject
|
@see eoObject
|
||||||
*/
|
*/
|
||||||
virtual string className() const {return "eoPop";};
|
virtual string className() const {return "eoPop";};
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoPopOps.h
|
eoPopOps.h
|
||||||
// (c) GeNeura Team, 1998
|
(c) GeNeura Team, 1998
|
||||||
/*
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
|
|
@ -108,7 +108,8 @@ class eoBinPopOp: public eoObject{
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/** eoSelect usually takes elements from one population, with or without transformation, and transfers them to the other population */
|
/** eoSelectone selects only one element from a whole population. Usually used to
|
||||||
|
select mates*/
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
class eoSelectOne: public eoObject{
|
class eoSelectOne: public eoObject{
|
||||||
|
|
||||||
|
|
|
||||||
51
eo/src/eoProportionalGOpSel.h
Normal file
51
eo/src/eoProportionalGOpSel.h
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
eoProportionalGOpSel.h
|
||||||
|
Proportional operator selector, selects operators proportionally to its rate
|
||||||
|
|
||||||
|
(c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000
|
||||||
|
|
||||||
|
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 eoProportionalGOpSel_h
|
||||||
|
#define eoProportionalGOpSel_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <eoGOpSelector.h> // eoOpSelector
|
||||||
|
|
||||||
|
template <class EOT>
|
||||||
|
class eoProportionalGOpSel : public eoGOpSelector<EOT>
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
eoProportionalGOpSel() : eoGOpSelector<EOT>() {}
|
||||||
|
|
||||||
|
/** Returns the operator proportionally selected */
|
||||||
|
virtual eoGeneralOp<EOT>& selectOp()
|
||||||
|
{
|
||||||
|
unsigned what = rng.roulette_wheel(rates);
|
||||||
|
return *operator[](what);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
virtual string className() const { return "eoGOpSelector"; };
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif eoProportionalGOpSel_h
|
||||||
|
|
@ -113,7 +113,7 @@ public :
|
||||||
@see reseed
|
@see reseed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
eoRng(uint32 s = (uint32) time(0) ) : state(0), next(0), left(-1), cached(false), N(624), M(397), K((0x9908B0DFU)) {
|
eoRng(uint32 s = (uint32) time(0) ) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) {
|
||||||
state = new uint32[N+1];
|
state = new uint32[N+1];
|
||||||
initialize(s);
|
initialize(s);
|
||||||
}
|
}
|
||||||
|
|
@ -197,7 +197,7 @@ public :
|
||||||
{
|
{
|
||||||
if (total == 0)
|
if (total == 0)
|
||||||
{ // count
|
{ // count
|
||||||
for (unsigned i = 0; i < vec.size(); ++i)
|
for (int i = 0; i < vec.size(); ++i)
|
||||||
total += vec[i];
|
total += vec[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
228
eo/src/eoRnd.h
228
eo/src/eoRnd.h
|
|
@ -1,114 +1,114 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoRnd.h
|
eoRnd.h
|
||||||
// (c) GeNeura Team, 1998
|
(c) GeNeura Team, 1998
|
||||||
/*
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
version 2 of the License, or (at your option) any later version.
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Lesser General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef _EORND_H
|
#ifndef _EORND_H
|
||||||
#define _EORND_H
|
#define _EORND_H
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eoObject.h>
|
#include <eoObject.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Class eoRnd
|
// Class eoRnd
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <stdlib.h> // srand
|
#include <stdlib.h> // srand
|
||||||
#include <time.h> // time
|
#include <time.h> // time
|
||||||
#include <stdexcept> // runtime_error
|
#include <stdexcept> // runtime_error
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eoPersistent.h>
|
#include <eoPersistent.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Base class for a family of random number generators. Generates numbers
|
* Base class for a family of random number generators. Generates numbers
|
||||||
* according to the parameters given in the constructor. Uses the machine's
|
* according to the parameters given in the constructor. Uses the machine's
|
||||||
* own random number generators. Which is not too good, after all.
|
* own random number generators. Which is not too good, after all.
|
||||||
*/
|
*/
|
||||||
template<class T>
|
template<class T>
|
||||||
class eoRnd: public eoObject, public eoPersistent
|
class eoRnd: public eoObject, public eoPersistent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// default constructor
|
/// default constructor
|
||||||
eoRnd(unsigned _seed = 0) {
|
eoRnd(unsigned _seed = 0) {
|
||||||
if ( !started ) {
|
if ( !started ) {
|
||||||
srand(_seed?_seed:time(0));
|
srand(_seed?_seed:time(0));
|
||||||
started = true;
|
started = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy cotor
|
/// Copy cotor
|
||||||
eoRnd(const eoRnd& _r ) {srand(time(0));};
|
eoRnd(const eoRnd& _r ) {srand(time(0));};
|
||||||
|
|
||||||
/** Main function: random generators act as functors, that return random numbers.
|
/** Main function: random generators act as functors, that return random numbers.
|
||||||
It´s non-const because it might modify a seed
|
It´s non-const because it might modify a seed
|
||||||
@return return a random number
|
@return return a random number
|
||||||
*/
|
*/
|
||||||
virtual T operator()() = 0;
|
virtual T operator()() = 0;
|
||||||
|
|
||||||
/** Return the class id.
|
/** Return the class id.
|
||||||
@return the class name as a string
|
@return the class name as a string
|
||||||
*/
|
*/
|
||||||
virtual string className() const { return "eoRandom"; };
|
virtual string className() const { return "eoRandom"; };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read object.
|
* Read object.
|
||||||
* @param is A istream.
|
* @param is A istream.
|
||||||
* @throw runtime_exception If a valid object can't be read.
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
*/
|
*/
|
||||||
virtual void readFrom(istream& _is);
|
virtual void readFrom(istream& _is);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* print object. Prints just the ID, since the seed is not accesible.
|
* print object. Prints just the ID, since the seed is not accesible.
|
||||||
* @param is A ostream.
|
* @param is A ostream.
|
||||||
*/
|
*/
|
||||||
void printOn(ostream& _os) const { _os << endl; };
|
void printOn(ostream& _os) const { _os << endl; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// true if the RNG has been started already. If it starts every time, means trouble.
|
/// true if the RNG has been started already. If it starts every time, means trouble.
|
||||||
static bool started;
|
static bool started;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T> bool eoRnd<T>::started = false;
|
template<class T> bool eoRnd<T>::started = false;
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read object.
|
* Read object.
|
||||||
* @param is A istream.
|
* @param is A istream.
|
||||||
* @throw runtime_exception If a valid object can't be read.
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
*/
|
*/
|
||||||
template<class T>
|
template<class T>
|
||||||
void eoRnd<T>::readFrom(istream& _is) {
|
void eoRnd<T>::readFrom(istream& _is) {
|
||||||
if (!_is)
|
if (!_is)
|
||||||
throw runtime_error("Problems reading from stream in eoRnd");
|
throw runtime_error("Problems reading from stream in eoRnd");
|
||||||
long seed;
|
long seed;
|
||||||
_is >> seed;
|
_is >> seed;
|
||||||
srand(seed);
|
srand(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,14 @@ public:
|
||||||
*this += _rnd();
|
*this += _rnd();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Ctor from a stream
|
||||||
|
@param _s input stream
|
||||||
|
*/
|
||||||
|
eoString( istream & _s )
|
||||||
|
: eo1d<char, fitnessT>(){
|
||||||
|
_s >> *this;
|
||||||
|
};
|
||||||
|
|
||||||
/// copy ctor
|
/// copy ctor
|
||||||
eoString( const eoString<fitnessT>& _eoStr )
|
eoString( const eoString<fitnessT>& _eoStr )
|
||||||
|
|
|
||||||
|
|
@ -1,71 +1,71 @@
|
||||||
// eoUniform.h
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
//-----------------------------------------------------------------------------
|
eoUniform.h
|
||||||
// EOUniform.h
|
Uniform random number generator;
|
||||||
// (c) GeNeura Team, 1998
|
(c) GeNeura Team, 1998
|
||||||
/*
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
version 2 of the License, or (at your option) any later version.
|
version 2 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This library is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Lesser General Public License for more details.
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef _EOUNIFORM_H
|
#ifndef _EOUNIFORM_H
|
||||||
#define _EOUNIFORM_H
|
#define _EOUNIFORM_H
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eoRnd.h>
|
#include <eoRnd.h>
|
||||||
#include <eoRNG.h>
|
#include <eoRNG.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Class eoUniform
|
// Class eoUniform
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Generates uniform random number over the interval [min, max)
|
/// Generates uniform random number over the interval [min, max)
|
||||||
template<class T>
|
template<class T>
|
||||||
class eoUniform: public eoRnd<T>
|
class eoUniform: public eoRnd<T>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
* @param _min The minimum value in the interval.
|
* @param _min The minimum value in the interval.
|
||||||
* @param _max The maximum value in the interval.
|
* @param _max The maximum value in the interval.
|
||||||
*/
|
*/
|
||||||
eoUniform(T _min = 0, T _max = 1)
|
eoUniform(T _min = 0, T _max = 1)
|
||||||
: eoRnd<T>(), min(_min), diff(_max - _min) {}
|
: eoRnd<T>(), min(_min), diff(_max - _min) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* copy constructor.
|
* copy constructor.
|
||||||
* @param _rnd the other rnd
|
* @param _rnd the other rnd
|
||||||
*/
|
*/
|
||||||
eoUniform( const eoUniform& _rnd)
|
eoUniform( const eoUniform& _rnd)
|
||||||
: eoRnd<T>( _rnd), min(_rnd.minim), diff(_rnd.diff) {}
|
: eoRnd<T>( _rnd), min(_rnd.minim), diff(_rnd.diff) {}
|
||||||
|
|
||||||
/** Returns an uniform random number over the interval [min, max)
|
/** Returns an uniform random number over the interval [min, max)
|
||||||
Uses global rng object */
|
Uses global rng object */
|
||||||
virtual T operator()() {
|
virtual T operator()() {
|
||||||
return min + T( rng.uniform( diff ) );
|
return min + T( rng.uniform( diff ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T min;
|
T min;
|
||||||
double diff;
|
double diff;
|
||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,169 +1,170 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
// eoVector.h
|
eoVector.h
|
||||||
// (c) GeNeura Team, 1998
|
Turns an STL vector into an EO
|
||||||
/*
|
(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
|
This library is free software; you can redistribute it and/or
|
||||||
License as published by the Free Software Foundation; either
|
modify it under the terms of the GNU Lesser General Public
|
||||||
version 2 of the License, or (at your option) any later version.
|
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
|
This library is distributed in the hope that it will be useful,
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
Lesser General Public License for more details.
|
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
|
You should have received a copy of the GNU Lesser General Public
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
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
|
|
||||||
*/
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
//-----------------------------------------------------------------------------
|
*/
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
#ifndef _eoVector_H
|
|
||||||
#define _eoVector_H
|
#ifndef _eoVector_H
|
||||||
|
#define _eoVector_H
|
||||||
// STL libraries
|
|
||||||
#include <vector> // For vector<int>
|
// STL libraries
|
||||||
#include <stdexcept>
|
#include <vector> // For vector<int>
|
||||||
#include <strstream>
|
#include <stdexcept>
|
||||||
|
#include <strstream>
|
||||||
#include <eo1d.h>
|
|
||||||
#include <eoRnd.h>
|
#include <eo1d.h>
|
||||||
|
#include <eoRnd.h>
|
||||||
/** Adaptor that turns an STL vector into an EO
|
|
||||||
with the same gene type as the type with which
|
/** Adaptor that turns an STL vector into an EO
|
||||||
the vector has been instantiated
|
with the same gene type as the type with which
|
||||||
*/
|
the vector has been instantiated
|
||||||
template <class T, class fitnessT>
|
*/
|
||||||
class eoVector: public eo1d<T, fitnessT>, public vector<T> {
|
template <class T, class fitnessT=float>
|
||||||
public:
|
class eoVector: public eo1d<T, fitnessT>, public vector<T> {
|
||||||
typedef T Type ;
|
public:
|
||||||
|
typedef T Type ;
|
||||||
/// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator
|
|
||||||
//@{
|
/// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator
|
||||||
|
//@{
|
||||||
/** Ctor.
|
|
||||||
@param _size Lineal length of the object
|
/** Ctor.
|
||||||
@param _val Common initial value
|
@param _size Lineal length of the object
|
||||||
*/
|
@param _val Common initial value
|
||||||
eoVector( unsigned _size = 0, T _val = 0)
|
*/
|
||||||
: eo1d<T, fitnessT>(), vector<T>( _size, _val ){ };
|
eoVector( unsigned _size = 0, T _val = 0)
|
||||||
|
: eo1d<T, fitnessT>(), vector<T>( _size, _val ){ };
|
||||||
/** Ctor using a random number generator
|
|
||||||
@param _size Lineal length of the object
|
/** Ctor using a random number generator
|
||||||
@param _rnd a random number generator, which returns a random value each time it´s called
|
@param _size Lineal length of the object
|
||||||
*/
|
@param _rnd a random number generator, which returns a random value each time it´s called
|
||||||
eoVector( unsigned _size, eoRnd<T>& _rnd );
|
*/
|
||||||
|
eoVector( unsigned _size, eoRnd<T>& _rnd );
|
||||||
/** Ctor from a istream. The T class should accept reading from a istream. It doesn't read fitness,
|
|
||||||
which is supposed to be dynamic and dependent on environment.
|
/** Ctor from a istream. The T class should accept reading from a istream. It doesn't read fitness,
|
||||||
@param _is the input stream; should have all values in a single line, separated by whitespace
|
which is supposed to be dynamic and dependent on environment.
|
||||||
*/
|
@param _is the input stream; should have all values in a single line, separated by whitespace
|
||||||
eoVector( istream& _is);
|
*/
|
||||||
|
eoVector( istream& _is);
|
||||||
|
|
||||||
/// copy ctor
|
|
||||||
eoVector( const eoVector & _eo )
|
/// copy ctor
|
||||||
: eo1d<T, fitnessT>( _eo ), vector<T>( _eo ){ };
|
eoVector( const eoVector & _eo )
|
||||||
|
: eo1d<T, fitnessT>( _eo ), vector<T>( _eo ){ };
|
||||||
/// Assignment operator
|
|
||||||
const eoVector& operator =( const eoVector & _eo ) {
|
/// Assignment operator
|
||||||
if ( this != &_eo ){
|
const eoVector& operator =( const eoVector & _eo ) {
|
||||||
eo1d<T, fitnessT>::operator=( _eo );
|
if ( this != &_eo ){
|
||||||
vector<T>::operator=( _eo );
|
eo1d<T, fitnessT>::operator=( _eo );
|
||||||
}
|
vector<T>::operator=( _eo );
|
||||||
return *this;
|
}
|
||||||
}
|
return *this;
|
||||||
|
}
|
||||||
/// dtor
|
|
||||||
virtual ~eoVector() {};
|
/// dtor
|
||||||
|
virtual ~eoVector() {};
|
||||||
//@}
|
|
||||||
|
//@}
|
||||||
/** methods that implement the eo1d <em>protocol</em>
|
|
||||||
@exception out_of_range if _i is larger than EO´s size
|
/** methods that implement the eo1d <em>protocol</em>
|
||||||
*/
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
virtual T getGene( unsigned _i ) const {
|
*/
|
||||||
if ( _i >= length() )
|
virtual T getGene( unsigned _i ) const {
|
||||||
throw out_of_range( "out_of_range when reading gene");
|
if ( _i >= length() )
|
||||||
return (*this)[_i];
|
throw out_of_range( "out_of_range when reading gene");
|
||||||
};
|
return (*this)[_i];
|
||||||
|
};
|
||||||
/** methods that implement the eo1d <em>protocol</em>
|
|
||||||
@exception out_of_range if _i is larger than EO´s size
|
/** methods that implement the eo1d <em>protocol</em>
|
||||||
*/
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
virtual void setGene( unsigned _i, const T& _value ) {
|
*/
|
||||||
if ( _i >= size() )
|
virtual void setGene( unsigned _i, const T& _value ) {
|
||||||
throw out_of_range( "out_of_range when writing a gene");
|
if ( _i >= size() )
|
||||||
(*this)[_i] = _value;
|
throw out_of_range( "out_of_range when writing a gene");
|
||||||
};
|
(*this)[_i] = _value;
|
||||||
|
};
|
||||||
/** methods that implement the eo1d <em>protocol</em>
|
|
||||||
@exception out_of_range if _i is larger than EO´s size
|
/** methods that implement the eo1d <em>protocol</em>
|
||||||
*/
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
virtual void insertGene( unsigned _i, T _val ) {
|
*/
|
||||||
if (_i <= size() ) {
|
virtual void insertGene( unsigned _i, T _val ) {
|
||||||
vector<T>::iterator i = begin()+_i;
|
if (_i <= size() ) {
|
||||||
insert( i, _val );
|
vector<T>::iterator i = begin()+_i;
|
||||||
} else {
|
insert( i, _val );
|
||||||
throw out_of_range( "out_of_range when inserting a gene");
|
} else {
|
||||||
}
|
throw out_of_range( "out_of_range when inserting a gene");
|
||||||
};
|
}
|
||||||
|
};
|
||||||
/** Eliminates the gene at position _i
|
|
||||||
@exception out_of_range if _i is larger than EO´s size
|
/** Eliminates the gene at position _i
|
||||||
*/
|
@exception out_of_range if _i is larger than EO´s size
|
||||||
virtual void deleteGene( unsigned _i ) {
|
*/
|
||||||
if (_i < this->size() ) {
|
virtual void deleteGene( unsigned _i ) {
|
||||||
vector<T>::iterator i = this->begin()+_i;
|
if (_i < this->size() ) {
|
||||||
this->erase( i );
|
vector<T>::iterator i = this->begin()+_i;
|
||||||
} else {
|
this->erase( i );
|
||||||
throw out_of_range( "out_of_range when deleting a gene");
|
} else {
|
||||||
};
|
throw out_of_range( "out_of_range when deleting a gene");
|
||||||
};
|
};
|
||||||
|
};
|
||||||
/// methods that implement the EO <em>protocol</em>
|
|
||||||
virtual unsigned length() const { return this->size(); };
|
/// methods that implement the EO <em>protocol</em>
|
||||||
|
virtual unsigned length() const { return this->size(); };
|
||||||
/** @name Methods from eoObject
|
|
||||||
readFrom and printOn are directly inherited from eo1d
|
/** @name Methods from eoObject
|
||||||
*/
|
readFrom and printOn are directly inherited from eo1d
|
||||||
//@{
|
*/
|
||||||
/** Inherited from eoObject
|
//@{
|
||||||
@see eoObject
|
/** Inherited from eoObject
|
||||||
*/
|
@see eoObject
|
||||||
string className() const {return "eoVector";};
|
*/
|
||||||
//@}
|
string className() const {return "eoVector";};
|
||||||
|
//@}
|
||||||
};
|
|
||||||
|
};
|
||||||
|
|
||||||
//____________________________ Some method implementation _______________________
|
|
||||||
|
//____________________________ Some method implementation _______________________
|
||||||
// Ctors______________________________________________________________________________
|
|
||||||
//____________________________________________________________________________________
|
// Ctors______________________________________________________________________________
|
||||||
template <class T, class fitnessT>
|
//____________________________________________________________________________________
|
||||||
eoVector<T,fitnessT>::eoVector( unsigned _size, eoRnd<T>& _rnd )
|
template <class T, class fitnessT>
|
||||||
: eo1d<T, fitnessT>(), vector<T>( _size ){
|
eoVector<T,fitnessT>::eoVector( unsigned _size, eoRnd<T>& _rnd )
|
||||||
for ( iterator i = begin(); i != end(); i ++ ) {
|
: eo1d<T, fitnessT>(), vector<T>( _size ){
|
||||||
*i = _rnd();
|
for ( iterator i = begin(); i != end(); i ++ ) {
|
||||||
}
|
*i = _rnd();
|
||||||
};
|
}
|
||||||
|
};
|
||||||
//____________________________________________________________________________________
|
|
||||||
template <class T, class fitnessT>
|
//____________________________________________________________________________________
|
||||||
eoVector<T,fitnessT>::eoVector( istream& _is)
|
template <class T, class fitnessT>
|
||||||
: eo1d<T, fitnessT>(), vector<T>( ){
|
eoVector<T,fitnessT>::eoVector( istream& _is)
|
||||||
while (_is ) {
|
: eo1d<T, fitnessT>(), vector<T>( ){
|
||||||
T tmp;
|
while (_is ) {
|
||||||
_is >> tmp;
|
T tmp;
|
||||||
push_back( tmp );
|
_is >> tmp;
|
||||||
}
|
push_back( tmp );
|
||||||
|
}
|
||||||
};
|
|
||||||
|
};
|
||||||
#endif
|
|
||||||
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,36 @@ LDADDS = $(top_builddir)/src/libeo.a
|
||||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness \
|
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness \
|
||||||
t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA\
|
t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA\
|
||||||
t-eoNonUniform t-eoUniform t-eoRandom t-parser t-eoESFull t-eoESOps\
|
t-eoNonUniform t-eoUniform t-eoRandom t-parser t-eoESFull t-eoESOps\
|
||||||
t-eoAtomOps t-selectOne
|
t-eoAtomOps t-selectOne t-eoaltbreeder t-eoGeneralOps t-eoGOpSel \
|
||||||
|
t-eoVector
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoVector_SOURCES = t-eoVector.cpp
|
||||||
|
t_eoVector_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoVector_LDFLAGS = -lm
|
||||||
|
t_eoVector_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoGOpSel_SOURCES = t-eoGOpSel.cpp
|
||||||
|
t_eoGOpSel_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoGOpSel_LDFLAGS = -lm
|
||||||
|
t_eoGOpSel_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoGeneralOps_SOURCES = t-eoGeneralOps.cpp
|
||||||
|
t_eoGeneralOps_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoGeneralOps_LDFLAGS = -lm
|
||||||
|
t_eoGeneralOps_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoaltbreeder_SOURCES = t-eoaltbreeder.cpp
|
||||||
|
t_eoaltbreeder_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoaltbreeder_LDFLAGS = -lm
|
||||||
|
t_eoaltbreeder_LDADD = $(LDADDS)
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
# Makefile.in generated automatically by automake 1.4 from Makefile.am
|
# Makefile.in generated automatically by automake 1.4a from Makefile.am
|
||||||
|
|
||||||
# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
|
# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
|
||||||
# This Makefile.in is free software; the Free Software Foundation
|
# This Makefile.in is free software; the Free Software Foundation
|
||||||
|
|
@ -49,9 +49,10 @@ AUTOMAKE = @AUTOMAKE@
|
||||||
AUTOHEADER = @AUTOHEADER@
|
AUTOHEADER = @AUTOHEADER@
|
||||||
|
|
||||||
INSTALL = @INSTALL@
|
INSTALL = @INSTALL@
|
||||||
INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS)
|
INSTALL_PROGRAM = @INSTALL_PROGRAM@
|
||||||
INSTALL_DATA = @INSTALL_DATA@
|
INSTALL_DATA = @INSTALL_DATA@
|
||||||
INSTALL_SCRIPT = @INSTALL_SCRIPT@
|
INSTALL_SCRIPT = @INSTALL_SCRIPT@
|
||||||
|
INSTALL_STRIP_FLAG =
|
||||||
transform = @program_transform_name@
|
transform = @program_transform_name@
|
||||||
|
|
||||||
NORMAL_INSTALL = :
|
NORMAL_INSTALL = :
|
||||||
|
|
@ -72,9 +73,9 @@ LN_S = @LN_S@
|
||||||
MAINT = @MAINT@
|
MAINT = @MAINT@
|
||||||
MAKEINFO = @MAKEINFO@
|
MAKEINFO = @MAKEINFO@
|
||||||
NM = @NM@
|
NM = @NM@
|
||||||
OBJDUMP = @OBJDUMP@
|
|
||||||
PACKAGE = @PACKAGE@
|
PACKAGE = @PACKAGE@
|
||||||
RANLIB = @RANLIB@
|
RANLIB = @RANLIB@
|
||||||
|
USE_SYMBOL_UNDERSCORE = @USE_SYMBOL_UNDERSCORE@
|
||||||
VERSION = @VERSION@
|
VERSION = @VERSION@
|
||||||
|
|
||||||
DEPS = $(top_builddir)/src/libeo.a
|
DEPS = $(top_builddir)/src/libeo.a
|
||||||
|
|
@ -87,9 +88,37 @@ LDADDS = $(top_builddir)/src/libeo.a
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom t-parser t-eoESFull t-eoESOps t-eoAtomOps t-selectOne
|
noinst_PROGRAMS = t-eobreeder t-eoinclusion t-eoinsertion t-eo t-eofitness t-eoproblem t-eobin t-eolottery t-eo2dVector t-eogeneration t-eoEasyEA t-eoNonUniform t-eoUniform t-eoRandom t-parser t-eoESFull t-eoESOps t-eoAtomOps t-selectOne t-eoaltbreeder t-eoGeneralOps t-eoGOpSel t-eoVector
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoVector_SOURCES = t-eoVector.cpp
|
||||||
|
t_eoVector_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoVector_LDFLAGS = -lm
|
||||||
|
t_eoVector_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoGOpSel_SOURCES = t-eoGOpSel.cpp
|
||||||
|
t_eoGOpSel_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoGOpSel_LDFLAGS = -lm
|
||||||
|
t_eoGOpSel_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoGeneralOps_SOURCES = t-eoGeneralOps.cpp
|
||||||
|
t_eoGeneralOps_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoGeneralOps_LDFLAGS = -lm
|
||||||
|
t_eoGeneralOps_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoaltbreeder_SOURCES = t-eoaltbreeder.cpp
|
||||||
|
t_eoaltbreeder_DEPENDENCIES = $(DEPS)
|
||||||
|
t_eoaltbreeder_LDFLAGS = -lm
|
||||||
|
t_eoaltbreeder_LDADD = $(LDADDS)
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
t_selectOne_SOURCES = t-selectOne.cpp
|
t_selectOne_SOURCES = t-selectOne.cpp
|
||||||
|
|
@ -250,6 +279,10 @@ t_eoESFull_OBJECTS = t-eoESFull.o
|
||||||
t_eoESOps_OBJECTS = t-eoESOps.o
|
t_eoESOps_OBJECTS = t-eoESOps.o
|
||||||
t_eoAtomOps_OBJECTS = t-eoAtomOps.o
|
t_eoAtomOps_OBJECTS = t-eoAtomOps.o
|
||||||
t_selectOne_OBJECTS = t-selectOne.o
|
t_selectOne_OBJECTS = t-selectOne.o
|
||||||
|
t_eoaltbreeder_OBJECTS = t-eoaltbreeder.o
|
||||||
|
t_eoGeneralOps_OBJECTS = t-eoGeneralOps.o
|
||||||
|
t_eoGOpSel_OBJECTS = t-eoGOpSel.o
|
||||||
|
t_eoVector_OBJECTS = t-eoVector.o
|
||||||
CXXFLAGS = @CXXFLAGS@
|
CXXFLAGS = @CXXFLAGS@
|
||||||
CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||||
LTCXXCOMPILE = $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
LTCXXCOMPILE = $(LIBTOOL) --mode=compile $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||||
|
|
@ -267,8 +300,8 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
|
||||||
|
|
||||||
TAR = gtar
|
TAR = gtar
|
||||||
GZIP_ENV = --best
|
GZIP_ENV = --best
|
||||||
SOURCES = $(t_eobreeder_SOURCES) $(t_eoinclusion_SOURCES) $(t_eoinsertion_SOURCES) $(t_eo_SOURCES) $(t_eofitness_SOURCES) $(t_eoproblem_SOURCES) $(t_eobin_SOURCES) $(t_eolottery_SOURCES) $(t_eo2dVector_SOURCES) $(t_eogeneration_SOURCES) $(t_eoEasyEA_SOURCES) $(t_eoNonUniform_SOURCES) $(t_eoUniform_SOURCES) $(t_eoRandom_SOURCES) $(t_parser_SOURCES) $(t_eoESFull_SOURCES) $(t_eoESOps_SOURCES) $(t_eoAtomOps_SOURCES) $(t_selectOne_SOURCES)
|
SOURCES = $(t_eobreeder_SOURCES) $(t_eoinclusion_SOURCES) $(t_eoinsertion_SOURCES) $(t_eo_SOURCES) $(t_eofitness_SOURCES) $(t_eoproblem_SOURCES) $(t_eobin_SOURCES) $(t_eolottery_SOURCES) $(t_eo2dVector_SOURCES) $(t_eogeneration_SOURCES) $(t_eoEasyEA_SOURCES) $(t_eoNonUniform_SOURCES) $(t_eoUniform_SOURCES) $(t_eoRandom_SOURCES) $(t_parser_SOURCES) $(t_eoESFull_SOURCES) $(t_eoESOps_SOURCES) $(t_eoAtomOps_SOURCES) $(t_selectOne_SOURCES) $(t_eoaltbreeder_SOURCES) $(t_eoGeneralOps_SOURCES) $(t_eoGOpSel_SOURCES) $(t_eoVector_SOURCES)
|
||||||
OBJECTS = $(t_eobreeder_OBJECTS) $(t_eoinclusion_OBJECTS) $(t_eoinsertion_OBJECTS) $(t_eo_OBJECTS) $(t_eofitness_OBJECTS) $(t_eoproblem_OBJECTS) $(t_eobin_OBJECTS) $(t_eolottery_OBJECTS) $(t_eo2dVector_OBJECTS) $(t_eogeneration_OBJECTS) $(t_eoEasyEA_OBJECTS) $(t_eoNonUniform_OBJECTS) $(t_eoUniform_OBJECTS) $(t_eoRandom_OBJECTS) $(t_parser_OBJECTS) $(t_eoESFull_OBJECTS) $(t_eoESOps_OBJECTS) $(t_eoAtomOps_OBJECTS) $(t_selectOne_OBJECTS)
|
OBJECTS = $(t_eobreeder_OBJECTS) $(t_eoinclusion_OBJECTS) $(t_eoinsertion_OBJECTS) $(t_eo_OBJECTS) $(t_eofitness_OBJECTS) $(t_eoproblem_OBJECTS) $(t_eobin_OBJECTS) $(t_eolottery_OBJECTS) $(t_eo2dVector_OBJECTS) $(t_eogeneration_OBJECTS) $(t_eoEasyEA_OBJECTS) $(t_eoNonUniform_OBJECTS) $(t_eoUniform_OBJECTS) $(t_eoRandom_OBJECTS) $(t_parser_OBJECTS) $(t_eoESFull_OBJECTS) $(t_eoESOps_OBJECTS) $(t_eoAtomOps_OBJECTS) $(t_selectOne_OBJECTS) $(t_eoaltbreeder_OBJECTS) $(t_eoGeneralOps_OBJECTS) $(t_eoGOpSel_OBJECTS) $(t_eoVector_OBJECTS)
|
||||||
|
|
||||||
all: all-redirect
|
all: all-redirect
|
||||||
.SUFFIXES:
|
.SUFFIXES:
|
||||||
|
|
@ -403,6 +436,22 @@ t-eoAtomOps: $(t_eoAtomOps_OBJECTS) $(t_eoAtomOps_DEPENDENCIES)
|
||||||
t-selectOne: $(t_selectOne_OBJECTS) $(t_selectOne_DEPENDENCIES)
|
t-selectOne: $(t_selectOne_OBJECTS) $(t_selectOne_DEPENDENCIES)
|
||||||
@rm -f t-selectOne
|
@rm -f t-selectOne
|
||||||
$(CXXLINK) $(t_selectOne_LDFLAGS) $(t_selectOne_OBJECTS) $(t_selectOne_LDADD) $(LIBS)
|
$(CXXLINK) $(t_selectOne_LDFLAGS) $(t_selectOne_OBJECTS) $(t_selectOne_LDADD) $(LIBS)
|
||||||
|
|
||||||
|
t-eoaltbreeder: $(t_eoaltbreeder_OBJECTS) $(t_eoaltbreeder_DEPENDENCIES)
|
||||||
|
@rm -f t-eoaltbreeder
|
||||||
|
$(CXXLINK) $(t_eoaltbreeder_LDFLAGS) $(t_eoaltbreeder_OBJECTS) $(t_eoaltbreeder_LDADD) $(LIBS)
|
||||||
|
|
||||||
|
t-eoGeneralOps: $(t_eoGeneralOps_OBJECTS) $(t_eoGeneralOps_DEPENDENCIES)
|
||||||
|
@rm -f t-eoGeneralOps
|
||||||
|
$(CXXLINK) $(t_eoGeneralOps_LDFLAGS) $(t_eoGeneralOps_OBJECTS) $(t_eoGeneralOps_LDADD) $(LIBS)
|
||||||
|
|
||||||
|
t-eoGOpSel: $(t_eoGOpSel_OBJECTS) $(t_eoGOpSel_DEPENDENCIES)
|
||||||
|
@rm -f t-eoGOpSel
|
||||||
|
$(CXXLINK) $(t_eoGOpSel_LDFLAGS) $(t_eoGOpSel_OBJECTS) $(t_eoGOpSel_LDADD) $(LIBS)
|
||||||
|
|
||||||
|
t-eoVector: $(t_eoVector_OBJECTS) $(t_eoVector_DEPENDENCIES)
|
||||||
|
@rm -f t-eoVector
|
||||||
|
$(CXXLINK) $(t_eoVector_LDFLAGS) $(t_eoVector_OBJECTS) $(t_eoVector_LDADD) $(LIBS)
|
||||||
.cc.o:
|
.cc.o:
|
||||||
$(CXXCOMPILE) -c $<
|
$(CXXCOMPILE) -c $<
|
||||||
.cc.lo:
|
.cc.lo:
|
||||||
|
|
@ -449,7 +498,7 @@ distdir: $(DISTFILES)
|
||||||
@for file in $(DISTFILES); do \
|
@for file in $(DISTFILES); do \
|
||||||
d=$(srcdir); \
|
d=$(srcdir); \
|
||||||
if test -d $$d/$$file; then \
|
if test -d $$d/$$file; then \
|
||||||
cp -pr $$/$$file $(distdir)/$$file; \
|
cp -pr $$d/$$file $(distdir)/$$file; \
|
||||||
else \
|
else \
|
||||||
test -f $(distdir)/$$file \
|
test -f $(distdir)/$$file \
|
||||||
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
|
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
|
||||||
|
|
@ -478,7 +527,7 @@ uninstall: uninstall-am
|
||||||
all-am: Makefile $(PROGRAMS)
|
all-am: Makefile $(PROGRAMS)
|
||||||
all-redirect: all-am
|
all-redirect: all-am
|
||||||
install-strip:
|
install-strip:
|
||||||
$(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install
|
$(MAKE) $(AM_MAKEFLAGS) INSTALL_STRIP_FLAG=-s install
|
||||||
installdirs:
|
installdirs:
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
#include <eo>
|
#include <eo>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
typedef eoBin<float> Chrom;
|
typedef eoBin<float> Chrom;
|
||||||
|
|
||||||
/** Just a simple function that takes binary value of a chromosome and sets
|
/** Just a simple function that takes binary value of a chromosome and sets
|
||||||
the fitnes.
|
the fitnes.
|
||||||
@param _chrom A binary chromosome
|
@param _chrom A binary chromosome
|
||||||
*/
|
*/
|
||||||
void binary_value(Chrom& _chrom)
|
float binary_value(const Chrom& _chrom)
|
||||||
{
|
{
|
||||||
float sum = 0;
|
float sum = 0;
|
||||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||||
if (_chrom[i])
|
if (_chrom[i])
|
||||||
sum += pow(2, _chrom.size() - i - 1);
|
sum += pow(2, _chrom.size() - i - 1);
|
||||||
_chrom.fitness(sum);
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,20 @@
|
||||||
#include <eoESFullChrom.h>
|
#include <eoESFullChrom.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
typedef vector<double> Vec;
|
typedef vector<double> Vec;
|
||||||
|
|
||||||
/** Just a simple function that takes an eoVector<float> and sets the fitnes
|
/** Just a simple function that takes an eoVector<float> and sets the fitnes
|
||||||
to -sphere (we'll see later how to minimize rather than maximize!)
|
to -sphere (we'll see later how to minimize rather than maximize!)
|
||||||
@param _ind A floatingpoint vector
|
@param _ind A floatingpoint vector
|
||||||
*/
|
*/
|
||||||
float the_real_value(Vec& _ind)
|
float real_value(const eoESFullChrom<float>& _ind)
|
||||||
{
|
{
|
||||||
double sum = 0; /* compute in double format, even if return a float */
|
double sum = 0; /* compute in double format, even if return a float */
|
||||||
for (unsigned i = 0; i < _ind.size(); i++)
|
for (unsigned i = 0; i < _ind.size(); i++)
|
||||||
sum += _ind[i] * _ind[i];
|
sum += _ind[i] * _ind[i];
|
||||||
return -sum;
|
return -sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef eoESFullChrom<float> Ind;
|
|
||||||
|
|
||||||
void real_value(Ind & _ind) {
|
|
||||||
_ind.fitness( the_real_value(_ind) );
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,66 @@
|
||||||
// Program to test several EO-ES features
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
t-eoAtomOps.cpp
|
||||||
#pragma warning(disable:4786)
|
Program that tests the atomic operator classes
|
||||||
#endif
|
|
||||||
|
(c) GeNeura Team, 1999
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
This program is free software; you can redistribute it and/or modify
|
||||||
#include <iterator>
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
using namespace std;
|
(at your option) any later version.
|
||||||
|
|
||||||
// Operators we are going to test
|
This program is distributed in the hope that it will be useful,
|
||||||
#include <eoAtomCreep.h>
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
#include <eoAtomBitFlip.h>
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
#include <eoAtomRandom.h>
|
GNU General Public License for more details.
|
||||||
#include <eoAtomMutation.h>
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
// Several EOs
|
along with this program; if not, write to the Free Software
|
||||||
#include <eoString.h>
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
main(int argc, char *argv[]) {
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
eoString<float> aString("123456");
|
|
||||||
eoAtomCreep<char> creeper;
|
*/
|
||||||
eoAtomMutation< eoString<float> > mutator( creeper, 0.5 );
|
|
||||||
|
//-----------------------------------------------------------------------------//
|
||||||
cout << "Before aString " << aString;
|
|
||||||
mutator( aString);
|
#ifdef _MSC_VER
|
||||||
cout << " after mutator " << aString;
|
#pragma warning(disable:4786)
|
||||||
|
#endif
|
||||||
return 0; // to avoid VC++ complaints
|
|
||||||
}
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Operators we are going to test
|
||||||
|
#include <eoAtomCreep.h>
|
||||||
|
#include <eoAtomBitFlip.h>
|
||||||
|
#include <eoAtomRandom.h>
|
||||||
|
#include <eoAtomMutation.h>
|
||||||
|
|
||||||
|
// Several EOs
|
||||||
|
#include <eoString.h>
|
||||||
|
|
||||||
|
// RNGs
|
||||||
|
#include <eoNegExp.h>
|
||||||
|
|
||||||
|
main(int argc, char *argv[]) {
|
||||||
|
eoString<float> aString("123456");
|
||||||
|
eoAtomCreep<char> creeper;
|
||||||
|
eoAtomMutation< eoString<float> > mutator( creeper, 0.5 );
|
||||||
|
|
||||||
|
eoNegExp<char> charNE( 2 );
|
||||||
|
eoAtomRandom<char> randomer( charNE );
|
||||||
|
eoAtomMutation< eoString<float> > mutator2 ( randomer, 0.5 );
|
||||||
|
|
||||||
|
cout << "Before aString " << aString << endl;
|
||||||
|
mutator( aString);
|
||||||
|
cout << " after mutator " << aString << endl;
|
||||||
|
mutator2( aString);
|
||||||
|
cout << " after mutator2 " << aString << endl;;
|
||||||
|
return 0; // to avoid VC++ complaints
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
107
eo/test/t-eoGOpSel.cpp
Normal file
107
eo/test/t-eoGOpSel.cpp
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
t-eoGOpSel.cpp
|
||||||
|
Testing proportional operator selectors
|
||||||
|
|
||||||
|
(c) Maarten Keijzer and GeNeura Team, 2000
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; 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
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
// to avoid long name warnings
|
||||||
|
#pragma warning(disable:4786)
|
||||||
|
|
||||||
|
#include "eoBin.h" // eoBin, eoPop, eoBreeder
|
||||||
|
#include <eoPop.h>
|
||||||
|
#include <eoBitOp.h>
|
||||||
|
#include <eoProportionalGOpSel.h>
|
||||||
|
//#include <eoAltBreeder.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Fitness evaluation
|
||||||
|
#include "binary_value.h"
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef eoBin<float> Chrom;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
eoUniform<Chrom::Type> uniform(false, true);
|
||||||
|
eoBinRandom<Chrom> random;
|
||||||
|
eoPop<Chrom> pop;
|
||||||
|
|
||||||
|
for (i = 0; i < POP_SIZE; ++i)
|
||||||
|
{
|
||||||
|
Chrom chrom(CHROM_SIZE);
|
||||||
|
random(chrom);
|
||||||
|
chrom.fitness(binary_value(chrom));
|
||||||
|
pop.push_back(chrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "population:" << endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
cout << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
eoBinBitFlip<Chrom> bitflip;
|
||||||
|
eoBinCrossover<Chrom> xover;
|
||||||
|
|
||||||
|
//Create the proportional operator selector and add the
|
||||||
|
// two operators creatd above to it.
|
||||||
|
|
||||||
|
eoProportionalGOpSel<Chrom > propSel;
|
||||||
|
propSel.addOp(bitflip, 0.5);
|
||||||
|
propSel.addOp(xover, 0.5);
|
||||||
|
for ( i = 0; i < POP_SIZE; i ++ ) {
|
||||||
|
eoGeneralOp<Chrom>& foo = propSel.selectOp();
|
||||||
|
cout << foo.nInputs() << " "
|
||||||
|
<< foo.nOutputs() << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// eoAltBreeder<Chrom> breeder( propSel );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// breeder(pop);
|
||||||
|
|
||||||
|
// eoSequentialOpSelector<Chrom, eoAltBreeder<Chrom>::outIt > seqSel;
|
||||||
|
|
||||||
|
// eoAltBreeder<Chrom> breeder2( seqSel );
|
||||||
|
// seqSel.addOp(bitflip, 0.25);
|
||||||
|
// seqSel.addOp(xover, 0.75);
|
||||||
|
|
||||||
|
// breeder2(pop);
|
||||||
|
|
||||||
|
// // reevaluation of fitness
|
||||||
|
// for_each(pop.begin(), pop.end(), binary_value);
|
||||||
|
|
||||||
|
// cout << "new population:" << endl;
|
||||||
|
// for (i = 0; i < pop.size(); ++i)
|
||||||
|
// cout << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
71
eo/test/t-eoGeneralOps.cpp
Normal file
71
eo/test/t-eoGeneralOps.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
t-eoGeneralOps.cpp
|
||||||
|
Program that tests the General operator interface, and the wrappers
|
||||||
|
for monary and unary operators.
|
||||||
|
|
||||||
|
(c) GeNeura Team, 1999
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; 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
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(disable:4786)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Operators we are going to test
|
||||||
|
#include <eoAtomCreep.h>
|
||||||
|
#include <eoAtomBitFlip.h>
|
||||||
|
#include <eoAtomRandom.h>
|
||||||
|
#include <eoAtomMutation.h>
|
||||||
|
|
||||||
|
// Several EOs
|
||||||
|
#include <eoString.h>
|
||||||
|
|
||||||
|
// generalOp we are testing
|
||||||
|
#include <eoGeneralOp.h>
|
||||||
|
|
||||||
|
main(int argc, char *argv[]) {
|
||||||
|
eoString<float> aString("123456");
|
||||||
|
eoAtomCreep<char> creeper;
|
||||||
|
eoAtomMutation< eoString<float> > mutator( creeper, 0.5 );
|
||||||
|
|
||||||
|
eoWrappedMonOp< eoString<float> > wCreeper( mutator );
|
||||||
|
cout << "Before aString " << aString;
|
||||||
|
mutator( aString);
|
||||||
|
cout << " after mutator " << aString;
|
||||||
|
|
||||||
|
// Test now the alternative interface
|
||||||
|
eoPop< eoString<float> > vIn, vOut;
|
||||||
|
insert_iterator<eoPop<eoString<float> > > ins( vOut, vOut.begin() );
|
||||||
|
vIn.push_back( aString );
|
||||||
|
wCreeper( vIn.begin(), ins );
|
||||||
|
|
||||||
|
cout << endl << "Before " << vIn[0] << endl << " after " << vOut[0] << endl;;
|
||||||
|
|
||||||
|
return 0; // to avoid VC++ complaints
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,31 +1,30 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------
|
t-eoRandom.cpp
|
||||||
* t-eoRandom
|
Test program for random generator
|
||||||
* Testing program for the eoRNG class
|
|
||||||
* (c) GeNeura Team, 1999
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
(c) GeNeura Team, 1999
|
||||||
modify it under the terms of the GNU General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
This program is free software; you can redistribute it and/or modify
|
||||||
version 2 of the License, or (at your option) any later version.
|
it under the terms of the GNU 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,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
Lesser General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU General Public License
|
||||||
License along with this library; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
#include <iostream> // cout
|
#include <iostream> // cout
|
||||||
#include <strstream> // ostrstream, istrstream
|
#include <strstream> // ostrstream, istrstream
|
||||||
#include <eoUniform.h> // eoBin
|
#include <eoUniform.h> // eoBin
|
||||||
|
|
|
||||||
57
eo/test/t-eoVector.cpp
Normal file
57
eo/test/t-eoVector.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
t-eoVectpr.cpp
|
||||||
|
This program tests vector-like chromosomes
|
||||||
|
(c) GeNeura Team, 1999, 2000
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; 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
|
||||||
|
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <iostream> // cout
|
||||||
|
#include <strstream> // ostrstream, istrstream
|
||||||
|
|
||||||
|
#include <eoUniform.h>
|
||||||
|
#include <eoVector.h> // eoVector
|
||||||
|
#include <eo1dWDistance.h>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef eoVector<float> Chrom;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
const unsigned SIZE = 4;
|
||||||
|
unsigned i, j;
|
||||||
|
eoUniform<Chrom::Type> uniform(-1,1);
|
||||||
|
|
||||||
|
Chrom chrom1(SIZE,uniform), chrom2( SIZE, uniform);
|
||||||
|
|
||||||
|
cout << "chrom1: " << chrom1 << endl <<
|
||||||
|
"chrom2: " << chrom2 << endl;
|
||||||
|
|
||||||
|
eo1dWDistance< float, float > chromDist( chrom1 );
|
||||||
|
cout << "Distance from chrom1 to chrom2 " << chromDist.distance( chrom2 ) << endl;
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
98
eo/test/t-eoaltbreeder.cpp
Normal file
98
eo/test/t-eoaltbreeder.cpp
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
t-eoaltbreeder.cpp
|
||||||
|
Extensive esting of the eoAltBreeder class
|
||||||
|
|
||||||
|
(c) Maarten Keijzer and GeNeura Team, 2000
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; 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
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
// to avoid long name warnings
|
||||||
|
#pragma warning(disable:4786)
|
||||||
|
|
||||||
|
#include "eoBin.h" // eoBin, eoPop, eoBreeder
|
||||||
|
#include <eoPop.h>
|
||||||
|
#include <eoBitOp.h>
|
||||||
|
#include <eoProportionalOpSel.h>
|
||||||
|
#include <eoAltBreeder.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Fitness evaluation
|
||||||
|
#include "binary_value.h"
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef eoBin<float> Chrom;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
eoUniform<Chrom::Type> uniform(false, true);
|
||||||
|
eoBinRandom<Chrom> random;
|
||||||
|
eoPop<Chrom> pop;
|
||||||
|
|
||||||
|
for (i = 0; i < POP_SIZE; ++i)
|
||||||
|
{
|
||||||
|
Chrom chrom(CHROM_SIZE);
|
||||||
|
random(chrom);
|
||||||
|
binary_value(chrom);
|
||||||
|
pop.push_back(chrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "population:" << endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
cout << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
eoBinBitFlip<Chrom> bitflip;
|
||||||
|
eoBinCrossover<Chrom> xover;
|
||||||
|
|
||||||
|
eoProportionalOpSelector<Chrom, eoAltBreeder<Chrom>::outIt > propSel;
|
||||||
|
|
||||||
|
eoAltBreeder<Chrom> breeder( propSel );
|
||||||
|
|
||||||
|
propSel.addOp(bitflip, 0.25);
|
||||||
|
propSel.addOp(xover, 0.75);
|
||||||
|
|
||||||
|
breeder(pop);
|
||||||
|
|
||||||
|
eoSequentialOpSelector<Chrom, eoAltBreeder<Chrom>::outIt > seqSel;
|
||||||
|
|
||||||
|
eoAltBreeder<Chrom> breeder2( seqSel );
|
||||||
|
seqSel.addOp(bitflip, 0.25);
|
||||||
|
seqSel.addOp(xover, 0.75);
|
||||||
|
|
||||||
|
breeder2(pop);
|
||||||
|
|
||||||
|
// reevaluation of fitness
|
||||||
|
for_each(pop.begin(), pop.end(), binary_value);
|
||||||
|
|
||||||
|
cout << "new population:" << endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
cout << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
@ -1,24 +1,23 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
t-eobin.cpp
|
||||||
// t-eobin.cpp
|
This program tests the the binary cromosomes and several genetic operators
|
||||||
// This program test the the binary cromosomes and several genetic operators
|
(c) GeNeura Team, 1999
|
||||||
// (c) GeNeura Team, 1999
|
|
||||||
/*
|
This program is free software; you can redistribute it and/or modify
|
||||||
This library is free software; you can redistribute it and/or
|
it under the terms of the GNU General Public License as published by
|
||||||
modify it under the terms of the GNU General Public
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
License as published by the Free Software Foundation; either
|
(at your option) any later version.
|
||||||
version 2 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
Lesser General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU General Public License
|
||||||
License along with this library; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,82 +1,145 @@
|
||||||
//-----------------------------------------------------------------------------
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
// t-eogeneration.cpp
|
|
||||||
//-----------------------------------------------------------------------------
|
t-eogeneration.cpp
|
||||||
|
Testing the eoGeneration classes, and classes related to it
|
||||||
// to avoid long name warnings
|
|
||||||
#pragma warning(disable:4786)
|
(c) GeNeura Team, 1999, 2000
|
||||||
|
|
||||||
#include <eo>
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
#include "binary_value.h"
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
typedef eoBin<float> Chrom;
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
//-----------------------------------------------------------------------------
|
GNU General Public License for more details.
|
||||||
|
|
||||||
main()
|
You should have received a copy of the GNU General Public License
|
||||||
{
|
along with this program; if not, write to the Free Software
|
||||||
const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
unsigned i;
|
|
||||||
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
eoUniform<Chrom::Type> uniform(false, true);
|
|
||||||
eoBinRandom<Chrom> random;
|
*/
|
||||||
eoPop<Chrom> pop;
|
|
||||||
|
//-----------------------------------------------------------------------------//
|
||||||
for (i = 0; i < POP_SIZE; ++i)
|
|
||||||
{
|
|
||||||
Chrom chrom(CHROM_SIZE);
|
// to avoid long name warnings
|
||||||
random(chrom);
|
#ifdef _MSC_VER
|
||||||
binary_value(chrom);
|
#pragma warning(disable:4786)
|
||||||
pop.push_back(chrom);
|
#endif
|
||||||
}
|
|
||||||
|
#include <eoGeneration.h>
|
||||||
cout << "population:" << endl;
|
#include <eoEvalFuncPtrCnt.h>
|
||||||
for (i = 0; i < pop.size(); ++i)
|
|
||||||
cout << "\t" << pop[i] << " " << pop[i].fitness() << endl;
|
#include "binary_value.h"
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
// selection
|
|
||||||
eoLottery<Chrom> lottery;
|
typedef eoBin<float> Chrom;
|
||||||
|
|
||||||
// breeder
|
//-----------------------------------------------------------------------------
|
||||||
eoBinBitFlip<Chrom> bitflip;
|
|
||||||
eoBinCrossover<Chrom> xover;
|
main()
|
||||||
eoProportionalOpSel<Chrom> propSel;
|
{
|
||||||
eoBreeder<Chrom> breeder( propSel );
|
const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
|
||||||
propSel.addOp(bitflip, 0.25);
|
unsigned i;
|
||||||
propSel.addOp(xover, 0.75);
|
|
||||||
|
eoUniform<Chrom::Type> uniform(false, true);
|
||||||
// replacement
|
eoBinRandom<Chrom> random;
|
||||||
eoInclusion<Chrom> inclusion;
|
eoPop<Chrom> pop;
|
||||||
|
// Evaluation
|
||||||
// Evaluation
|
eoEvalFuncPtr<Chrom> eval( binary_value );
|
||||||
eoEvalFuncPtr<Chrom> eval( binary_value );
|
|
||||||
|
for (i = 0; i < POP_SIZE; ++i)
|
||||||
// GA generation
|
{
|
||||||
eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
|
Chrom chrom(CHROM_SIZE);
|
||||||
|
random(chrom);
|
||||||
// evolution
|
eval(chrom);
|
||||||
unsigned g = 0;
|
pop.push_back(chrom);
|
||||||
do {
|
}
|
||||||
try
|
|
||||||
{
|
cout << "population:" << endl;
|
||||||
generation(pop);
|
for (i = 0; i < pop.size(); ++i)
|
||||||
}
|
cout << "\t" << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
catch (exception& e)
|
|
||||||
{
|
|
||||||
cout << "exception: " << e.what() << endl;;
|
// selection
|
||||||
exit(EXIT_FAILURE);
|
eoLottery<Chrom> lottery;
|
||||||
}
|
|
||||||
|
// breeder
|
||||||
cout << "pop[" << ++g << "]" << endl;
|
eoBinBitFlip<Chrom> bitflip;
|
||||||
for (i = 0; i < pop.size(); ++i)
|
eoBinCrossover<Chrom> xover;
|
||||||
cout << "\t" << pop[i] << " " << pop[i].fitness() << endl;
|
eoProportionalOpSel<Chrom> propSel;
|
||||||
|
eoBreeder<Chrom> breeder( propSel );
|
||||||
} while (pop[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
|
propSel.addOp(bitflip, 0.25);
|
||||||
|
propSel.addOp(xover, 0.75);
|
||||||
return 0;
|
|
||||||
}
|
// replacement
|
||||||
|
eoInclusion<Chrom> inclusion;
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
|
// GA generation
|
||||||
|
eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
|
||||||
|
|
||||||
|
// evolution
|
||||||
|
unsigned g = 0;
|
||||||
|
do {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
generation(pop);
|
||||||
|
}
|
||||||
|
catch (exception& e)
|
||||||
|
{
|
||||||
|
cout << "exception: " << e.what() << endl;;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "pop[" << ++g << "]" << endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
cout << "\t" << pop[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
} while (pop[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
|
||||||
|
|
||||||
|
// Try again, with a "counted" evaluation function
|
||||||
|
// GA generation
|
||||||
|
// Evaluation
|
||||||
|
eoEvalFuncPtrCnt<Chrom> eval2( binary_value );
|
||||||
|
eoPop<Chrom> pop2;
|
||||||
|
|
||||||
|
for (i = 0; i < POP_SIZE; ++i)
|
||||||
|
{
|
||||||
|
Chrom chrom(CHROM_SIZE);
|
||||||
|
random(chrom);
|
||||||
|
binary_value(chrom);
|
||||||
|
eval2(chrom);
|
||||||
|
pop2.push_back(chrom);
|
||||||
|
}
|
||||||
|
eoGeneration<Chrom> generation2(lottery, breeder, inclusion, eval2);
|
||||||
|
|
||||||
|
// evolution
|
||||||
|
do {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
generation2(pop2);
|
||||||
|
}
|
||||||
|
catch (exception& e)
|
||||||
|
{
|
||||||
|
cout << "exception: " << e.what() << endl;;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "pop[" << ++g << "]" << endl;
|
||||||
|
for (i = 0; i < pop2.size(); ++i)
|
||||||
|
cout << "\t" << pop2[i] << " " << pop[i].fitness() << endl;
|
||||||
|
|
||||||
|
} while (pop2[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
|
||||||
|
|
||||||
|
cout << "Number of evaluations " << eval2.getNumOfEvaluations() << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
#Some dsps are missing here
|
||||||
EXTRA_DIST=EO.dsw random.dsp t_eoinsertion.dsp t_ops.dsp\
|
EXTRA_DIST=EO.dsw random.dsp t_eoinsertion.dsp t_ops.dsp\
|
||||||
atomops.dsp t_eoaged.dsp t_eornd.dsp t_opsel.dsp\
|
atomops.dsp t_eoaged.dsp t_eornd.dsp t_opsel.dsp\
|
||||||
t_eobin.dsp t_eostring.dsp t_opselmason.dsp\
|
t_eobin.dsp t_eostring.dsp t_opselmason.dsp\
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue