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 */
|
||||
|
||||
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
||||
template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// The class name.
|
||||
|
|
@ -205,7 +205,7 @@ template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
|
|||
|
||||
/** eoBinNxOver --> n-point crossover */
|
||||
|
||||
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
||||
template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Defualt) Constructor.
|
||||
|
|
@ -263,7 +263,7 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
|
|||
|
||||
/** eoBinGxOver --> gene crossover */
|
||||
|
||||
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
||||
template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
|
|
|
|||
|
|
@ -35,6 +35,9 @@
|
|||
#include <eoPopOps.h> // eoTransform
|
||||
#include <eoOpSelector.h> // eoOpSelector
|
||||
|
||||
#include "eoRandomIndiSelector.h"
|
||||
#include "eoBackInserter.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*****************************************************************************
|
||||
|
|
@ -57,33 +60,40 @@ template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
|
|||
*/
|
||||
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->readArity()) {
|
||||
case unary:
|
||||
switch (op->getType()) {
|
||||
case eoOp<Chrom>::unary:
|
||||
{
|
||||
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
|
||||
(*monop)( pop[i] );
|
||||
break;
|
||||
}
|
||||
case binary:
|
||||
case eoOp<Chrom>::binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*binop)(pop[i], pop[ u() ] );
|
||||
break;
|
||||
}
|
||||
case Nary:
|
||||
case eoOp<Chrom>::quadratic:
|
||||
{
|
||||
eoNaryOp<Chrom>* Nop = static_cast<eoNaryOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
eoPop<Chrom> inVec, outVec;
|
||||
inVec.push_back( pop[i] );
|
||||
unsigned numberOfOperands = u();
|
||||
for ( unsigned i = 0; i < numberOfOperands; i ++ ) {
|
||||
inVec.push_back( pop[ u() ] );
|
||||
}
|
||||
(*Nop)( inVec, outVec );
|
||||
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
|
||||
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*Qop)(pop[i], pop[ u() ] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::general :
|
||||
{
|
||||
eoGeneralOp<Chrom>* Gop = static_cast<eoGeneralOp<Chrom>* >(op);
|
||||
|
||||
eoRandomIndiSelector<Chrom> selector;
|
||||
eoBackInserter<Chrom> inserter;
|
||||
|
||||
(*Gop)(selector(pop, orgsize, i), inserter(pop));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@
|
|||
#include <functional> //
|
||||
#include <numeric> // accumulate
|
||||
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
|
||||
#include <eoRNG.h>
|
||||
#include "selectors.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** 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) {
|
||||
// consistency check
|
||||
if (Tsize < 2) {
|
||||
cout << "Warning, Tournament size shoudl be >= 2\nAdjusted\n";
|
||||
cout << "Warning, Tournament size should be >= 2\nAdjusted\n";
|
||||
Tsize = 2;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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! */
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
||||
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] ;
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||
{
|
||||
return deterministic_tournament(pop, Tsize)();
|
||||
}
|
||||
|
||||
/** Inherited from eoObject
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ struct eoEvalFuncPtr: public eoEvalFunc<EOT> {
|
|||
};
|
||||
|
||||
private:
|
||||
EOFitT (* evalFunc )( EOT& );
|
||||
EOFitT (* evalFunc )( const EOT& );
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
-----------------------------------------------------------------------------
|
||||
eoGOpSelector.h
|
||||
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
|
||||
|
||||
|
|
@ -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 "eoOpSelector.h"
|
||||
#include "eoWrappedOps.h" // for eoCombinedOp
|
||||
#include "eoRNG.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** Base class for alternative selectors, which use the generalized operator
|
||||
interface */
|
||||
|
||||
interface. eoGOpBreeders expects this class */
|
||||
template<class EOT>
|
||||
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
|
||||
{
|
||||
|
|
@ -52,46 +48,14 @@ public:
|
|||
/// Dtor
|
||||
virtual ~eoGOpSelector() {
|
||||
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
|
||||
i != ownOpList.begin(); i ++ ) {
|
||||
i != ownOpList.end(); i++ ) {
|
||||
delete *i;
|
||||
}
|
||||
}
|
||||
|
||||
/// Add any kind of operator to the operator mix, with an argument
|
||||
virtual ID addOp( eoOp<EOT>& _op, float _arg ) {
|
||||
eoGeneralOp<EOT>* op = dynamic_cast<eoGeneralOp<EOT>*>(&_op);
|
||||
|
||||
// if it's not a general op, it's a "old" op; create a wrapped op from it
|
||||
// and keep it on a list to delete them afterwards
|
||||
// will use auto_ptr when they're readily available
|
||||
if (op == 0) {
|
||||
switch(_op.readArity())
|
||||
{
|
||||
case unary :
|
||||
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
|
||||
break;
|
||||
case binary :
|
||||
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
|
||||
break;
|
||||
}
|
||||
ownOpList.push_back( op );
|
||||
}
|
||||
|
||||
iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer
|
||||
|
||||
if (result == end())
|
||||
{
|
||||
push_back(op);
|
||||
rates.push_back(_arg);
|
||||
return size();
|
||||
}
|
||||
// else
|
||||
|
||||
*result = op;
|
||||
ID id = result - begin();
|
||||
rates[id] = _arg;
|
||||
return id;
|
||||
}
|
||||
virtual ID addOp( eoOp<EOT>& _op, float _arg );
|
||||
// implementation can be found below
|
||||
|
||||
/** Retrieve the operator using its integer handle
|
||||
@param _id The id number. Should be a valid id, or an exception
|
||||
|
|
@ -104,11 +68,8 @@ public:
|
|||
}
|
||||
|
||||
///
|
||||
virtual void deleteOp( ID _id )
|
||||
{
|
||||
operator[](_id) = 0; // TODO, check oplist and clear it there too.
|
||||
rates[_id] = 0.0;
|
||||
}
|
||||
virtual void deleteOp( ID _id );
|
||||
// implemented below
|
||||
|
||||
///
|
||||
virtual eoOp<EOT>* Op()
|
||||
|
|
@ -131,9 +92,80 @@ public:
|
|||
}
|
||||
|
||||
|
||||
protected :
|
||||
const vector<float>& getRates(void) const { return rates; }
|
||||
|
||||
private :
|
||||
vector<float> rates;
|
||||
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
|
||||
|
|
|
|||
|
|
@ -30,63 +30,63 @@
|
|||
|
||||
#include <functional> //
|
||||
#include <numeric> // accumulate
|
||||
#include "eoPopOps.h"
|
||||
#include "eoRNG.h"
|
||||
#include "selectors.h"
|
||||
#include <eo> // eoPop eoSelect MINFLOAT
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/// eoLottery: a selection method.
|
||||
/// requires EOT::Fitness to be float castable
|
||||
/** 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 eoBinPopOp<EOT>
|
||||
{
|
||||
public:
|
||||
/// (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
|
||||
* until breeders has the right size: rate*pop.size()
|
||||
* BUT what happens if breeders is already too big?
|
||||
*/
|
||||
/** 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()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
|
||||
{
|
||||
// scores of chromosomes
|
||||
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());
|
||||
int target = (int)(rate * pop.size());
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// selection of chromosomes
|
||||
while (breeders.size() < target) {
|
||||
unsigned indloc = rng.roulette_wheel(score, total);
|
||||
breeders.push_back(pop[indloc]);
|
||||
while (breeders.size() < target)
|
||||
{
|
||||
breeders.push_back(roulette_wheel(pop, total));
|
||||
}
|
||||
}
|
||||
|
||||
/// accessor to private 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";};
|
||||
//@}
|
||||
double Rate(void) const { return rate; }
|
||||
|
||||
private:
|
||||
double rate; // selection rate
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
/** -*- 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
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoObject.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
|
||||
|
|
@ -22,7 +19,7 @@
|
|||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOOBJECT_H
|
||||
|
|
@ -34,6 +31,8 @@
|
|||
#include <iostream> // istream, ostream
|
||||
#include <string> // string
|
||||
|
||||
#include "compatibility.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
120
eo/src/eoOp.h
120
eo/src/eoOp.h
|
|
@ -25,15 +25,16 @@
|
|||
#ifndef _eoOp_H
|
||||
#define _eoOp_H
|
||||
|
||||
#include <vector>
|
||||
#include <eoObject.h>
|
||||
#include <eoPrintable.h>
|
||||
|
||||
/** @name Genetic operators
|
||||
|
||||
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
|
||||
should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\
|
||||
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp and eoQuadraticOp (binary operators)
|
||||
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
|
||||
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.
|
||||
|
|
@ -41,10 +42,7 @@ factory, which know how to build them from a description in a file.
|
|||
@version 0.1
|
||||
@see eoOpFactory
|
||||
*/
|
||||
//@{
|
||||
|
||||
///
|
||||
enum Arity { unary = 0, binary = 1, Nary = 2};
|
||||
|
||||
/** Abstract data types for EO operators.
|
||||
* 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 {
|
||||
public:
|
||||
|
||||
//@{
|
||||
enum OpType { unary = 0, binary = 1, quadratic = 2, general = 3};
|
||||
///
|
||||
|
||||
/// Ctor
|
||||
eoOp( Arity _arity = unary )
|
||||
:arity( _arity ) {};
|
||||
eoOp(OpType _type)
|
||||
:opType( _type ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoOp( const eoOp& _eop )
|
||||
:arity( _eop.arity ) {};
|
||||
:opType( _eop.opType ) {};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoOp(){};
|
||||
|
||||
/// Arity: number of operands
|
||||
Arity readArity() const {return arity;};
|
||||
/// getType: number of operands it takes and individuals it produces
|
||||
OpType getType() const {return opType;};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
|
|
@ -88,8 +90,8 @@ public:
|
|||
//@}
|
||||
|
||||
private:
|
||||
/// arity is the number of operands it takes
|
||||
Arity arity;
|
||||
/// OpType is the type of the operator: how many operands it takes and how many it produces
|
||||
OpType opType;
|
||||
|
||||
};
|
||||
|
||||
|
|
@ -111,6 +113,40 @@ public:
|
|||
/// Dtor
|
||||
~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.
|
||||
*/
|
||||
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
|
||||
|
|
@ -136,7 +172,7 @@ public:
|
|||
|
||||
/// Ctor
|
||||
eoMonOp( )
|
||||
:eoOp<EOType>( unary ) {};
|
||||
: eoOp<EOType>( eoOp<EOType>::unary ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoMonOp( const eoMonOp& _emop )
|
||||
|
|
@ -159,45 +195,41 @@ public:
|
|||
*/
|
||||
virtual string className() const {return "eoMonOp";};
|
||||
//@}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#include <eoPop.h>
|
||||
/** eoNaryOp is the N-ary operator: genetic operator that takes
|
||||
several EOs. It could be called an {\em orgy} operator. It's a general operator
|
||||
that takes any number of inputs and spits out any number of outputs
|
||||
// some forward declarations
|
||||
template<class EOT>
|
||||
class eoIndiSelector;
|
||||
|
||||
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>
|
||||
class eoNaryOp: public eoOp<EOType> {
|
||||
template<class EOT>
|
||||
class eoGeneralOp: public eoOp<EOT>
|
||||
{
|
||||
public:
|
||||
|
||||
/// Ctor
|
||||
eoNaryOp( )
|
||||
:eoOp<EOType>( Nary ) {};
|
||||
/// Ctor that honors its superclass
|
||||
eoGeneralOp(): eoOp<EOT>( eoOp<EOT>::general ) {};
|
||||
|
||||
/// Copy Ctor
|
||||
eoNaryOp( const eoNaryOp& _emop )
|
||||
: eoOp<EOType>( _emop ){};
|
||||
/// Virtual dtor
|
||||
virtual ~eoGeneralOp () {};
|
||||
|
||||
/// Dtor
|
||||
~eoNaryOp() {};
|
||||
|
||||
/** applies randomly operator, to the object.
|
||||
*/
|
||||
virtual void operator()( const eoPop<EOType> & _in, eoPop<EOType> _out ) const = 0;
|
||||
|
||||
/** @name Methods from eoObject
|
||||
readFrom and printOn are directly inherited from eoObject.
|
||||
/** Method that really does the stuff. Applies the genetic operator
|
||||
to a individuals dispensed by an eoIndividualSelector,
|
||||
and puts the results in the eoIndividualInserter.
|
||||
Any number of inputs can be requested and any number of outputs
|
||||
can be produced.
|
||||
*/
|
||||
//@{
|
||||
/** Inherited from eoObject
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoNaryOp";};
|
||||
//@}
|
||||
virtual void operator()( eoIndiSelector<EOT>& _in,
|
||||
eoInserter<EOT>& _out) const = 0;
|
||||
|
||||
virtual string className() const {return "eoGeneralOp";};
|
||||
};
|
||||
//@}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ template <class EOT> class eoStochTournament: public eoSelectOne<EOT>
|
|||
public:
|
||||
|
||||
///
|
||||
eoStochTournament(float _Trate = 1.0 ):eoSelectOne(), Trate(_Trate) {
|
||||
eoStochTournament(float _Trate = 1.0 ):eoSelectOne<EOT>(), Trate(_Trate) {
|
||||
// consistency check
|
||||
if (Trate < 0.5) {
|
||||
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
|
||||
DON'T cast the fitness to a float, use the EOT comparator! */
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
||||
unsigned i1 = rng.random(pop.size()),
|
||||
i2 = rng.random(pop.size());
|
||||
|
||||
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 ];
|
||||
}
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop)
|
||||
{
|
||||
return stochastic_tournament(pop, Trate)();
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -46,13 +46,13 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class EOT>
|
||||
class eoUniformXOver: public eoBinOp< EOT >
|
||||
class eoUniformXOver: public eoQuadraticOp< EOT >
|
||||
{
|
||||
public:
|
||||
|
||||
///
|
||||
eoUniformXOver( float _rate = 0.5 ):
|
||||
eoBinOp< EOT > ( ), rate( _rate ) {
|
||||
eoQuadraticOp< EOT > ( ), rate( _rate ) {
|
||||
if (rate < 0 || rate > 1)
|
||||
runtime_error("UxOver --> invalid rate");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,11 +41,11 @@
|
|||
and interchanges it
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoXOver2: public eoBinOp<EOT> {
|
||||
class eoXOver2: public eoQuadraticOp<EOT> {
|
||||
public:
|
||||
///
|
||||
eoXOver2()
|
||||
: eoBinOp< EOT >(){};
|
||||
: eoQuadraticOp< EOT >(){};
|
||||
|
||||
///
|
||||
virtual ~eoXOver2() {};
|
||||
|
|
|
|||
Reference in a new issue