Added a full algorithm to the new EO, with terminators, and things like that
This commit is contained in:
parent
378ee811e7
commit
259420d9ce
9 changed files with 396 additions and 48 deletions
10
eo/src/eo
10
eo/src/eo
|
|
@ -59,10 +59,17 @@
|
|||
#include <eoPopOps.h>
|
||||
#include <eoBitOp.h>
|
||||
|
||||
// Evaluation functions
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoEvalFuncPtr.h>
|
||||
#include <eoEvaluator.h>
|
||||
|
||||
// Terminators
|
||||
#include <eoTerm.h>
|
||||
#include <eoGenTerm.h>
|
||||
#include <eoFitTerm.h>
|
||||
|
||||
// Selection and reproduction stuff
|
||||
#include <eoLottery.h>
|
||||
#include <eoBreeder.h>
|
||||
#include <eoInsertion.h>
|
||||
|
|
@ -71,6 +78,9 @@
|
|||
|
||||
#include <eoProportionalOpSel.h>
|
||||
|
||||
// Algorithms
|
||||
#include <eoEasyEA.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// to be continued ...
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvalFunc.h
|
||||
// (c) GeNeura Team, 1998
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvalFunc.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
|
||||
|
|
@ -20,32 +20,31 @@
|
|||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoEvalFunc_H
|
||||
#define eoEvalFunc_H
|
||||
|
||||
/** Evaluate: takes one EO and sets its "fitness" property
|
||||
* returning this fitness also. That is why EOT is passed by
|
||||
* non-const reference: it must be altered within evaluate.\\
|
||||
|
||||
The requirements on the types with which this class is to be
|
||||
instantiated with are null, or else, they depend on the particular
|
||||
class it's going to be applied to; EO does not impose any requirement
|
||||
on it. If you subclass this abstract class, and use it to evaluate an
|
||||
EO, the requirements on this EO will depend on the evaluator.
|
||||
*/
|
||||
template< class EOT >
|
||||
struct eoEvalFunc {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Fitness EOFitT;
|
||||
#else
|
||||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
|
||||
/// Effectively applies the evaluation function to an EO or urEO
|
||||
virtual void operator() ( EOT & _eo ) const = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoEvalFunc_H
|
||||
#define eoEvalFunc_H
|
||||
|
||||
/** Evaluate: takes one EO and sets its "fitness" property
|
||||
* returning this fitness also. That is why EOT is passed by
|
||||
* non-const reference: it must be altered within evaluate.\\
|
||||
|
||||
The requirements on the types with which this class is to be
|
||||
instantiated with are null, or else, they depend on the particular
|
||||
class it's going to be applied to; EO does not impose any requirement
|
||||
on it. If you subclass this abstract class, and use it to evaluate an
|
||||
EO, the requirements on this EO will depend on the evaluator.
|
||||
*/
|
||||
template<class EOT> struct eoEvalFunc {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Fitness EOFitT;
|
||||
#else
|
||||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
|
||||
/// Effectively applies the evaluation function to an EO or urEO
|
||||
virtual void operator() ( EOT & _eo ) const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
64
eo/src/eoFitTerm.h
Normal file
64
eo/src/eoFitTerm.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// -*- 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 <eoTerm.h>
|
||||
|
||||
|
||||
/** Fitness termination: terminates after a the difference between the
|
||||
fitness of the best individual and a maximum fitness to achieve is less
|
||||
than certain number called epsilon., i.e., |maximum-fitness|<epsilon
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoFitTerm: public eoTerm<EOT> {
|
||||
public:
|
||||
|
||||
/// Ctors/dtors
|
||||
eoFitTerm( const float _maximum, const float _epsilon )
|
||||
: eoTerm<EOT> (), maximum( _maximum ), epsilon(_epsilon){};
|
||||
|
||||
/// Copy ctor
|
||||
eoFitTerm( const eoFitTerm& _t )
|
||||
: eoTerm<EOT> ( _t ), maximum( _t.maximum ),
|
||||
epsilon(_t.epsilon){};
|
||||
|
||||
///
|
||||
virtual ~eoFitTerm() {};
|
||||
|
||||
/** Returns false when a certain number of generations is
|
||||
* reached */
|
||||
virtual bool operator() ( const eoPop<EOT>& _vEO ) {
|
||||
float bestFitness=_vEO[0].fitness();
|
||||
float dif=bestFitness-maximum;
|
||||
dif=(dif<0)?-dif:dif;
|
||||
return (dif>epsilon ) || (bestFitness > maximum);
|
||||
}
|
||||
|
||||
private:
|
||||
float maximum, epsilon;
|
||||
};
|
||||
|
||||
#endif
|
||||
82
eo/src/eoGenTerm.h
Normal file
82
eo/src/eoGenTerm.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// -*- 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 <eoTerm.h>
|
||||
|
||||
/** Generational termination: terminates after a number of generations
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoGenTerm: public eoTerm<EOT> {
|
||||
public:
|
||||
|
||||
/// Ctors/dtors
|
||||
eoGenTerm( unsigned _totalGens)
|
||||
: eoTerm<EOT> (), repTotalGenerations( _totalGens ),
|
||||
thisGeneration(0){};
|
||||
|
||||
/// Copy Ctor
|
||||
eoGenTerm( const eoGenTerm& _t)
|
||||
: eoTerm<EOT> ( _t ), repTotalGenerations( _t.repTotalGenerations ),
|
||||
thisGeneration(0){};
|
||||
|
||||
/// Assignment Operator
|
||||
const eoGenTerm& operator = ( const eoGenTerm& _t) {
|
||||
if ( &_t != this ) {
|
||||
repTotalGenerations = _t.repTotalGenerations;
|
||||
thisGeneration = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoGenTerm() {};
|
||||
|
||||
/** Returns false when a certain number of generations is
|
||||
* reached */
|
||||
virtual bool operator() ( const eoPop<EOT>& _vEO ) {
|
||||
thisGeneration++;
|
||||
// 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;
|
||||
};
|
||||
|
||||
private:
|
||||
unsigned repTotalGenerations, thisGeneration;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -28,6 +28,7 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoAlgo.h> // eoPop
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPopOps.h> // eoSelect, eoTranform, eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -43,8 +44,14 @@ template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
|||
eoMerge<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {}
|
||||
|
||||
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;
|
||||
|
|
|
|||
50
eo/src/eoTerm.h
Normal file
50
eo/src/eoTerm.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoTerm.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 _EOTERM_H
|
||||
#define _EOTERM_H
|
||||
|
||||
#include <eoPop.h>
|
||||
|
||||
/** Termination condition for the genetic algorithm
|
||||
* Takes the population as input, returns true for continue,
|
||||
* false for termination
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoTerm {
|
||||
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 a counter
|
||||
*/
|
||||
virtual bool operator() ( const eoPop< EOT >& _pop ) = 0 ;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in a new issue