delete the obsolete directory (still versionned in the repository if necessary)
This commit is contained in:
parent
e131c2178e
commit
2670bd857f
79 changed files with 0 additions and 9009 deletions
|
|
@ -1,6 +0,0 @@
|
|||
*.lo
|
||||
*.la
|
||||
.deps
|
||||
.libs
|
||||
Makefile
|
||||
Makefile.in
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src)
|
||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
||||
######################################################################################
|
||||
### 2) Define the ga target
|
||||
######################################################################################
|
||||
|
||||
SET(EOOBSOLETE_LIB_OUTPUT_PATH ${EO_BINARY_DIR}/lib)
|
||||
SET(LIBRARY_OUTPUT_PATH ${EOOBSOLETE_LIB_OUTPUT_PATH})
|
||||
|
||||
SET (EOOBSOLETE_SOURCES eoParserUtils.cpp)
|
||||
|
||||
|
||||
ADD_LIBRARY(eoobsolete STATIC ${EOOBSOLETE_SOURCES})
|
||||
######################################################################################
|
||||
|
||||
|
||||
|
||||
######################################################################################
|
||||
### 3) Optionnal
|
||||
######################################################################################
|
||||
|
||||
SET(EOOBSOLETE_VERSION ${GLOBAL_VERSION})
|
||||
SET_TARGET_PROPERTIES(eoobsolete PROPERTIES VERSION "${EOOBSOLETE_VERSION}")
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eo1d.h
|
||||
Serial 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
|
||||
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 _EO1D_H
|
||||
#define _EO1D_H
|
||||
|
||||
#include <iostream> // for std::ostream
|
||||
|
||||
// EO Includes
|
||||
#include <EO.h>
|
||||
#include <eoRnd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** @name eo1d class
|
||||
* Randomly accesible evolvable object with one dimension, with variable
|
||||
* length. Use this if you want to evolve "linear" things, like bitstrings,
|
||||
* or floating-point arrays. If you don't, subclass directly from EO
|
||||
* @see EO
|
||||
* @author GeNeura
|
||||
* @version 0.2
|
||||
*/
|
||||
|
||||
//@{
|
||||
/**
|
||||
@deprecated {eo1d} eo1d
|
||||
\deprecated
|
||||
*/
|
||||
|
||||
/**
|
||||
|
||||
eo1d: Base class for "chromosomes" with a single dimension
|
||||
#T# is the type it will be instantiated with; this type must have, at
|
||||
least, a copy ctor, assignment operators,
|
||||
|
||||
\deprecated
|
||||
As eo1d provides a so-called 'fat' interface, it might be wiser to
|
||||
use eoFixedLength or eoVariableLength instead, that derive from
|
||||
std::vector and std::list respectively and (important) redirect the less than
|
||||
comparison operator to EO rather than the STL variants.
|
||||
|
||||
@see eoFixedLength eoVariableLength
|
||||
*/
|
||||
template<class T, class fitnessT = float>
|
||||
class eo1d: public EO< fitnessT > {
|
||||
public:
|
||||
|
||||
/// Declaration to make it accessible from subclasses
|
||||
typedef T Type;
|
||||
|
||||
/** Can be used as default ctor; should be called from derived
|
||||
classes. Fitness should be at birth
|
||||
*/
|
||||
eo1d()
|
||||
:EO<fitnessT> ( ) {};
|
||||
|
||||
/** Ctor using a random number generator and with an specified size
|
||||
@param _rndGen Random number generator
|
||||
@param _size lineal dimension of the eo1d
|
||||
@param _ID An ID std::string, preferably unique
|
||||
*/
|
||||
eo1d( unsigned _size, eoRnd<T>& _rndGen, const std::string& _ID = "");
|
||||
|
||||
/** Ctor from a std::istream. It just passes the stream to EO, subclasses should
|
||||
have to implement this.
|
||||
@param _is the input stream
|
||||
*/
|
||||
eo1d( std::istream& _is): EO<fitnessT>(){ readFrom(is); }
|
||||
|
||||
/// Copy ctor
|
||||
eo1d( const eo1d& _eo )
|
||||
:EO<fitnessT> ( _eo ) {};
|
||||
|
||||
/// Assignment operator
|
||||
const eo1d& operator= ( const eo1d& _eo ) {
|
||||
EO<fitnessT>::operator = ( _eo );
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Needed virtual dtor
|
||||
virtual ~eo1d(){};
|
||||
|
||||
/** Reads and returns a copy of the gene in position _i
|
||||
This implies that T must have a copy ctor .
|
||||
@param _i index of the gene, which is the minimal unit. Must be
|
||||
an unsigned less than #length()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _i > size()
|
||||
*/
|
||||
virtual T getGene( unsigned _i ) const = 0;
|
||||
|
||||
/** Overwrites the gene placed in position _i with a
|
||||
* new value. This means that the assignment operator
|
||||
* for T must be defined .
|
||||
@param _i index
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _i > size()
|
||||
*/
|
||||
virtual void setGene( unsigned _i, const T& _value ) = 0;
|
||||
|
||||
/** Inserts a gene, moving the rest to the right. If
|
||||
* _i = length(), it should insert it at the end.
|
||||
* Obviously, changes length
|
||||
@param _i index
|
||||
@param _val new value
|
||||
*/
|
||||
virtual void insertGene( unsigned _i, T _val ) = 0;
|
||||
|
||||
/** Eliminates the gene at position _i; all the other genes will
|
||||
be shifted left
|
||||
@param _i index of the gene that is going to be modified.
|
||||
*/
|
||||
virtual void deleteGene( unsigned _i ) = 0;
|
||||
|
||||
/// Returns the number of genes in the eo1d
|
||||
virtual unsigned length() const = 0;
|
||||
|
||||
/// @name Methods from eoObject
|
||||
//@{
|
||||
/**
|
||||
* Read object. Theoretically, the length is known in advance. All objects
|
||||
* Should call base class
|
||||
* @param _s A std::istream.
|
||||
* @throw runtime_std::exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(std::istream& _s) {
|
||||
|
||||
for ( unsigned i = 0; i < length(); i ++ ) {
|
||||
T tmp;
|
||||
_s >> tmp;
|
||||
setGene( i, tmp );
|
||||
}
|
||||
// there is no way of distinguishing fitness from the object, so
|
||||
// it's skipped
|
||||
}
|
||||
|
||||
/** Print itself: inherited from eoObject implementation.
|
||||
Instance from base classes are processed in
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
@param _s the std::ostream in which things are written*/
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
for ( unsigned i = 0; i < length(); i ++ ) {
|
||||
_s << getGene( i ) << " ";
|
||||
}
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eo1d";};
|
||||
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
// --------------- Implementations --------------------------
|
||||
|
||||
/* Ctor using a random number generator and with an specified size
|
||||
@param _rndGen Random number generator
|
||||
@param _size lineal dimension of the eo1d
|
||||
@param _ID An ID std::string, preferably unique
|
||||
*/
|
||||
template< class T, class fitnessT>
|
||||
eo1d<T,fitnessT>::eo1d<T,fitnessT>( unsigned _size, eoRnd<T>& _rndGen,
|
||||
const std::string& _ID )
|
||||
:EO<fitnessT> ( _ID ) {
|
||||
for ( unsigned i = 0; i < _size; i ++ ) {
|
||||
insertGene( i, _rndGen() );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
/* -*- 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 std::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
|
||||
@std::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
|
||||
@std::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
|
||||
*/
|
||||
std::string className() const {return "eo1dWDistance";};
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
///Private ctor. Do not use
|
||||
eo1dWDistance() {};
|
||||
|
||||
|
||||
eo1d<T,fitnessT>& innereo1d;
|
||||
};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,242 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
/*
|
||||
-----------------------------------------------------------------------------
|
||||
File............: eo2d.h
|
||||
Author..........: Geneura Team (this file: Victor Rivas, vrivas@ujaen.es)
|
||||
Date............: 21-Sep-1999, at Fac. of Sciences, Univ. of Granada (Spain)
|
||||
Description.....: Implementation of a 2-dimensional chromosome.
|
||||
|
||||
================ Modif. 1 ================
|
||||
Author........:
|
||||
Date..........:
|
||||
Description...:
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
// eo2d.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EO2D_H
|
||||
#define _EO2D_H
|
||||
|
||||
#include <iostream> // for std::ostream
|
||||
#include <vector>
|
||||
|
||||
// EO Includes
|
||||
#include <EO.h>
|
||||
#include <eoRnd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** @name eo2d class
|
||||
* Randomly accesible evolvable object with two dimension, with
|
||||
variable length each of them.
|
||||
* Use this if you want to evolve "two-dimensional" things, like bit arrays, or
|
||||
floating-point arrays. If you don't, subclass directly from EO
|
||||
* @see EO
|
||||
* @author GeNeura
|
||||
* @version 0.2
|
||||
*/
|
||||
|
||||
//@{
|
||||
|
||||
/** eo2d: Base class for "chromosomes" with a double dimension
|
||||
#T# is the type it will be instantiated with; this type must have, at
|
||||
least, a copy ctor, assignment operators,
|
||||
*/
|
||||
template<class T, class fitnessT = float>
|
||||
class eo2d: public EO< fitnessT > {
|
||||
public:
|
||||
|
||||
/// Declaration to make it accessible from subclasses
|
||||
typedef T Type;
|
||||
|
||||
/** Can be used as default ctor; should be called from derived
|
||||
classes. Fitness should be at birth
|
||||
*/
|
||||
eo2d()
|
||||
:EO<fitnessT> ( ) {};
|
||||
|
||||
/** Ctor using a random number generator and with an specified size
|
||||
@param _rows Initial number of rows
|
||||
@param _columns Initial number of columns
|
||||
@param _rndGen Random "T-type" generator
|
||||
@param _ID An ID std::string, preferably unique
|
||||
*/
|
||||
eo2d( const unsigned _rows,
|
||||
const unsigned _columns,
|
||||
eoRnd<T>& _rndGen,
|
||||
const std::string& _ID = "");
|
||||
|
||||
/** Ctor from an std::istream. It just passes the stream to EO, subclasses should
|
||||
have to implement this.
|
||||
@param _is the input stream
|
||||
*/
|
||||
eo2d( std::istream& _is): EO<fitnessT>( _is ){};
|
||||
|
||||
/// Copy ctor
|
||||
eo2d( const eo2d& _eo )
|
||||
:EO<fitnessT> ( _eo ) {};
|
||||
|
||||
/// Assignment operator
|
||||
const eo2d& operator= ( const eo2d& _eo ) {
|
||||
EO<fitnessT>::operator = ( _eo );
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Needed virtual dtor
|
||||
virtual ~eo2d(){};
|
||||
|
||||
/** Reads and returns a copy of the gene in position _r,_c.\
|
||||
This implies that T must have a copy ctor .
|
||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual T getGene( const unsigned _r,
|
||||
const unsigned _j ) const = 0;
|
||||
|
||||
/** Overwrites the gene placed in position _r,_c with a
|
||||
* new value. This means that the assignment operator
|
||||
* for T must be defined .
|
||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual void setGene( const unsigned _r,
|
||||
const unsigned _c,
|
||||
const T& _value ) = 0;
|
||||
|
||||
/** Inserts a row, moving the rest to the bottom.
|
||||
* If _r = numOfRows(), it insert it at the end.
|
||||
* Obviously, changes number of rows.
|
||||
@param _r Position where the new row will be inserted.
|
||||
@param _val Vector containing the new values to be inserted.
|
||||
@std::exception invalid_argument If _val has not numOfCols() components.
|
||||
@std::exception out_of_range If _r is greater than numOfRows()
|
||||
*/
|
||||
virtual void insertRow( const unsigned _r,
|
||||
const std::vector<T>& _val ) = 0;
|
||||
|
||||
/** Eliminates the row at position _r; all the other genes will
|
||||
be shifted up.
|
||||
@param _r Number of he row to be deleted.
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
*/
|
||||
virtual void deleteRow( const unsigned _r ) = 0;
|
||||
|
||||
/** Inserts a column, moving the rest to the right.
|
||||
* If _c = numOfCols(), it insert it at the end.
|
||||
* Obviously, changes number of cols.
|
||||
@param _r Position where the new column will be inserted.
|
||||
@param _val Vector containing the new values to be inserted.
|
||||
@std::exception invalid_argument if _val has not numOfRows() components.
|
||||
*/
|
||||
virtual void insertCol( const unsigned _c,
|
||||
const std::vector<T>& _val ) = 0;
|
||||
|
||||
/** Eliminates the column at position _c; all the other columns will
|
||||
be shifted left.
|
||||
@param _c Number of he column to be deleted.
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual void deleteCol( const unsigned _c ) = 0;
|
||||
|
||||
/// Returns the number of rows in the eo2d
|
||||
virtual unsigned numOfRows() const = 0;
|
||||
|
||||
/// Returns the number of columns in the eo2d
|
||||
virtual unsigned numOfCols() const = 0;
|
||||
|
||||
/// @name Methods from eoObject
|
||||
//@{
|
||||
/**
|
||||
* Read object. Theoretically, the length is known in advance. All objects
|
||||
* Should call base class
|
||||
* @param _s A std::istream.
|
||||
* @throw runtime_std::exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(std::istream& _s) {
|
||||
|
||||
for ( unsigned r = 0; r < numOfRows(); ++r ) {
|
||||
for ( unsigned c = 0; c < numOfCols(); ++c ) {
|
||||
T tmp;
|
||||
_s >> tmp;
|
||||
setGene( r, c, tmp );
|
||||
}
|
||||
}
|
||||
// there is no way of distinguishing fitness from the object, so
|
||||
// it's skipped
|
||||
}
|
||||
|
||||
/** Print itself: inherited from eoObject implementation.
|
||||
Instance from base classes are processed in
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
@param _s the std::ostream in which things are written*/
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
for ( unsigned r = 0; r < numOfRows(); ++r ) {
|
||||
for ( unsigned c = 0; c < numOfCols(); ++c ) {
|
||||
_s << getGene( r,c ) << " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eo2d";};
|
||||
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
// --------------- Implementations --------------------------
|
||||
|
||||
/** Ctor using a random number generator and with an specified size
|
||||
@param _rows Initial number of rows
|
||||
@param _columns Initial number of columns
|
||||
@param _rndGen Random "T-type" generator
|
||||
@param _ID An ID std::string, preferably unique
|
||||
*/
|
||||
template< class T, class fitnessT>
|
||||
eo2d<T,fitnessT>::eo2d<T,fitnessT>( const unsigned _rows,
|
||||
const unsigned _columns,
|
||||
eoRnd<T>& _rndGen,
|
||||
const std::string& _ID = "")
|
||||
:EO<fitnessT> ( _ID ) {
|
||||
for ( unsigned r = 0; r < _rows; ++r ) {
|
||||
for ( unsigned c = 0; c < _cols; ++c ) {
|
||||
insertGene( r, c, _rndGen() );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,334 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
/*
|
||||
-----------------------------------------------------------------------------
|
||||
File............: eo2dVector.h
|
||||
Author..........: Geneura Team (this file: Victor Rivas, vrivas@ujaen.es)
|
||||
Date............: 29-Sep-1999, at Fac. of Sciences, Univ. of Granada (Spain)
|
||||
Description.....: Implementation of a 2-dimensional chromosome usign STL
|
||||
std::vectors.
|
||||
|
||||
================ Modif. 1 ================
|
||||
Author........:
|
||||
Date..........:
|
||||
Description...:
|
||||
|
||||
QUEDA: Operador de asignación, lectura desde std::istream, escritura a std::ostream
|
||||
-----------------------------------------------------------------------------
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
// eo2dVector.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _eo2dVector_H
|
||||
#define _eo2dVector_H
|
||||
|
||||
// STL libraries
|
||||
#include <vector> // For std::vector<int>
|
||||
#include <stdexcept>
|
||||
#include <strstream>
|
||||
#include <ostream.h>
|
||||
|
||||
#include <eo2d.h>
|
||||
#include <eoRnd.h>
|
||||
|
||||
/** Adaptor that turns an STL std::vector of vectror into an EO
|
||||
with the same gene type as the type with which
|
||||
the std::vector of std::vector has been instantiated.
|
||||
*/
|
||||
template <class T, class fitnessT>
|
||||
class eo2dVector: public eo2d<T, fitnessT>, public std::vector< std::vector<T> > {
|
||||
public:
|
||||
typedef T Type ;
|
||||
|
||||
/** @name Canonical part of the objects: several ctors, copy ctor, \
|
||||
* dtor and assignment operator.
|
||||
*/
|
||||
//@{
|
||||
|
||||
/** Ctor.
|
||||
@param _rows Number of rows.
|
||||
@param _cols Number of columns.
|
||||
@param _val Common initial value
|
||||
*/
|
||||
eo2dVector( const unsigned _rows = 0,
|
||||
const unsigned _cols = 0,
|
||||
T _val = T() )
|
||||
: eo2d<T, fitnessT>(), std::vector< std::vector<T> >( _rows, std::vector<T>( _cols, _val ) ){};
|
||||
|
||||
/** Ctor using a random number generator.
|
||||
@param _rows Number of rows.
|
||||
@param _cols Number of columns.
|
||||
@param _rnd A random "T-type" generator, which returns a random value each time it´s called.
|
||||
*/
|
||||
eo2dVector( const unsigned _rows,
|
||||
const unsigned _cols,
|
||||
eoRnd<T>& _rnd );
|
||||
|
||||
/** Ctor from a std::istream. The T class should accept reading from a std::istream. It doesn't read fitness,
|
||||
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
|
||||
*/
|
||||
//eo2dVector( std::istream& _is);
|
||||
|
||||
|
||||
/// copy ctor
|
||||
eo2dVector( const eo2dVector & _eo )
|
||||
: eo2d<T, fitnessT>( _eo ), std::vector< std::vector<T> >( _eo ){ };
|
||||
|
||||
/// Assignment operator
|
||||
/*
|
||||
const eo2dVector& operator =( const eo2dVector & _eo ) {
|
||||
if ( this != &_eo ){
|
||||
eo2d<T, fitnessT>::operator=( _eo );
|
||||
std::vector< <std::vector<T> >::operator=( _eo );
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
/// dtor
|
||||
virtual ~eo2dVector() {};
|
||||
|
||||
//@}
|
||||
/** Reads and returns a copy of the gene in position _r,_c.\
|
||||
This implies that T must have a copy ctor .
|
||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual T getGene( const unsigned _r,
|
||||
const unsigned _c ) const {
|
||||
if ( _r >= numOfRows() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::getGene: row out of range. "
|
||||
<< "It should be <" << numOfRows() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
if ( _c >= numOfCols() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::getGene: column out of range. "
|
||||
<< "It should be <" << numOfCols() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
return (*this)[_r][_c];
|
||||
};
|
||||
/** Overwrites the gene placed in position _r,_c with a
|
||||
* new value. This means that the assignment operator
|
||||
* for T must be defined .
|
||||
@param _r Index for rows. Must be an unsigned less than #numOfRows()#
|
||||
@param _c Index for columns. Must be an unsigned less than #numOfCols()#
|
||||
@return what's inside the gene, with the correct type
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual void setGene( const unsigned _r,
|
||||
const unsigned _c,
|
||||
const T& _value ) {
|
||||
if ( _r >= numOfRows() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::setGene: row out of range. "
|
||||
<< "It should be <" << numOfRows() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
if ( _c >= numOfCols() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::setGene: column out of range. "
|
||||
<< "It should be <" << numOfCols() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
(*this)[_r][_c]=_value;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/** Inserts a row, moving the rest to the bottom.
|
||||
* If _r = numOfRows(), it insert it at the end.
|
||||
* Obviously, changes number of rows.
|
||||
@param _r Position where the new row will be inserted.
|
||||
@param _val Vector containing the new values to be inserted.
|
||||
@std::exception invalid_argument If _val has not numOfCols() components.
|
||||
@std::exception out_of_range If _r is greater than numOfRows()
|
||||
*/
|
||||
virtual void insertRow( const unsigned _r,
|
||||
const std::vector<T>& _val ) {
|
||||
// Test errors.
|
||||
if ( _r > numOfRows() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::insertRow: row out of range. "
|
||||
<< "It should be <=" << numOfRows() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
if ( _val.size() != numOfCols() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::insertRow: "
|
||||
<< "Incorrect number of values to be added. "
|
||||
<< "It should be ==" << numOfCols() << '\0' << std::endl;
|
||||
throw invalid_argument( msg.str() );
|
||||
}
|
||||
|
||||
// Insert the row.
|
||||
std::vector< std::vector<T> >::iterator ite = begin()+_r;
|
||||
insert( ite, _val );
|
||||
};
|
||||
|
||||
/** Eliminates the row at position _r; all the other genes will
|
||||
be shifted up.
|
||||
@param _r Number of he row to be deleted.
|
||||
@std::exception out_of_range if _r >=numOfRows()
|
||||
*/
|
||||
virtual void deleteRow( const unsigned _r ) {
|
||||
// Test error.
|
||||
if ( _r >= numOfRows() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::deleteRow: "
|
||||
<< "Row out of range. "
|
||||
<< "It should be <" << numOfRows() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
// Delete row.
|
||||
std::vector< std::vector<T> >::iterator ite = this->begin()+_r;
|
||||
this->erase( ite );
|
||||
};
|
||||
|
||||
/** Inserts a column, moving the rest to the right.
|
||||
* If _c = numOfCols(), it insert it at the end.
|
||||
* Obviously, changes number of cols.
|
||||
@param _r Position where the new column will be inserted.
|
||||
@param _val Vector containing the new values to be inserted.
|
||||
@std::exception invalid_argument if _val has not numOfRows() components.
|
||||
*/
|
||||
virtual void insertCol( const unsigned _c,
|
||||
const std::vector<T>& _val ) {
|
||||
// Test errors.
|
||||
if ( _c > numOfCols() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::insertCol: "
|
||||
<< "Column out of range. "
|
||||
<< "It should be >=" << numOfCols() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
if ( _val.size() != numOfRows() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::insertCol: "
|
||||
<< "Incorrect number of values to be added. "
|
||||
<< "It should be ==" << numOfRows() << '\0' << std::endl;
|
||||
throw invalid_argument( msg.str() );
|
||||
}
|
||||
|
||||
// Insert column.
|
||||
for( unsigned r=0; r<numOfRows(); ++r ) {
|
||||
std::vector<std::vector<T> >::iterator it1 = begin()+r;
|
||||
std::vector<T>::iterator it2 = (*it1).begin()+_c;
|
||||
(*it1).insert( it2, _val[r] );
|
||||
};
|
||||
}
|
||||
|
||||
/** Eliminates the column at position _c; all the other columns will
|
||||
be shifted left.
|
||||
@param _c Number of he column to be deleted.
|
||||
@std::exception out_of_range if _c >=numOfCols()
|
||||
*/
|
||||
virtual void deleteCol( const unsigned _c ) {
|
||||
// Test error.
|
||||
if ( _c >= numOfCols() ) {
|
||||
std::ostrstream msg;
|
||||
msg << "ERROR in eo2dVector::deleteCol: "
|
||||
<< "Column out of range. "
|
||||
<< "It should be <" << numOfCols() << '\0' << std::endl;
|
||||
throw out_of_range( msg.str() );
|
||||
}
|
||||
// Delete columns.
|
||||
for( unsigned r=0; r<numOfRows(); ++r ) {
|
||||
std::vector<std::vector<T> >::iterator it1 = begin()+r;
|
||||
std::vector<T>::iterator it2 = (*it1).begin()+_c;
|
||||
(*it1).erase( it2 );
|
||||
}
|
||||
};
|
||||
|
||||
/// Returns the number of rows in the eo2d
|
||||
virtual unsigned numOfRows() const {
|
||||
return size();
|
||||
};
|
||||
|
||||
/// Returns the number of columns in the eo2d
|
||||
virtual unsigned numOfCols() const {
|
||||
return begin()->size();
|
||||
};
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eo2dVector";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
//____________________________ Some method implementation ____________________
|
||||
|
||||
// Ctors_______________________________________________________________________
|
||||
//_____________________________________________________________________________
|
||||
template <class T, class fitnessT>
|
||||
eo2dVector<T,fitnessT>::eo2dVector( const unsigned _rows,
|
||||
const unsigned _cols,
|
||||
eoRnd<T>& _rnd )
|
||||
: eo2d<T, fitnessT>(), std::vector< std::vector<T> >( _rows, std::vector<T>( _cols, T() ) ){
|
||||
for ( std::vector< std::vector<T> >::iterator i = begin(); i != end(); ++i ) {
|
||||
for( std::vector<T>::iterator j=(*i).begin(); j!= (*i).end(); ++j ) {
|
||||
*j = _rnd();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//_____________________________________________________________________________
|
||||
/*template <class T, class fitnessT>
|
||||
eoVector<T,fitnessT>::eoVector( std::istream& _is)
|
||||
: eo1d<T, fitnessT>(), std::vector<T>( ){
|
||||
while (_is ) {
|
||||
T tmp;
|
||||
_is >> tmp;
|
||||
push_back( tmp );
|
||||
}
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
//_____________________________________________________________________________
|
||||
template <class T, class fitnessT>
|
||||
std::ostream& operator<<( std::ostream& _os, const eo2dVector<T,fitnessT>& _eo) {
|
||||
for( unsigned i=0; i<_eo.numOfRows(); ++i ) {
|
||||
for( unsigned j=0; j<_eo.numOfCols(); ++j ) {
|
||||
_os << _eo.getGene( i,j ) << " ";
|
||||
}
|
||||
_os << std::endl;
|
||||
}
|
||||
return _os;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAtomBitFlip.h
|
||||
// Increments or decrements by one a single element
|
||||
// (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 _EOATOMBITFLIP_H
|
||||
#define _EOATOMBITFLIP_H
|
||||
|
||||
/** Flips a single bit
|
||||
*/
|
||||
template <class T>
|
||||
class eoAtomBitFlip: public eoAtomMutator<bool> {
|
||||
public:
|
||||
|
||||
///
|
||||
eoAtomBitFlip() {};
|
||||
|
||||
///
|
||||
virtual ~eoAtomBitFlip() {};
|
||||
|
||||
///
|
||||
virtual void operator()( T& _val ) const {
|
||||
_val = !val;
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoAtomBitFlip";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAtomCreep.h
|
||||
// Increments or decrements by one a single element
|
||||
// (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 _EOATOMCREEP_H
|
||||
#define _EOATOMCREEP_H
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoAtomMutator.h>
|
||||
|
||||
/** With uniform probability, decrements or increments by one the value.
|
||||
The value type must admit post increment and decrement.
|
||||
*/
|
||||
template <class T>
|
||||
class eoAtomCreep: public eoAtomMutator<T> {
|
||||
public:
|
||||
|
||||
///
|
||||
eoAtomCreep() {};
|
||||
|
||||
///
|
||||
virtual ~eoAtomCreep() {};
|
||||
|
||||
///
|
||||
virtual void operator()( T& _val ) const {
|
||||
if ( rng.flip( 0.5 ) ) {
|
||||
_val++;
|
||||
} else {
|
||||
_val--;
|
||||
}
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoAtomCreep";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoAtomRandom.h
|
||||
Increments or decrements by one a single element
|
||||
(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 _EOATOMRANDOM_H
|
||||
#define _EOATOMRANDOM_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.
|
||||
*/
|
||||
template <class T>
|
||||
class eoAtomRandom: public eoAtomMutator<T> {
|
||||
public:
|
||||
|
||||
///
|
||||
eoAtomRandom( eoRnd<T>& _rng): rnd( _rng ) {};
|
||||
|
||||
///
|
||||
virtual ~eoAtomRandom() {};
|
||||
|
||||
/// Adds the value generated by the RNG to the former value
|
||||
virtual void operator()( T& _val ) const {
|
||||
_val += rnd();
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoAtomRandom";};
|
||||
//@}
|
||||
|
||||
private:
|
||||
eoRnd<T>& rnd;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoInserter.h
|
||||
Abstract population insertion operator, which is used by the eoGeneralOps
|
||||
to insert the results in the (intermediate) population. This file also
|
||||
contains the definitions of a derived classes that implements a back inserter,
|
||||
probably the only efficient inserter for populations of type std::vector.
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoBackInserter_h
|
||||
#define eoBackInserter_h
|
||||
|
||||
#include <eoInserter.h>
|
||||
|
||||
/**
|
||||
\ingroup inserters
|
||||
* eoBackInserter: Interface class that enables an operator to insert
|
||||
new individuals at the back of the new population.
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoBackInserter : public eoPopInserter<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoBackInserter(void) : eoPopInserter<EOT>() {}
|
||||
|
||||
eoInserter<EOT>& operator()(const EOT& _eot)
|
||||
{
|
||||
pop().push_back(_eot);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string className(void) const { return "eoBackInserter"; }
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
/*
|
||||
eoBin.h
|
||||
(c) GeNeura Team 1998
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
|
||||
#ifndef eoBin_h
|
||||
#define eoBin_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // std::ostream, std::istream
|
||||
#include <functional> // bind2nd
|
||||
#include <string> // std::string
|
||||
|
||||
#include <eoFixedLength.h>
|
||||
|
||||
/**
|
||||
\defgroup bitstring
|
||||
|
||||
Various functions for a bitstring representation
|
||||
*/
|
||||
|
||||
/** eoBin: implementation of binary chromosome.
|
||||
\class eoBin eoBin.h ga/eoBin.h
|
||||
\ingroup bitstring
|
||||
* based on STL's bit_std::vector (std::vector<bool>).
|
||||
*/
|
||||
template <class F> class eoBin: public eoFixedLength<F, bool>
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
* @param size Size of the binary std::string.
|
||||
*/
|
||||
eoBin(unsigned size = 0, bool value = false):
|
||||
eoVector<bool,F>(size, value) {}
|
||||
|
||||
/// My class name.
|
||||
std::string className() const
|
||||
{
|
||||
return "eoBin";
|
||||
}
|
||||
|
||||
/**
|
||||
* To print me on a stream.
|
||||
* @param os The std::ostream.
|
||||
*/
|
||||
void printOn(std::ostream& os) const
|
||||
{
|
||||
copy(begin(), end(), std::ostream_iterator<bool>(os));
|
||||
}
|
||||
|
||||
/**
|
||||
* To read me from a stream.
|
||||
* @param is The std::istream.
|
||||
*/
|
||||
void readFrom(std::istream& is)
|
||||
{
|
||||
std::string bits;
|
||||
is >> bits;
|
||||
if (is)
|
||||
{
|
||||
resize(bits.size());
|
||||
transform(bits.begin(), bits.end(), begin(),
|
||||
bind2nd(equal_to<char>(), '1'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoBin_h
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoBreeder.h
|
||||
// Takes two populations and mixes them
|
||||
// (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 eoBreeder_h
|
||||
#define eoBreeder_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // std::vector
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoOpSelector.h> // eoOpSelector
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
#include <eoRandomIndiSelector.h>
|
||||
#include <eoBackInserter.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
* eoBreeder: transforms a population using genetic operators. *
|
||||
* For every operator there is a rated to be applyed. *
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoBreeder: public eoTransform<EOT>
|
||||
//eoUnaryFunctor<void, eoPop<Chrom>&>
|
||||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
|
||||
|
||||
/// Destructor.
|
||||
virtual ~eoBreeder() {}
|
||||
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
size_t orgsize = pop.size();
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); i++)
|
||||
{
|
||||
eoOp<Chrom>* op = opSel.Op();
|
||||
switch (op->getType())
|
||||
{
|
||||
case eoOp<Chrom>::unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
(*monop)( pop[i] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
(*binop)(pop[i], pop[ rng.random(pop.size()) ] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::quadratic:
|
||||
{
|
||||
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
|
||||
|
||||
(*Qop)(pop[i], pop[ rng.random(pop.size()) ] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::general :
|
||||
{
|
||||
eoGeneralOp<Chrom>* Gop = static_cast<eoGeneralOp<Chrom>* >(op);
|
||||
|
||||
eoRandomIndiSelector<Chrom> selector;
|
||||
eoBackInserter<Chrom> inserter;
|
||||
|
||||
(*Gop)(selector.bind(pop, orgsize).bias(i), inserter.bind(pop));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// The class name.
|
||||
std::string className() const { return "eoBreeder"; }
|
||||
|
||||
private:
|
||||
eoOpSelector<Chrom>& opSel;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoBreeder_h
|
||||
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoCopyElite.h
|
||||
// Base class for elitist-merging classes
|
||||
// (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 eoCopyElite_h
|
||||
#define eoCopyElite_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoFunctor.h> // eoCopyElite
|
||||
|
||||
/**
|
||||
* eoCopyElite: Base class for elitist replacement algorithms.
|
||||
* Merges the old population (first argument), with the new generation
|
||||
*
|
||||
* Its signature is exactly
|
||||
* that of the selection base eoSelect, but its purpose is to merge the
|
||||
* two populations into one (the second argument).
|
||||
* Note that the algorithms assume that the second argument denotes the
|
||||
* next generation.
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoCopyElite: public eoBinaryFunctor<void, const eoPop<Chrom>&, eoPop<Chrom>&>
|
||||
{};
|
||||
|
||||
/**
|
||||
Straightforward elitism class, specify the number of individuals to copy
|
||||
into new geneneration
|
||||
*/
|
||||
template <class EOT> class eoElitism : public eoCopyElite<EOT>
|
||||
{
|
||||
public :
|
||||
eoElitism(unsigned _howmany) : howmany(_howmany) {}
|
||||
|
||||
void operator()(const eoPop<EOT>& _pop, eoPop<EOT>& offspring)
|
||||
{
|
||||
if (howmany == 0)
|
||||
return;
|
||||
|
||||
if (howmany > _pop.size())
|
||||
throw logical_error("Elite larger than population");
|
||||
|
||||
std::vector<const EOT*> result;
|
||||
_pop.nth_element(howmany, result);
|
||||
|
||||
for (int i = 0; i < result.size(); ++i)
|
||||
{
|
||||
offspring.push_back(*result[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private :
|
||||
unsigned howmany;
|
||||
};
|
||||
|
||||
/**
|
||||
No elite
|
||||
*/
|
||||
template <class EOT> class eoNoElitism : public eoElitism<EOT>
|
||||
{
|
||||
public :
|
||||
eoNoElitism() : eoElitism(0) {}
|
||||
}
|
||||
|
||||
/**
|
||||
Very elitist class, copies entire population into next gen
|
||||
*/
|
||||
template <class EOT> class eoPlus : public eoCopyElite<EOT>
|
||||
{
|
||||
public :
|
||||
void operator()(const eoPop<EOT>& _pop, eoPop<EOT>& offspring)
|
||||
{
|
||||
offspring.reserve(offspring.size() + _pop.size());
|
||||
|
||||
for (int i = 0; i < _pop.size(); ++i)
|
||||
{
|
||||
offspring.push_back(*result[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private :
|
||||
unsigned howmany;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoDEA.h
|
||||
// (c) Marc Schoenauer, Maarten Keijzer, 2001
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoDEA_h
|
||||
#define _eoDEA_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoDistribution.h>
|
||||
|
||||
/** eoDEA: Distribution Evolution Algorithm within EO
|
||||
*
|
||||
* The abstract class for algorithms that evolve a probability distribution
|
||||
* on the spaces of populations rather than a population
|
||||
*
|
||||
* It IS NOT an eoAlgo, as it evolves a distribution, not a population
|
||||
*/
|
||||
|
||||
template<class EOT> class eoDEA: public eoUF<eoDistribution<EOT>&, void>
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoDetTournament.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoDetTournament_h
|
||||
#define eoDetTournament_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <functional> //
|
||||
#include <numeric> // accumulate
|
||||
|
||||
#include <eoFunctor.h>
|
||||
#include <eoPop.h>
|
||||
#include <utils/selectors.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoDetTournament: a selection method that selects ONE individual by
|
||||
deterministic tournament
|
||||
-MS- 24/10/99 */
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne<EOT>(), Tsize(_Tsize) {
|
||||
// consistency check
|
||||
if (Tsize < 2) {
|
||||
std::cout << "Warning, Tournament size should be >= 2\nAdjusted\n";
|
||||
Tsize = 2;
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform deterministic tournament
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||
{
|
||||
return deterministic_tournament(pop, Tsize);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned Tsize;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoDetTournament_h
|
||||
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoDetTournamentIndiSelector.h
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoDetTournamentIndiSelector_h
|
||||
#define eoDetTournamentIndiSelector_h
|
||||
|
||||
#include "eoIndiSelector.h"
|
||||
#include "utils/selectors.h"
|
||||
|
||||
|
||||
/**
|
||||
\ingroup selectors
|
||||
* eoDetTournamentIndiSelector: selects children through a deterministic_tournament
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoDetTournamentIndiSelector : public eoPopIndiSelector<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoDetTournamentIndiSelector(int _tournamentSize)
|
||||
: eoPopIndiSelector<EOT>(),
|
||||
tournamentSize(_tournamentSize)
|
||||
{}
|
||||
|
||||
virtual ~eoDetTournamentIndiSelector(void) {}
|
||||
|
||||
const EOT& do_select(void)
|
||||
{
|
||||
return *deterministic_tournament(begin(), end(), tournamentSize);
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
int tournamentSize;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoDetTournamentInserter.h
|
||||
Concrete steady state inserter. It is initialized with a population and
|
||||
inserts individuals in the population based on an inverse deterministic
|
||||
tournament
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoDetTournamentInserter_h
|
||||
#define eoDetTournamentInserter_h
|
||||
|
||||
|
||||
#include <eoSteadyStateInserter.h>
|
||||
#include <utils/selectors.h>
|
||||
|
||||
/**
|
||||
* eoDetTournamentInserter: Uses an inverse deterministic tournament to figure
|
||||
* out who gets overridden by the new individual. It resets the fitness of the
|
||||
* individual.
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoDetTournamentInserter : public eoSteadyStateInserter<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoDetTournamentInserter(eoEvalFunc<EOT>& _eval, unsigned _t_size):
|
||||
eoSteadyStateInserter<EOT>(_eval),
|
||||
t_size(_t_size)
|
||||
{
|
||||
if (t_size < 2)
|
||||
{ // warning, error?
|
||||
t_size = 2;
|
||||
}
|
||||
}
|
||||
|
||||
eoInserter<EOT>& operator()(const EOT& _eot)
|
||||
{
|
||||
EOT& eo = inverse_deterministic_tournament<EOT>(pop(), t_size);
|
||||
eo = _eot; // overwrite loser of tournament
|
||||
|
||||
eval(eo); // Evaluate after insert
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string className(void) const { return "eoDetTournamentInserter"; }
|
||||
|
||||
private :
|
||||
unsigned t_size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
/* -*- 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 std::string className() const { return "eoDistance"; }
|
||||
|
||||
};
|
||||
|
||||
#endif EOOBJECT_H
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
// eoDup.h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoKill.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EODUP_h
|
||||
#define _EODUP_h
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/// Dup or duplicate: duplicates a gene in a chromosome
|
||||
template <class FitT, class Atomic>
|
||||
class eoDup: public eoMonOp<eoVariableLength<FitT, Atomic> > {
|
||||
public:
|
||||
///
|
||||
eoDup( )
|
||||
: eoMonOp< EOT >( ){};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoDup() {};
|
||||
|
||||
///
|
||||
virtual void operator()(eoVariableLength<FitT, Atomic>& _eo ) const
|
||||
{
|
||||
unsigned pos = rng.random(_eo.size());
|
||||
eoVariableLength<FitT, Atomic>::iterator it = begin();
|
||||
|
||||
while (pos--) {++it;}
|
||||
|
||||
_eo.insert(it, 1, *it);
|
||||
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoDup";};
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,164 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESChrom.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _eoESCHROM_H
|
||||
#define _eoESCHROM_H
|
||||
|
||||
// STL libraries
|
||||
#include <vector> // For std::vector<>
|
||||
#include <stdexcept>
|
||||
#include <strstream>
|
||||
#include <iostream> // for std::ostream
|
||||
|
||||
// EO includes
|
||||
#include <eoVector.h>
|
||||
|
||||
/**@name Chromosomes for evolution strategies
|
||||
Each chromosome in an evolution strategies is composed of a std::vector of floating point
|
||||
values plus a std::vector of sigmas, that are added to them during mutation
|
||||
*/
|
||||
//@{
|
||||
|
||||
/**
|
||||
\class eoESValue
|
||||
|
||||
*/
|
||||
template <class T>
|
||||
struct eoESValue
|
||||
{
|
||||
T value;
|
||||
|
||||
/**
|
||||
The gene has a mutate member because this particular gaussian mutation
|
||||
is paradigmatic for an evolution strategy.
|
||||
*/
|
||||
void mutate(double sigma, T minimum = T(), T maximum = T())
|
||||
{
|
||||
value += rng.normal(0.0, sigma);
|
||||
|
||||
if (minimum > value)
|
||||
value = minimum;
|
||||
if (value > maximum)
|
||||
value = maximum;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
\class eoESGene eoESGene.h es/eoESGene.h
|
||||
Each gene in an Evolution Strategies is composed of a value plus an standard
|
||||
deviation, sigma, used for mutation*/
|
||||
template <class T>
|
||||
struct eoESGene : public eoESValue<T>
|
||||
{
|
||||
double sigma;
|
||||
|
||||
eoESGene( double _val = 0, double _sigma = 1.0 ): eoESValue( _val ), sigma( _sigma ) {};
|
||||
|
||||
/**
|
||||
The gene has a mutate member because the sigma member implies
|
||||
that the updating routine should use a normal distribution
|
||||
*/
|
||||
void mutate(T minimum, T maximum)
|
||||
{
|
||||
mutate(sigma, minimum, maximum);
|
||||
}
|
||||
};
|
||||
|
||||
/// Tricky operator to avoid errors in some VC++ systems, namely VC 5.0 SP3
|
||||
bool operator < ( eoESGene _e1, eoESGene _e2 ) {
|
||||
return _e1.val < _e2.val;
|
||||
}
|
||||
|
||||
/// Tricky operator to avoid errors in some VC++ systems
|
||||
bool operator == ( eoESGene _e1, eoESGene _e2 ) {
|
||||
return ( _e1.val == _e2.val ) && ( _e1.sigma == _e2.sigma ) ;
|
||||
}
|
||||
|
||||
///
|
||||
std::ostream & operator << ( std::ostream& _s, const eoESGene& _e ) {
|
||||
_s << _e.val << ", " << _e.sigma << " | ";
|
||||
return _s;
|
||||
}
|
||||
|
||||
/// Dummy >>
|
||||
std::istream & operator >> ( std::istream& _s, const eoESGene& _e ) {
|
||||
_s >> _e.val;
|
||||
_s >> _e.sigma;
|
||||
return _s;
|
||||
}
|
||||
|
||||
|
||||
/** Basic chromosome for evolution strategies (ES), as defined by Rechenberg and
|
||||
Schwefel. Each chromosomes is composed of "genes"; each one of then is an eoESGene
|
||||
@see eoESGene
|
||||
*/
|
||||
template <typename fitT = float >
|
||||
class eoESChrom: public eoVector<eoESGene, fitT> {
|
||||
public:
|
||||
/// Basic ctor
|
||||
eoESChrom( ):eoVector<eoESGene, fitT>() {};
|
||||
|
||||
/** Ctor using a couple of random number generators
|
||||
@param _size Lineal length of the object
|
||||
@param _rnd a random number generator, which returns a random value each time it´s called.
|
||||
@param _rndS another one, for the sigma
|
||||
*/
|
||||
eoESChrom( unsigned _size, eoRnd<double>& _rnd, eoRnd<double>& _rndS )
|
||||
: eoVector<eoESGene, fitT>( _size ){
|
||||
for ( iterator i = begin(); i != end(); i ++ ) {
|
||||
i->val = _rnd();
|
||||
i->sigma = _rndS();
|
||||
}
|
||||
};
|
||||
|
||||
/// Copy ctor
|
||||
eoESChrom( const eoESChrom& _eoes): eoVector<eoESGene, fitT>( _eoes ) {};
|
||||
|
||||
/// Assignment operator
|
||||
const eoESChrom& operator =( const eoESChrom & _eoes ) {
|
||||
if ( this != &_eoes ){
|
||||
eoVector<eoESGene, fitT>::operator=( _eoes );
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
///
|
||||
~eoESChrom() {};
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoESChrom";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//@}
|
||||
#endif
|
||||
|
||||
|
|
@ -1,164 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESChrom.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _eoESCHROM_H
|
||||
#define _eoESCHROM_H
|
||||
|
||||
// STL libraries
|
||||
#include <vector> // For std::vector<>
|
||||
#include <stdexcept>
|
||||
#include <strstream>
|
||||
#include <iostream> // for std::ostream
|
||||
|
||||
// EO includes
|
||||
#include <eoVector.h>
|
||||
|
||||
/**@name Chromosomes for evolution strategies
|
||||
Each chromosome in an evolution strategies is composed of a std::vector of floating point
|
||||
values plus a std::vector of sigmas, that are added to them during mutation
|
||||
*/
|
||||
//@{
|
||||
|
||||
/**
|
||||
\class eoESValue
|
||||
|
||||
*/
|
||||
template <class T>
|
||||
struct eoESValue
|
||||
{
|
||||
T value;
|
||||
|
||||
/**
|
||||
The gene has a mutate member because this particular gaussian mutation
|
||||
is paradigmatic for an evolution strategy.
|
||||
*/
|
||||
void mutate(double sigma, T minimum, T maximum)
|
||||
{
|
||||
value += rng.normal(0.0, sigma);
|
||||
|
||||
if (minimum > value)
|
||||
value = minimum;
|
||||
if (value > maximum)
|
||||
value = maximum;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
\class eoESGene eoESGene.h es/eoESGene.h
|
||||
Each gene in an Evolution Strategies is composed of a value plus an standard
|
||||
deviation, sigma, used for mutation*/
|
||||
template <class T>
|
||||
struct eoESGene : public eoESValue<T>
|
||||
{
|
||||
double sigma;
|
||||
|
||||
eoESGene( double _val = 0, double _sigma = 1.0 ): eoESValue( _val ), sigma( _sigma ) {};
|
||||
|
||||
/**
|
||||
The gene has a mutate member because the sigma member implies
|
||||
that the updating routine should use a normal distribution
|
||||
*/
|
||||
void mutate(T minimum, T maximum)
|
||||
{
|
||||
mutate(sigma, minimum, maximum);
|
||||
}
|
||||
};
|
||||
|
||||
/// Tricky operator to avoid errors in some VC++ systems, namely VC 5.0 SP3
|
||||
bool operator < ( eoESGene _e1, eoESGene _e2 ) {
|
||||
return _e1.val < _e2.val;
|
||||
}
|
||||
|
||||
/// Tricky operator to avoid errors in some VC++ systems
|
||||
bool operator == ( eoESGene _e1, eoESGene _e2 ) {
|
||||
return ( _e1.val == _e2.val ) && ( _e1.sigma == _e2.sigma ) ;
|
||||
}
|
||||
|
||||
///
|
||||
std::ostream & operator << ( std::ostream& _s, const eoESGene& _e ) {
|
||||
_s << _e.val << ", " << _e.sigma << " | ";
|
||||
return _s;
|
||||
}
|
||||
|
||||
/// Dummy >>
|
||||
std::istream & operator >> ( std::istream& _s, const eoESGene& _e ) {
|
||||
_s >> _e.val;
|
||||
_s >> _e.sigma;
|
||||
return _s;
|
||||
}
|
||||
|
||||
|
||||
/** Basic chromosome for evolution strategies (ES), as defined by Rechenberg and
|
||||
Schwefel. Each chromosomes is composed of "genes"; each one of then is an eoESGene
|
||||
@see eoESGene
|
||||
*/
|
||||
template <typename fitT = float >
|
||||
class eoESChrom: public eoVector<eoESGene, fitT> {
|
||||
public:
|
||||
/// Basic ctor
|
||||
eoESChrom( ):eoVector<eoESGene, fitT>() {};
|
||||
|
||||
/** Ctor using a couple of random number generators
|
||||
@param _size Lineal length of the object
|
||||
@param _rnd a random number generator, which returns a random value each time it´s called.
|
||||
@param _rndS another one, for the sigma
|
||||
*/
|
||||
eoESChrom( unsigned _size, eoRnd<double>& _rnd, eoRnd<double>& _rndS )
|
||||
: eoVector<eoESGene, fitT>( _size ){
|
||||
for ( iterator i = begin(); i != end(); i ++ ) {
|
||||
i->val = _rnd();
|
||||
i->sigma = _rndS();
|
||||
}
|
||||
};
|
||||
|
||||
/// Copy ctor
|
||||
eoESChrom( const eoESChrom& _eoes): eoVector<eoESGene, fitT>( _eoes ) {};
|
||||
|
||||
/// Assignment operator
|
||||
const eoESChrom& operator =( const eoESChrom & _eoes ) {
|
||||
if ( this != &_eoes ){
|
||||
eoVector<eoESGene, fitT>::operator=( _eoes );
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
///
|
||||
~eoESChrom() {};
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoESChrom";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//@}
|
||||
#endif
|
||||
|
||||
|
|
@ -1,267 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESInd.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOESFULLCHROM_H
|
||||
#define _EOESFULLCHROM_H
|
||||
|
||||
// STL libraries
|
||||
#include <vector> // For std::vector<>
|
||||
#include <stdexcept>
|
||||
#include <strstream>
|
||||
#include <iostream> // for std::ostream
|
||||
|
||||
// EO includes
|
||||
#include <eoVector.h>
|
||||
#include <utils/eoRNG.h>
|
||||
/**@name Chromosomes for evolution strategies
|
||||
Each chromosome in an evolution strategies is composed of a std::vector of floating point
|
||||
values plus a std::vector of sigmas, that are added to them during mutation and a std::vector of correlations
|
||||
*/
|
||||
//@{
|
||||
|
||||
|
||||
/**@name individuals for evolution strategies -MS- 22/10/99
|
||||
Each individual in an evolution strategy is composed of
|
||||
a std::vector of floating point values
|
||||
a std::vector of std deviations
|
||||
a std::vector of rotation angles (for correlated mutations)
|
||||
|
||||
These individuals CANNOT BE IMPLEMENTED as std::vectors of anything
|
||||
at least in the case of correlated mutations
|
||||
*/
|
||||
//@{
|
||||
|
||||
template <typename fitT = float >
|
||||
class eoESFullChrom : public eoVector<double, fitT> {
|
||||
public:
|
||||
/// constructor
|
||||
eoESFullChrom( unsigned _num_genes = 1,
|
||||
unsigned _num_sigma = 1, unsigned _num_correl = 0,
|
||||
bool _verbose = false,
|
||||
double _ObjMin = 0, double _ObjMax = 1,
|
||||
double _StdDevInit = 0.3 ):
|
||||
eoVector<double, fitT>(_num_genes),
|
||||
// ObjVar( _num_genes ), now an eoVector<double>
|
||||
StdDev( _num_sigma ),
|
||||
CorCff( _num_correl ),
|
||||
verbose( _verbose ),
|
||||
ObjMin( _ObjMin ),
|
||||
ObjMax(_ObjMax ),
|
||||
StdDevInit( _StdDevInit )
|
||||
{ // check consistency
|
||||
}
|
||||
|
||||
|
||||
/* And now the useful constructor: from a parser (should be in the
|
||||
factory, if such a thing exists one day for eoESFullChrom
|
||||
*/
|
||||
eoESFullChrom(eoParser & parser) : StdDev(0), CorCff(0) {
|
||||
parser.AddTitle("Description of ES individuals");
|
||||
int num_genes, num_sigma;
|
||||
bool correlated_mutations;
|
||||
try {
|
||||
num_genes = parser.getInt("-Io", "--NbObjVar", "2",
|
||||
"Number of Object Variables" );
|
||||
num_sigma = parser.getInt("-Is", "--NbSigma", "1",
|
||||
"Number of Standard Deviations" );
|
||||
correlated_mutations = parser.getBool("-Ic", "--Correlated",
|
||||
"Correlated mutation?" );
|
||||
ObjMin = parser.getFloat("-Im", "--min", "0",
|
||||
"Minimum value for object variables" );
|
||||
ObjMax = parser.getFloat("-IM", "--max", "1",
|
||||
"Maximum value for object variables" );
|
||||
StdDevInit = parser.getFloat("-II", "--SigmaInit", "0.3",
|
||||
"Initial value for std. dev. (scaled by range)" );
|
||||
verbose = parser.getBool("-Iv", "--verbose",
|
||||
"Verbose std::listing of ES individuals (mutation parameters");
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// consistency tests
|
||||
if (! num_sigma) { // no std dev??? EXCEPTION
|
||||
throw invalid_argument( "No standard deviation: choose another representation please" );
|
||||
}
|
||||
if (num_sigma > num_genes) {
|
||||
std::cout << "WARNING, Number of Standard Deviations > Number of Object Variables\nAdjusted!\n";
|
||||
num_sigma = num_genes;
|
||||
// modify the Param value - so .status is OK
|
||||
std::ostrstream sloc;
|
||||
sloc << num_genes;
|
||||
parser.setParamValue("--NbSigma", sloc.str());
|
||||
}
|
||||
// adjust the sizes!!!
|
||||
resize(num_genes);
|
||||
if (num_sigma)
|
||||
StdDev.resize(num_sigma);
|
||||
if (correlated_mutations) {
|
||||
if (num_sigma < num_genes) {
|
||||
std::cout << "WARNING less Std Dev. than number of variables + Correlated mutations\n";
|
||||
std::cout << "Though possible, this is a strange setting" << std::endl;
|
||||
}
|
||||
// nb of rotation angles: N*(N-1)/2 (in general!)
|
||||
CorCff.resize ( (2*num_genes - num_sigma)*(num_sigma - 1) / 2 );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// Operator =
|
||||
const eoESFullChrom& operator = ( const eoESFullChrom& _eo ) {
|
||||
if ( this != &_eo ) {
|
||||
// Change EO part
|
||||
eoVector<double, fitT>::operator = (_eo);
|
||||
|
||||
// Change this part
|
||||
// ObjVar = _eo.ObjVar;
|
||||
StdDev = _eo.StdDev;
|
||||
CorCff = _eo.CorCff;
|
||||
verbose = _eo.verbose;
|
||||
ObjMin = _eo.ObjMin;
|
||||
ObjMax = _eo.ObjMax;
|
||||
StdDevInit = _eo.StdDevInit;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// destructor
|
||||
virtual ~eoESFullChrom() {}
|
||||
|
||||
///
|
||||
double getStdDev( unsigned _i ) const {
|
||||
if ( _i >= length() )
|
||||
throw out_of_range( "out_of_range when reading StdDev");
|
||||
return StdDev[ _i ];
|
||||
}
|
||||
|
||||
///
|
||||
void setStdDev( unsigned _i, double _val ) {
|
||||
if ( _i < length() ) {
|
||||
StdDev[_i] = _val;
|
||||
} else
|
||||
throw out_of_range( "out_of_range when writing StdDev");
|
||||
}
|
||||
|
||||
///
|
||||
double getCorCff( unsigned _i ) const {
|
||||
if ( _i >= length() )
|
||||
throw out_of_range( "out_of_range when reading CorCff");
|
||||
return CorCff[ _i ];
|
||||
}
|
||||
|
||||
///
|
||||
void setCorCff( unsigned _i, double _val ) {
|
||||
if ( _i < length() ) {
|
||||
CorCff[_i] = _val;
|
||||
} else
|
||||
throw out_of_range( "out_of_range when writing CorCff");
|
||||
}
|
||||
|
||||
///
|
||||
void insertGene( unsigned _i, double _val ) {
|
||||
throw FixedLengthChromosome();
|
||||
};
|
||||
|
||||
///
|
||||
void deleteGene( unsigned _i ) {
|
||||
throw FixedLengthChromosome();
|
||||
};
|
||||
|
||||
///
|
||||
unsigned length() const { return size();}/* formerly ObjVar.size() */
|
||||
unsigned StdDevLength() const { return StdDev.size();}
|
||||
unsigned CorCffLength() const { return CorCff.size();}
|
||||
|
||||
|
||||
/** Print itself: inherited from eoObject implementation.
|
||||
Instance from base classes are processed in
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
@param _s the std::ostream in which things are written*/
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
copy( begin(), end(), std::ostream_iterator<double>( _s, " ") );
|
||||
// The formatting instructinos shoudl be left to the caller
|
||||
// _s << "\n";
|
||||
if (verbose) {
|
||||
_s << "\n\tStd Dev. " ;
|
||||
copy( StdDev.begin(), StdDev.end(), std::ostream_iterator<double>( _s, " ") );
|
||||
if (CorCff.size()) {
|
||||
_s << "\n\t";
|
||||
copy( CorCff.begin(), CorCff.end(), std::ostream_iterator<double>( _s, " ") );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** This std::exception should be thrown when trying to insert or delete a gene
|
||||
in a fixed length chromosome
|
||||
*/
|
||||
class FixedLengthChromosome : public std::exception {
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
FixedLengthChromosome()
|
||||
: std::exception() { };
|
||||
|
||||
~FixedLengthChromosome() {};
|
||||
};
|
||||
|
||||
// accessors
|
||||
double getObjMin() const {return ObjMin;}
|
||||
double getObjMax() const {return ObjMax;}
|
||||
double getStdDevInit () const {return StdDevInit;}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoESFullChrom";};
|
||||
|
||||
private:
|
||||
// std::vector<double> ObjVar; /* object variable std::vector */
|
||||
// or shoudl the class be subclass of EOVector<double> ???
|
||||
|
||||
std::vector<double> StdDev; /* standard deviation std::vector */
|
||||
std::vector<double> CorCff; /* correlation coefficient std::vector */
|
||||
|
||||
bool verbose; /* Print std deviations or not */
|
||||
|
||||
/** the range is used for mutation AND random initialization,
|
||||
* while the StdDevInit is used only for random initialization
|
||||
* this in a little inconsistent!
|
||||
*/
|
||||
double ObjMin, ObjMax; /* Range for Object variables */
|
||||
double StdDevInit; /* Initial value of Standard Deviations */
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
@ -1,261 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoESMute.h : ES mutation
|
||||
// (c) Maarten Keijzer 2000 & GeNeura Team, 1998 for the EO part
|
||||
// Th. Baeck 1994 and EEAAX 1999 for the ES part
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
marc.schoenauer@polytechnique.fr
|
||||
http://eeaax.cmap.polytchnique.fr/
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef _EOESMUT_H
|
||||
#define _EOESMUT_H
|
||||
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoRNG.h>
|
||||
#include <cmath> // for exp
|
||||
|
||||
#include <es/eoESFullChrom.h>
|
||||
#include <es/eoESInit.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.1415926535897932384626433832795
|
||||
#endif
|
||||
|
||||
/** ES-style mutation in the large: Obviously, valid only for eoES*
|
||||
|
||||
It is currently valid for three types of ES chromosomes:
|
||||
|
||||
eoEsSimple: only 1 std deviation
|
||||
eoEsStdev: as many standard deviations as object variables
|
||||
eoEsFull: The whole guacemole: correlations, stdevs and object variables
|
||||
|
||||
Each of these three variant has it's own operator() in eoEsMutate
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoESMutate: public eoMonOp< EOT > {
|
||||
public:
|
||||
|
||||
typedef EOT::Fitness FitT;
|
||||
|
||||
/** Initialization
|
||||
parameters:
|
||||
|
||||
@param _init proxy class for initializating the three parameters eoEsMutate needs
|
||||
@param _bounds the bounds for the objective variables
|
||||
*/
|
||||
eoESMutate(eoESMutationInit& _init, eoESObjectiveBounds& _bounds) : bounds(_bounds)
|
||||
{
|
||||
unsigned size = bounds.chromSize();
|
||||
|
||||
if (eoEsInfo<EOT>::single_stdev)
|
||||
{
|
||||
TauLcl = _init.TauLcl();
|
||||
TauLcl /= sqrt((double) size);
|
||||
}
|
||||
else
|
||||
{
|
||||
TauLcl = _init.TauLcl();
|
||||
TauGlb = _init.TauGlb();
|
||||
|
||||
// renormalization
|
||||
TauLcl /= sqrt( 2.0 * sqrt( (double)size ) );
|
||||
TauGlb /= sqrt( 2.0 * ( (double) size ) );
|
||||
|
||||
if (eoEsInfo<EOT>::has_correlation)
|
||||
{ // Correlated Mutations
|
||||
TauBeta = _init.TauBeta();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoESMutate() {};
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoESMutate";};
|
||||
|
||||
/**
|
||||
Mutate eoEsSimple
|
||||
*/
|
||||
virtual void operator()( eoEsSimple<FitT>& _eo) const
|
||||
{
|
||||
_eo.stdev *= exp(TauLcl * rng.normal());
|
||||
|
||||
if (_eo.stdev < eoEsInfo<EOT>::stdev_eps)
|
||||
_eo.stdev = eoEsInfo<EOT>::stdev_eps;
|
||||
|
||||
// now apply to all
|
||||
|
||||
for (unsigned i = 0; i < _eo.size(); ++i)
|
||||
{
|
||||
_eo[i] += _eo.stdev * rng.normal();
|
||||
}
|
||||
|
||||
keepInBounds(_eo);
|
||||
}
|
||||
|
||||
/// mutations - standard and correlated
|
||||
// =========
|
||||
/*
|
||||
* Standard mutation of object variables and standard
|
||||
* deviations in ESs.
|
||||
* If there are fewer different standard deviations available
|
||||
* than the dimension of the objective function requires, the
|
||||
* last standard deviation is responsible for ALL remaining
|
||||
* object variables.
|
||||
* Schwefel 1977: Numerische Optimierung von Computer-Modellen
|
||||
* mittels der Evolutionsstrategie, pp. 165 ff.
|
||||
*/
|
||||
|
||||
virtual void operator()( eoESStdev<FitT>& _eo ) const
|
||||
{
|
||||
double global = exp(TauGlb * rng.normal());
|
||||
for (unsigned i = 0; i < _eo.size(); i++)
|
||||
{
|
||||
double stdev = _eo.stdevs[i];
|
||||
stdev *= global * exp(TauLcl * rng.normal());
|
||||
|
||||
if (stdev < eoEsInfo<EOT>::stdev_eps)
|
||||
stdev = eoEsInfo<EOT>::stdev_eps;
|
||||
|
||||
_eo.stdevs[i] = stdev;
|
||||
_eo[i] += stdev * rng.normal();
|
||||
}
|
||||
|
||||
keepInBounds(_eo);
|
||||
}
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
|
||||
// Code from Thomas Baeck
|
||||
|
||||
virtual void operator()( eoEsFull<fitT> & _eo ) const
|
||||
{
|
||||
|
||||
/*
|
||||
* First: mutate standard deviations (as above).
|
||||
*/
|
||||
|
||||
double global = exp(TauGlb * rng.normal());
|
||||
for (unsigned i = 0; i < _eo.size(); i++)
|
||||
{
|
||||
double stdev = _eo.stdevs[i];
|
||||
stdev *= global * exp(TauLcl * rng.normal());
|
||||
|
||||
if (stdev < eoEsInfo<EOT>::stdev_eps)
|
||||
stdev = eoEsInfo<EOT>::stdev_eps;
|
||||
|
||||
_eo.stdevs[i] = stdev;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Mutate rotation angles.
|
||||
*/
|
||||
|
||||
for (i = 0; i < _eo.correlations.size(); i++)
|
||||
{
|
||||
_eo.correlations[i] += TauBeta * rng.normal();
|
||||
if ( fabs(_eo.correlations[i]) > M_PI )
|
||||
{
|
||||
_eo.correlations[i] -= M_PI * (int) (_eo.correlations[i]/M_PI) ;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform correlated mutations.
|
||||
*/
|
||||
unsigned i,k;
|
||||
|
||||
std::vector<double> VarStp(_eo.size());
|
||||
for (i = 0; i < _eo.size(); i++)
|
||||
VarStp[i] = _eo.stdevs[i] * rng.normal();
|
||||
|
||||
unsigned nq = _eo.correlations.size() - 1;
|
||||
|
||||
for (k = 0; k < _eo.size()-1; k++)
|
||||
{
|
||||
n1 = _eo.size() - k - 1;
|
||||
n2 = _eo.size() - 1;
|
||||
|
||||
for (i = 0; i < k; i++)
|
||||
{
|
||||
d1 = VarStp[n1];
|
||||
d2 = VarStp[n2];
|
||||
S = sin( _eo.correlations[nq] );
|
||||
C = cos( _eo.correlations[nq] );
|
||||
VarStp[n2] = d1 * S + d2 * C;
|
||||
VarStp[n1] = d1 * C - d2 * S;
|
||||
n2--;
|
||||
nq--;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < _eo.size(); i++)
|
||||
_eo[i] += VarStp[i];
|
||||
|
||||
keepInBounds(_eo);
|
||||
}
|
||||
|
||||
void keepInBounds(EOT& _eo) const
|
||||
{
|
||||
for (unsigned i = 0; i < _eo.size(); ++i)
|
||||
{
|
||||
if (_eo[i] < bounds.minimum(i))
|
||||
_eo[i] = bounds.minimum(i);
|
||||
else if (_eo[i] > bounds.maximum(i))
|
||||
_eo[i] = bounds.maximum(i);
|
||||
}
|
||||
}
|
||||
|
||||
private :
|
||||
// the data
|
||||
//=========
|
||||
double TauLcl; /* Local factor for mutation of std deviations */
|
||||
double TauGlb; /* Global factor for mutation of std deviations */
|
||||
double TauBeta; /* Factor for mutation of correlation parameters */
|
||||
|
||||
eoESObjectiveBounds& bounds;
|
||||
};
|
||||
|
||||
/*
|
||||
* Correlated mutations in ESs, according to the following
|
||||
* sources:
|
||||
* H.-P. Schwefel: Internal Report of KFA Juelich, KFA-STE-IB-3/80
|
||||
* p. 43, 1980
|
||||
* G. Rudolph: Globale Optimierung mit parallelen Evolutions-
|
||||
* strategien, Diploma Thesis, University of Dortmund, 1990
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEsObjectiveBounds.h
|
||||
// (c) Maarten Keijzer 2000, GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoEsObjectiveBounds_h
|
||||
#define _eoEsObjectiveBounds_h
|
||||
|
||||
/**
|
||||
\defgroup EvolutionStrategies
|
||||
|
||||
Various classes for the initialization and mutation of real valued std::vectors.
|
||||
|
||||
Supports simple mutations and various more adaptable mutations, including
|
||||
correlated mutations.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
\class eoEsObjectiveBounds eoEsObjectiveBounds.h es/eoEsObjectiveBounds.h
|
||||
\ingroup EvolutionStrategies
|
||||
|
||||
Defines the minima and maxima of the object variables. Needed by eoEsChromInit
|
||||
and eoEsMutate
|
||||
|
||||
@see eoEsChromInit eoEsMutate
|
||||
*/
|
||||
class eoEsObjectiveBounds
|
||||
{
|
||||
public :
|
||||
|
||||
/**
|
||||
Objective bounds for a global minimum and maximum
|
||||
*/
|
||||
eoEsObjectiveBounds(int _nGenes, double _min, double _max) : repMinimum(_nGenes), repMaximum(_nGenes)
|
||||
{
|
||||
std::fill(repMinimum.begin(), repMinimum.end(), _min);
|
||||
std::fill(repMaximum.begin(), repMaximum.end(), _max);
|
||||
}
|
||||
|
||||
/**
|
||||
Objective bounds for a per gene minimum and maximum
|
||||
*/
|
||||
eoEsObjectiveBounds(const std::vector<double>& _min, const std::vector<double>& _max)
|
||||
: repMinimum(_min), repMaximum(_max) {}
|
||||
|
||||
typedef double doubleype;
|
||||
|
||||
double minimum(size_t i) { return repMinimum[i]; }
|
||||
double maximum(size_t i) { return repMaximum[i]; }
|
||||
|
||||
unsigned chromSize(void) const { return repMinimum.size(); }
|
||||
|
||||
private :
|
||||
std::vector<double> repMinimum;
|
||||
std::vector<double> repMaximum;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
/** -*- 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
|
||||
|
|
@ -1,101 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvolutionStrategy.h
|
||||
// (c) Maarten Keijzer 2000, 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 _eoEvolutionStrategy_h
|
||||
#define _eoEvolutionStrategy_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoEasyEA.h>
|
||||
#include <eoInplaceTransform.h>
|
||||
/** eoEvolutionStrategy:
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
class eoEvolutionStrategy: public eoAlgo<EOT>
|
||||
{
|
||||
public:
|
||||
struct plus_strategy{};
|
||||
struct comma_strategy{};
|
||||
|
||||
eoEvolutionStrategy(
|
||||
eoContinue<EOT>& _continuator,
|
||||
eoEvalFunc<EOT>& _eval,
|
||||
eoGOpSelector<EOT>& _opSel,
|
||||
float _lambdaRate,
|
||||
comma_strategy)
|
||||
: selectPerc(randomSelect, _lambdaRate),
|
||||
transform(_opSel),
|
||||
easyEA(_continuator, _eval, selectPerc, transform, noElitism, truncate)
|
||||
{}
|
||||
|
||||
eoEvolutionStrategy(
|
||||
eoContinue<EOT>& _continuator,
|
||||
eoEvalFunc<EOT>& _eval,
|
||||
eoGOpSelector<EOT>& _opSel,
|
||||
float _lambdaRate,
|
||||
plus_strategy)
|
||||
: selectPerc(randomSelect, _lambdaRate),
|
||||
transform(_opSel),
|
||||
easyEA(_continuator, _eval, selectPerc, transform, plus, truncate)
|
||||
{}
|
||||
|
||||
|
||||
/// Apply a few generation of evolution to the population.
|
||||
virtual void operator()(eoPop<EOT>& _pop)
|
||||
{
|
||||
easyEA(_pop);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
eoPlus<EOT> plus;
|
||||
eoNoElitism<EOT> noElitism;
|
||||
eoTruncate<EOT> truncate;
|
||||
eoRandomSelect<EOT> randomSelect;
|
||||
eoSelectPerc<EOT> selectPerc;
|
||||
eoInplaceTransform2<EOT> transform;
|
||||
|
||||
/// easyEA is contained rather than a base because of member initialization order!
|
||||
eoEasyEA<EOT> easyEA;
|
||||
};
|
||||
|
||||
template <class EOT>
|
||||
eoEvolutionStrategy<EOT> make_es(eoContinue<EOT>& _continuator,
|
||||
eoEvalFunc<EOT>& _eval,
|
||||
eoGOpSelector<EOT>& _opSel,
|
||||
float _lambdaRate,
|
||||
bool _comma)
|
||||
|
||||
{
|
||||
if (_comma)
|
||||
return eoEvolutionStrategy<EOT>(_continuator, _eval, _opSel, _lambdaRate, eoEvolutionStrategy<EOT>::comma_strategy());
|
||||
//else
|
||||
return eoEvolutionStrategy<EOT>(_continuator, _eval, _opSel, _lambdaRate, eoEvolutionStrategy<EOT>::plus_strategy());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoSelectTransformReduce_h
|
||||
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGenTerm.h
|
||||
// (c) GeNeura Team, 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOFITTERM_H
|
||||
#define _EOFITTERM_H
|
||||
|
||||
#include <eoContinue.h>
|
||||
|
||||
|
||||
/**
|
||||
Fitness continuation:
|
||||
|
||||
Continues until the maximum fitness level is reached.
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoFitContinue: public eoContinue<EOT> {
|
||||
public:
|
||||
|
||||
/// Define Fitness
|
||||
typedef typename EOT::Fitness FitnessType;
|
||||
|
||||
/// Ctor
|
||||
eoFitContinue( const FitnessType _maximum)
|
||||
: eoContinuator<EOT> (), maximum( _maximum ) {};
|
||||
|
||||
/** Returns false when a fitness criterium is
|
||||
* reached, assumes sorted population */
|
||||
virtual bool operator() ( const eoPop<EOT>& _vEO )
|
||||
{
|
||||
return (bestFitness < maximum);
|
||||
}
|
||||
|
||||
private:
|
||||
FitnessType maximum;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
// eoFitness.h
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoFitness.cpp
|
||||
// (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 EOFITNESS_H
|
||||
#define EOFITNESS_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/**
|
||||
\deprecated This class will dissapear in time, use eoScalarFitness instead
|
||||
*/
|
||||
class eoFitness: public eoPersistent
|
||||
{
|
||||
public:
|
||||
virtual operator float() const = 0;
|
||||
|
||||
virtual bool operator<(const eoFitness& other) const = 0;
|
||||
|
||||
virtual bool operator>(const eoFitness& other) const
|
||||
{
|
||||
return !(*this < other || *this == other);
|
||||
}
|
||||
|
||||
virtual bool operator==(const eoFitness& other) const
|
||||
{
|
||||
return !(other < *this || *this < other);
|
||||
}
|
||||
|
||||
virtual bool operator!=(const eoFitness& other) const
|
||||
{
|
||||
return other < *this || *this < other;
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EOFITNESS_H
|
||||
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoBreeder.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoGopBreeder_h
|
||||
#define eoGopBreeder_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/*****************************************************************************
|
||||
* eoBreeder: transforms a population using genetic operators. *
|
||||
* For every operator there is a rated to be applyed. *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <eoFunctor.h>
|
||||
#include <eoPop.h>
|
||||
#include <eoGOpSelector.h>
|
||||
#include <eoIndiSelector.h>
|
||||
#include <eoBackInserter.h>
|
||||
|
||||
/**
|
||||
Base class for breeders using generalized operators, I'm not sure if we
|
||||
will maintain the generalized operators in their current form, so
|
||||
it might change.
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoGOpBreeder: public eoUF<eoPop<EOT>&, void>
|
||||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
eoGOpBreeder( eoGOpSelector<EOT>& _opSel,
|
||||
eoSelectOneIndiSelector<EOT>& _selector)
|
||||
: opSel( _opSel ), selector(_selector)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Enlarges the population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<EOT>& _pop)
|
||||
{
|
||||
unsigned size = _pop.size();
|
||||
|
||||
for (unsigned i = 0; i < size; i++)
|
||||
{ // and the one liner
|
||||
opSel.selectOp()(selector.bind(_pop,size).bias(i), inserter.bind(_pop));
|
||||
}
|
||||
}
|
||||
|
||||
/// The class name.
|
||||
std::string className() const { return "eoGOpBreeder"; }
|
||||
|
||||
private:
|
||||
eoGOpSelector<EOT>& opSel;
|
||||
eoSelectOneIndiSelector<EOT>& selector;
|
||||
|
||||
// the inserter can be local as there's no point in changing it from the outside
|
||||
eoBackInserter<EOT> inserter;
|
||||
};
|
||||
|
||||
#endif eoBreeder_h
|
||||
|
||||
|
|
@ -1,181 +0,0 @@
|
|||
/** -*- 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.
|
||||
Also included eoProportionalGOpSelector and eoSequentialGOpSelector, that offer
|
||||
a few concrete implementations.
|
||||
|
||||
(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 <list>
|
||||
#include "eoOpSelector.h"
|
||||
#include "eoWrappedOps.h" // for eoCombinedOp
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** Base class for alternative selectors, which use the generalized operator
|
||||
interface. eoGOpBreeders expects this class */
|
||||
template<class EOT>
|
||||
class eoGOpSelector: public eoOpSelector<EOT>, public std::vector<eoGeneralOp<EOT>*>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef eoOpSelector<EOT>::ID ID;
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoGOpSelector() {
|
||||
for ( std::list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
|
||||
i != ownOpList.end(); i++ ) {
|
||||
delete *i;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Add any kind of operator to the operator mix,
|
||||
@param _op operator, one of eoMonOp, eoBinOp, eoQuadraticOp or eoGeneralOp
|
||||
@param _rate the rate at which it should be applied, it should be a probability
|
||||
|
||||
*/
|
||||
virtual ID addOp( eoOp<EOT>& _op, float _rate );
|
||||
// implementation can be found below
|
||||
|
||||
/** Retrieve the operator using its integer handle
|
||||
@param _id The id number. Should be a valid id, or an std::exception
|
||||
will be thrown
|
||||
@return a reference of the operator corresponding to that id.
|
||||
*/
|
||||
virtual eoOp<EOT>& getOp( ID _id )
|
||||
{
|
||||
return *operator[](_id);
|
||||
}
|
||||
|
||||
///
|
||||
virtual void deleteOp( ID _id );
|
||||
// implemented below
|
||||
|
||||
///
|
||||
virtual eoOp<EOT>* Op()
|
||||
{
|
||||
return &selectOp();
|
||||
}
|
||||
|
||||
/// Select an operator from the operators present here
|
||||
virtual eoGeneralOp<EOT>& selectOp() = 0;
|
||||
|
||||
///
|
||||
virtual std::string className() const { return "eoGOpSelector"; };
|
||||
|
||||
///
|
||||
void printOn(std::ostream& _os) const {}
|
||||
// _os << className().c_str() << std::endl;
|
||||
// for ( unsigned i=0; i!= rates.size(); i++ ) {
|
||||
// _os << *(operator[](i)) << "\t" << rates[i] << std::endl;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
const std::vector<float>& getRates(void) const { return rates; }
|
||||
|
||||
private :
|
||||
std::vector<float> rates;
|
||||
std::list< eoGeneralOp<EOT>* > ownOpList;
|
||||
};
|
||||
|
||||
/* Implementation of longish functions defined above */
|
||||
|
||||
template <class EOT>
|
||||
inline eoOpSelector<EOT>::ID eoGOpSelector<EOT>::addOp( eoOp<EOT>& _op,
|
||||
float _arg )
|
||||
{
|
||||
eoGeneralOp<EOT>* op;
|
||||
|
||||
if (_op.getType() == eoOp<EOT>::general)
|
||||
{
|
||||
op = static_cast<eoGeneralOp<EOT>*>(&_op);
|
||||
}
|
||||
else
|
||||
{
|
||||
// if it's not a general op, it's a "old" op; create a wrapped op from it
|
||||
// and keep it on a std::list to delete them afterwards
|
||||
// will use auto_ptr when they're readily available
|
||||
|
||||
switch(_op.getType())
|
||||
{
|
||||
case eoOp<EOT>::unary :
|
||||
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
|
||||
break;
|
||||
case eoOp<EOT>::binary :
|
||||
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
|
||||
break;
|
||||
case eoOp<EOT>::quadratic :
|
||||
op = new eoWrappedQuadraticOp<EOT>(static_cast<eoQuadraticOp<EOT>&>(_op));
|
||||
break;
|
||||
case eoOp<EOT>::general : break; // cannot happen, but gcc issued a warning
|
||||
}
|
||||
ownOpList.push_back( op );
|
||||
}
|
||||
|
||||
// Now 'op' is a general operator, either because '_op' was one or
|
||||
// because we wrapped it in an appropriate wrapper in the code above.
|
||||
|
||||
typename eoGOpSelector<EOT>::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;
|
||||
}
|
||||
|
||||
template <class EOT>
|
||||
inline void eoGOpSelector<EOT>::deleteOp( ID _id )
|
||||
{
|
||||
eoGeneralOp<EOT>* op = operator[](_id);
|
||||
|
||||
operator[](_id) = 0;
|
||||
rates[_id] = 0.0;
|
||||
|
||||
// check opstd::list and clear it there too.
|
||||
|
||||
std::list< eoGeneralOp<EOT>* >::iterator it = find(ownOpList.begin(), ownOpList.end(), op);
|
||||
|
||||
if(it != ownOpList.end())
|
||||
{
|
||||
ownOpList.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
#endif eoGOpSelector_h
|
||||
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGenTerm.h
|
||||
// (c) GeNeura Team, 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOGENTERM_H
|
||||
#define _EOGENTERM_H
|
||||
|
||||
#include <eoContinue.h>
|
||||
|
||||
/** Generational continuator: continues until a number of generations is reached
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoGenContinue: public eoContinue<EOT> {
|
||||
public:
|
||||
|
||||
/// Ctors/dtors
|
||||
eoGenContinue( unsigned _totalGens)
|
||||
: repTotalGenerations( _totalGens ),
|
||||
thisGeneration(0){};
|
||||
|
||||
/** Returns false when a certain number of generations is
|
||||
* reached */
|
||||
virtual bool operator() ( const eoPop<EOT>& _vEO ) {
|
||||
thisGeneration++;
|
||||
// std::cout << " [" << thisGeneration << "] ";
|
||||
return (thisGeneration < repTotalGenerations) ; // for the postincrement
|
||||
}
|
||||
|
||||
/** Sets the number of generations to reach
|
||||
and sets the current generation to 0 (the begin)*/
|
||||
virtual void totalGenerations( unsigned _tg ) {
|
||||
repTotalGenerations = _tg;
|
||||
// thisGeneration = 0;
|
||||
};
|
||||
|
||||
/** Returns the number of generations to reach*/
|
||||
virtual unsigned totalGenerations( ) {
|
||||
return repTotalGenerations;
|
||||
};
|
||||
|
||||
std::string className(void) const { return "eoGenTerm"; }
|
||||
|
||||
private:
|
||||
unsigned repTotalGenerations, thisGeneration;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoOp.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoGeneration_h
|
||||
#define eoGeneration_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoGeneration
|
||||
* Single step of a evolutionary algorithm. Applies selection, then genetic
|
||||
* operators, replaces using a replacement policy, and finally evaluates the
|
||||
* new ones */
|
||||
template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGeneration(eoSelect<Chrom>& _select,
|
||||
eoBreeder<Chrom>& _breeder,
|
||||
eoBinPopOp<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {};
|
||||
|
||||
/// Copy Constructor.
|
||||
eoGeneration(eoGeneration<Chrom>& _gen):
|
||||
select(_gen.select), transform(_gen.transform),
|
||||
replace(_gen.replace), evaluator( _gen.evaluator ) {};
|
||||
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<Chrom>& pop) {
|
||||
eoPop<Chrom> breeders;
|
||||
try {
|
||||
select(pop, breeders);
|
||||
transform(breeders);
|
||||
eoPop<Chrom>::iterator i;
|
||||
// Can't use foreach here since foreach takes the
|
||||
// parameter by reference
|
||||
for ( i = breeders.begin(); i != breeders.end(); i++)
|
||||
evaluator(*i);
|
||||
replace(breeders, pop);
|
||||
} catch ( std::exception& e ) {
|
||||
throw std::runtime_error( e.what() );
|
||||
}
|
||||
}
|
||||
|
||||
/// Class name.
|
||||
std::string className() const { return "eoGeneration"; }
|
||||
|
||||
private:
|
||||
eoBinPopOp<Chrom>& select;
|
||||
eoMonPopOp<Chrom>& transform;
|
||||
eoBinPopOp<Chrom>& replace;
|
||||
eoEvalFunc<Chrom>& evaluator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoGeneration_h
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGenericBinOp.h
|
||||
// (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoGenericBinOp_h
|
||||
#define _eoGenericBinOp_h
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/** Contains base classes for generic binary operators for eoFixedLength
|
||||
and eoVariableLength (They also derive from the eoOp) as well as
|
||||
the corresponding converters to actual Ops.
|
||||
*/
|
||||
|
||||
/** eoGenericBinOp is the generic binary operator:
|
||||
it takes two arguments, modifies the first one, and returns a boolean
|
||||
indicating if the argument has actually been modified
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGenericBinOp: public eoOp<EOT>, public eoBF<EOT &, const EOT &, bool>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGenericBinOp()
|
||||
: eoOp<EOT>( eoOp<EOT>::binary ) {};
|
||||
virtual std::string className() const {return "eoGenericBinOp";};
|
||||
};
|
||||
|
||||
/** Converter from eoGenericBinOp to eoBinOp
|
||||
the only thinig to do is to transform the boolean into invalidation
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGeneric2TrueBinOp: public eoBinOp<EOT>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGeneric2TrueBinOp(eoGenericBinOp<EOT> & _binOp)
|
||||
: binOp( _binOp ) {};
|
||||
virtual std::string className() const {return "eoGeneric2TrueBinOp";}
|
||||
|
||||
virtual void operator()(EOT & _eo1, const EOT & _eo2)
|
||||
{
|
||||
if (binOp(_eo1, _eo2))
|
||||
_eo1.invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
eoGenericBinOp<EOT> & binOp;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGenericMonOp.h
|
||||
// (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoGenericMonOp_h
|
||||
#define _eoGenericMonOp_h
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/** Contains base classes for generic operators for eoFixedLength
|
||||
and eoVariableLength (They also derive from the eoOp) as well as
|
||||
the corresponding converters to actual Ops.
|
||||
*/
|
||||
|
||||
/** eoGenericMonOp is the generic unary operator:
|
||||
it takes one argument, and returns a boolean indicating if the argument
|
||||
has been modified
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGenericMonOp: public eoOp<EOT>, public eoUF<EOT&, bool>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGenericMonOp()
|
||||
: eoOp<EOT>( eoOp<EOT>::unary ) {};
|
||||
virtual std::string className() const {return "eoGenericMonOp";};
|
||||
};
|
||||
|
||||
/** COnverter from eoGenericMonOp to eoMonOp
|
||||
the only thinig to do is to transform the boolean into invalidation
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGeneric2TrueMonOp: public eoMonOp<EOT>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGeneric2TrueMonOp(eoGenericMonOp<EOT> & _monOp)
|
||||
: monOp( _monOp ) {};
|
||||
virtual std::string className() const {return "eoGeneric2trueMonOp";}
|
||||
|
||||
virtual void operator()(EOT & _eo)
|
||||
{
|
||||
if (monOp(_eo))
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
eoGenericMonOp<EOT> & monOp;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoGenericQuadOp.h
|
||||
// (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoGenericQuadOp_h
|
||||
#define _eoGenericQuadOp_h
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/** Contains base classes for generic quadratic operators for eoFixedLength
|
||||
and eoVariableLength (They also derive from the eoOp) as well as
|
||||
the corresponding converters to actual Ops.
|
||||
*/
|
||||
|
||||
/** eoGenericQuadOp is the generic quadratic operator:
|
||||
it takes two arguments, modifies the first one, and returns a boolean
|
||||
indicating if the arguments have actually been modified
|
||||
|
||||
WARNING: even if only 1 argument is modified, it should return true,
|
||||
and both fitnesses will be invalidated. It is assumed that
|
||||
quadratic operators do some exchange of genetic material, so
|
||||
if one is modified, the other is, too!
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGenericQuadOp: public eoOp<EOT>, public eoBF<EOT &, EOT &, bool>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGenericQuadOp()
|
||||
: eoOp<EOT>( eoOp<EOT>::quadratic ) {};
|
||||
virtual std::string className() const {return "eoGenericQuadOp";};
|
||||
};
|
||||
|
||||
/** Converter from eoGenericQuadOp to eoQuadOp
|
||||
the only thinig to do is to transform the boolean into invalidation
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoGeneric2TrueQuadOp: public eoQuadOp<EOT>
|
||||
{
|
||||
public:
|
||||
/// Ctor
|
||||
eoGeneric2TrueQuadOp(eoGenericQuadOp<EOT> & _quadOp)
|
||||
: quadOp( _quadOp ) {};
|
||||
virtual std::string className() const {return "eoGeneric2TrueQuadOp";}
|
||||
|
||||
virtual void operator()(EOT & _eo1, EOT & _eo2)
|
||||
{
|
||||
if (quadOp(_eo1, _eo2))
|
||||
{
|
||||
_eo1.invalidate();
|
||||
_eo2.invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
eoGenericQuadOp<EOT> & quadOp;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoID.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOID_H
|
||||
#define EOID_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream> // std::istream, std::ostream
|
||||
#include <string> // for std::string
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoID
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoID is a template class that adds an ID to an object.\\
|
||||
Requisites for template instantiation are that the object must admit a default ctor
|
||||
and a copy ctor. The Object must be an eoObject, thus, it must have its methods: className,
|
||||
printOn, readFrom, that is why we don´t subclass eoObject to avoid multiple inheritance.\\
|
||||
IDs can be used to count objects of a a kind, or track them, or whatever.
|
||||
@see eoObject
|
||||
*/
|
||||
template <class Object>
|
||||
class eoID: public Object
|
||||
{
|
||||
public:
|
||||
/// Main ctor from an already built Object.
|
||||
eoID( const Object& _o): Object( _o ), thisID(globalID++) {};
|
||||
|
||||
/// Copy constructor.
|
||||
eoID( const eoID& _id): Object( _id ), thisID(globalID++ ) {};
|
||||
|
||||
/// Virtual dtor. They are needed in virtual class hierarchies
|
||||
virtual ~eoID() {};
|
||||
|
||||
|
||||
///returns the age of the object
|
||||
unsigned long ID() const {return thisID;}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Return the class id. This should be redefined in each class; but
|
||||
it's got code as an example of implementation. Only "leaf" classes
|
||||
can be non-virtual.
|
||||
*/
|
||||
virtual std::string className() const { return std::string("[eoID]")+Object::className(); };
|
||||
|
||||
/**
|
||||
* Read object.
|
||||
* @param _is A std::istream.
|
||||
* @throw runtime_std::exception If a valid object can't be read.
|
||||
*/
|
||||
virtual void readFrom(std::istream& _is) {
|
||||
Object::readFrom( _is );
|
||||
_is >> thisID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A std::ostream.
|
||||
*/
|
||||
virtual void printOn(std::ostream& _os) const{
|
||||
Object::printOn( _os );
|
||||
_os << thisID;
|
||||
}
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
||||
/** Default Constructor. \\
|
||||
It´s private so that it is not used anywhere; the right way of using this object
|
||||
is to create an Object and passing it to an aged by means of the copy ctor; that way
|
||||
it´s turned into an Aged object*/
|
||||
eoID(): Object(), thisID( globalID++ ) {};
|
||||
|
||||
unsigned long thisID;
|
||||
static unsigned long globalID;
|
||||
};
|
||||
|
||||
template< class Object>
|
||||
unsigned long eoID< Object >::globalID = 0;
|
||||
|
||||
#endif EOID_H
|
||||
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoInclusion.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoInclusion_h
|
||||
#define eoInclusion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h>
|
||||
#include <eoMerge.h>
|
||||
|
||||
/*****************************************************************************
|
||||
* eoInclusion: A replacement algorithm. *
|
||||
* Creates a new population by selecting the best individuals from the *
|
||||
* breeders and original populations *
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from std::istream
|
||||
eoInclusion( std::istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInclusion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders.
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
unsigned target = min(static_cast<unsigned>(pop.size() * rate()),
|
||||
pop.size() + breeders.size());
|
||||
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
|
||||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoInclusion";};
|
||||
//@}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInclusion_h
|
||||
|
|
@ -1,160 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoIndiSelector.h
|
||||
Abstract selection operator, which is used by the eoGeneralOps
|
||||
to obtain individuals from a source population. It also gives a
|
||||
direct descended eoPopIndiSelector that can be used to
|
||||
initialize objects with an eoPop<EOT>. For most uses use eoPopIndividualSelector
|
||||
rather than eoIndividualSelector to derive from.
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoIndiSelector_h
|
||||
#define eoIndiSelector_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
/**
|
||||
* eoIndividualSelector: This class defines the interface. This
|
||||
* interface is used by the eoGeneralOp to get new individuals
|
||||
* from a pop, a subpop or a remote pop
|
||||
* for convenience when implementing an nary operator a size() and operator[]
|
||||
* need to be implemented.
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoIndiSelector : public eoF<const EOT&>
|
||||
{
|
||||
public :
|
||||
|
||||
eoIndiSelector() {}
|
||||
|
||||
virtual ~eoIndiSelector(void) {}
|
||||
|
||||
/**
|
||||
return the number of individuals that can be selected by an nary operator (through operator[] below)
|
||||
*/
|
||||
virtual size_t size(void) const = 0;
|
||||
|
||||
/**
|
||||
return the specified individual, the size_t argument should be between 0 and eoIndiSelector::size()
|
||||
*/
|
||||
virtual const EOT& operator[](size_t i) const = 0;
|
||||
};
|
||||
|
||||
|
||||
#include <eoSelectOne.h>
|
||||
|
||||
/**
|
||||
* eoSelectOneAdaptor: Adaptor class for dispensing individuals.
|
||||
|
||||
It produces the eoIndiSelector interface and an eoSelectOne implementation
|
||||
This class can then be used for general operators
|
||||
|
||||
various useful things can be done with this class:
|
||||
you can specify how many of the population can ever be dispensed to the
|
||||
operators, but you can also specify a preference to the first guy being
|
||||
dispensed. This is useful if you want to perform the operator on a specific
|
||||
individual.
|
||||
|
||||
@see eoSelectOne, eoIndiSelector
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoSelectOneIndiSelector : public eoIndiSelector<EOT>
|
||||
{
|
||||
public :
|
||||
eoSelectOneIndiSelector(eoSelectOne<EOT>& _select) : pop(0), last(0), firstChoice(-1), secondChoice(-1), select(_select) {}
|
||||
|
||||
struct eoUnitializedException{};
|
||||
|
||||
/** Initialization function, binds the population to the selector, can also
|
||||
be used to specify an optional end
|
||||
*/
|
||||
eoSelectOneIndiSelector& bind(const eoPop<EOT>& _pop, int _end = -1)
|
||||
{
|
||||
pop = &_pop;
|
||||
last = _end;
|
||||
|
||||
if (last < 0 || last > (int) pop->size())
|
||||
{
|
||||
last = pop->size();
|
||||
}
|
||||
|
||||
select.setup(*pop);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Bias function to bias the selection function to select specific individuals
|
||||
first before applying a selection algorithm defined in derived classes
|
||||
*/
|
||||
eoSelectOneIndiSelector& bias(int _first, int _second = -1)
|
||||
{
|
||||
firstChoice = _first;
|
||||
secondChoice = _second;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
size_t size(void) const { valid(); return last; }
|
||||
const EOT& operator[](size_t _i) const { valid(); return pop->operator[](_i); }
|
||||
|
||||
eoPop<EOT>::const_iterator begin(void) const { valid(); return pop->begin(); }
|
||||
eoPop<EOT>::const_iterator end(void) const { valid(); return pop->end(); }
|
||||
|
||||
/// operator() does the work. Note that it is not virtual. It calls do_select that needs to be implemented by the derived classes
|
||||
const EOT& operator()(void)
|
||||
{
|
||||
valid();
|
||||
if (firstChoice < 0 || firstChoice >= last)
|
||||
{
|
||||
// see if we have a second choice
|
||||
if (secondChoice < 0 || secondChoice >= last)
|
||||
{
|
||||
return select(*pop); // let the embedded selector figure out what to do
|
||||
}
|
||||
|
||||
const EOT& result = pop->operator[](secondChoice);
|
||||
secondChoice = -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
const EOT& result = pop->operator[](firstChoice);
|
||||
firstChoice = -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
void valid(void) const
|
||||
{
|
||||
if (pop == 0)
|
||||
throw eoUnitializedException();
|
||||
}
|
||||
|
||||
const eoPop<EOT>* pop; // need a pointer as this the pop argument can be re-instated
|
||||
int last;
|
||||
int firstChoice;
|
||||
int secondChoice;
|
||||
eoSelectOne<EOT>& select;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,193 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInplaceTransform.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoInplaceTransform_h
|
||||
#define eoInplaceTransform_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // std::vector
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoOpSelector.h> // eoOpSelector
|
||||
#include <eoFunctor.h>
|
||||
#include <eoIndiSelector.h>
|
||||
#include <eoBackInserter.h>
|
||||
|
||||
/*****************************************************************************
|
||||
* eoInplaceTransform1: transforms a population using genetic operators. *
|
||||
* It does it in an SGA like manner
|
||||
*****************************************************************************/
|
||||
template<class Chrom> class eoInplaceTransform1 : public eoTransform<Chrom>
|
||||
{
|
||||
public:
|
||||
|
||||
/// Default constructor.
|
||||
eoInplaceTransform1( eoOpSelector<Chrom>& _opSel): opSel( _opSel ), select(defaultSelect) {}
|
||||
eoInplaceTransform1( eoOpSelector<Chrom>& _opSel, eoSelectOne<Chrom>& _select)
|
||||
: opSel(_opSel), select(_select) {}
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
// copy the guys in a newpop
|
||||
// because otherwise eoSelectRandom might select freshly created individuals
|
||||
eoPop<Chrom> newpop;
|
||||
newpop.reserve(pop.size());
|
||||
|
||||
// Set up general op helper classes
|
||||
eoSelectOneIndiSelector<Chrom> inplace(select);
|
||||
eoBackInserter<Chrom> inserter;
|
||||
|
||||
// set up selection routine
|
||||
select.setup(pop);
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); i++)
|
||||
{
|
||||
eoOp<Chrom>* op = opSel.Op();
|
||||
|
||||
switch (op->getType())
|
||||
{
|
||||
case eoOp<Chrom>::unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
newpop.push_back(pop[i]);
|
||||
(*monop)( newpop.back() );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
newpop.push_back(pop[i]);
|
||||
(*binop)(newpop[i], select(pop));
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::quadratic:
|
||||
{
|
||||
|
||||
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
|
||||
|
||||
newpop.push_back(pop[i]);
|
||||
Chrom& indy1 = newpop.back();
|
||||
|
||||
if (++i == pop.size())
|
||||
newpop.push_back(select(pop));
|
||||
else
|
||||
newpop.push_back(pop[i]);
|
||||
|
||||
(*Qop)(indy1, newpop.back() );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::general :
|
||||
{
|
||||
eoGeneralOp<Chrom>* Gop = static_cast<eoGeneralOp<Chrom>* >(op);
|
||||
|
||||
inplace.bind(pop);
|
||||
inplace.bias(i,i + 1);
|
||||
|
||||
inserter.bind(newpop);
|
||||
unsigned orgsize = newpop.size();
|
||||
(*Gop)(inplace, inserter);
|
||||
unsigned diff = newpop.size() - orgsize;
|
||||
i = i + (diff-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pop.swap(newpop); // overwrite existing pop
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
eoOpSelector<Chrom>& opSel;
|
||||
eoRandomSelect<Chrom> defaultSelect;
|
||||
eoSelectOne<Chrom>& select;
|
||||
|
||||
};
|
||||
|
||||
#include <eoGOpSelector.h>
|
||||
|
||||
/*****************************************************************************
|
||||
* eoInplaceTransform2: transforms a population using general genetic operators. *
|
||||
* It does it in an SGA like manner
|
||||
*****************************************************************************/
|
||||
template<class Chrom> class eoInplaceTransform2 : public eoTransform<Chrom>
|
||||
{
|
||||
public:
|
||||
|
||||
/// Default constructor.
|
||||
eoInplaceTransform2( eoGOpSelector<Chrom>& _opSel): opSel( _opSel ), select(defaultSelect) {}
|
||||
eoInplaceTransform2( eoGOpSelector<Chrom>& _opSel, eoSelectOne<Chrom>& _select)
|
||||
: opSel(_opSel), select(_select) {}
|
||||
/**
|
||||
* Transforms a population.
|
||||
* @param pop The population to be transformed.
|
||||
*/
|
||||
void operator()(eoPop<Chrom>& pop)
|
||||
{
|
||||
// copy the guys in a newpop
|
||||
// because otherwise eoSelectRandom might select freshly created individuals
|
||||
eoPop<Chrom> newpop;
|
||||
newpop.reserve(pop.size());
|
||||
|
||||
// Set up general op helper classes
|
||||
eoSelectOneIndiSelector<Chrom> inplace(select);
|
||||
eoBackInserter<Chrom> inserter;
|
||||
|
||||
// set up selection routine
|
||||
select.setup(pop);
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); i++)
|
||||
{
|
||||
eoGeneralOp<Chrom>& Gop = opSel.selectOp();
|
||||
|
||||
inplace.bind(pop);
|
||||
inplace.bias(i,i + 1);
|
||||
|
||||
inserter.bind(newpop);
|
||||
unsigned orgsize = newpop.size();
|
||||
Gop(inplace, inserter);
|
||||
|
||||
// see how many have been inserted and add that to i (minus one ofcourse)
|
||||
unsigned diff = newpop.size() - orgsize;
|
||||
i = i + (diff-1);
|
||||
}
|
||||
|
||||
pop.swap(newpop); // overwrite existing pop
|
||||
}
|
||||
|
||||
private:
|
||||
eoGOpSelector<Chrom>& opSel;
|
||||
eoRandomSelect<Chrom> defaultSelect;
|
||||
eoSelectOne<Chrom>& select;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoBreeder_h
|
||||
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoInserter.h
|
||||
Abstract population insertion operator, which is used by the eoGeneralOps
|
||||
to insert the results in the (intermediate) population. It also contains
|
||||
a direct descended eoPopInserter that defines a convenient inbetween class
|
||||
for working with eoPop<EOT>. The user will most likely derive from eoPopInserter
|
||||
rather than eoInserter.
|
||||
|
||||
(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 eoInserter_h
|
||||
#define eoInserter_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
#include <eoPop.h>
|
||||
|
||||
/**
|
||||
* eoInserter: Interface class that enables an operator to insert
|
||||
new individuals into the (intermediate) population for example.
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoInserter : public eoUF<const EOT&, eoInserter<EOT>&>
|
||||
{
|
||||
public :
|
||||
virtual ~eoInserter() {}
|
||||
|
||||
struct eoInserterException{};
|
||||
};
|
||||
|
||||
/**
|
||||
* eoPopInserter: In-between class that defines an initialization
|
||||
* of the eoIndividualInserter.
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoPopInserter : public eoInserter<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoPopInserter(void) : eoInserter<EOT>(), thePop(0) {}
|
||||
|
||||
/// Binds the population to this class. This is an initialization routine used by breeders
|
||||
eoInserter<EOT>& bind(eoPop<EOT>& _pop)
|
||||
{
|
||||
thePop = &_pop;
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected :
|
||||
|
||||
eoPop<EOT>& pop(void) const { valid(); return *thePop; }
|
||||
|
||||
private :
|
||||
|
||||
void valid(void) const
|
||||
{
|
||||
if (thePop == 0)
|
||||
throw eoInserterException();
|
||||
}
|
||||
|
||||
// Need a pointer as the inserter should be able to bind to different populations.
|
||||
// This is caused by the 'one template parameter only' convention in EO.
|
||||
|
||||
eoPop<EOT>* thePop;
|
||||
|
||||
// If eoGOpBreeder could be templatized over the inserter and the selector,
|
||||
// the pop could be a ref as this class could be created every time it is applied
|
||||
// and subsequently would get the population through the constructor
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,96 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
// Inserts new members into the population
|
||||
// (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 eoInsertion_h
|
||||
#define eoInsertion_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoMerge.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
* Creates a new population with all the breeders and the best individuals
|
||||
* from the original population.
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoInsertion: public eoBinaryFunctor<eoPop<Chrom>&, const eoPop<Chrom>&>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from std::istream
|
||||
eoInsertion( std::istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInsertion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()( eoPop<Chrom>& _breeders, const eoPop<Chrom>& _pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>((_pop.size() * rate()));
|
||||
|
||||
_pop.swap(_breeders);
|
||||
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoInsertion";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoKill.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOKILL_h
|
||||
#define _EOKILL_h
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/// Kill eliminates a gen in a chromosome
|
||||
template <class EOT >
|
||||
class eoKill: public eoMonOp<EOT> {
|
||||
public:
|
||||
///
|
||||
eoKill( )
|
||||
: eoMonOp< EOT >(){};
|
||||
|
||||
/// needed virtual dtor
|
||||
virtual ~eoKill() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const
|
||||
{
|
||||
unsigned pos = rng.random(_eo.length());
|
||||
_eo.deleteGene( pos );
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoKill";};
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,105 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoLottery.h
|
||||
// Implements the lottery procedure for selection
|
||||
// (c) GeNeura Team, 1998 - Marc Schoenauer, 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 eoLottery_h
|
||||
#define eoLottery_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdexcept> // std::logic_error
|
||||
#include <utils/selectors.h> // sum_fitness
|
||||
#include <eoFunctor.h>
|
||||
|
||||
// #include <functional> //
|
||||
// #include <numeric> // accumulate
|
||||
// #include <eo> // eoPop eoSelect MINFLOAT
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoLottery: a selection method. Puts into the output a group of individuals
|
||||
selected using lottery; individuals with higher probability will have more
|
||||
chances of being selected.
|
||||
Requires EOT::Fitness to be float castable
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class EOT> class eoLottery: public eoBinaryFunctor<void, const eoPop<EOT>&, eoPop<EOT>& >
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoLottery(const float& _rate = 1.0): eoBinPopOp<EOT>(), rate_(_rate)
|
||||
{
|
||||
if (minimizing_fitness<EOT>())
|
||||
{
|
||||
throw std::logic_error("eoLottery: minimizing fitness");
|
||||
}
|
||||
}
|
||||
|
||||
/** actually selects individuals from pop and pushes them back them into 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()(const eoPop<EOT>& pop, eoPop<EOT>& breeders)
|
||||
{
|
||||
size_t target = static_cast<size_t>(rate_ * pop.size());
|
||||
|
||||
/* Gustavo: uncomment this if it must be here
|
||||
// test of consistency
|
||||
if (breeders.size() >= target) {
|
||||
throw("Problem in eoLottery: already too many offspring");
|
||||
}
|
||||
|
||||
double total;
|
||||
|
||||
try
|
||||
{
|
||||
total = sum_fitness (pop);
|
||||
}
|
||||
catch (eoNegativeFitnessException&)
|
||||
{ // say where it occured...
|
||||
throw eoNegativeFitnessException(*this);
|
||||
}
|
||||
*/
|
||||
|
||||
double total = sum_fitness (pop);
|
||||
|
||||
// selection of chromosomes
|
||||
while (breeders.size() < target)
|
||||
{
|
||||
breeders.push_back(roulette_wheel(pop, total));
|
||||
}
|
||||
}
|
||||
|
||||
double rate(void) const { return rate_; }
|
||||
|
||||
private:
|
||||
double rate_; // selection rate
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoLottery_h
|
||||
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMutation.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _EOMUTATION_H
|
||||
#define _EOMUTATION_H
|
||||
|
||||
#include <math.h>
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
|
||||
/** Generic Mutation of an EO.
|
||||
This is a virtual class, just to establish the interface for the ctor.
|
||||
Mutation is usually type-specific
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoMutation: public eoMonOp<EOT> {
|
||||
public:
|
||||
|
||||
///
|
||||
eoMutation(const double _rate=0.0) : eoMonOp< EOT >(), rate(_rate) {};
|
||||
|
||||
///
|
||||
virtual ~eoMutation() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
typename EOT::iterator i;
|
||||
for ( i = _eo.begin(); i != _eo.end(); i ++ )
|
||||
applyAt( _eo, *i );
|
||||
}
|
||||
|
||||
/// To print me on a stream.
|
||||
/// @param os The std::ostream.
|
||||
void printOn(std::ostream& os) const {
|
||||
os << rate ;
|
||||
}
|
||||
|
||||
/// To read me from a stream.
|
||||
/// @param is The std::istream.
|
||||
void readFrom(std::istream& is) {
|
||||
is >> rate ;
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoMutation";};
|
||||
//@}
|
||||
|
||||
protected:
|
||||
double rate;
|
||||
|
||||
private:
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Type Type;
|
||||
#else
|
||||
typedef typename EOT::Type Type;
|
||||
#endif
|
||||
|
||||
/// applies operator to one gene in the EO. It is empty, so each descent class must define it.
|
||||
virtual void applyAt( EOT& _eo, unsigned _i ) const = 0 ;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoNegExp.h
|
||||
(c) GeNeura Team, 1998
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EONEGEXP_H
|
||||
#define _EONEGEXP_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <eoRnd.h> // for base class
|
||||
#include <utils/eoRNG.h> // for base class
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Class eoNegExp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/// Generates random numbers using a negative exponential distribution
|
||||
template<class T>
|
||||
class eoNegExp: public eoRnd<T>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Default constructor.
|
||||
* @param _mean Distribution mean
|
||||
*/
|
||||
eoNegExp(T _mean): eoRnd<T>(), mean(_mean) {};
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* @param _rnd the copyee
|
||||
*/
|
||||
eoNegExp( const eoNegExp& _rnd): eoRnd<T>( _rnd), mean(_rnd.mean) {};
|
||||
|
||||
/// Returns an uniform dandom number over the interval [min, max).
|
||||
virtual T operator()() {
|
||||
return T( -mean*log((double)rng.rand() / rng.rand_max())); }
|
||||
|
||||
private:
|
||||
T mean;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoNonUniform.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EONONUNIFORM_H
|
||||
#define EONONUNIFORM_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <math.h> // pow
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoNonUniform
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoNonUniform
|
||||
{
|
||||
public:
|
||||
eoNonUniform(const unsigned _num_step):
|
||||
step_value(0), num_step_value(_num_step) {}
|
||||
|
||||
void reset() { step_value = 0; }
|
||||
|
||||
const unsigned& step() const { return step_value; }
|
||||
const unsigned& num_step() const { return num_step_value; }
|
||||
|
||||
operator int() const { return step_value < num_step_value; }
|
||||
|
||||
void operator++() { ++step_value; }
|
||||
void operator++(int) { ++step_value; }
|
||||
|
||||
private:
|
||||
unsigned step_value, num_step_value;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoLinear
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoLinear
|
||||
{
|
||||
public:
|
||||
eoLinear(const double _first,
|
||||
const double _last,
|
||||
const eoNonUniform& _non_uniform):
|
||||
first(_first),
|
||||
diff((_last - _first) / (_non_uniform.num_step() - 1)),
|
||||
non_uniform(_non_uniform) {}
|
||||
|
||||
double operator()() const
|
||||
{
|
||||
return first + diff * non_uniform.step();
|
||||
}
|
||||
|
||||
private:
|
||||
double first, diff;
|
||||
const eoNonUniform& non_uniform;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoNegExp2
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoNegExp2
|
||||
{
|
||||
public:
|
||||
eoNegExp2(const double _r,
|
||||
const double _b,
|
||||
const eoNonUniform& _non_uniform):
|
||||
r(_r), b(_b),
|
||||
non_uniform(_non_uniform) {}
|
||||
|
||||
double operator()() const
|
||||
{
|
||||
return 1.0 - pow(r, pow(1.0 - (double)non_uniform.step() /
|
||||
non_uniform.num_step(), b));
|
||||
}
|
||||
|
||||
private:
|
||||
double r, b;
|
||||
const eoNonUniform& non_uniform;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif NON_UNIFORM_HH
|
||||
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoNormal.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EONORMAL_H
|
||||
#define _EONORMAL_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <math.h>
|
||||
#include <eoRnd.h> // for base class
|
||||
#include <utils/eoRNG.h> // for random number generator
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Class eoNormal
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/// Generates random number using a normal distribution
|
||||
template<class T>
|
||||
class eoNormal: public eoRnd<T>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Default constructor.
|
||||
* @param _mean Dsitribution mean
|
||||
* @param _sd Standard Deviation
|
||||
*/
|
||||
eoNormal(T _mean, T _sd)
|
||||
: eoRnd<T>(), mean(_mean), sd(_sd), phase(false) {}
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* @param _rnd the other one
|
||||
*/
|
||||
eoNormal( const eoNormal& _rnd )
|
||||
: eoRnd<T>( _rnd), mean(_rnd.mean), sd(_rnd.sd), phase(false) {}
|
||||
|
||||
/** Returns an uniform random number over the interval [min, max).
|
||||
@return an uniform random number over the interval [min, max).
|
||||
*/
|
||||
virtual T operator()() {
|
||||
if (phase) { // Already have one stored up.
|
||||
phase = false;
|
||||
return T ( (sqRatio * q * sd) + mean );
|
||||
}
|
||||
|
||||
double p, v;
|
||||
do {
|
||||
p = ((double)rng.rand() / rng.rand_max())*2-1;
|
||||
q = ((double)rng.rand() / rng.rand_max())*2-1;
|
||||
v = p*p + q*q;
|
||||
} while(v > 1.0 || v <0.25);
|
||||
|
||||
sqRatio = sqrt(-2*log((double)rand() / rng.rand_max()) / v);
|
||||
phase = true;
|
||||
return T( (sqRatio * p * sd) + mean );
|
||||
};
|
||||
|
||||
private:
|
||||
T mean;
|
||||
T sd;
|
||||
bool phase;
|
||||
double sqRatio, q;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
// eoOpFactory.h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMonOpFactory.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOOPFACTORY_H
|
||||
#define _EOOPFACTORY_H
|
||||
|
||||
#include <eoFactory.h>
|
||||
#include <eoDup.h>
|
||||
#include <eoKill.h>
|
||||
#include <eoTranspose.h>
|
||||
#include <eoXOver2.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** EO Factory. An instance of the factory class to create monary operators.
|
||||
@see eoSelect*/
|
||||
template< class EOT>
|
||||
class eoOpFactory: public eoFactory< eoOp<EOT> > {
|
||||
|
||||
public:
|
||||
|
||||
/// @name ctors and dtors
|
||||
//{@
|
||||
/// constructor
|
||||
eoOpFactory( ) {}
|
||||
|
||||
/// destructor
|
||||
virtual ~eoOpFactory() {}
|
||||
//@}
|
||||
|
||||
/** Another factory method: creates an object from an std::istream, reading from
|
||||
it whatever is needed to create the object. Usually, the format for the std::istream will be\\
|
||||
objectType parameter1 parameter2 ... parametern\\
|
||||
If there are problems, an std::exception is raised; it should be caught at the
|
||||
upper level, because it might be something for that level
|
||||
@param _is an stream from where a single line will be read
|
||||
@throw runtime_std::exception if the object type is not known
|
||||
*/
|
||||
virtual eoOp<EOT>* make(std::istream& _is) {
|
||||
eoOp<EOT> * opPtr = NULL;
|
||||
std::string objectTypeStr;
|
||||
_is >> objectTypeStr;
|
||||
if ( objectTypeStr == "eoDup") {
|
||||
opPtr = new eoDup<EOT>();
|
||||
}
|
||||
if ( objectTypeStr == "eoKill" ) {
|
||||
opPtr = new eoKill<EOT>( );
|
||||
}
|
||||
if ( objectTypeStr == "eoTranspose" ) {
|
||||
opPtr = new eoTranspose<EOT>( );
|
||||
}
|
||||
if ( objectTypeStr == "eoXOver2" ) {
|
||||
opPtr = new eoXOver2<EOT>( );
|
||||
}
|
||||
if ( !opPtr ) {
|
||||
throw objectTypeStr;
|
||||
}
|
||||
return opPtr;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif _EOOPFACTORY_H
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoOpSelector.h
|
||||
Base class for operator selectors, which return 1 operator according
|
||||
to some criterium
|
||||
|
||||
(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 EOOPSELECTOR_H
|
||||
#define EOOPSELECTOR_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdexcept> // std::runtime_error
|
||||
|
||||
#include <eoObject.h>
|
||||
#include <eoPrintable.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** An operator selector is an object that contains a set of EO operators,
|
||||
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
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoOpSelector: public eoObject, public eoPrintable
|
||||
{
|
||||
public:
|
||||
|
||||
// Need virtual destructor for derived classes
|
||||
virtual ~eoOpSelector() {}
|
||||
|
||||
/// type of IDs assigned to each operators, used to handle them
|
||||
typedef unsigned ID;
|
||||
|
||||
/** add an operator to the operator set
|
||||
@param _op a genetic operator, that will be applied in some way
|
||||
@param _arg the operator rate, usually, or any other argument to the operator
|
||||
@return an ID that will be used to identify the operator
|
||||
*/
|
||||
virtual ID addOp( eoOp<EOT>& _op, float _arg ) = 0;
|
||||
|
||||
/** Gets a non-const reference to an operator, so that it can be changed,
|
||||
modified or whatever
|
||||
@param _id a previously assigned ID
|
||||
@throw runtime_std::exception if the ID does not exist*/
|
||||
virtual eoOp<EOT>& getOp( ID _id ) = 0;
|
||||
|
||||
/** Remove an operator from the operator set
|
||||
@param _id a previously assigned ID
|
||||
@throw runtime_std::exception if the ID does not exist
|
||||
*/
|
||||
virtual void deleteOp( ID _id ) = 0;
|
||||
|
||||
/// Returns a genetic operator according to the established criteria
|
||||
virtual eoOp<EOT>* Op() = 0;
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
//@{
|
||||
|
||||
/** Return the class id.
|
||||
@return the class name as a std::string
|
||||
*/
|
||||
virtual std::string className() const { return "eoOpSelector"; };
|
||||
|
||||
/**
|
||||
* Read object and print objects are left for subclasses to define.
|
||||
*/
|
||||
//@}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EO_H
|
||||
|
|
@ -1,838 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/* eoParser.h
|
||||
some classes to parser either the command line or a parameter file
|
||||
|
||||
(c) Marc Schoenauer and Geneura team, 1999
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _PARSER_H
|
||||
#define _PARSER_H
|
||||
|
||||
#include <string.h> // for strcasecmp ... maybe there's a c++ way of doing it?
|
||||
// Yep there is, but needs either a simple functor for the equal function
|
||||
// or a hand-rolled std::string template class (this isn't that horrible as
|
||||
// it sounds, it just means a new std::string_traits class with two changed
|
||||
// function definitions. (Maarten)
|
||||
#ifdef _MSC_VER
|
||||
#define strcasecmp(a,b) _strnicmp(a,b,strlen(a))
|
||||
#endif
|
||||
|
||||
// STL includes
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <strstream>
|
||||
#include <ctime>
|
||||
|
||||
// include for std::exceptions
|
||||
#include <stdexcept> // std::logic_error
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Class Param
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* A param repesents an argument that can be passed to a program in the command line
|
||||
*/
|
||||
class Param {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Type of params
|
||||
*/
|
||||
enum valueType { INT, UL, FLOAT, STRING, BOOL, ARRAY, TITLE };
|
||||
|
||||
/**
|
||||
* Construct an Param.
|
||||
* @param _shortName Short name of the argument
|
||||
* @param _longName Long name of the argument
|
||||
* @param _default The default value
|
||||
* @param _valueType Type of the parameter ("integer","unsigned long", "float","char", "bool" and so on)
|
||||
* @param _description Description of the parameter. What is useful for.
|
||||
* @param _required If it is a necessary parameter or not
|
||||
*/
|
||||
Param (std::string _shortName="-h", std::string _longName="--help",
|
||||
std::string _default = "", valueType _valType= STRING,
|
||||
std::string _description="Shows this help",
|
||||
bool _required=false )
|
||||
: repShortName(_shortName), repLongName(_longName),
|
||||
repDescription(_description ), repEnv(""), repDefault(_default),
|
||||
repValue(_default), repValType( _valType),
|
||||
repRequired( _required), repChanged(false) {
|
||||
|
||||
const char *c = repLongName.c_str();
|
||||
for(unsigned i=0; i<repLongName.length() ; i++,c++) {
|
||||
if( *c != '-' ) break;
|
||||
}
|
||||
//initialize "repEnv" depending on the long name of the parameter.
|
||||
//previously the "-" (if exist) are skiped.
|
||||
repEnv = c ;
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
* @param _param The source param.
|
||||
*/
|
||||
Param (const Param& _param) :
|
||||
repShortName(_param.repShortName), repLongName(_param.repLongName),
|
||||
repDescription(_param.repDescription ), repEnv(_param.repEnv),
|
||||
repDefault(_param.repDefault),
|
||||
repValue(_param.repValue), repValType(_param.repValType),
|
||||
repRequired(_param.repRequired), repChanged(_param.repChanged) {};
|
||||
|
||||
/**
|
||||
* Virtual destructor is needed.
|
||||
*/
|
||||
virtual ~Param () {};
|
||||
|
||||
/**
|
||||
* Returns the short name.
|
||||
*/
|
||||
const std::string& shortName ( void ) const { return repShortName; };
|
||||
|
||||
/**
|
||||
* Returns the long name.
|
||||
*/
|
||||
const std::string& longName ( void ) const { return repLongName; };
|
||||
|
||||
/**
|
||||
* Returns the description of the argument
|
||||
*/
|
||||
const std::string& description ( void ) const { return repDescription; };
|
||||
|
||||
/**
|
||||
* Returns the environment variable of the argument
|
||||
*/
|
||||
const std::string& environment ( void ) const { return repEnv; };
|
||||
|
||||
/**
|
||||
* Returns the default value of the argument
|
||||
*/
|
||||
const std::string& defValue ( void ) const { return repDefault; };
|
||||
|
||||
/**
|
||||
* Sets a value for the param.
|
||||
* @param _value The new value.
|
||||
*/
|
||||
void value ( const std::string& _value ) { repValue = _value; repChanged = true; };
|
||||
|
||||
/**
|
||||
* Returns the value of the param.
|
||||
*/
|
||||
const std::string& value ( void ) const { return repValue; };
|
||||
|
||||
/**
|
||||
* Returns if required or not.
|
||||
*/
|
||||
bool required ( void ) const { return repRequired; };
|
||||
|
||||
/**
|
||||
* Returns the type of the param's value.
|
||||
*/
|
||||
Param::valueType valType( void ) const { return repValType; };
|
||||
|
||||
/**
|
||||
* Returns true if the default value of the param has changed.
|
||||
*/
|
||||
bool changed( void ) const { return repChanged; };
|
||||
|
||||
private:
|
||||
std::string repShortName;
|
||||
std::string repLongName;
|
||||
std::string repDescription;
|
||||
std::string repEnv;
|
||||
std::string repDefault;
|
||||
|
||||
std::string repValue;
|
||||
Param::valueType repValType;
|
||||
bool repRequired;
|
||||
bool repChanged;
|
||||
};
|
||||
|
||||
/// This operator is defined to avoid errors in some systems
|
||||
inline bool operator < ( const Param& _p1, const Param& _p2 ) {
|
||||
return ( _p1.shortName() < _p2.shortName() );
|
||||
}
|
||||
|
||||
/// This operator is defined to avoid errors in some systems
|
||||
inline bool operator == ( const Param& _p1, const Param& _p2 ) {
|
||||
return ( _p1.shortName() == _p2.shortName() );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Class Parser
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Parses the command line / input parameter file / environment variables.
|
||||
*/
|
||||
class Parser {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _argc, _ argv command line arguments
|
||||
* @param _programDescription Description of the work the program does
|
||||
*/
|
||||
Parser ( int _argc, char **_argv , std::string _programDescription,
|
||||
std::string _sFileParamName = "-P",
|
||||
std::string _lFileParamName = "--Param") :
|
||||
params(),
|
||||
programName( _argv[0]), programDescription( _programDescription),
|
||||
parse_argc(_argc), parse_argv(_argv), InputFileName("") {
|
||||
|
||||
// the input file name has to be read immediately - from command-line or environement (not input0file :-)
|
||||
std::string _default = _argv[0];
|
||||
_default += ".param";
|
||||
Param param (_sFileParamName, _lFileParamName, _default, Param::STRING, "Name of the input file", 0);
|
||||
|
||||
// FIRST: look for the corresponding environment variable
|
||||
if( getenv( param.environment().c_str() ) )
|
||||
param.value(getenv(param.environment().c_str()) );
|
||||
|
||||
// LAST (highest priority) parse the command line arguments
|
||||
for (int i=1 ; i<parse_argc ; i++)
|
||||
if( ( ! strcasecmp(param.longName().c_str(), parse_argv[i]) ) ||
|
||||
( ! strcmp(param.shortName().c_str(), parse_argv[i]) )
|
||||
) { // found the parameter name
|
||||
param.value(parse_argv[i+1]);
|
||||
break;
|
||||
}
|
||||
// Now try to open the file
|
||||
ifstream is(param.value().c_str());
|
||||
if (is) // file exists ???
|
||||
InputFileName = param.value().c_str();
|
||||
|
||||
params.push_back( param );
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
* @param _parser The source parser
|
||||
*/
|
||||
Parser ( const Parser& _parser ) :
|
||||
params(_parser.params),
|
||||
programName( _parser.programName),
|
||||
programDescription(_parser.programDescription),
|
||||
parse_argc(_parser.parse_argc),
|
||||
parse_argv(_parser.parse_argv),
|
||||
InputFileName(_parser.InputFileName)
|
||||
{};
|
||||
|
||||
/**
|
||||
* Virtual destructor is needed.
|
||||
*/
|
||||
virtual ~Parser () {};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a fake parameter == title in the output file
|
||||
* @param the title
|
||||
*/
|
||||
void AddTitle (const std::string& _title)
|
||||
{
|
||||
Param param ( "", "", "", Param::TITLE, _title, false );
|
||||
params.push_back( param );
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of all parameter readings:
|
||||
* @param _shortName Short name of the param.
|
||||
* @param _longName Long name of the param.
|
||||
* @param _default Default value.
|
||||
* @param _valType Type of the parameter
|
||||
* @param _description Parameter utility
|
||||
* @param _required If the parameter is necessary or not
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the std::string value of a param from the full parameter description
|
||||
* @param see above
|
||||
*/
|
||||
std::string getString (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _default = "",
|
||||
const std::string& _description="", bool _required=false) {
|
||||
Param param ( _shortName, _longName, _default, Param::STRING, _description, _required );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
return param.value();
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the bool value of a param-flag from the full parameter description
|
||||
* @param see above
|
||||
*/
|
||||
|
||||
bool getBool (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _description="") {
|
||||
Param param ( _shortName, _longName, "false", Param::BOOL, _description, false );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
if (param.value() == "true") {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the "array" (std::vector of std::strings) value of a param from the full parameter description
|
||||
* @param see above
|
||||
*/
|
||||
std::vector<std::string> getArray (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _default = "",
|
||||
const std::string& _description="", bool _required=false) {
|
||||
Param param ( _shortName, _longName, _default, Param::ARRAY, _description, _required );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
istrstream is(param.value().c_str());
|
||||
std::vector<std::string> retValue;
|
||||
std::string tmpStr;
|
||||
|
||||
is >> tmpStr;
|
||||
while(is){
|
||||
retValue.push_back(tmpStr);
|
||||
is >> tmpStr;
|
||||
}
|
||||
return retValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the int value of a param given the full description of the parameter
|
||||
* @param see above
|
||||
* @std::exception BadType if the param's value isn't a correct int
|
||||
*/
|
||||
|
||||
int getInt (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _default = "",
|
||||
const std::string& _description="", bool _required=false) {
|
||||
Param param ( _shortName, _longName, _default, Param::INT, _description, _required );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
// now gets the value
|
||||
istrstream is( param.value().c_str());
|
||||
int retValue;
|
||||
is >> retValue;
|
||||
|
||||
if (!is) {
|
||||
throw Parser::BadType(param.longName().c_str(), param.value().c_str(), "float");
|
||||
return 0;
|
||||
} else {
|
||||
return retValue;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the unsigned lon value of a param given ...
|
||||
* @param see above
|
||||
* @std::exception BadType if the param's value isn't a correct unsigned long
|
||||
*/
|
||||
|
||||
int getUnsignedLong (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _default = "",
|
||||
const std::string& _description="", bool _required=false) {
|
||||
Param param ( _shortName, _longName, _default, Param::UL, _description, _required );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
// now gets the value
|
||||
istrstream is( param.value().c_str());
|
||||
unsigned long retValue;
|
||||
is >> retValue;
|
||||
|
||||
if (!is) {
|
||||
throw Parser::BadType(param.longName().c_str(), param.value().c_str(), "float");
|
||||
return 0;
|
||||
} else {
|
||||
return retValue;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the float value of a param given the description of the parameter
|
||||
* @param see above
|
||||
* @std::exception BadType if the param's value isn't a correct int
|
||||
*/
|
||||
|
||||
float getFloat (const std::string& _shortName, const std::string& _longName,
|
||||
const std::string& _default = "",
|
||||
const std::string& _description="", bool _required=false) {
|
||||
Param param ( _shortName, _longName, _default, Param::FLOAT, _description, _required );
|
||||
parse( param );
|
||||
params.push_back( param );
|
||||
|
||||
// now gets the value
|
||||
istrstream is( param.value().c_str());
|
||||
float retValue;
|
||||
is >> retValue;
|
||||
|
||||
if (!is) {
|
||||
throw Parser::BadType(param.longName().c_str(), param.value().c_str(), "float");
|
||||
return 0;
|
||||
} else {
|
||||
return retValue;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
std::string parse_std::string (std::istream & _is) {
|
||||
std::string paramValue;
|
||||
_is >> paramValue;
|
||||
//if the first character of the std::string or array is not a " => just one word or array-element.
|
||||
if( paramValue[0] != '\"' )
|
||||
return paramValue;
|
||||
|
||||
if( paramValue[1] == '\"' ) // the empty std::string
|
||||
return "" ;
|
||||
|
||||
//else => read until the next " (the end of the std::string).
|
||||
const char *c = paramValue.c_str();
|
||||
std::string tmpStr = c+1;// skip the "
|
||||
if (tmpStr[tmpStr.length()-1] == '\"') { // one word only
|
||||
//tmpStr[tmpStr.length()-1] = '\0';
|
||||
tmpStr.erase( &tmpStr[tmpStr.length()-1] );
|
||||
return tmpStr;
|
||||
}
|
||||
|
||||
bool stop = false;
|
||||
while (_is && !stop) {
|
||||
_is >> paramValue;
|
||||
// test last character of paramValue for "
|
||||
if (paramValue[paramValue.length()-1] == '\"') {
|
||||
paramValue.erase( ¶mValue[paramValue.length()-1] );
|
||||
//paramValue[paramValue.length()-1] = '\0';
|
||||
stop = true;
|
||||
}
|
||||
tmpStr = tmpStr + " " + paramValue ;
|
||||
}
|
||||
return tmpStr;
|
||||
};
|
||||
|
||||
|
||||
void parse (Param & param) {
|
||||
int i;
|
||||
std::string tmpStr, ReadStr, FirstWord;
|
||||
|
||||
// FIRST: look if the associated environment variables have any value, to use them.
|
||||
if( getenv( param.environment().c_str() ) ) {
|
||||
//std::cout <<"\t\t ENV param: ,"<<p->shortName()<<", ,"<<getenv(p->environment().c_str())<<std::endl;
|
||||
param.value(getenv(param.environment().c_str()) );
|
||||
}
|
||||
|
||||
|
||||
// SECOND: search the file parameter, if present
|
||||
if ( InputFileName != "" ) {
|
||||
ifstream is(InputFileName.c_str());
|
||||
while (is) {
|
||||
is >> tmpStr;
|
||||
if ( ( !strcmp(tmpStr.c_str(), param.shortName().c_str()) ) ||
|
||||
( !strcasecmp(tmpStr.c_str(), param.longName().c_str()) )
|
||||
) { // found the keyword
|
||||
|
||||
Param::valueType tmp = param.valType();
|
||||
switch ( tmp ) {
|
||||
case Param::TITLE:
|
||||
std::cerr << "Error, we should not be there" << std::endl;
|
||||
exit(1);
|
||||
break;
|
||||
case Param::BOOL :
|
||||
param.value("true" );
|
||||
break;
|
||||
|
||||
case Param::INT:
|
||||
case Param::UL:
|
||||
case Param::FLOAT:
|
||||
is >> tmpStr;
|
||||
param.value(tmpStr);
|
||||
break;
|
||||
|
||||
case Param::STRING:
|
||||
tmpStr = parse_std::string(is);
|
||||
param.value(tmpStr);
|
||||
break;
|
||||
|
||||
case Param::ARRAY:
|
||||
ReadStr = parse_std::string(is);
|
||||
if ( ReadStr != "<" ) { // no "<" ">" --> a single std::string in the array
|
||||
param.value(ReadStr);
|
||||
break;
|
||||
}
|
||||
// read next word - and keep it in case of <> mismatch
|
||||
FirstWord = parse_std::string(is);
|
||||
// test for empty array
|
||||
if (FirstWord == ">") {
|
||||
param.value("");
|
||||
break;
|
||||
}
|
||||
// else, read all words until ">"
|
||||
tmpStr = FirstWord;
|
||||
ReadStr = parse_std::string(is);
|
||||
while ( is && (ReadStr != ">") ) {
|
||||
tmpStr = tmpStr + " " + ReadStr;
|
||||
ReadStr = parse_std::string(is);
|
||||
}
|
||||
|
||||
if (!is) { // there was a "<" without the corresponding ">"
|
||||
throw Parser::BadArrayParam( param.longName(), FirstWord );
|
||||
param.value(FirstWord); // assume unique std::string
|
||||
}
|
||||
else
|
||||
param.value(tmpStr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// LAST (highest priority) parse the command line arguments
|
||||
for (i=1 ; i<parse_argc ; i++)
|
||||
if( ( ! strcasecmp(param.longName().c_str(), parse_argv[i]) ) ||
|
||||
( ! strcmp(param.shortName().c_str(), parse_argv[i]) )
|
||||
) { // found the parameter name
|
||||
if (param.valType() == Param::BOOL) {
|
||||
//std::cout <<"BOOL: "<<parse_argv[i]<<" <-- true"<<std::endl;
|
||||
param.value("true");
|
||||
}else{
|
||||
if (param.valType() != Param::ARRAY) { //only if it is not an array
|
||||
//std::cout <<"TYPE: "<<parse_argv[i]<<" <-- "<<parse_argv[i+1]<<std::endl;
|
||||
param.value(parse_argv[i+1]);
|
||||
}else{ //if it is an ARRAY
|
||||
i++;
|
||||
ReadStr = parse_argv[i++];
|
||||
//std::cout <<"ARRAY: <-- ";
|
||||
|
||||
if ( ReadStr != "<" ) { // no "<" ">" --> a single std::string in the array
|
||||
param.value(ReadStr);
|
||||
}else{
|
||||
// read next word - and keep it in case of <> mismatch
|
||||
FirstWord = parse_argv[i++];
|
||||
|
||||
// test for empty array
|
||||
if (FirstWord == ">") {
|
||||
param.value("");
|
||||
}else{
|
||||
// else, read all words until ">"
|
||||
tmpStr = FirstWord;
|
||||
ReadStr = parse_argv[i++];
|
||||
while ( (i<parse_argc) && (ReadStr != ">") ) {
|
||||
tmpStr = tmpStr + " " + ReadStr;
|
||||
ReadStr = parse_argv[i++];
|
||||
}
|
||||
//std::cout <<"tmpStr ;"<<tmpStr<<"; ("<<i<<","<<parse_argc<<") "<<std::endl;
|
||||
|
||||
if ( (i>=parse_argc) && (ReadStr != ">") ) { // there was a "<" without the corresponding ">"
|
||||
throw Parser::BadArrayParam( param.longName(), FirstWord );
|
||||
param.value(FirstWord); // assume unique std::string
|
||||
}else{
|
||||
param.value(tmpStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
//MS after trying all possibilities, and if the value has not changed
|
||||
// though the parameter was required, protest!
|
||||
if (param.required() && !param.changed())
|
||||
throw Parser::MissingReqParam(param.shortName());
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a new value for a param given its short name or its long name.
|
||||
* @param _name One of the names of the param.
|
||||
* @param _value Value to be assigned.
|
||||
* @std::exception UnknownArg if the param doesn't exist
|
||||
* @std::exception MissingVal if the param hasn't got a value
|
||||
*/
|
||||
Param::valueType setParamValue (const std::string& _name, const char* _value){
|
||||
std::vector<Param>::iterator pos;
|
||||
|
||||
for (pos=params.begin() ; pos!=params.end() ; pos++)
|
||||
if (pos->shortName()==_name || pos->longName()==_name)
|
||||
break;
|
||||
|
||||
// if found ...
|
||||
if (pos!=params.end()) {
|
||||
switch ( pos->valType() ) {
|
||||
case Param::TITLE:
|
||||
std::cerr << "Error, we should not be there" << std::endl;
|
||||
exit(1);
|
||||
break;
|
||||
case Param::BOOL :
|
||||
pos->value("true");
|
||||
break;
|
||||
case Param::ARRAY :
|
||||
case Param::INT:
|
||||
case Param::UL:
|
||||
case Param::FLOAT:
|
||||
case Param::STRING:
|
||||
if (_value != NULL){
|
||||
pos->value(_value);
|
||||
}else{
|
||||
throw Parser::MissingVal(_name);
|
||||
return Param::BOOL;
|
||||
}
|
||||
break;
|
||||
} // switch
|
||||
|
||||
return pos->valType();
|
||||
|
||||
}else{
|
||||
throw Parser::UnknownArg(_name);
|
||||
return Param::BOOL;
|
||||
}
|
||||
};
|
||||
|
||||
/// the output method - generate the .status file (unless other name is given)
|
||||
friend std::ostream & operator<< ( std::ostream & os, Parser & _parser )
|
||||
{
|
||||
std::vector<Param>::iterator p;
|
||||
//print every param with its value
|
||||
for ( p=_parser.params.begin(); p!=_parser.params.end(); p++ ) {
|
||||
switch ( p->valType() ) {
|
||||
case Param::BOOL :
|
||||
if( p->value() == (std::string) "true")
|
||||
os << p->longName();
|
||||
else
|
||||
os << "#" << p->longName() ; // so the name of the bool is commented out
|
||||
break;
|
||||
|
||||
case Param::INT:
|
||||
case Param::UL:
|
||||
case Param::FLOAT:
|
||||
os << p->longName()<<" "<<p->value();
|
||||
break;
|
||||
|
||||
case Param::ARRAY :
|
||||
os << p->longName() << " < " << p->value().c_str() << " >" ;
|
||||
break;
|
||||
case Param::STRING:
|
||||
os << p->longName()<<" \""<<p->value().c_str()<<"\" ";
|
||||
break;
|
||||
case Param::TITLE:
|
||||
os << std::endl; // Title is in the description below
|
||||
break;
|
||||
} // switch
|
||||
os << "\t #" << p->shortName() << " : " << p->description();
|
||||
if (p->valType() != Param::TITLE)
|
||||
os << " [" << p->defValue() << "]" ;
|
||||
os << std::endl;
|
||||
}
|
||||
return os;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prints out the std::list of parameters in the output file (if specified)
|
||||
*/
|
||||
void outputParam(std::string _OutputFile="")
|
||||
{
|
||||
if (_OutputFile == "") {
|
||||
_OutputFile = parse_argv[0];
|
||||
_OutputFile += ".status";
|
||||
}
|
||||
|
||||
std::ofstream os(_OutputFile.c_str());
|
||||
os << "Parameters used by \"" << programName << "\" ("
|
||||
<< programDescription << ")" << std::endl << std::endl;
|
||||
os << *this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prints an automatic help in the standard output using the information
|
||||
* provided by parameters
|
||||
*/
|
||||
void printHelp() {
|
||||
std::vector<Param>::iterator p;
|
||||
// unsigned i;
|
||||
|
||||
// print program name and description
|
||||
std::cout << this->programName <<": "<<programDescription<<std::endl<<std::endl;
|
||||
|
||||
// print the usage when calling the program from the command line
|
||||
std::cout << "Usage: "<< programName<<" [Options]\n";
|
||||
// only short usage!
|
||||
std::cout << "Options of the form \"-ShortName value\" or \"--LongName value\"" << std::endl;
|
||||
|
||||
// for ( i=0,p=params.begin(); p!=params.end(); i++,p++ )
|
||||
// if( p->valType() != Param::TITLE ) {
|
||||
// if( p->valType() != Param::BOOL ){
|
||||
// std::cout << ( (!p->required())?"[":"");
|
||||
// std::cout <<p->shortName()<<" value"<<i;
|
||||
// std::cout << ( (!p->required())?"]":"")<<" ";
|
||||
// }else{
|
||||
// std::cout << "["<<p->shortName()<<"] ";
|
||||
// }
|
||||
// } // for p
|
||||
std::cout << "Where:"<<std::endl;
|
||||
|
||||
for ( p=params.begin(); p!=params.end(); p++ ) {
|
||||
if( p->valType() != Param::TITLE ) {
|
||||
// Victor: 04-Jan-2000
|
||||
// Modified because the - and -- prefixes are not needed.
|
||||
/*
|
||||
std::cout << "-" << p->shortName()
|
||||
<<", --"<<p->longName()<<":\t"
|
||||
<<p->description()<<std::endl;
|
||||
*/
|
||||
std::cout << p->shortName()
|
||||
<<", " << p->longName()<<":\t"
|
||||
<<p->description()<<std::endl;
|
||||
std::cout << "\t(";
|
||||
switch ( p->valType() ) {
|
||||
case Param::INT: std::cout <<"Integer"; break;
|
||||
case Param::UL: std::cout <<"Unsigned Long Integer"; break;
|
||||
case Param::FLOAT: std::cout <<"Float"; break;
|
||||
case Param::STRING: std::cout <<"String"; break;
|
||||
case Param::ARRAY: std::cout <<"An array of std::strings, enclosed within < >"; break;
|
||||
case Param::BOOL: std::cout << "Flag"; break;
|
||||
case Param::TITLE: break;
|
||||
} // switch
|
||||
if(p->valType() == Param::BOOL)
|
||||
std::cout << ") True if present" << std::endl;
|
||||
else
|
||||
std::cout<<") "<<( (p->required())?"Required":"Optional" )<<". By default: "<<p->defValue()<<std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "\n\t # " << p->description() << std::endl;
|
||||
}
|
||||
} // for p
|
||||
std::cout << std::endl;
|
||||
};
|
||||
|
||||
/**
|
||||
* This class managges unknown argument std::exceptions.
|
||||
*/
|
||||
class UnknownArg : public std::logic_error {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _arg std::string to be shown when the std::exception occurs
|
||||
*/
|
||||
UnknownArg( const std::string& _arg): std::logic_error( "Invalid argument: "+_arg ) { };
|
||||
};
|
||||
|
||||
/**
|
||||
* This class managges bad param types.
|
||||
*/
|
||||
class BadType : public std::logic_error {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _param The param
|
||||
* @param _value The value of the param
|
||||
*/
|
||||
BadType(const std::string& _param, const std::string& _value, const std::string& _correctType)
|
||||
: std::logic_error("The value '" + _value + "' assigned to the argument " + _param + " isn't a correct "+_correctType) { };
|
||||
};
|
||||
|
||||
/**
|
||||
* This class managges std::exceptions produced when there isn't a value for a parameter.
|
||||
*/
|
||||
class MissingVal : public std::logic_error {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _param The param
|
||||
*/
|
||||
MissingVal(const std::string& _param) : std::logic_error("Missing value for parameter " + _param) {};
|
||||
};
|
||||
|
||||
/**
|
||||
* This class managges std::exceptions produced when the user forgot a required parameter.
|
||||
*/
|
||||
class MissingReqParam : public std::logic_error {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _shortName The param's short name
|
||||
*/
|
||||
MissingReqParam(const std::string& _shortName) : std::logic_error("Missing required parameter " + _shortName) {};
|
||||
};
|
||||
|
||||
/**
|
||||
* This class managges std::exceptions du to < without a > in array value
|
||||
*/
|
||||
class BadArrayParam : public std::logic_error {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param _param The param
|
||||
* @param _first_word The first word read after the "<"
|
||||
*/
|
||||
BadArrayParam(const std::string& _param, const std::string &_first_word) :
|
||||
std::logic_error("Array parameter " + _param + ": No matching > (" + _first_word
|
||||
+ "... )") {};
|
||||
};
|
||||
|
||||
void createParamFile( std::ostream& _os ) {
|
||||
std::vector<Param>::iterator p;
|
||||
for ( p=params.begin(); p!=params.end(); p++ ) {
|
||||
switch( p->valType() ) {
|
||||
case Param::TITLE:
|
||||
_os << std::endl << "# -- ";
|
||||
break;
|
||||
case Param::BOOL:
|
||||
_os << ((p->value()=="true" )?"":"#")
|
||||
<< p->longName();
|
||||
break;
|
||||
case Param::STRING:
|
||||
_os << p->longName()<<"\t\""<<p->value()<<"\"";
|
||||
break;
|
||||
case Param::ARRAY:
|
||||
_os << p->longName()<<"\t< "<<p->value()<<" >";
|
||||
break;
|
||||
default:
|
||||
_os << p->longName()<<"\t"<<p->value();
|
||||
break;
|
||||
} // switch
|
||||
_os << "\t #" << p->description() << std::endl;
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::vector<Param> params;
|
||||
std::string programName;
|
||||
std::string programDescription;
|
||||
int parse_argc;
|
||||
char **parse_argv;
|
||||
std::string InputFileName;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
// See eoParserUtils.h
|
||||
|
||||
#include <iostream.h>
|
||||
#include <eoParserUtils.h>
|
||||
/// Reproducible random seed
|
||||
|
||||
// For the Mersenne-Twister used in EO, the entire rng needs to be saved
|
||||
|
||||
//----------------------------------
|
||||
void InitRandom( Parser & parser) {
|
||||
//----------------------------------
|
||||
unsigned long _seed;
|
||||
try {
|
||||
_seed = parser.getUnsignedLong("-S", "--seed", "0",
|
||||
"Seed for Random number generator" );
|
||||
}
|
||||
catch (logic_error & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (_seed == 0) { // use clock to get a "random" seed
|
||||
_seed = (unsigned long)( time( 0 ) );
|
||||
ostrstream s;
|
||||
s << _seed;
|
||||
parser.setParamValue("--seed", s.str()); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
|
||||
}
|
||||
//#error This does not work: load and save the entire state of the rng object.
|
||||
rng.reseed(_seed);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
/*-------------------------------------------------------
|
||||
File..........: eoParserUtils.h
|
||||
Author........: Geneura Team, Marc Shoenauer
|
||||
(this file: Victor Rivas, vrivas@ujaen.es)
|
||||
Date..........: 17-Dec-1999
|
||||
Description...: Some useful things that use eoParser.
|
||||
Modifications.:
|
||||
------------------- 1 -------------------
|
||||
Author.......:
|
||||
Date.........:
|
||||
Description..:
|
||||
*/
|
||||
|
||||
#ifndef EO_PARSER_UTILS
|
||||
#define EO_PARSER_UTILS
|
||||
|
||||
#include "../utils/eoRNG.h"
|
||||
#include "eoParser.h"
|
||||
/// Reproducible random seed
|
||||
|
||||
//----------------------------------
|
||||
void InitRandom( Parser & parser);
|
||||
//----------------------------------
|
||||
#endif
|
||||
|
|
@ -1,142 +0,0 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoPopOps.h
|
||||
(c) GeNeura Team, 1998
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOPOPOPS_H
|
||||
#define _EOPOPOPS_H
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
@author Geneura Team
|
||||
@version 0.0
|
||||
|
||||
@version 0.1 : -MS- 22/10/99
|
||||
added the added the derived class eoSelectOne for which you only have
|
||||
to define the selection of 1 individual
|
||||
(e.g. for tournament, age selection in SSGA, ...)
|
||||
added at the BASE level (after all it's themost frequen case)
|
||||
the pure virtual operator that selects one single individual:
|
||||
EOT eoSelect::operator ( const eoPop<EOT>& _parents,
|
||||
const EOT& _first = 0)
|
||||
added the optional second parameter to transform::operator()
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoPop.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoTransform is a class that transforms or does something on a population.
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoMonPopOp: public eoObject{
|
||||
|
||||
public:
|
||||
/** ctor */
|
||||
eoMonPopOp() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoMonPopOp(){};
|
||||
|
||||
/// Pure virtual transformation function. Does something on the population
|
||||
virtual void operator () ( eoPop<EOT>& _pop ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn are not overriden
|
||||
*/
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoMonPopOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoSelect usually takes elements from one population, with or without transformation, and transfers them to the other population */
|
||||
template<class EOT>
|
||||
class eoBinPopOp: public eoObject{
|
||||
|
||||
public:
|
||||
/** ctor
|
||||
*/
|
||||
eoBinPopOp() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoBinPopOp(){};
|
||||
|
||||
/** Pure virtual transformation function. Extracts something from the parents,
|
||||
and transfers it to the siblings
|
||||
@param _parents the initial generation. Will be kept constant
|
||||
@param _siblings the created offspring. Will be usually an empty population
|
||||
*/
|
||||
virtual void operator () (eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn are not overriden
|
||||
*/
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoBinPopOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoSelectone selects only one element from a whole population. Usually used to
|
||||
select mates*/
|
||||
template<class EOT>
|
||||
class eoSelectOne: public eoObject{
|
||||
|
||||
public:
|
||||
/** ctor
|
||||
*/
|
||||
eoSelectOne() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoSelectOne(){};
|
||||
|
||||
/** Pure virtual transformation function. Extracts something from the parents,
|
||||
and transfers it to the siblings
|
||||
@param _parents the initial generation. Will be kept constant
|
||||
@param _siblings the created offspring. Will be usually an empty population
|
||||
*/
|
||||
virtual const EOT& operator () ( const eoPop<EOT>& _parents ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn are not overriden
|
||||
*/
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoSelectOne";};
|
||||
//@}
|
||||
|
||||
};
|
||||
#endif
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoProblem.h
|
||||
|
||||
// (c) GeNeura Team 1998
|
||||
|
||||
/*
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOPROBLEM_H
|
||||
#define EOPROBLEM_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> class Problem
|
||||
{
|
||||
public:
|
||||
typedef T Chrom;
|
||||
typedef typename T::Gene Gene;
|
||||
typedef typename T::Fitness Fitness;
|
||||
|
||||
virtual Fitness operator()(const Chrom& chrom) = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EOPROBLEM_H
|
||||
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoProportional.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999, Maarten Keijzer 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
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoProportional_h
|
||||
#define eoProportional_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
#include <utils/selectors.h>
|
||||
#include <eoSelectOne.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoProportional: select an individual proportional to her stored fitness
|
||||
value
|
||||
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoProportional: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
/// Sanity check
|
||||
eoProportional(const eoPop<EOT>& pop = eoPop<EOT>()):
|
||||
total((pop.size() == 0) ? -1.0 : sum_fitness(pop))
|
||||
{
|
||||
if (minimizing_fitness<EOT>())
|
||||
throw std::logic_error("eoProportional: minimizing fitness");
|
||||
}
|
||||
|
||||
void setup(const eoPop<EOT>& _pop)
|
||||
{
|
||||
total = sum_fitness(_pop);
|
||||
}
|
||||
|
||||
/** do the selection, call roulette_wheel.
|
||||
*/
|
||||
const EOT& operator()(const eoPop<EOT>& _pop)
|
||||
{
|
||||
return roulette_wheel(_pop, total) ;
|
||||
}
|
||||
|
||||
private :
|
||||
typename EOT::Fitness total;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
/** -*- 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(getRates());
|
||||
return *operator[](what);
|
||||
}
|
||||
|
||||
///
|
||||
virtual std::string className() const { return "eoGOpSelector"; };
|
||||
|
||||
};
|
||||
|
||||
#endif eoProportionalGOpSel_h
|
||||
|
|
@ -1,158 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoProportionalOpSel.h
|
||||
|
||||
// (c) GeNeura Team 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOPROPORTIONALOPSEL_H
|
||||
#define EOPROPORTIONALOPSEL_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdexcept> // std::runtime_error
|
||||
#include <functional> // greater
|
||||
#include <map>
|
||||
|
||||
// Includes from EO
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOpSelector.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** This class selects operators according to probability. All operator percentages
|
||||
should add up to one; if not, an std::exception will be raised.\\
|
||||
Operators are represented as std::pairs (proportion,operator)
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoProportionalOpSel: public eoOpSelector<EOT>,
|
||||
public multimap<float,eoOp<EOT>*,greater<float> >
|
||||
{
|
||||
public:
|
||||
|
||||
typedef multimap<float, eoOp<EOT>*,greater<float> > MMF;
|
||||
|
||||
/// default ctor
|
||||
eoProportionalOpSel()
|
||||
: eoOpSelector<EOT>(), MMF(), opID(1) {};
|
||||
|
||||
/// virtual dtor
|
||||
virtual ~eoProportionalOpSel() {};
|
||||
|
||||
/** Gets a non-const reference to an operator, so that it can be changed,
|
||||
modified or whatever
|
||||
@param _id a previously assigned ID
|
||||
@throw std::runtime_error if the ID does not exist*/
|
||||
|
||||
virtual eoOp<EOT>& getOp( ID _id ) {
|
||||
MMF::iterator i=begin();
|
||||
ID j = 1;
|
||||
while ( (i++!=end()) && (j++ != _id) );
|
||||
if ( i == end() )
|
||||
throw std::runtime_error( "No such id in eoProportionalOpSel::op\n" );
|
||||
return *(i->second);
|
||||
//return i->second;
|
||||
}
|
||||
|
||||
/** add an operator to the operator set
|
||||
@param _op a genetic operator, that will be applied in some way
|
||||
@param _arg an argument to the operator, usually operator rate
|
||||
@return an ID that will be used to identify the operator
|
||||
*/
|
||||
virtual ID addOp( eoOp<EOT>& _op, float _arg ) {
|
||||
insert( MMF::value_type( _arg,& _op ) );
|
||||
return opID++;
|
||||
}
|
||||
|
||||
/** Remove an operator from the operator set
|
||||
@param _id a previously assigned ID
|
||||
@throw std::runtime_error if the ID does not exist
|
||||
*/
|
||||
virtual void deleteOp( ID _id ) {
|
||||
unsigned j;
|
||||
MMF::iterator i;
|
||||
for ( i=begin(), j=1; i!=end(); i++,j++ ) {
|
||||
if( j == _id )
|
||||
erase( i );
|
||||
return;
|
||||
}
|
||||
if ( i == end() )
|
||||
throw std::runtime_error( "No such id in eoProportionalOpSel::op\n" );
|
||||
};
|
||||
|
||||
/// Returns a genetic operator according to the established criteria
|
||||
virtual eoOp<EOT>* Op() {
|
||||
// Check that all add up to one
|
||||
float acc = 0;
|
||||
MMF::iterator i;
|
||||
unsigned j;
|
||||
for ( i=begin(), j=1; i!=end(); i++,j++ ) {
|
||||
acc +=i->first;
|
||||
}
|
||||
if ( acc != 1.0 )
|
||||
throw std::runtime_error( "Operator rates added up different from 1.0" );
|
||||
|
||||
// If here, operators ordered by rate and no problem
|
||||
float aRnd = rng.uniform();
|
||||
i=begin();
|
||||
acc = 0;
|
||||
do {
|
||||
acc += i->first;
|
||||
} while ( (acc <= aRnd ) && (i++!=end() ) );
|
||||
if ( i == end() )
|
||||
throw std::runtime_error( "Operator not found in eoProportionalOpSelector" );
|
||||
return i->second;
|
||||
//return i->second;
|
||||
}
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
//@{
|
||||
|
||||
/** Return the class id.
|
||||
@return the class name as a std::string
|
||||
*/
|
||||
virtual std::string className() const { return "eoProportionalOpSel"; };
|
||||
|
||||
/** Print itself: inherited from eoObject implementation. Declared virtual so that
|
||||
it can be reimplemented anywhere. Instance from base classes are processed in
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
@param _s the std::ostream in which things are written*/
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
_s << className().c_str() << std::endl;
|
||||
for ( MMF::const_iterator i=begin(); i!=end(); i++ ) {
|
||||
_s << i->first << "\t" << *(i->second )<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
ID opID;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EO_H
|
||||
|
||||
|
|
@ -1,200 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoRandomBreed.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EORANDOMBREED_H
|
||||
#define _EORANDOMBREED_H
|
||||
|
||||
#include <eoSelector.h>
|
||||
|
||||
/** Takes those on the selection std::list and creates a std::list of new individuals
|
||||
* Destroys the genetic pool */
|
||||
template<class EOT>
|
||||
class EORandomBreed: public EOBreeder<EOT>{
|
||||
public:
|
||||
|
||||
typedef std::vector< EOOp<EOT > * > vecOpT;
|
||||
|
||||
/// Ctor
|
||||
EORandomBreed():vecOp() {};
|
||||
|
||||
/** Copy ctor
|
||||
* Needs a copy ctor for the EO operators */
|
||||
EORandomBreed( const EORandomBreed& _rndBreeder)
|
||||
:vecOp() {
|
||||
copy( _rndBreeder.vecOp.begin(), _rndBreeder.vecOp.end(),
|
||||
vecOp.begin() );
|
||||
};
|
||||
|
||||
/// Dtor
|
||||
virtual ~EORandomBreed() {};
|
||||
|
||||
/// Adds a genetic operator to the Breeder with a rate
|
||||
virtual void addOp( EOOp<EOT>* _eop ) {
|
||||
vecOp.push_back( _eop);
|
||||
};
|
||||
|
||||
/// Takes the operator pointed to from the operator std::list
|
||||
virtual void deleteOp( const EOOp<EOT>* _eop);
|
||||
|
||||
/** Takes the genetic pool, and returns next generation, destroying the
|
||||
* genetic pool container
|
||||
* Non-const because it might order the operator std::vector. In this case,
|
||||
* it mates all members of the population randomly */
|
||||
virtual void operator() ( EOPop< EOT >& _ptVeo );
|
||||
|
||||
// I don´t like this, but I need it for the RandomBreedLog
|
||||
protected:
|
||||
vecOpT vecOp;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//_________________________ IMPLEMENTATIONS _____________________________
|
||||
|
||||
template< class EOT>
|
||||
void EORandomBreed<EOT>::deleteOp( const EOOp<EOT>* _eop) {
|
||||
vecOpT::iterator i;
|
||||
for ( i = vecOp.begin(); i != vecOp.end(); i++ ) {
|
||||
if ( *i == _eop ) {
|
||||
vecOp.erase( i );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//________________________________________________________________________
|
||||
template<class EOT>
|
||||
void EORandomBreed<EOT>::operator() ( EOPop< EOT >& _ptVeo ) {
|
||||
|
||||
unsigned select= _ptVeo.size(); // New population same size than old
|
||||
sort( vecOp.begin(), vecOp.end(), SortEOpPt<EOT>() );
|
||||
|
||||
unsigned i;
|
||||
float totalPriority = 0;
|
||||
for ( i = 0; i < vecOp.size(); i ++ ) {
|
||||
totalPriority += vecOp[i]->Priority();
|
||||
}
|
||||
|
||||
unsigned inLen = _ptVeo.size(); // size of in subPop
|
||||
for ( i = 0; i < select; i ++ ) {
|
||||
// Create an alias of a random input EO with copy ctor
|
||||
EOT& newEO = _ptVeo[ i ];
|
||||
|
||||
// Choose operator
|
||||
float randomDraw = totalPriority *(rand() % 1000) /1000.0;
|
||||
vecOpT::const_iterator j;
|
||||
float accumulated = 0;
|
||||
for ( j = vecOp.begin();
|
||||
( j != vecOp.end() ) && ( accumulated < randomDraw);
|
||||
j ++ ) {
|
||||
accumulated+= (*j)->Priority(); // the priority
|
||||
}
|
||||
|
||||
if ( j != vecOp.begin() )
|
||||
j--; // previous one
|
||||
EOOp<EOT >* thisOp = *j;
|
||||
|
||||
if (thisOp->readArity() == unary ) {
|
||||
MonOp<EOT>* mopPt = dynamic_cast< MonOp<EOT>* > ( thisOp );
|
||||
(*mopPt)( newEO );
|
||||
} else {
|
||||
unsigned chosenMatch = rand() % inLen;
|
||||
BinOp<EOT>* bopPt = dynamic_cast< BinOp<EOT>* > ( thisOp );
|
||||
(*bopPt)( newEO, _ptVeo[chosenMatch] );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#include <ADT/EOFactory.h> // For factory
|
||||
|
||||
/** Exactly as RandomBreed, except that uses factories*/
|
||||
template<class EOT>
|
||||
class EORandomBreedLog: public EORandomBreed<EOT>{
|
||||
public:
|
||||
|
||||
typedef std::vector< EOOp<EOT > * > vecOpT;
|
||||
|
||||
/// Ctor
|
||||
EORandomBreedLog( EOFactory<EOT> & _eof ):EORandomBreed<EOT>(), factory( _eof ) {};
|
||||
|
||||
/** Copy ctor
|
||||
* Needs a copy ctor for the EO operators */
|
||||
EORandomBreedLog( const EORandomBreedLog& _rndBreeder)
|
||||
:EORandomBreed<EOT>( _rndBreeder ), factory( _rndBreeder.factory) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~EORandomBreedLog() {};
|
||||
|
||||
/** Takes the genetic pool, and returns next generation, destroying the
|
||||
* genetic pool container
|
||||
* Non-const because it might order the operator std::vector. In this case, it mates
|
||||
* all population randomly */
|
||||
virtual void operator() ( EOPop< EOT >& _ptVeo ) {
|
||||
|
||||
unsigned select= _ptVeo.size(); // New population same size than old
|
||||
sort( vecOp.begin(), vecOp.end(), SortEOpPt<EOT>() );
|
||||
|
||||
unsigned i;
|
||||
float totalPriority = 0;
|
||||
for ( i = 0; i < vecOp.size(); i ++ ) {
|
||||
totalPriority += vecOp[i]->Priority();
|
||||
}
|
||||
|
||||
unsigned inLen = _ptVeo.size(); // size of in subPop
|
||||
for ( i = 0; i < select; i ++ ) {
|
||||
// Create a copy of a random input EO with copy ctor
|
||||
EOT* newEO = factory.make( _ptVeo[ i ] );
|
||||
|
||||
// Choose operator
|
||||
float randomDraw = totalPriority *(rand() % 1000) /1000.0;
|
||||
vecOpT::const_iterator j;
|
||||
float accumulated = 0;
|
||||
for ( j = vecOp.begin();
|
||||
( j != vecOp.end() ) && ( accumulated < randomDraw);
|
||||
j ++ ) {
|
||||
accumulated+= (*j)->Priority(); // the priority
|
||||
}
|
||||
|
||||
if ( j != vecOp.begin() )
|
||||
j--; // previous one
|
||||
EOOp<EOT >* thisOp = *j;
|
||||
|
||||
if (thisOp->readArity() == unary ) {
|
||||
MonOp<EOT>* mopPt = dynamic_cast< MonOp<EOT>* > ( thisOp );
|
||||
(*mopPt)( *newEO );
|
||||
} else {
|
||||
unsigned chosenMatch = rand() % inLen;
|
||||
BinOp<EOT>* bopPt = dynamic_cast< BinOp<EOT>* > ( thisOp );
|
||||
(*bopPt)( *newEO, _ptVeo[chosenMatch] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
private:
|
||||
EOFactory<EOT>& factory;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoRandomIndiSelector.h
|
||||
Selects individuals at random.
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoRandomIndiSelector_h
|
||||
#define eoRandomIndiSelector_h
|
||||
|
||||
#include <eoIndiSelector.h>
|
||||
|
||||
/**
|
||||
\ingroup selectors
|
||||
* eoRandomSelector: just selects a random child
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoRandomIndiSelector : public eoPopIndiSelector<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoRandomIndiSelector(void) : eoPopIndiSelector<EOT>() {}
|
||||
virtual ~eoRandomIndiSelector(void) {}
|
||||
|
||||
/// very complex function that returns just an individual
|
||||
const EOT& do_select(void)
|
||||
{
|
||||
return operator[](rng.random(size()));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoRandomSelect.h
|
||||
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EORANDOMSELECT_H
|
||||
#define EORANDOMSELECT_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric> // for accumulate
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <eoPopOps.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* eoRandomSelect: an selection operator, which selects randomly a percentage
|
||||
|
||||
of the initial population.
|
||||
*/
|
||||
template<class EOT> class eoRandomSelect: public eoBinPopOp<EOT>
|
||||
{
|
||||
public:
|
||||
///
|
||||
eoRandomSelect(const float& _percent = 0.4):
|
||||
eoBinPopOp<EOT>(), repRate(_percent) {};
|
||||
|
||||
///
|
||||
virtual ~eoRandomSelect() {};
|
||||
|
||||
/// Takes a percentage of the population randomly, and transfers it to siblings
|
||||
virtual void operator() ( eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) {
|
||||
// generates random numbers
|
||||
unsigned num_chroms = (unsigned)(repRate * _parents.size());
|
||||
|
||||
// selection of chromosomes
|
||||
do {
|
||||
_siblings.push_back(_parents[rng.random(_parents.size())]);
|
||||
} while (_siblings.size() < num_chroms);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// @name Methods from eoObject
|
||||
|
||||
//@{
|
||||
|
||||
/**
|
||||
|
||||
* Read object. Reads the percentage
|
||||
|
||||
* Should call base class, just in case.
|
||||
|
||||
* @param _s A std::istream.
|
||||
|
||||
*/
|
||||
|
||||
virtual void readFrom(std::istream& _s) {
|
||||
|
||||
_s >> repRate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Print itself: inherited from eoObject implementation. Declared virtual so that
|
||||
|
||||
it can be reimplemented anywhere. Instance from base classes are processed in
|
||||
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
|
||||
@param _s the std::ostream in which things are written*/
|
||||
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
|
||||
_s << repRate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Inherited from eoObject
|
||||
|
||||
@see eoObject
|
||||
|
||||
*/
|
||||
|
||||
std::string className() const {return "eoRandomSelect";};
|
||||
|
||||
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
float repRate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif EOGSRANDOMSELECT_H
|
||||
|
|
@ -1,140 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoRank.h
|
||||
|
||||
// (c) GeNeura Team 1999
|
||||
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _eoRank_H
|
||||
|
||||
#define _eoRank_H
|
||||
|
||||
|
||||
#include <eoOpSelector.h>
|
||||
|
||||
#include <eoPopOps.h>
|
||||
|
||||
|
||||
/**
|
||||
* Takes those on the selection std::list and creates a std::list of new individuals
|
||||
* Destroys the genetic pool. There's no requisite on EOT, other than the
|
||||
* genetic operators can be instantiated with it, so it fully depstd::ends on
|
||||
* the genetic operators used. If generic genetic operators are used, then
|
||||
* EOT must be an EO
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
class eoRank: public eoSelect<EOT>, public eoObject, public eoPrintable
|
||||
{
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoRank( unsigned _newPopSize, eoOpSelector<EOT>& _opSelector)
|
||||
:eoSelect<EOT>(), opSelector( _opSelector ), repNewPopSize( _newPopSize ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoRank() {};
|
||||
|
||||
/** Takes the genetic pool, and returns next generation, destroying the
|
||||
* genetic pool container
|
||||
* Non-const because it might order the operator std::vector*/
|
||||
virtual void operator() ( const eoPop< EOT >& _ptVeo,
|
||||
|
||||
eoPop< EOT >& _siblings ) const {
|
||||
|
||||
unsigned inLen = _ptVeo.size(); // size of subPop
|
||||
if ( !inLen )
|
||||
throw std::runtime_error( "zero population in eoRank");
|
||||
|
||||
for ( unsigned i = 0; i < repNewPopSize; i ++ ) {
|
||||
// Create a copy of a random input EO with copy ctor. The members of the
|
||||
// population will be selected by rank, with a certain probability of
|
||||
// being selected several times if the new population is bigger than the
|
||||
// old
|
||||
|
||||
EOT newEO = _ptVeo[ i%inLen ];
|
||||
|
||||
// Choose operator
|
||||
|
||||
const eoOp<EOT >& thisOp = opSelector.Op();
|
||||
if ( thisOp.readArity() == unary ) {
|
||||
const eoMonOp<EOT>& mopPt = dynamic_cast< const eoMonOp<EOT>& > ( thisOp );
|
||||
mopPt( newEO );
|
||||
} else {
|
||||
const eoBinOp<EOT>& bopPt = dynamic_cast< const eoBinOp<EOT>& > ( thisOp );
|
||||
|
||||
EOT mate = _ptVeo[ rng.random(inLen) ];
|
||||
bopPt( newEO, mate );
|
||||
}
|
||||
|
||||
|
||||
_siblings.push_back( newEO );
|
||||
}
|
||||
};
|
||||
|
||||
/** This is a _new_ function, non defined in the parent class
|
||||
* It´s used to set the selection rate */
|
||||
void select( unsigned _select ) {
|
||||
repNewPopSize = _select;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
|
||||
//@{
|
||||
|
||||
|
||||
|
||||
/** Return the class id.
|
||||
|
||||
@return the class name as a std::string
|
||||
|
||||
*/
|
||||
|
||||
virtual std::string className() const { return "eoRank"; };
|
||||
|
||||
virtual void printOn( std::ostream& _s ) const
|
||||
{
|
||||
_s << repNewPopSize;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
private:
|
||||
eoOpSelector<EOT> & opSelector;
|
||||
unsigned repNewPopSize;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,360 +0,0 @@
|
|||
// eoScheme.h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoScheme.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOSCHEME_H
|
||||
#define _EOSCHEME_H
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
@author Geneura Team -- EEAAX 99
|
||||
@version 0.0
|
||||
Evolution scheme
|
||||
|
||||
|
||||
It seems to me that some important conceptual object is missing
|
||||
(and God knows I hate turning everything into objects!
|
||||
|
||||
So here is the evolution scheme:
|
||||
|
||||
regroups selection (nb of offspring to generate AND selection method
|
||||
and replacement (from parents + offspring)
|
||||
|
||||
allows to include elitism, eugenism of the worst, ... more easily
|
||||
|
||||
a generation is then simply an evolution scheme and some operators
|
||||
and a full algo is a generation + a termination condition
|
||||
|
||||
this is mainly a container class
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPopOps.h>
|
||||
#include "eoStochTournament.h"
|
||||
#include "eoDetTournament.h"
|
||||
#include "eoLottery.h"
|
||||
#include "eoUniformSelect.h"
|
||||
#include "eoInclusion.h"
|
||||
#include "eoESReplace.h"
|
||||
#include "eoEPTournament.h"
|
||||
|
||||
/** eoScheme is a class that does more sophisticated evolution that eoEasyEA
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoScheme: public eoAlgo<EOT>{
|
||||
|
||||
public:
|
||||
|
||||
// Dtor
|
||||
virtual ~eoScheme() {};
|
||||
|
||||
// copy ctor is impossible because of pointers to pure virual types.
|
||||
// any idea???? --- leave the default copy ctor -- JJ
|
||||
|
||||
/** the Parser-based constructor
|
||||
these switch cases could be turned into as many subclasses
|
||||
- but how do you return the subclass to the caller of the constructor???
|
||||
*/
|
||||
eoScheme(Parser & parser) {
|
||||
// read the popsize
|
||||
parser.AddTitle("Description of evolution");
|
||||
std::string Evol;
|
||||
std::string SelectString;
|
||||
// temporary
|
||||
float rate_offspring;
|
||||
|
||||
try {
|
||||
Evol = parser.getString("-EE", "--evolution", "GGA",
|
||||
"Evolution scheme (GGA, SSGA, ESPlus, ESComma, EP, General)" );
|
||||
popsize = parser.getInt("-EP", "--population", "10",
|
||||
"Population size" );
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
// now the big switch
|
||||
if (! strcasecmp(Evol.c_str(), "GGA") ) {
|
||||
// GGA parameters: popsize, selection method (and its parameters)
|
||||
nb_offspring = 0;
|
||||
rate_offspring = 1.0; // generational replacement: #offspring=popsize
|
||||
try {
|
||||
// parser.AddTitle("GGA Parameters");
|
||||
SelectString = parser.getString("-ES", "--selection", "Tournament",
|
||||
"Selection method (Roulette, tournament)" );
|
||||
if (! strcasecmp(SelectString.c_str(), "roulette") ) {
|
||||
ptselect = new eoLottery<EOT> ();
|
||||
ptselect_mate = new eoLottery<EOT> ();
|
||||
}
|
||||
if (! strcasecmp(SelectString.c_str(), "tournament") ) {
|
||||
float rate = parser.getFloat("-Et", "--TselectSize", "2",
|
||||
"Tournament size or rate" );
|
||||
if (rate < 0.5)
|
||||
throw out_of_range("Invalid tournament rate");
|
||||
else if ( rate < 1 ) { // binary stochastic tournament
|
||||
ptselect = new eoStochTournament<EOT>(rate);
|
||||
ptselect_mate = new eoStochTournament<EOT>(rate);
|
||||
} else { // determeinistic tournament of size (int)rate
|
||||
ptselect = new eoDetTournament<EOT>((int)rate);
|
||||
ptselect_mate = new eoDetTournament<EOT>((int)rate);
|
||||
}
|
||||
}
|
||||
// end choice of selection
|
||||
ptreplace = new eoInclusion<EOT>();
|
||||
// put here the choice of elitism
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of GGA
|
||||
|
||||
// SSGA - standard, one offspring only at the moment
|
||||
if (! strcasecmp(Evol.c_str(), "SSGA") ) {
|
||||
// SSGA parameters: popsize, selection tournament size
|
||||
// the replacement is limited to repace_worst, though
|
||||
// it could be easy to add the anti-tournament replacement method
|
||||
nb_offspring = 1;
|
||||
// NOTE: of course it's a bit of a waste to use the standard loop
|
||||
// for one single offspring ...
|
||||
try {
|
||||
// parser.AddTitle("SSGA Parameters");
|
||||
float _rate = parser.getFloat("-ET", "--TSelectSize", "2",
|
||||
"Selection tournament size" );
|
||||
if ( _rate < 1 ) { // binary stochastic tournament
|
||||
ptselect = new eoStochTournament<EOT>(_rate);
|
||||
ptselect_mate = new eoStochTournament<EOT>(_rate);
|
||||
} else { // determeinistic tournament of size (int)rate
|
||||
ptselect = new eoDetTournament<EOT>((int)_rate);
|
||||
ptselect_mate = new eoDetTournament<EOT>((int)_rate);
|
||||
}
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
// end choice of selection
|
||||
ptreplace = new eoInclusion<EOT>();
|
||||
// put here the choice of elitism
|
||||
} // end of SSGA
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "ESPlus") ) {
|
||||
// ES evolution parameters: lambda = _nb_offspring
|
||||
|
||||
try {
|
||||
// parser.AddTitle("ES Scheme parameters");
|
||||
nb_offspring = parser.getInt("-EL", "--lambda", "50",
|
||||
"Lambda" );
|
||||
ptselect = new eoUniformSelect<EOT>();
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
ptreplace = new eoESPlus<EOT>();
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of ESPlus
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "ESComma") ) {
|
||||
// ES evolution parameters: lambda = _nb_offspring
|
||||
|
||||
try {
|
||||
// parser.AddTitle("ES Scheme parameters");
|
||||
nb_offspring = parser.getInt("-EL", "--lambda", "50",
|
||||
"Lambda" );
|
||||
ptselect = new eoUniformSelect<EOT>();
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
ptreplace = new eoESComma<EOT>();
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of ESCOmma
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "EP") ) {
|
||||
// EP evoltion scheme: only the EP-tournament replacement size is neede
|
||||
|
||||
try {
|
||||
// parser.AddTitle("EP Scheme parameters");
|
||||
nb_offspring = popsize;
|
||||
ptselect = new eoCopySelect<EOT>; /* no selection */
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
/* What, crossover in EP :-) */
|
||||
unsigned tsize = parser.getInt("-ET", "--TournamentSize", "6",
|
||||
"Size of stocahstic replacement tournament" );
|
||||
ptreplace = new eoEPTournament<EOT>(tsize);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of EP
|
||||
|
||||
|
||||
// everyting is read: now the consistency checks and other preliminary steps
|
||||
nb_offspring = (nb_offspring ? nb_offspring :
|
||||
(int) (rate_offspring * popsize) );
|
||||
if (!nb_offspring)
|
||||
nb_offspring = 1; /* al least one offspring */
|
||||
|
||||
}
|
||||
|
||||
|
||||
// accessors
|
||||
unsigned PopSize(){return popsize;}
|
||||
unsigned NbOffspring(){return nb_offspring ;}
|
||||
eoSelect<EOT>* PtSelect(){return ptselect;}
|
||||
eoSelect<EOT>* PtSelectMate(){return ptselect_mate;}
|
||||
eoMerge<EOT>* PtMerge() {return ptreplace;}
|
||||
// NOTE: need pointers otherwise you have many warnings when initializing
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoScheme";};
|
||||
//@}
|
||||
private:
|
||||
unsigned popsize; /* but should it be here ??? */
|
||||
unsigned nb_offspring; /* to generate through selection+operators */
|
||||
|
||||
// these are provisional for later use
|
||||
|
||||
// float rate_offspring; /* or rate */
|
||||
// unsigned nb_survive; /* the best guys that are transmitted anyway */
|
||||
// float rate_survive; /* or rate */
|
||||
// unsigned nb_die; /* the worst guys that do not enev enter selection */
|
||||
// float rate_die; /* or rate */
|
||||
|
||||
eoSelect<EOT> * ptselect;
|
||||
eoSelect<EOT>* ptselect_mate;
|
||||
eoMerge<EOT>* ptreplace;
|
||||
bool elitism; /* put back old best in the new population if necessary */
|
||||
};
|
||||
/* examples:
|
||||
for most populat schemes, nb_survive = nb_die = 0
|
||||
in GGA and EP, nb_offspring = pop.size()
|
||||
in ES, nb_offspring = lambda
|
||||
in SSGA, nb_offspring = 1 (usually)
|
||||
|
||||
elitism can be used anywhere - though stupid in ES, EP and SSGA who are
|
||||
estd::list by definition
|
||||
*/
|
||||
|
||||
#endif _EOSCHEME_H
|
||||
|
||||
/*
|
||||
/////////////////////////////////
|
||||
/// Applies one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<EOT>& pop) {
|
||||
// Determine the number of offspring to create
|
||||
// either prescribed, or given as a rate
|
||||
unsigned nb_off_local = (nb_offspring ? nb_offspring :
|
||||
(int) rint (rate_offspring * pop.size()) );
|
||||
nb_off_local = (nb_off_local ? nb_off_local : 1); // in case it is rounded to 0!
|
||||
|
||||
// the worst die immediately
|
||||
unsigned nb_die_local = (nb_die ? nb_die :
|
||||
(int) rint (rate_die * pop.size()) );
|
||||
// and the best will survive without selection
|
||||
unsigned nb_survive_local = (nb_survive ? nb_survive :
|
||||
(int) rint (rate_survive * pop.size()) );
|
||||
|
||||
// before selection, erase the one to die
|
||||
// sort old pop - just in case!
|
||||
sort(pop.begin(), pop.end(), greater<EOT>());
|
||||
Fitness oldBest = pop[0].fitness(); // store for elitism
|
||||
eoPop<EOT> fertilepop = pop;
|
||||
if (nb_die_local)
|
||||
erase(fertilepop.end()-nb_die_local, fertilepop.end());
|
||||
|
||||
eoPop<EOT> offspring; // = select(fertilepop, nb_off_local);
|
||||
select(fertilepop, offspring, nb_off_local);
|
||||
|
||||
// now apply the operators to offspring
|
||||
for (unsigned i=0; i<nb_local; i++) {
|
||||
EOT tmp = offspring[i];
|
||||
unsigned id = 0; // first operator
|
||||
eoOp<EOT>* op = seqselop.Op(&id);
|
||||
while (op) { // NULL if no more operator
|
||||
EOT mate;
|
||||
if (op->readArity() == binary) // can eventually be skipped
|
||||
mate = select_mate(pop, tmp); // though useless ig mutation
|
||||
else
|
||||
mate = tmp; // assumed: mate will not be used!
|
||||
std::cout << op->className() << " for offspring " << i << std::endl;
|
||||
tmp = (*op)( tmp, mate, pop );
|
||||
op = seqselop.Op(&id);
|
||||
}
|
||||
offspring[i]=tmp; //* where it belongs
|
||||
}
|
||||
|
||||
eoPop<EOT>::iterator i;
|
||||
// Can't use foreach here since foreach takes the
|
||||
// parameter by reference
|
||||
for ( i = offspring.begin(); i != offspring.end(); i++)
|
||||
evaluator(*i);
|
||||
|
||||
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
||||
// not exact - later!!!
|
||||
// -MS-
|
||||
|
||||
// first, copy the ones that need to survive
|
||||
// assumed: pop is still sorted!
|
||||
eoPop<EOT> finalPop;
|
||||
// and the best survive without selection
|
||||
if (nb_survive_local) {
|
||||
finalPop.resize(nb_survive_local);
|
||||
copy( finalPop.begin(), fertilepop.begin(),
|
||||
fertilepop.begin()+nb_survive_local );
|
||||
}
|
||||
|
||||
// now call the replacement method
|
||||
replace(finalPop, tmpPop);
|
||||
|
||||
// handle elitlism
|
||||
sort(finalPop.begin(), finalPop.end(), greater<EOT>());
|
||||
if (elitism) {
|
||||
if ( finalPop[0].fitness() < oldBest ) // best fitness has decreased!
|
||||
copy(finalPop.end()-1, pop[0]);
|
||||
}
|
||||
// return finalPop;
|
||||
}
|
||||
*/
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSelectRandom.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999, Maarten Keijzer 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
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoSelectRandom_h
|
||||
#define eoSelectRandom_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoSelectOne.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoSelectRandom: a selection method that selects ONE individual randomly
|
||||
-MS- 22/10/99 */
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoSelectRandom: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
|
||||
/// not a big deal!!!
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||
{
|
||||
return pop[rng.random(pop.size())] ;
|
||||
}
|
||||
};
|
||||
|
||||
#endif eoSelectRandom_h
|
||||
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoSequentialGOpSel.h
|
||||
Sequential Generalized Operator Selector.
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com), 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 eoSequentialGOpSel_h
|
||||
#define eoSequentialGOpSel_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoGOpSelector.h>
|
||||
/** eoSequentialGOpSel: return a sequence of
|
||||
operations to be applied one after the other. The order of the
|
||||
operators is significant. If for instance you first add a
|
||||
quadratic operator and then a mutation operator,
|
||||
|
||||
@see eoGeneralOp, eoCombinedOp
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoSequentialGOpSel : public eoGOpSelector<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
virtual eoGeneralOp<EOT>& selectOp()
|
||||
{
|
||||
return combined.bind(*this, getRates());
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
eoCombinedOp<EOT> combined;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,105 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSimpleDEA.h
|
||||
// (c) Marc Schoenauer, Maarten Keijzer, 2001
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoSimpleDEA_h
|
||||
#define _eoSimpleDEA_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <apply.h>
|
||||
#include <eoDEA.h>
|
||||
#include <eoContinue.h>
|
||||
#include <eoDistribUpdater.h>
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
/** eoSimpleDEA: a very simple Distribution Evolution Algorithm
|
||||
*
|
||||
* The algorithm that evolves a probability distribution
|
||||
* on the spaces of populations with the loop
|
||||
* generate a population from the current distribution
|
||||
* evaluate that population
|
||||
* update the distribution
|
||||
*/
|
||||
|
||||
template<class EOT> class eoSimpleDEA: public eoDEA<EOT>
|
||||
{
|
||||
public:
|
||||
|
||||
/** Ctor from an eoDistribUpdater
|
||||
* ... plus an eoEval and eoContinue of course
|
||||
*/
|
||||
eoSimpleDEA(eoDistribUpdater<EOT>& _update,
|
||||
eoEvalFunc<EOT>& _eval,
|
||||
unsigned _popSize,
|
||||
eoContinue<EOT>& _continuator
|
||||
) :
|
||||
update(_update),
|
||||
eval(_eval),
|
||||
popSize(_popSize),
|
||||
continuator(_continuator)
|
||||
{}
|
||||
|
||||
/** The algorithm:
|
||||
* generate pop from distrib,
|
||||
* evaluate pop,
|
||||
* update distrib
|
||||
*/
|
||||
virtual void operator()(eoDistribution<EOT>& _distrib)
|
||||
{
|
||||
eoPop<EOT> pop(popSize, _distrib);
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
apply<EOT>(_distrib, pop); // re-init. of _pop from distrib
|
||||
|
||||
apply<EOT>(eval, pop); // eval of current population
|
||||
|
||||
update(_distrib, pop); // updates distrib from _pop
|
||||
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::string s = e.what();
|
||||
s.append( " in eoSimpleDEA");
|
||||
throw std::runtime_error( s );
|
||||
}
|
||||
} while ( continuator( pop ) );
|
||||
}
|
||||
|
||||
private:
|
||||
eoDistribUpdater<EOT> & update;
|
||||
|
||||
eoEvalFunc<EOT>& eval;
|
||||
|
||||
unsigned popSize;
|
||||
|
||||
eoContinue<EOT>& continuator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSteadyStateEA.h
|
||||
// (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 _eoSteadyStateEA_h
|
||||
#define _eoSteadyStateEA_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoAlgo.h>
|
||||
#include <eoSteadyStateTransform.h>
|
||||
|
||||
/** EOSteadyStateEA:
|
||||
An easy-to-use evolutionary algorithm, just supply
|
||||
a general operator selector, a selector for choosing the ones
|
||||
to reproduce and an eoSteadyStateInserter that takes care of evaluating
|
||||
and inserter the guy/girl in the steady state population.
|
||||
*/
|
||||
template<class EOT> class eoSteadyStateEA: public eoAlgo<EOT>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoSteadyStateEA(
|
||||
eoGOpSelector<EOT>& _opSelector,
|
||||
eoSelectOne<EOT>& _selector,
|
||||
eoSteadyStateInserter<EOT>& _inserter,
|
||||
eoContinue<EOT>& _continuator,
|
||||
unsigned _steps = 0 )
|
||||
: step(_opSelector, _selector, _inserter, _steps),
|
||||
continuator( _continuator)
|
||||
{};
|
||||
|
||||
/// Constructor from an already created generation
|
||||
eoSteadyStateEA(eoSteadyStateTransform<EOT>& _gen,
|
||||
eoContinue<EOT>& _continuator):
|
||||
step(_gen),
|
||||
continuator( _continuator){};
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<EOT>& pop) {
|
||||
do {
|
||||
try
|
||||
{
|
||||
step(pop);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::string s = e.what();
|
||||
s.append( " in eoSteadyStateEA ");
|
||||
throw std::runtime_error( s );
|
||||
}
|
||||
} while ( continuator( pop ) );
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
eoSteadyStateTransform<EOT> step;
|
||||
eoContinue<EOT>& continuator;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoEasyEA_h
|
||||
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoSteadyStateInserter.h
|
||||
Still abstract population insertion operator that is initialized with
|
||||
and eoEvalFunc object to be able to evaluate individuals before inserting
|
||||
them.
|
||||
|
||||
(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 eoSteadyStateInserter_h
|
||||
#define eoSteadyStateInserter_h
|
||||
|
||||
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoInserter.h>
|
||||
|
||||
/**
|
||||
* eoSteadyStateInserter: Interface class that enables an operator to update
|
||||
* a population with a new individual... it contains an eoEvalFunc derived object to
|
||||
* make sure that every individual is evaluated before it is inserted
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoSteadyStateInserter : public eoPopInserter<EOT>
|
||||
{
|
||||
public :
|
||||
eoSteadyStateInserter(eoEvalFunc<EOT>& _eval):
|
||||
eoPopInserter<EOT>(),
|
||||
eval(_eval) {}
|
||||
|
||||
protected :
|
||||
eoEvalFunc<EOT>& eval;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSteadyStateTransform.h
|
||||
// (c) Maarten Keijzer 2000, 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 eoSteadyStateTransform_h
|
||||
#define eoSteadyStateTransform_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
#include <eoGOpSelector.h>
|
||||
#include <eoIndiSelector.h>
|
||||
#include <eoSteadyStateInserter.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoSteadyStateTransform
|
||||
* Single step of a steady state evolutionary algorithm.
|
||||
* Proceeds by updating one individual at a time, by first selecting parents,
|
||||
* creating one or more children and subsequently overwrite (a) bad individual(s)
|
||||
*/
|
||||
template<class EOT> class eoSteadyStateTransform: public eoTransform<EOT>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoSteadyStateTransform(
|
||||
eoGOpSelector<EOT>& _opSelector,
|
||||
eoSelectOne<EOT>& _selector,
|
||||
eoSteadyStateInserter<EOT>& _inserter,
|
||||
unsigned _steps = 0) :
|
||||
opSelector(_opSelector),
|
||||
selector(_selector),
|
||||
inserter(_inserter) ,
|
||||
steps(_steps) {};
|
||||
|
||||
|
||||
/// Apply one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
unsigned nSteps = steps;
|
||||
if (nSteps == 0)
|
||||
{
|
||||
nSteps = pop.size(); // make a 'generation equivalent'
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < nSteps; ++i)
|
||||
{
|
||||
selector.bind(pop);
|
||||
inserter.bind(pop);
|
||||
|
||||
opSelector.selectOp()(selector, inserter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
eoGOpSelector<EOT>& opSelector;
|
||||
eoSelectOneIndiSelector<EOT> selector;
|
||||
eoSteadyStateInserter<EOT>& inserter;
|
||||
unsigned steps;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoGeneration_h
|
||||
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoStochTournament.h
|
||||
// (c) GeNeura Team, 1998 - EEAAX 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoStochTournament_h
|
||||
#define eoStochTournament_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <functional>
|
||||
#include <numeric> // accumulate
|
||||
#include <eoSelectOne.h> // eoSelectOne
|
||||
#include <utils/selectors.h> // stochastic_tournament
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoStochTournament: a selection method that selects ONE individual by
|
||||
binary stochastic tournament
|
||||
-MS- 24/10/99 */
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoStochTournament: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
|
||||
///
|
||||
eoStochTournament(float _Trate = 1.0 ) : eoSelectOne<EOT>(), Trate(_Trate)
|
||||
{
|
||||
// consistency check
|
||||
if (Trate < 0.5) {
|
||||
std::cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
|
||||
Trate = 0.55;
|
||||
}
|
||||
}
|
||||
|
||||
/** Perform the stochastic tournament */
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||
{
|
||||
return stochastic_tournament(pop, Trate);
|
||||
}
|
||||
|
||||
private:
|
||||
float Trate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoDetTournament_h
|
||||
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoStochTournamentInserter.h
|
||||
Concrete steady state inserter. It is initialized with a population and
|
||||
inserts individuals in the population based on an inverse stochastic
|
||||
tournament
|
||||
|
||||
(c) Maarten Keijzer (mkeijzer@mad.scientist.com) 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 eoStochTournamentInserter_h
|
||||
#define eoStochTournamentInserter_h
|
||||
|
||||
|
||||
#include <eoSteadyStateInserter.h>
|
||||
#include <utils/selectors.h>
|
||||
|
||||
/**
|
||||
\ingroup inserters
|
||||
* eoStochTournamentInserter: Uses an inverse stochastic tournament to figure
|
||||
* out who gets overridden by the new individual. It resets the fitness of the
|
||||
* individual.
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoStochTournamentInserter : public eoSteadyStateInserter<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
eoStochTournamentInserter(eoEvalFunc<EOT>& _eval, double _t_rate):
|
||||
eoSteadyStateInserter<EOT>(_eval), t_rate(_t_rate)
|
||||
{
|
||||
if (t_rate < 0.5)
|
||||
{ // warning, error?
|
||||
t_rate = 0.55;
|
||||
}
|
||||
|
||||
if (t_rate >= 1.0)
|
||||
{
|
||||
t_rate = 0.99; // 1.0 would mean deterministic tournament
|
||||
}
|
||||
}
|
||||
|
||||
eoInserter<EOT>& operator()(const EOT& _eot)
|
||||
{
|
||||
EOT& eo = inverse_stochastic_tournament<EOT>(pop(), t_rate);
|
||||
eo = _eot; // overwrite loser of tournament
|
||||
|
||||
eo.invalidate();
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string className(void) const { return "eoStochTournamentInserter"; }
|
||||
|
||||
private :
|
||||
double t_rate;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoStringMutation.h
|
||||
// (c) GeNeura Team, 1999
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOSTRINGMUTATION_H
|
||||
#define _EOSRTINGMUTATION_H
|
||||
|
||||
#include <math.h>
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoMutation.h>
|
||||
|
||||
/** Mutation of an eoString.
|
||||
The eoString's genes are changed by adding or substracting 1 to
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
class eoStringMutation: public eoMutation<EOT> {
|
||||
public:
|
||||
|
||||
///
|
||||
eoStringMutation(const double _rate=0.0) : eoMutation< EOT >(_rate) {};
|
||||
|
||||
///
|
||||
virtual ~eoStringMutation() {};
|
||||
|
||||
/** @name Methods from eoObject
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoStringMutation";};
|
||||
//@}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Type Type;
|
||||
#else
|
||||
typedef typename EOT::Type Type;
|
||||
#endif
|
||||
|
||||
/// applies operator to one gene in the EO. It increments or decrements the value of that gene by one.
|
||||
virtual void applyAt( EOT& _eo, unsigned _i ) const {
|
||||
if( rate < uniform() ) {
|
||||
_eo.gene(_i) += ( rng.flip(0.5))? (1) : (-1) ;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoTerm.h
|
||||
// (c) 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 _EOTERM_H
|
||||
#define _EOTERM_H
|
||||
|
||||
//#include <eoPop.h>
|
||||
|
||||
// forward definition for fast(er) compilation
|
||||
template <class EOT> class eoPop;
|
||||
|
||||
|
||||
/** Termination condition for the genetic algorithm
|
||||
* Takes the population as input, returns true for continue,
|
||||
* false for termination (although this begs the question why this
|
||||
* terminator is not called a continuator)
|
||||
*
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoTerm : public eoObject {
|
||||
public:
|
||||
|
||||
/// Ctors/dtors
|
||||
virtual ~eoTerm() {};
|
||||
|
||||
/** Returns false if the training has to stop, true if it
|
||||
continues \\
|
||||
It is non-const since it might change the internal state
|
||||
of the object, for instance, updating local data.
|
||||
*/
|
||||
virtual bool operator() ( const eoPop< EOT >& _pop ) = 0 ;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,157 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoTournament.h
|
||||
|
||||
// (c) GeNeura Team, 1998
|
||||
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _EOGSTOURN_H
|
||||
#define _EOGSTOURN_H
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoPopOps.h>
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
/** Selects those who are going to reproduce using Tournament selection:
|
||||
|
||||
a subset of the population of size tournamentSize is chosen,
|
||||
|
||||
and the best is selected for the new population .
|
||||
@author JJ Merelo, 1998
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoTournament:public eoBinPopOp<EOT>{
|
||||
public:
|
||||
|
||||
/// Proportion of guys that are going to be eliminated
|
||||
eoTournament( float _perc, unsigned _tSize):
|
||||
eoBinPopOp<EOT>(), perc( _perc), repTournamentSize(_tSize){};
|
||||
|
||||
/// Virtual dtor
|
||||
~eoTournament(){};
|
||||
|
||||
/// Set tourn size
|
||||
void tournamentSize( unsigned _size ) { repTournamentSize = _size; };
|
||||
|
||||
/**
|
||||
* Selects from the initial pop using tournament selection, and copies it
|
||||
|
||||
* to the other population.
|
||||
*/
|
||||
virtual void operator() ( eoPop<EOT>& _vEO, eoPop<EOT>& _aVEO) {
|
||||
|
||||
unsigned thisSize = _vEO.size();
|
||||
|
||||
// Build std::vector
|
||||
for ( unsigned j = 0; j < thisSize*perc; j ++ ) {
|
||||
// Randomly select a tournamentSize set, and choose the best
|
||||
eoPop<EOT> veoTournament;
|
||||
|
||||
for ( unsigned k = 0; k < repTournamentSize; k++ ) {
|
||||
unsigned chosen = rng.random(thisSize);
|
||||
EOT newEO = _vEO[chosen];
|
||||
veoTournament.push_back( newEO );
|
||||
}
|
||||
|
||||
eoPop<EOT>::const_iterator best = std::max_element(veoTournament.begin(),
|
||||
veoTournament.end());
|
||||
if (best == veoTournament.end()) {
|
||||
throw std::runtime_error("error in void eoTournament::operator(eoPop<EOT>&, eoPop<EOT>&)");
|
||||
}
|
||||
|
||||
// The best individual is chosen for the new population
|
||||
_aVEO.push_back( *best );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// @name Methods from eoObject
|
||||
|
||||
//@{
|
||||
|
||||
/**
|
||||
|
||||
* Read object. Reads the percentage
|
||||
|
||||
* Should call base class, just in case.
|
||||
|
||||
* @param _s A std::istream.
|
||||
|
||||
*/
|
||||
|
||||
virtual void readFrom(std::istream& _s) {
|
||||
|
||||
_s >> perc >> repTournamentSize;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Print itself: inherited from eoObject implementation. Declared virtual so that
|
||||
|
||||
it can be reimplemented anywhere. Instance from base classes are processed in
|
||||
|
||||
base classes, so you don´t have to worry about, for instance, fitness.
|
||||
|
||||
@param _s the std::ostream in which things are written*/
|
||||
|
||||
virtual void printOn( std::ostream& _s ) const{
|
||||
|
||||
_s << perc << std::endl << repTournamentSize << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Inherited from eoObject
|
||||
|
||||
@see eoObject
|
||||
|
||||
*/
|
||||
|
||||
std::string className() const {return "eoTournament";};
|
||||
|
||||
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
float perc;
|
||||
unsigned repTournamentSize;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoTranspose.h
|
||||
// (c) GeNeura Team, 1998 Maarten Keijzer 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 _EOTRANSPOSE_h
|
||||
#define _EOTRANSPOSE_h
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOp.h>
|
||||
#include <eoFixedLength.h>
|
||||
#include <eoVariableLength.h>
|
||||
/**
|
||||
Transposition operator: interchanges the position of two genes
|
||||
of an EO.
|
||||
*/
|
||||
template <class EOT >
|
||||
class eoTranspose: public eoMonOp<EOT>
|
||||
{
|
||||
public:
|
||||
|
||||
// Specialization for a std::vector
|
||||
void operator()(eoFixedLength<typename EOT::Fitness, typename EOT::AtomType>& _eo )
|
||||
{
|
||||
unsigned pos1 = rng.random(_eo.size()),
|
||||
pos2 = rng.random(_eo.size());
|
||||
|
||||
if (pos1 != pos2)
|
||||
swap(_eo[pos1], _eo[pos2]);
|
||||
|
||||
if (_eo[pos1] != _eo[pos2])
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
// Specialization for a std::list
|
||||
void operator()(eoVariableLength<typename EOT::Fitness, typename EOT::AtomType>& _eo )
|
||||
{
|
||||
unsigned pos1 = rng.random(_eo.size()),
|
||||
pos2 = rng.random(_eo.size());
|
||||
|
||||
if (pos1 == pos2)
|
||||
return;
|
||||
|
||||
if (pos1 > pos2)
|
||||
swap(pos1,pos2);
|
||||
|
||||
pos2 -= pos1;
|
||||
|
||||
typename EOT::iterator it1 = _eo.begin();
|
||||
|
||||
while (pos1--) {it1++;}
|
||||
|
||||
typename EOT::iterator it2 = it1;
|
||||
|
||||
while (pos2--) {it2++;}
|
||||
|
||||
swap(*it1, *it2);
|
||||
|
||||
if (*it1 != *it2)
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
virtual std::string className() const {return "eoTranspose";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,176 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoUniformXOver.h
|
||||
|
||||
// (c) GeNeura Team, 1998
|
||||
|
||||
/*
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
|
||||
License as published by the Free Software Foundation; either
|
||||
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
|
||||
Lesser General Public License for more details.
|
||||
|
||||
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
|
||||
License along with this library; if not, write to the Free Software
|
||||
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _EOUNIFORMXOVER_h
|
||||
|
||||
#define _EOUNIFORMXOVER_h
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for swap
|
||||
|
||||
#if defined( __BORLANDC__ )
|
||||
|
||||
#include <algorith>
|
||||
|
||||
#else
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// EO includes
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
* EOUniformCrossover: operator for binary chromosomes
|
||||
|
||||
* implementation of uniform crossover for EO
|
||||
|
||||
* swaps ranges of bits between the parents
|
||||
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
template<class EOT>
|
||||
|
||||
class eoUniformXOver: public eoQuadraticOp< EOT >
|
||||
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
|
||||
|
||||
///
|
||||
|
||||
eoUniformXOver( float _rate = 0.5 ):
|
||||
|
||||
eoQuadraticOp< EOT > ( ), rate( _rate ) {
|
||||
|
||||
if (rate < 0 || rate > 1)
|
||||
|
||||
std::runtime_error("UxOver --> invalid rate");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
///
|
||||
|
||||
void operator() ( EOT& chrom1, EOT& chrom2 ) const {
|
||||
|
||||
unsigned end = min(chrom1.length(),chrom2.length()) - 1;
|
||||
|
||||
// select bits to change
|
||||
|
||||
// aply changes
|
||||
|
||||
for (unsigned bit = 0; bit < end; bit++)
|
||||
|
||||
if (rng.flip(rate))
|
||||
|
||||
swap(chrom1[ bit], chrom2[ bit]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @name Methods from eoObject
|
||||
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
|
||||
*/
|
||||
|
||||
//@{
|
||||
|
||||
/** Inherited from eoObject
|
||||
|
||||
@see eoObject
|
||||
|
||||
*/
|
||||
|
||||
std::string className() const {return "eoUniformXOver";};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
float rate; /// rate of uniform crossover
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoVector.h
|
||||
Turns an STL std::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
|
||||
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 _eoVector_H
|
||||
#define _eoVector_H
|
||||
|
||||
// STL libraries
|
||||
#include <vector> // For std::vector<int>
|
||||
#include <stdexcept>
|
||||
#include <strstream>
|
||||
|
||||
#include <eo1d.h>
|
||||
#include <eoRnd.h>
|
||||
|
||||
/** Adaptor that turns an STL std::vector into an EO
|
||||
with the same gene type as the type with which
|
||||
the std::vector has been instantiated
|
||||
*/
|
||||
template <class T, class fitnessT=float>
|
||||
class eoVector: public eo1d<T, fitnessT>, public std::vector<T> {
|
||||
public:
|
||||
typedef T Type ;
|
||||
|
||||
/// Canonical part of the objects: several ctors, copy ctor, dtor and assignment operator
|
||||
//@{
|
||||
|
||||
/** Ctor.
|
||||
@param _size Lineal length of the object
|
||||
@param _val Common initial value
|
||||
*/
|
||||
eoVector( unsigned _size = 0, T _val = 0)
|
||||
: eo1d<T, fitnessT>(), std::vector<T>( _size, _val ){ };
|
||||
|
||||
/** Ctor using a random number generator
|
||||
@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 );
|
||||
|
||||
/** Ctor from a std::istream. The T class should accept reading from a std::istream. It doesn't read fitness,
|
||||
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( std::istream& _is);
|
||||
|
||||
|
||||
/// copy ctor
|
||||
eoVector( const eoVector & _eo )
|
||||
: eo1d<T, fitnessT>( _eo ), std::vector<T>( _eo ){ };
|
||||
|
||||
/// Assignment operator
|
||||
const eoVector& operator =( const eoVector & _eo ) {
|
||||
if ( this != &_eo ){
|
||||
eo1d<T, fitnessT>::operator=( _eo );
|
||||
std::vector<T>::operator=( _eo );
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// dtor
|
||||
virtual ~eoVector() {};
|
||||
|
||||
//@}
|
||||
|
||||
/** methods that implement the eo1d <em>protocol</em>
|
||||
@std::exception out_of_range if _i is larger than EO´s size
|
||||
*/
|
||||
virtual T getGene( unsigned _i ) const {
|
||||
if ( _i >= length() )
|
||||
throw out_of_range( "out_of_range when reading gene");
|
||||
return (*this)[_i];
|
||||
};
|
||||
|
||||
/** methods that implement the eo1d <em>protocol</em>
|
||||
@std::exception out_of_range if _i is larger than EO´s size
|
||||
*/
|
||||
virtual void setGene( unsigned _i, const T& _value ) {
|
||||
if ( _i >= size() )
|
||||
throw out_of_range( "out_of_range when writing a gene");
|
||||
(*this)[_i] = _value;
|
||||
};
|
||||
|
||||
/** methods that implement the eo1d <em>protocol</em>
|
||||
@std::exception out_of_range if _i is larger than EO´s size
|
||||
*/
|
||||
virtual void insertGene( unsigned _i, T _val ) {
|
||||
if (_i <= size() ) {
|
||||
std::vector<T>::iterator i = begin()+_i;
|
||||
insert( i, _val );
|
||||
} else {
|
||||
throw out_of_range( "out_of_range when inserting a gene");
|
||||
}
|
||||
};
|
||||
|
||||
/** Eliminates the gene at position _i
|
||||
@std::exception out_of_range if _i is larger than EO´s size
|
||||
*/
|
||||
virtual void deleteGene( unsigned _i ) {
|
||||
if (_i < this->size() ) {
|
||||
std::vector<T>::iterator i = this->begin()+_i;
|
||||
this->erase( i );
|
||||
} 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(); };
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eo1d
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoVector";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
|
||||
//____________________________ Some method implementation _______________________
|
||||
|
||||
// Ctors______________________________________________________________________________
|
||||
//____________________________________________________________________________________
|
||||
template <class T, class fitnessT>
|
||||
eoVector<T,fitnessT>::eoVector( unsigned _size, eoRnd<T>& _rnd )
|
||||
: eo1d<T, fitnessT>(), std::vector<T>( _size ){
|
||||
for ( iterator i = begin(); i != end(); i ++ ) {
|
||||
*i = _rnd();
|
||||
}
|
||||
};
|
||||
|
||||
//____________________________________________________________________________________
|
||||
template <class T, class fitnessT>
|
||||
eoVector<T,fitnessT>::eoVector( std::istream& _is)
|
||||
: eo1d<T, fitnessT>(), std::vector<T>( ){
|
||||
while (_is ) {
|
||||
T tmp;
|
||||
_is >> tmp;
|
||||
push_back( tmp );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,200 +0,0 @@
|
|||
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
eoWrappedOps.h
|
||||
Derived from the General genetic operator, which can be used to wrap any unary or binary
|
||||
operator. File also contains the eoCombinedOp, needed by the eoSequentialGOpSelector
|
||||
|
||||
(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 eoWrappedOps_h
|
||||
#define eoWrappedOps_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Wraps monary operators
|
||||
template <class EOT>
|
||||
class eoWrappedMonOp : public eoGeneralOp<EOT>
|
||||
{
|
||||
public :
|
||||
///
|
||||
eoWrappedMonOp(eoMonOp<EOT>& _op) : eoGeneralOp<EOT>(), op(_op) {};
|
||||
|
||||
///
|
||||
virtual ~eoWrappedMonOp() {}
|
||||
|
||||
/// Instantiates the abstract method
|
||||
void operator()( eoIndiSelector<EOT>& _in,
|
||||
eoInserter<EOT>& _out)
|
||||
{
|
||||
EOT result = _in();
|
||||
op( result );
|
||||
_out(result);
|
||||
}
|
||||
|
||||
private :
|
||||
eoMonOp<EOT>& op;
|
||||
};
|
||||
|
||||
|
||||
/// Wraps binary operators
|
||||
template <class EOT>
|
||||
class eoWrappedBinOp : public eoGeneralOp<EOT>
|
||||
{
|
||||
public :
|
||||
///
|
||||
eoWrappedBinOp(eoBinOp<EOT>& _op) : eoGeneralOp<EOT>(), op(_op) {}
|
||||
|
||||
///
|
||||
virtual ~eoWrappedBinOp() {}
|
||||
|
||||
/// Instantiates the abstract method. EOT should have copy ctor.
|
||||
void operator()(eoIndiSelector<EOT>& _in,
|
||||
eoInserter<EOT>& _out)
|
||||
{
|
||||
EOT out1 = _in();
|
||||
const EOT& out2 = _in();
|
||||
op(out1, out2);
|
||||
_out(out1);
|
||||
}
|
||||
|
||||
private :
|
||||
eoBinOp<EOT>& op;
|
||||
};
|
||||
|
||||
/// Wraps Quadratic operators
|
||||
template <class EOT>
|
||||
class eoWrappedQuadraticOp : public eoGeneralOp<EOT>
|
||||
{
|
||||
public :
|
||||
///
|
||||
eoWrappedQuadraticOp(eoQuadraticOp<EOT>& _op) : eoGeneralOp<EOT>(), op(_op) {}
|
||||
|
||||
///
|
||||
virtual ~eoWrappedQuadraticOp() {}
|
||||
|
||||
/// Instantiates the abstract method. EOT should have copy ctor.
|
||||
void operator()(eoIndiSelector<EOT>& _in,
|
||||
eoInserter<EOT>& _out)
|
||||
{
|
||||
EOT out1 = _in();
|
||||
EOT out2 = _in();
|
||||
op(out1, out2);
|
||||
_out(out1)(out2);
|
||||
}
|
||||
|
||||
private :
|
||||
eoQuadraticOp<EOT>& op;
|
||||
};
|
||||
|
||||
#include <eoBackInserter.h>
|
||||
|
||||
template <class EOT>
|
||||
class eoCombinedOp : public eoGeneralOp<EOT>
|
||||
{
|
||||
public :
|
||||
eoCombinedOp& bind(const std::vector<eoGeneralOp<EOT>*>& _ops, const std::vector<float>& _rates)
|
||||
{
|
||||
ops = &_ops;
|
||||
rates = &_rates;
|
||||
return *this;
|
||||
}
|
||||
|
||||
class eoDelayedSelector : public eoIndiSelector<EOT>
|
||||
{
|
||||
public :
|
||||
eoDelayedSelector(eoIndiSelector<EOT>& _select, const eoPop<EOT>& _pop) : select(_select), pop(_pop), it(pop.begin()) {}
|
||||
|
||||
unsigned size() const { return select.size();}
|
||||
const EOT& operator[](size_t i) const { return select[i]; }
|
||||
|
||||
/// will first dispense all previously selected individuals before returning new ones
|
||||
const EOT& operator()(void)
|
||||
{
|
||||
if (it == pop.end())
|
||||
{
|
||||
return select();
|
||||
}
|
||||
// else
|
||||
return *it++;
|
||||
}
|
||||
|
||||
eoPop<EOT>::const_iterator get_it(void) const { return it; }
|
||||
private :
|
||||
eoIndiSelector<EOT>& select;
|
||||
const eoPop<EOT>& pop;
|
||||
eoPop<EOT>::const_iterator it;
|
||||
};
|
||||
|
||||
/** Applies all ops in the combined op
|
||||
It first applies the
|
||||
*/
|
||||
void operator()( eoIndiSelector<EOT>& _in,
|
||||
eoInserter<EOT>& _out )
|
||||
{
|
||||
eoPop<EOT> intermediate;
|
||||
eoPop<EOT> next;
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < ops->size(); ++i)
|
||||
{
|
||||
eoDelayedSelector delay(_in, intermediate);
|
||||
inserter.bind(next);
|
||||
|
||||
unsigned counter = 0;
|
||||
|
||||
// apply operators until we have as many outputs as inputs
|
||||
do
|
||||
{
|
||||
if (rng.flip(rates->operator[](i))) // should this flip be here?
|
||||
(*ops->operator[](i))(delay, inserter);
|
||||
|
||||
counter++;
|
||||
if (counter > 1000)
|
||||
{
|
||||
throw std::logic_error("eoCombinedOp: no termination after 1000 tries, did you forget to insert individuals in your eoGeneralOp?");
|
||||
}
|
||||
}
|
||||
while (next.size() < intermediate.size());
|
||||
|
||||
intermediate.swap(next);
|
||||
next.resize(0);
|
||||
}
|
||||
|
||||
// after last swap, results can be found in intermediate
|
||||
for (i = 0; i < intermediate.size(); ++i)
|
||||
_out(intermediate[i]);
|
||||
}
|
||||
|
||||
private :
|
||||
const std::vector<eoGeneralOp<EOT>*>* ops;
|
||||
const std::vector<float>* rates;
|
||||
eoBackInserter<EOT> inserter;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoXOver2.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOXOVER2_h
|
||||
#define _EOXOVER2_h
|
||||
|
||||
|
||||
// for swap
|
||||
#if defined( __BORLANDC__ )
|
||||
#include <algorith>
|
||||
#else
|
||||
#include <algorithm>
|
||||
#endif
|
||||
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
/** 2-point crossover: takes the genes in the central section of two EOs
|
||||
and interchanges it
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoXOver2: public eoQuadraticOp<EOT> {
|
||||
public:
|
||||
///
|
||||
eoXOver2()
|
||||
: eoQuadraticOp< EOT >(){};
|
||||
|
||||
///
|
||||
virtual ~eoXOver2() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo1,
|
||||
EOT& _eo2 ) const {
|
||||
unsigned len1 = _eo1.length(), len2 = _eo2.length(),
|
||||
len= (len1 > len2)?len2:len1;
|
||||
|
||||
unsigned pos1 = rng.random(len), pos2 = rng.random(len) ;
|
||||
|
||||
applyAt( _eo1, _eo2, pos1, pos2 );
|
||||
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoOp
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
std::string className() const {return "eoXOver2";};
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Type Type;
|
||||
#else
|
||||
typedef typename EOT::Type Type;
|
||||
#endif
|
||||
|
||||
/// applies operator to one gene in the EO
|
||||
virtual void applyAt( EOT& _eo, EOT& _eo2,
|
||||
unsigned _i, unsigned _j = 0) const {
|
||||
|
||||
if ( _j < _i )
|
||||
swap( _i, _j );
|
||||
|
||||
unsigned len1 = _eo.length(), len2 = _eo2.length(),
|
||||
len= (len1 > len2)?len2:len1;
|
||||
|
||||
if ( (_j > len) || (_i> len ) )
|
||||
throw std::runtime_error( "xOver2: applying xOver past boundaries");
|
||||
|
||||
for ( unsigned i = _i; i < _j; i++ ) {
|
||||
Type tmp = _eo.gene( i );
|
||||
_eo.gene( i ) = _eo2.gene( i );
|
||||
_eo2.gene( i ) = tmp ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
#ifndef _eoEs_h
|
||||
#define _eoEs_h
|
||||
|
||||
#include <es/eoEsObjectiveBounds.h>
|
||||
#include <es/eoEsSimple.h>
|
||||
#include <es/eoEsStdev.h>
|
||||
#include <es/eoEsFull.h>
|
||||
|
||||
#include <es/eoEsChromInit.h>
|
||||
#include <es/eoEsMutationInit.h>
|
||||
|
||||
#include <es/eoEsMutate.h>
|
||||
|
||||
|
||||
#endif
|
||||
Reference in a new issue