Mak: Added the eoQuadratic Op and more ... (and I hate VI)
This commit is contained in:
parent
4ac1850226
commit
9fba2bfbb9
13 changed files with 1247 additions and 1124 deletions
68
eo/src/compatibility.h
Normal file
68
eo/src/compatibility.h
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
compatibility.h
|
||||||
|
File to store some compiler specific stuff in. Currently handles, or
|
||||||
|
least tries to handle the min() max() problems when using MSVC
|
||||||
|
|
||||||
|
|
||||||
|
(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 COMPAT_H
|
||||||
|
#define COMPAT_H
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
/*
|
||||||
|
Maarten: added this code here because Mirkosoft has the
|
||||||
|
nasty habit of #define min and max in stdlib.h (and windows.h)
|
||||||
|
I'm trying to undo this horrible macro magic (microsoft yet macrohard)
|
||||||
|
here. Sure hope it works
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifdef min
|
||||||
|
#undef min
|
||||||
|
#undef max // as they come in pairs
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// add min and max to std...
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template <class T> T min(const T& a, const T& b)
|
||||||
|
{
|
||||||
|
if(a < b)
|
||||||
|
return a;
|
||||||
|
// else
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T> T max(const T& a, const T& b)
|
||||||
|
{
|
||||||
|
if(a > b)
|
||||||
|
return a;
|
||||||
|
// else
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // _MSC_VER
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -184,7 +184,7 @@ template<class Chrom> class eoBinPrev: public eoMonOp<Chrom>
|
||||||
|
|
||||||
/** eoBinCrossover --> classic crossover */
|
/** eoBinCrossover --> classic crossover */
|
||||||
|
|
||||||
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// The class name.
|
/// The class name.
|
||||||
|
|
@ -205,7 +205,7 @@ template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
||||||
|
|
||||||
/** eoBinNxOver --> n-point crossover */
|
/** eoBinNxOver --> n-point crossover */
|
||||||
|
|
||||||
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// (Defualt) Constructor.
|
/// (Defualt) Constructor.
|
||||||
|
|
@ -263,7 +263,7 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||||
|
|
||||||
/** eoBinGxOver --> gene crossover */
|
/** eoBinGxOver --> gene crossover */
|
||||||
|
|
||||||
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,9 @@
|
||||||
#include <eoPopOps.h> // eoTransform
|
#include <eoPopOps.h> // eoTransform
|
||||||
#include <eoOpSelector.h> // eoOpSelector
|
#include <eoOpSelector.h> // eoOpSelector
|
||||||
|
|
||||||
|
#include "eoRandomIndiSelector.h"
|
||||||
|
#include "eoBackInserter.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
|
|
@ -57,33 +60,40 @@ template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
|
||||||
*/
|
*/
|
||||||
void operator()(eoPop<Chrom>& pop)
|
void operator()(eoPop<Chrom>& pop)
|
||||||
{
|
{
|
||||||
|
size_t orgsize = pop.size();
|
||||||
|
|
||||||
for (unsigned i = 0; i < pop.size(); i++) {
|
for (unsigned i = 0; i < pop.size(); i++) {
|
||||||
eoOp<Chrom>* op = opSel.Op();
|
eoOp<Chrom>* op = opSel.Op();
|
||||||
switch (op->readArity()) {
|
switch (op->getType()) {
|
||||||
case unary:
|
case eoOp<Chrom>::unary:
|
||||||
{
|
{
|
||||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||||
(*monop)( pop[i] );
|
(*monop)( pop[i] );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case binary:
|
case eoOp<Chrom>::binary:
|
||||||
{
|
{
|
||||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||||
eoUniform<unsigned> u(0, pop.size() );
|
eoUniform<unsigned> u(0, pop.size() );
|
||||||
(*binop)(pop[i], pop[ u() ] );
|
(*binop)(pop[i], pop[ u() ] );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Nary:
|
case eoOp<Chrom>::quadratic:
|
||||||
{
|
{
|
||||||
eoNaryOp<Chrom>* Nop = static_cast<eoNaryOp<Chrom>* >(op);
|
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
|
||||||
eoUniform<unsigned> u(0, pop.size() );
|
|
||||||
eoPop<Chrom> inVec, outVec;
|
eoUniform<unsigned> u(0, pop.size() );
|
||||||
inVec.push_back( pop[i] );
|
(*Qop)(pop[i], pop[ u() ] );
|
||||||
unsigned numberOfOperands = u();
|
break;
|
||||||
for ( unsigned i = 0; i < numberOfOperands; i ++ ) {
|
}
|
||||||
inVec.push_back( pop[ u() ] );
|
case eoOp<Chrom>::general :
|
||||||
}
|
{
|
||||||
(*Nop)( inVec, outVec );
|
eoGeneralOp<Chrom>* Gop = static_cast<eoGeneralOp<Chrom>* >(op);
|
||||||
|
|
||||||
|
eoRandomIndiSelector<Chrom> selector;
|
||||||
|
eoBackInserter<Chrom> inserter;
|
||||||
|
|
||||||
|
(*Gop)(selector(pop, orgsize, i), inserter(pop));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@
|
||||||
#include <functional> //
|
#include <functional> //
|
||||||
#include <numeric> // accumulate
|
#include <numeric> // accumulate
|
||||||
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
|
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
|
||||||
#include <eoRNG.h>
|
#include "selectors.h"
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** eoDetTournament: a selection method that selects ONE individual by
|
/** eoDetTournament: a selection method that selects ONE individual by
|
||||||
|
|
@ -46,22 +46,14 @@ template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
|
||||||
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) {
|
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) {
|
||||||
// consistency check
|
// consistency check
|
||||||
if (Tsize < 2) {
|
if (Tsize < 2) {
|
||||||
cout << "Warning, Tournament size shoudl be >= 2\nAdjusted\n";
|
cout << "Warning, Tournament size should be >= 2\nAdjusted\n";
|
||||||
Tsize = 2;
|
Tsize = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** DANGER: if you want to be able to minimize as well as maximizem
|
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||||
DON'T cast the fitness to a float, use the EOT comparator! */
|
{
|
||||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
return deterministic_tournament(pop, Tsize)();
|
||||||
unsigned best = rng.random(pop.size()); // random individual
|
|
||||||
|
|
||||||
for (unsigned i = 0; i<Tsize-1; i++) {
|
|
||||||
unsigned tmp = rng.random(pop.size());
|
|
||||||
if (pop[best] < pop[tmp])
|
|
||||||
best = tmp;
|
|
||||||
}
|
|
||||||
return pop[best] ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Inherited from eoObject
|
/** Inherited from eoObject
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ struct eoEvalFuncPtr: public eoEvalFunc<EOT> {
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EOFitT (* evalFunc )( EOT& );
|
EOFitT (* evalFunc )( const EOT& );
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
eoGOpSelector.h
|
eoGOpSelector.h
|
||||||
Base class for generalized (n-inputs, n-outputs) operator selectors.
|
Base class for generalized (n-inputs, n-outputs) operator selectors.
|
||||||
Includes code and variables that contain operators and rates
|
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
|
(c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000
|
||||||
|
|
||||||
|
|
@ -29,21 +31,15 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <vector> // vector
|
|
||||||
#include <iterator>
|
|
||||||
#include <eoUniform.h> // eoUniform
|
|
||||||
#include <eoGeneralOp.h> // eoOp, eoMonOp, eoBinOp
|
|
||||||
#include <eoPop.h> // eoPop
|
|
||||||
#include <eoPopOps.h> // eoTransform
|
|
||||||
#include <eoOpSelector.h> // eoOpSelector
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include "eoOpSelector.h"
|
||||||
|
#include "eoWrappedOps.h" // for eoCombinedOp
|
||||||
#include "eoRNG.h"
|
#include "eoRNG.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/** Base class for alternative selectors, which use the generalized operator
|
/** Base class for alternative selectors, which use the generalized operator
|
||||||
interface */
|
interface. eoGOpBreeders expects this class */
|
||||||
|
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
|
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
|
||||||
{
|
{
|
||||||
|
|
@ -52,46 +48,14 @@ public:
|
||||||
/// Dtor
|
/// Dtor
|
||||||
virtual ~eoGOpSelector() {
|
virtual ~eoGOpSelector() {
|
||||||
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
|
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
|
||||||
i != ownOpList.begin(); i ++ ) {
|
i != ownOpList.end(); i++ ) {
|
||||||
delete *i;
|
delete *i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add any kind of operator to the operator mix, with an argument
|
/// Add any kind of operator to the operator mix, with an argument
|
||||||
virtual ID addOp( eoOp<EOT>& _op, float _arg ) {
|
virtual ID addOp( eoOp<EOT>& _op, float _arg );
|
||||||
eoGeneralOp<EOT>* op = dynamic_cast<eoGeneralOp<EOT>*>(&_op);
|
// implementation can be found below
|
||||||
|
|
||||||
// if it's not a general op, it's a "old" op; create a wrapped op from it
|
|
||||||
// and keep it on a list to delete them afterwards
|
|
||||||
// will use auto_ptr when they're readily available
|
|
||||||
if (op == 0) {
|
|
||||||
switch(_op.readArity())
|
|
||||||
{
|
|
||||||
case unary :
|
|
||||||
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
|
|
||||||
break;
|
|
||||||
case binary :
|
|
||||||
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ownOpList.push_back( op );
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer
|
|
||||||
|
|
||||||
if (result == end())
|
|
||||||
{
|
|
||||||
push_back(op);
|
|
||||||
rates.push_back(_arg);
|
|
||||||
return size();
|
|
||||||
}
|
|
||||||
// else
|
|
||||||
|
|
||||||
*result = op;
|
|
||||||
ID id = result - begin();
|
|
||||||
rates[id] = _arg;
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Retrieve the operator using its integer handle
|
/** Retrieve the operator using its integer handle
|
||||||
@param _id The id number. Should be a valid id, or an exception
|
@param _id The id number. Should be a valid id, or an exception
|
||||||
|
|
@ -104,11 +68,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
virtual void deleteOp( ID _id )
|
virtual void deleteOp( ID _id );
|
||||||
{
|
// implemented below
|
||||||
operator[](_id) = 0; // TODO, check oplist and clear it there too.
|
|
||||||
rates[_id] = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
virtual eoOp<EOT>* Op()
|
virtual eoOp<EOT>* Op()
|
||||||
|
|
@ -131,9 +92,80 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected :
|
const vector<float>& getRates(void) const { return rates; }
|
||||||
|
|
||||||
|
private :
|
||||||
vector<float> rates;
|
vector<float> rates;
|
||||||
list< eoGeneralOp<EOT>* > ownOpList;
|
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 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));
|
||||||
|
case eoOp<EOT>::quadratic :
|
||||||
|
op = new eoWrappedQuadraticOp<EOT>(static_cast<eoQuadraticOp<EOT>&>(_op));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
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.
|
||||||
|
|
||||||
|
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 oplist and clear it there too.
|
||||||
|
|
||||||
|
list< eoGeneralOp<EOT>* >::iterator it = find(ownOpList.begin(), ownOpList.end(), op);
|
||||||
|
|
||||||
|
if(it != ownOpList.end())
|
||||||
|
{
|
||||||
|
ownOpList.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif eoGOpSelector_h
|
#endif eoGOpSelector_h
|
||||||
|
|
|
||||||
|
|
@ -30,63 +30,63 @@
|
||||||
|
|
||||||
#include <functional> //
|
#include <functional> //
|
||||||
#include <numeric> // accumulate
|
#include <numeric> // accumulate
|
||||||
#include "eoPopOps.h"
|
#include "selectors.h"
|
||||||
#include "eoRNG.h"
|
#include <eo> // eoPop eoSelect MINFLOAT
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/// eoLottery: a selection method.
|
/** eoLottery: a selection method. Puts into the output a group of individuals
|
||||||
/// requires EOT::Fitness to be float castable
|
selected using lottery; individuals with higher probability will have more
|
||||||
|
chances of being selected.
|
||||||
|
Requires EOT::Fitness to be float castable
|
||||||
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
template<class EOT> class eoLottery: public eoBinPopOp<EOT>
|
template<class EOT> class eoLottery: public eoBinPopOp<EOT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// (Default) Constructor.
|
/// (Default) Constructor.
|
||||||
eoLottery(const double & _rate = 1.0): rate(_rate) {}
|
eoLottery(const float& _rate = 1.0): eoBinPopOp<EOT>(), rate(_rate)
|
||||||
|
{
|
||||||
|
if (minimizing_fitness<EOT>())
|
||||||
|
{
|
||||||
|
eoMinimizingFitnessException up(*this);
|
||||||
|
throw up; // :-)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** actually selects individuals from pop and put them into breeders
|
/** actually selects individuals from pop and pushes them back them into breeders
|
||||||
* until breeders has the right size: rate*pop.size()
|
* until breeders has the right size: rate*pop.size()
|
||||||
* BUT what happens if breeders is already too big?
|
* BUT what happens if breeders is already too big?
|
||||||
*/
|
* Too big for what?
|
||||||
|
*/
|
||||||
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
|
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
|
||||||
{
|
{
|
||||||
// scores of chromosomes
|
int target = (int)(rate * pop.size());
|
||||||
vector<double> score(pop.size());
|
|
||||||
|
|
||||||
// calculates total scores for chromosomes
|
|
||||||
double total = 0;
|
|
||||||
for (unsigned i = 0; i < pop.size(); i++) {
|
|
||||||
score[i] = static_cast<double>(pop[i].fitness());
|
|
||||||
total += score[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// number of offspring needed
|
|
||||||
int target = (int)rint(rate * pop.size());
|
|
||||||
|
|
||||||
// test of consistency
|
// test of consistency
|
||||||
if (breeders.size() >= target) {
|
if (breeders.size() >= target) {
|
||||||
throw("Problem in eoLottery: already too many offspring");
|
throw("Problem in eoLottery: already too many offspring");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double total;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
total = sum_fitness(pop);
|
||||||
|
}
|
||||||
|
catch (eoNegativeFitnessException&)
|
||||||
|
{ // say where it occured...
|
||||||
|
throw eoNegativeFitnessException(*this);
|
||||||
|
}
|
||||||
|
|
||||||
// selection of chromosomes
|
// selection of chromosomes
|
||||||
while (breeders.size() < target) {
|
while (breeders.size() < target)
|
||||||
unsigned indloc = rng.roulette_wheel(score, total);
|
{
|
||||||
breeders.push_back(pop[indloc]);
|
breeders.push_back(roulette_wheel(pop, total));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// accessor to private rate
|
double Rate(void) const { return rate; }
|
||||||
double Rate() {return rate;}
|
|
||||||
|
|
||||||
/** @name Methods from eoObject */
|
|
||||||
//@{
|
|
||||||
/** readFrom and printOn inherited from eoMerge */
|
|
||||||
|
|
||||||
/** Inherited from eoObject. Returns the class name.
|
|
||||||
@see eoObject
|
|
||||||
*/
|
|
||||||
virtual string className() const {return "eoLottery";};
|
|
||||||
//@}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double rate; // selection rate
|
double rate; // selection rate
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
|
||||||
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
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoObject.h
|
||||||
|
// (c) GeNeura Team, 1998
|
||||||
|
/*
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
|
|
@ -22,7 +19,7 @@
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef EOOBJECT_H
|
#ifndef EOOBJECT_H
|
||||||
|
|
@ -34,6 +31,8 @@
|
||||||
#include <iostream> // istream, ostream
|
#include <iostream> // istream, ostream
|
||||||
#include <string> // string
|
#include <string> // string
|
||||||
|
|
||||||
|
#include "compatibility.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
120
eo/src/eoOp.h
120
eo/src/eoOp.h
|
|
@ -25,15 +25,16 @@
|
||||||
#ifndef _eoOp_H
|
#ifndef _eoOp_H
|
||||||
#define _eoOp_H
|
#define _eoOp_H
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <eoObject.h>
|
#include <eoObject.h>
|
||||||
#include <eoPrintable.h>
|
#include <eoPrintable.h>
|
||||||
|
|
||||||
/** @name Genetic operators
|
/** @name Genetic operators
|
||||||
|
|
||||||
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with
|
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with
|
||||||
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp (binary operator) as siblings. Nobody
|
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp and eoQuadraticOp (binary operators)
|
||||||
should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\
|
as siblings). Nobody should subclass eoOp, you should subclass eoGeneralOp, eoBinOp, eoQuadraticOp or eoMonOp,
|
||||||
|
those are the ones actually used here.\\
|
||||||
|
|
||||||
#eoOp#s are only printable objects, so if you want to build them from a file, it has to
|
#eoOp#s are only printable objects, so if you want to build them from a file, it has to
|
||||||
be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own
|
be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own
|
||||||
factory, which know how to build them from a description in a file.
|
factory, which know how to build them from a description in a file.
|
||||||
|
|
@ -41,10 +42,7 @@ factory, which know how to build them from a description in a file.
|
||||||
@version 0.1
|
@version 0.1
|
||||||
@see eoOpFactory
|
@see eoOpFactory
|
||||||
*/
|
*/
|
||||||
//@{
|
|
||||||
|
|
||||||
///
|
|
||||||
enum Arity { unary = 0, binary = 1, Nary = 2};
|
|
||||||
|
|
||||||
/** Abstract data types for EO operators.
|
/** Abstract data types for EO operators.
|
||||||
* Genetic operators act on chromosomes, changing them. The type to instantiate them should
|
* Genetic operators act on chromosomes, changing them. The type to instantiate them should
|
||||||
|
|
@ -55,19 +53,23 @@ template<class EOType>
|
||||||
class eoOp: public eoObject, public eoPrintable {
|
class eoOp: public eoObject, public eoPrintable {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
//@{
|
||||||
|
enum OpType { unary = 0, binary = 1, quadratic = 2, general = 3};
|
||||||
|
///
|
||||||
|
|
||||||
/// Ctor
|
/// Ctor
|
||||||
eoOp( Arity _arity = unary )
|
eoOp(OpType _type)
|
||||||
:arity( _arity ) {};
|
:opType( _type ) {};
|
||||||
|
|
||||||
/// Copy Ctor
|
/// Copy Ctor
|
||||||
eoOp( const eoOp& _eop )
|
eoOp( const eoOp& _eop )
|
||||||
:arity( _eop.arity ) {};
|
:opType( _eop.opType ) {};
|
||||||
|
|
||||||
/// Needed virtual destructor
|
/// Needed virtual destructor
|
||||||
virtual ~eoOp(){};
|
virtual ~eoOp(){};
|
||||||
|
|
||||||
/// Arity: number of operands
|
/// getType: number of operands it takes and individuals it produces
|
||||||
Arity readArity() const {return arity;};
|
OpType getType() const {return opType;};
|
||||||
|
|
||||||
/** @name Methods from eoObject */
|
/** @name Methods from eoObject */
|
||||||
//@{
|
//@{
|
||||||
|
|
@ -88,8 +90,8 @@ public:
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// arity is the number of operands it takes
|
/// OpType is the type of the operator: how many operands it takes and how many it produces
|
||||||
Arity arity;
|
OpType opType;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -111,6 +113,40 @@ public:
|
||||||
/// Dtor
|
/// Dtor
|
||||||
~eoBinOp () {};
|
~eoBinOp () {};
|
||||||
|
|
||||||
|
/** applies operator, to the object. Modifies only the first operand.
|
||||||
|
*/
|
||||||
|
virtual void operator()( EOType& _eo1, const EOType& _eo2 ) const = 0;
|
||||||
|
|
||||||
|
/** @name Methods from eoObject
|
||||||
|
readFrom and printOn are directly inherited from eoObject
|
||||||
|
*/
|
||||||
|
//@{
|
||||||
|
/** Inherited from eoObject
|
||||||
|
@see eoObject
|
||||||
|
*/
|
||||||
|
virtual string className() const {return "eoBinOp";};
|
||||||
|
//@}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Quadratic genetic operator: subclasses eoOp, and defines
|
||||||
|
basically the operator() with two operands
|
||||||
|
*/
|
||||||
|
template<class EOType>
|
||||||
|
class eoQuadraticOp: public eoOp<EOType> {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Ctor
|
||||||
|
eoQuadraticOp()
|
||||||
|
:eoOp<EOType>( eoOp<EOType>::quadratic ) {};
|
||||||
|
|
||||||
|
/// Copy Ctor
|
||||||
|
eoQuadraticOp( const eoQuadraticOp& _ebop )
|
||||||
|
: eoOp<EOType>( _ebop ){};
|
||||||
|
|
||||||
|
/// Dtor
|
||||||
|
~eoQuadraticOp() {};
|
||||||
|
|
||||||
/** applies operator, to the object. Modifies both operands.
|
/** applies operator, to the object. Modifies both operands.
|
||||||
*/
|
*/
|
||||||
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
|
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
|
||||||
|
|
@ -136,7 +172,7 @@ public:
|
||||||
|
|
||||||
/// Ctor
|
/// Ctor
|
||||||
eoMonOp( )
|
eoMonOp( )
|
||||||
:eoOp<EOType>( unary ) {};
|
: eoOp<EOType>( eoOp<EOType>::unary ) {};
|
||||||
|
|
||||||
/// Copy Ctor
|
/// Copy Ctor
|
||||||
eoMonOp( const eoMonOp& _emop )
|
eoMonOp( const eoMonOp& _emop )
|
||||||
|
|
@ -159,45 +195,41 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual string className() const {return "eoMonOp";};
|
virtual string className() const {return "eoMonOp";};
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <eoPop.h>
|
// some forward declarations
|
||||||
/** eoNaryOp is the N-ary operator: genetic operator that takes
|
template<class EOT>
|
||||||
several EOs. It could be called an {\em orgy} operator. It's a general operator
|
class eoIndiSelector;
|
||||||
that takes any number of inputs and spits out any number of outputs
|
|
||||||
|
template<class EOT>
|
||||||
|
class eoInserter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* eGeneralOp: General genetic operator; for objects used to transform sets
|
||||||
|
of EOs. Nary ("orgy") operators should be derived from this class
|
||||||
*/
|
*/
|
||||||
template <class EOType>
|
template<class EOT>
|
||||||
class eoNaryOp: public eoOp<EOType> {
|
class eoGeneralOp: public eoOp<EOT>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Ctor
|
/// Ctor that honors its superclass
|
||||||
eoNaryOp( )
|
eoGeneralOp(): eoOp<EOT>( eoOp<EOT>::general ) {};
|
||||||
:eoOp<EOType>( Nary ) {};
|
|
||||||
|
|
||||||
/// Copy Ctor
|
/// Virtual dtor
|
||||||
eoNaryOp( const eoNaryOp& _emop )
|
virtual ~eoGeneralOp () {};
|
||||||
: eoOp<EOType>( _emop ){};
|
|
||||||
|
|
||||||
/// Dtor
|
/** Method that really does the stuff. Applies the genetic operator
|
||||||
~eoNaryOp() {};
|
to a individuals dispensed by an eoIndividualSelector,
|
||||||
|
and puts the results in the eoIndividualInserter.
|
||||||
/** applies randomly operator, to the object.
|
Any number of inputs can be requested and any number of outputs
|
||||||
*/
|
can be produced.
|
||||||
virtual void operator()( const eoPop<EOType> & _in, eoPop<EOType> _out ) const = 0;
|
|
||||||
|
|
||||||
/** @name Methods from eoObject
|
|
||||||
readFrom and printOn are directly inherited from eoObject.
|
|
||||||
*/
|
*/
|
||||||
//@{
|
virtual void operator()( eoIndiSelector<EOT>& _in,
|
||||||
/** Inherited from eoObject
|
eoInserter<EOT>& _out) const = 0;
|
||||||
@see eoObject
|
|
||||||
*/
|
|
||||||
string className() const {return "eoNaryOp";};
|
|
||||||
//@}
|
|
||||||
|
|
||||||
|
virtual string className() const {return "eoGeneralOp";};
|
||||||
};
|
};
|
||||||
//@}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ template <class EOT> class eoStochTournament: public eoSelectOne<EOT>
|
||||||
public:
|
public:
|
||||||
|
|
||||||
///
|
///
|
||||||
eoStochTournament(float _Trate = 1.0 ):eoSelectOne(), Trate(_Trate) {
|
eoStochTournament(float _Trate = 1.0 ):eoSelectOne<EOT>(), Trate(_Trate) {
|
||||||
// consistency check
|
// consistency check
|
||||||
if (Trate < 0.5) {
|
if (Trate < 0.5) {
|
||||||
cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
|
cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
|
||||||
|
|
@ -54,19 +54,9 @@ template <class EOT> class eoStochTournament: public eoSelectOne<EOT>
|
||||||
|
|
||||||
/** DANGER: if you want to be able to minimize as well as maximizem
|
/** DANGER: if you want to be able to minimize as well as maximizem
|
||||||
DON'T cast the fitness to a float, use the EOT comparator! */
|
DON'T cast the fitness to a float, use the EOT comparator! */
|
||||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||||
unsigned i1 = rng.random(pop.size()),
|
{
|
||||||
i2 = rng.random(pop.size());
|
return stochastic_tournament(pop, Trate)();
|
||||||
|
|
||||||
bool ok = ( rng.flip(Trate) );
|
|
||||||
if (pop[i1] < pop[ i2 ] ) {
|
|
||||||
if (ok) return pop[ i2 ];
|
|
||||||
else return pop[ i1 ];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (ok) return pop[ i1 ];
|
|
||||||
else return pop[ i2 ];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -46,13 +46,13 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
class eoUniformXOver: public eoBinOp< EOT >
|
class eoUniformXOver: public eoQuadraticOp< EOT >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
///
|
///
|
||||||
eoUniformXOver( float _rate = 0.5 ):
|
eoUniformXOver( float _rate = 0.5 ):
|
||||||
eoBinOp< EOT > ( ), rate( _rate ) {
|
eoQuadraticOp< EOT > ( ), rate( _rate ) {
|
||||||
if (rate < 0 || rate > 1)
|
if (rate < 0 || rate > 1)
|
||||||
runtime_error("UxOver --> invalid rate");
|
runtime_error("UxOver --> invalid rate");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,11 @@
|
||||||
and interchanges it
|
and interchanges it
|
||||||
*/
|
*/
|
||||||
template <class EOT>
|
template <class EOT>
|
||||||
class eoXOver2: public eoBinOp<EOT> {
|
class eoXOver2: public eoQuadraticOp<EOT> {
|
||||||
public:
|
public:
|
||||||
///
|
///
|
||||||
eoXOver2()
|
eoXOver2()
|
||||||
: eoBinOp< EOT >(){};
|
: eoQuadraticOp< EOT >(){};
|
||||||
|
|
||||||
///
|
///
|
||||||
virtual ~eoXOver2() {};
|
virtual ~eoXOver2() {};
|
||||||
|
|
|
||||||
Reference in a new issue