Mak: Added the eoQuadratic Op and more ... (and I hate VI)

This commit is contained in:
mac 2000-02-16 15:05:19 +00:00
commit 9fba2bfbb9
13 changed files with 1247 additions and 1124 deletions

68
eo/src/compatibility.h Normal file
View 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

View file

@ -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.
@ -318,7 +318,7 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
};
//-----------------------------------------------------------------------------
//@}
#endif eoBitOp_h

View file

@ -1,103 +1,113 @@
// -*- 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> // vector
#include <eoUniform.h> // eoUniform
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
#include <eoPop.h> // eoPop
#include <eoPopOps.h> // eoTransform
#include <eoOpSelector.h> // eoOpSelector
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 eoMonPopOp<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)
{
for (unsigned i = 0; i < pop.size(); i++) {
eoOp<Chrom>* op = opSel.Op();
switch (op->readArity()) {
case unary:
{
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
(*monop)( pop[i] );
break;
}
case binary:
{
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
eoUniform<unsigned> u(0, pop.size() );
(*binop)(pop[i], pop[ u() ] );
break;
}
case Nary:
{
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 );
break;
}
}
}
};
/// The class name.
string classname() const { return "eoBreeder"; }
private:
eoOpSelector<Chrom>& opSel;
};
//-----------------------------------------------------------------------------
#endif eoBreeder_h
// -*- 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> // vector
#include <eoUniform.h> // eoUniform
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
#include <eoPop.h> // eoPop
#include <eoPopOps.h> // eoTransform
#include <eoOpSelector.h> // eoOpSelector
#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 eoMonPopOp<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);
eoUniform<unsigned> u(0, pop.size() );
(*binop)(pop[i], pop[ u() ] );
break;
}
case eoOp<Chrom>::quadratic:
{
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;
}
}
}
};
/// The class name.
string classname() const { return "eoBreeder"; }
private:
eoOpSelector<Chrom>& opSel;
};
//-----------------------------------------------------------------------------
#endif eoBreeder_h

View file

@ -1,78 +1,70 @@
// -*- 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 <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.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(), Tsize(_Tsize) {
// consistency check
if (Tsize < 2) {
cout << "Warning, Tournament size shoudl 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] ;
}
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoDetTournament";};
private:
unsigned Tsize;
};
//-----------------------------------------------------------------------------
#endif eoDetTournament_h
// -*- 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 <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include "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(), Tsize(_Tsize) {
// consistency check
if (Tsize < 2) {
cout << "Warning, Tournament size should be >= 2\nAdjusted\n";
Tsize = 2;
}
}
virtual const EOT& operator()(const eoPop<EOT>& pop)
{
return deterministic_tournament(pop, Tsize)();
}
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoDetTournament";};
private:
unsigned Tsize;
};
//-----------------------------------------------------------------------------
#endif eoDetTournament_h

View file

@ -1,59 +1,59 @@
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
-----------------------------------------------------------------------------
eoEvalFuncPtr.h
Converts a classical C fitness evaluation function into a fitness
evaluation object
(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 EOEVALFUNCPTR_H
#define EOEVALFUNCPTR_H
#include <eoEvalFunc.h>
/** EOEvalFuncPtr: 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 eoEvalFuncPtr: public eoEvalFunc<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.
*/
eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) )
: eoEvalFunc<EOT>(), evalFunc( _eval ) {};
/// Effectively applies the evaluation function to an EO
virtual void operator() ( EOT & _eo ) const {
_eo.fitness((*evalFunc)( _eo ));
};
private:
EOFitT (* evalFunc )( EOT& );
};
#endif
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
-----------------------------------------------------------------------------
eoEvalFuncPtr.h
Converts a classical C fitness evaluation function into a fitness
evaluation object
(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 EOEVALFUNCPTR_H
#define EOEVALFUNCPTR_H
#include <eoEvalFunc.h>
/** EOEvalFuncPtr: 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 eoEvalFuncPtr: public eoEvalFunc<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.
*/
eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) )
: eoEvalFunc<EOT>(), evalFunc( _eval ) {};
/// Effectively applies the evaluation function to an EO
virtual void operator() ( EOT & _eo ) const {
_eo.fitness((*evalFunc)( _eo ));
};
private:
EOFitT (* evalFunc )( const EOT& );
};
#endif

View file

@ -1,139 +1,171 @@
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
-----------------------------------------------------------------------------
eoGOpSelector.h
Base class for generalized (n-inputs, n-outputs) operator selectors.
Includes code and variables that contain operators and rates
(c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/
#ifndef eoGOpSelector_h
#define eoGOpSelector_h
//-----------------------------------------------------------------------------
#include <vector> // vector
#include <iterator>
#include <eoUniform.h> // eoUniform
#include <eoGeneralOp.h> // eoOp, eoMonOp, eoBinOp
#include <eoPop.h> // eoPop
#include <eoPopOps.h> // eoTransform
#include <eoOpSelector.h> // eoOpSelector
#include <list>
#include "eoRNG.h"
using namespace std;
/** Base class for alternative selectors, which use the generalized operator
interface */
template<class EOT>
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
{
public:
/// Dtor
virtual ~eoGOpSelector() {
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
i != ownOpList.begin(); i ++ ) {
delete *i;
}
}
/// Add any kind of operator to the operator mix, with an argument
virtual ID addOp( eoOp<EOT>& _op, float _arg ) {
eoGeneralOp<EOT>* op = dynamic_cast<eoGeneralOp<EOT>*>(&_op);
// if it's not a general op, it's a "old" op; create a wrapped op from it
// and keep it on a list to delete them afterwards
// will use auto_ptr when they're readily available
if (op == 0) {
switch(_op.readArity())
{
case unary :
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
break;
case binary :
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
break;
}
ownOpList.push_back( op );
}
iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer
if (result == end())
{
push_back(op);
rates.push_back(_arg);
return size();
}
// else
*result = op;
ID id = result - begin();
rates[id] = _arg;
return id;
}
/** Retrieve the operator using its integer handle
@param _id The id number. Should be a valid id, or an exception
will be thrown
@return a reference of the operator corresponding to that id.
*/
virtual const eoOp<EOT>& getOp( ID _id )
{
return *operator[](_id);
}
///
virtual void deleteOp( ID _id )
{
operator[](_id) = 0; // TODO, check oplist and clear it there too.
rates[_id] = 0.0;
}
///
virtual eoOp<EOT>* Op()
{
return &selectOp();
}
///
virtual eoGeneralOp<EOT>& selectOp() = 0;
///
virtual string className() const { return "eoGOpSelector"; };
///
void printOn(ostream& _os) const {
_os << className() << endl;
for ( unsigned i=0; i!= rates.size(); i++ ) {
_os << *(operator[](i)) << "\t" << rates[i] << endl;
}
}
protected :
vector<float> rates;
list< eoGeneralOp<EOT>* > ownOpList;
};
#endif eoGOpSelector_h
/** -*- 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 "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 vector<eoGeneralOp<EOT>*>
{
public:
/// Dtor
virtual ~eoGOpSelector() {
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
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 );
// 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
will be thrown
@return a reference of the operator corresponding to that id.
*/
virtual const eoOp<EOT>& getOp( ID _id )
{
return *operator[](_id);
}
///
virtual void deleteOp( ID _id );
// implemented below
///
virtual eoOp<EOT>* Op()
{
return &selectOp();
}
///
virtual eoGeneralOp<EOT>& selectOp() = 0;
///
virtual string className() const { return "eoGOpSelector"; };
///
void printOn(ostream& _os) const {
_os << className() << endl;
for ( unsigned i=0; i!= rates.size(); i++ ) {
_os << *(operator[](i)) << "\t" << rates[i] << endl;
}
}
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

View file

@ -1,97 +1,97 @@
// -*- 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 <functional> //
#include <numeric> // accumulate
#include "eoPopOps.h"
#include "eoRNG.h"
//-----------------------------------------------------------------------------
/// eoLottery: a selection method.
/// 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) {}
/** 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?
*/
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());
// test of consistency
if (breeders.size() >= target) {
throw("Problem in eoLottery: already too many offspring");
}
// selection of chromosomes
while (breeders.size() < target) {
unsigned indloc = rng.roulette_wheel(score, total);
breeders.push_back(pop[indloc]);
}
}
/// 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";};
//@}
private:
double rate; // selection rate
};
//-----------------------------------------------------------------------------
#endif eoLottery_h
// -*- 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 <functional> //
#include <numeric> // accumulate
#include "selectors.h"
#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 eoBinPopOp<EOT>
{
public:
/// (Default) Constructor.
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 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)
{
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)
{
breeders.push_back(roulette_wheel(pop, total));
}
}
double Rate(void) const { return rate; }
private:
double rate; // selection rate
};
//-----------------------------------------------------------------------------
#endif eoLottery_h

View file

@ -1,99 +1,99 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoMultiBinOp.h
// Class that combines several binary or unary operators
// (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 _EOMULTIBINOP_h
#define _EOMULTIBINOP_h
#include <iterator>
#include <eoOp.h>
/** MultiMonOp combines several monary operators. By itself, it does nothing to the
EO it´s handled*/
template <class EOT>
class eoMultiBinOp: public eoBinOp<EOT> {
public:
/// Ctor from an already existing op
eoMultiBinOp( const eoBinOp<EOT>* _op )
: eoBinOp< EOT >( ), vOp(){
vOp.push_back( _op );
};
///
eoMultiBinOp( )
: eoBinOp< EOT >( ), vOp(){};
/// Ads a new operator
void adOp( const eoOp<EOT>* _op ){
vOp.push_back( _op );
};
/// needed virtual dtor
virtual ~eoMultiBinOp() {};
///
/// Applies all operators to the EO
virtual void operator()( EOT& _eo1, EOT& _eo2 ) const {
if ( vOp.begin() != vOp.end() ) { // which would mean it's empty
for ( vector< const eoOp<EOT>* >::const_iterator i = vOp.begin();
i != vOp.end(); i++ ) {
// Admits only unary or binary operator
switch ((*i)->readArity()) {
case unary:
{
const eoMonOp<EOT>* monop = static_cast<const eoMonOp<EOT>* >(*i);
(*monop)( _eo1 );
(*monop)( _eo2 );
break;
}
case binary:
{
const eoBinOp<EOT>* binop = static_cast<const eoBinOp<EOT>* >(*i);
(*binop)( _eo1, _eo2 );
break;
}
}
}
}
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoMultiBinOp";};
//@}
private:
/// uses pointers to base class since operators can be unary or binary
vector< const eoOp<EOT>* > vOp;
};
#endif
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoMultiBinOp.h
// Class that combines several binary or unary operators
// (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 _EOMULTIBINOP_h
#define _EOMULTIBINOP_h
#include <iterator>
#include <eoOp.h>
/** MultiMonOp combines several monary operators. By itself, it does nothing to the
EO it´s handled*/
template <class EOT>
class eoMultiBinOp: public eoBinOp<EOT> {
public:
/// Ctor from an already existing op
eoMultiBinOp( const eoBinOp<EOT>* _op )
: eoBinOp< EOT >( ), vOp(){
vOp.push_back( _op );
};
///
eoMultiBinOp( )
: eoBinOp< EOT >( ), vOp(){};
/// Ads a new operator
void adOp( const eoOp<EOT>* _op ){
vOp.push_back( _op );
};
/// needed virtual dtor
virtual ~eoMultiBinOp() {};
///
/// Applies all operators to the EO
virtual void operator()( EOT& _eo1, EOT& _eo2 ) const {
if ( vOp.begin() != vOp.end() ) { // which would mean it's empty
for ( vector< const eoOp<EOT>* >::const_iterator i = vOp.begin();
i != vOp.end(); i++ ) {
// Admits only unary or binary operator
switch ((*i)->readArity()) {
case unary:
{
const eoMonOp<EOT>* monop = static_cast<const eoMonOp<EOT>* >(*i);
(*monop)( _eo1 );
(*monop)( _eo2 );
break;
}
case binary:
{
const eoBinOp<EOT>* binop = static_cast<const eoBinOp<EOT>* >(*i);
(*binop)( _eo1, _eo2 );
break;
}
}
}
}
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoMultiBinOp";};
//@}
private:
/// uses pointers to base class since operators can be unary or binary
vector< const eoOp<EOT>* > vOp;
};
#endif

View file

@ -1,69 +1,68 @@
/** -*- 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 EOOBJECT_H
#define EOOBJECT_H
//-----------------------------------------------------------------------------
#include <eoData.h> // For limits definition
#include <iostream> // istream, ostream
#include <string> // string
using namespace std;
//-----------------------------------------------------------------------------
// eoObject
//-----------------------------------------------------------------------------
/**
This is the base class for the whole hierarchy; an eoObject defines
basically an interface for the whole hierarchy: each object should
know its name (#className#). Previously, this object defined a print and read
interface, but it´s been moved to eoPrintable and eoPersistent.
*/
class eoObject
{
public:
/// Default Constructor.
eoObject() {}
/// Copy constructor.
eoObject( const eoObject& ) {}
/// Virtual dtor. They are needed in virtual class hierarchies.
virtual ~eoObject() {}
/** 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 string className() const { return "eoObject"; }
};
#endif EOOBJECT_H
// -*- 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
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 EOOBJECT_H
#define EOOBJECT_H
//-----------------------------------------------------------------------------
#include <eoData.h> // For limits definition
#include <iostream> // istream, ostream
#include <string> // string
#include "compatibility.h"
using namespace std;
//-----------------------------------------------------------------------------
// eoObject
//-----------------------------------------------------------------------------
/**
This is the base class for the whole hierarchy; an eoObject defines
basically an interface for the whole hierarchy: each object should
know its name (#className#). Previously, this object defined a print and read
interface, but it´s been moved to eoPrintable and eoPersistent.
*/
class eoObject
{
public:
/// Default Constructor.
eoObject() {}
/// Copy constructor.
eoObject( const eoObject& ) {}
/// Virtual dtor. They are needed in virtual class hierarchies.
virtual ~eoObject() {}
/** 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 string className() const { return "eoObject"; }
};
#endif EOOBJECT_H

View file

@ -1,203 +1,235 @@
// -*- 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 _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#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.
@author GeNeura Team
@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
* be an eoObject, but in any case, they are type-specific; each kind of evolvable object
* can have its own operators
*/
template<class EOType>
class eoOp: public eoObject, public eoPrintable {
public:
/// Ctor
eoOp( Arity _arity = unary )
:arity( _arity ) {};
/// Copy Ctor
eoOp( const eoOp& _eop )
:arity( _eop.arity ) {};
/// Needed virtual destructor
virtual ~eoOp(){};
/// Arity: number of operands
Arity readArity() const {return arity;};
/** @name Methods from eoObject */
//@{
/**
* Write object. It's called printOn since it prints the object _on_ a stream.
* @param _os A ostream.
*/
virtual void printOn(ostream& _os) const {
_os << className();
// _os << arity;
};
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoOp";};
//@}
private:
/// arity is the number of operands it takes
Arity arity;
};
/** Binary genetic operator: subclasses eoOp, and defines
basically the operator() with two operands
*/
template<class EOType>
class eoBinOp: public eoOp<EOType> {
public:
/// Ctor
eoBinOp()
:eoOp<EOType>( binary ) {};
/// Copy Ctor
eoBinOp( const eoBinOp& _ebop )
: eoOp<EOType>( _ebop ){};
/// Dtor
~eoBinOp () {};
/** applies operator, to the object. Modifies both operands.
*/
virtual void operator()( EOType& _eo1, 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";};
//@}
};
/** eoMonOp is the monary operator: genetic operator that takes
only one EO
*/
template <class EOType>
class eoMonOp: public eoOp<EOType> {
public:
/// Ctor
eoMonOp( )
:eoOp<EOType>( unary ) {};
/// Copy Ctor
eoMonOp( const eoMonOp& _emop )
: eoOp<EOType>( _emop ){};
/// Dtor
~eoMonOp() {};
/** applies randomly operator, to the object. If arity is more than 1,
* keeps a copy of the operand in a cache.
*/
virtual void operator()( EOType& _eo1) const = 0;
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
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
*/
template <class EOType>
class eoNaryOp: public eoOp<EOType> {
public:
/// Ctor
eoNaryOp( )
:eoOp<EOType>( Nary ) {};
/// Copy Ctor
eoNaryOp( const eoNaryOp& _emop )
: eoOp<EOType>( _emop ){};
/// 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.
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoNaryOp";};
//@}
};
//@}
#endif
// -*- 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 _eoOp_H
#define _eoOp_H
#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 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.
@author GeNeura Team
@version 0.1
@see eoOpFactory
*/
/** Abstract data types for EO operators.
* Genetic operators act on chromosomes, changing them. The type to instantiate them should
* be an eoObject, but in any case, they are type-specific; each kind of evolvable object
* can have its own operators
*/
template<class EOType>
class eoOp: public eoObject, public eoPrintable {
public:
//@{
enum OpType { unary = 0, binary = 1, quadratic = 2, general = 3};
///
/// Ctor
eoOp(OpType _type)
:opType( _type ) {};
/// Copy Ctor
eoOp( const eoOp& _eop )
:opType( _eop.opType ) {};
/// Needed virtual destructor
virtual ~eoOp(){};
/// getType: number of operands it takes and individuals it produces
OpType getType() const {return opType;};
/** @name Methods from eoObject */
//@{
/**
* Write object. It's called printOn since it prints the object _on_ a stream.
* @param _os A ostream.
*/
virtual void printOn(ostream& _os) const {
_os << className();
// _os << arity;
};
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoOp";};
//@}
private:
/// OpType is the type of the operator: how many operands it takes and how many it produces
OpType opType;
};
/** Binary genetic operator: subclasses eoOp, and defines
basically the operator() with two operands
*/
template<class EOType>
class eoBinOp: public eoOp<EOType> {
public:
/// Ctor
eoBinOp()
:eoOp<EOType>( binary ) {};
/// Copy Ctor
eoBinOp( const eoBinOp& _ebop )
: eoOp<EOType>( _ebop ){};
/// 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;
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoBinOp";};
//@}
};
/** eoMonOp is the monary operator: genetic operator that takes
only one EO
*/
template <class EOType>
class eoMonOp: public eoOp<EOType> {
public:
/// Ctor
eoMonOp( )
: eoOp<EOType>( eoOp<EOType>::unary ) {};
/// Copy Ctor
eoMonOp( const eoMonOp& _emop )
: eoOp<EOType>( _emop ){};
/// Dtor
~eoMonOp() {};
/** applies randomly operator, to the object. If arity is more than 1,
* keeps a copy of the operand in a cache.
*/
virtual void operator()( EOType& _eo1) const = 0;
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
virtual string className() const {return "eoMonOp";};
//@}
};
// 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 EOT>
class eoGeneralOp: public eoOp<EOT>
{
public:
/// Ctor that honors its superclass
eoGeneralOp(): eoOp<EOT>( eoOp<EOT>::general ) {};
/// Virtual dtor
virtual ~eoGeneralOp () {};
/** 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.
*/
virtual void operator()( eoIndiSelector<EOT>& _in,
eoInserter<EOT>& _out) const = 0;
virtual string className() const {return "eoGeneralOp";};
};
#endif

View file

@ -1,78 +1,68 @@
// -*- 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 <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h>
//-----------------------------------------------------------------------------
/** 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(), Trate(_Trate) {
// consistency check
if (Trate < 0.5) {
cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
Trate = 0.55;
}
}
/** 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 ];
}
}
private:
float Trate;
};
//-----------------------------------------------------------------------------
#endif eoDetTournament_h
// -*- 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 <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h>
//-----------------------------------------------------------------------------
/** 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) {
cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
Trate = 0.55;
}
}
/** 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)
{
return stochastic_tournament(pop, Trate)();
}
private:
float Trate;
};
//-----------------------------------------------------------------------------
#endif eoDetTournament_h

View file

@ -1,90 +1,90 @@
// -*- 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 <eoUniform.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 eoBinOp< EOT >
{
public:
///
eoUniformXOver( float _rate = 0.5 ):
eoBinOp< EOT > ( ), rate( _rate ) {
// -*- 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 <eoUniform.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)
runtime_error("UxOver --> invalid rate");
}
///
void operator() ( EOT& chrom1, EOT& chrom2 ) const {
unsigned end = min(chrom1.length(),chrom2.length()) - 1;
// select bits to change
eoUniform<float> rnd(0, 1);
// aply changes
for (unsigned bit = 0; bit < end; bit++)
if (rnd() < rate)
swap(chrom1[ bit], chrom2[ bit]);
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoUniformXOver";};
//@}
private:
float rate; /// rate of uniform crossover
};
//-----------------------------------------------------------------------------
#endif
runtime_error("UxOver --> invalid rate");
}
///
void operator() ( EOT& chrom1, EOT& chrom2 ) const {
unsigned end = min(chrom1.length(),chrom2.length()) - 1;
// select bits to change
eoUniform<float> rnd(0, 1);
// aply changes
for (unsigned bit = 0; bit < end; bit++)
if (rnd() < rate)
swap(chrom1[ bit], chrom2[ bit]);
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoUniformXOver";};
//@}
private:
float rate; /// rate of uniform crossover
};
//-----------------------------------------------------------------------------
#endif

View file

@ -1,106 +1,106 @@
// -*- 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 <eoUniform.h>
/** 2-point crossover: takes the genes in the central section of two EOs
and interchanges it
*/
template <class EOT>
class eoXOver2: public eoBinOp<EOT> {
public:
///
eoXOver2()
: eoBinOp< EOT >(){};
///
virtual ~eoXOver2() {};
///
virtual void operator()( EOT& _eo1,
EOT& _eo2 ) const {
unsigned len1 = _eo1.length(), len2 = _eo2.length(),
len= (len1 > len2)?len2:len1;
eoUniform<unsigned> uniform( 0, len );
unsigned pos1 = uniform(), pos2 = uniform() ;
applyAt( _eo1, _eo2, pos1, pos2 );
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
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 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
// -*- 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 <eoUniform.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;
eoUniform<unsigned> uniform( 0, len );
unsigned pos1 = uniform(), pos2 = uniform() ;
applyAt( _eo1, _eo2, pos1, pos2 );
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
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 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