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 */ /** eoBinCrossover --> classic crossover */
template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom> template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
{ {
public: public:
/// The class name. /// The class name.
@ -205,7 +205,7 @@ template<class Chrom> class eoBinCrossover: public eoBinOp<Chrom>
/** eoBinNxOver --> n-point crossover */ /** eoBinNxOver --> n-point crossover */
template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom> template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
{ {
public: public:
/// (Defualt) Constructor. /// (Defualt) Constructor.
@ -263,7 +263,7 @@ template<class Chrom> class eoBinNxOver: public eoBinOp<Chrom>
/** eoBinGxOver --> gene crossover */ /** eoBinGxOver --> gene crossover */
template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom> template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
{ {
public: public:
/// Constructor. /// Constructor.
@ -318,7 +318,7 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//@} //@}
#endif eoBitOp_h #endif eoBitOp_h

View file

@ -1,103 +1,113 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoBreeder.h // eoBreeder.h
// Takes two populations and mixes them // Takes two populations and mixes them
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoBreeder_h #ifndef eoBreeder_h
#define eoBreeder_h #define eoBreeder_h
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <vector> // vector #include <vector> // vector
#include <eoUniform.h> // eoUniform #include <eoUniform.h> // eoUniform
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp #include <eoOp.h> // eoOp, eoMonOp, eoBinOp
#include <eoPop.h> // eoPop #include <eoPop.h> // eoPop
#include <eoPopOps.h> // eoTransform #include <eoPopOps.h> // eoTransform
#include <eoOpSelector.h> // eoOpSelector #include <eoOpSelector.h> // eoOpSelector
using namespace std; #include "eoRandomIndiSelector.h"
#include "eoBackInserter.h"
/*****************************************************************************
* eoBreeder: transforms a population using genetic operators. * using namespace std;
* For every operator there is a rated to be applyed. *
*****************************************************************************/ /*****************************************************************************
* eoBreeder: transforms a population using genetic operators. *
template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom> * For every operator there is a rated to be applyed. *
{ *****************************************************************************/
public:
/// Default constructor. template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {} {
public:
/// Destructor. /// Default constructor.
virtual ~eoBreeder() {} eoBreeder( eoOpSelector<Chrom>& _opSel): opSel( _opSel ) {}
/** /// Destructor.
* Transforms a population. virtual ~eoBreeder() {}
* @param pop The population to be transformed.
*/ /**
void operator()(eoPop<Chrom>& pop) * Transforms a population.
{ * @param pop The population to be transformed.
for (unsigned i = 0; i < pop.size(); i++) { */
eoOp<Chrom>* op = opSel.Op(); void operator()(eoPop<Chrom>& pop)
switch (op->readArity()) { {
case unary: size_t orgsize = pop.size();
{
eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op); for (unsigned i = 0; i < pop.size(); i++) {
(*monop)( pop[i] ); eoOp<Chrom>* op = opSel.Op();
break; switch (op->getType()) {
} case eoOp<Chrom>::unary:
case binary: {
{ eoMonOp<Chrom>* monop = static_cast<eoMonOp<Chrom>* >(op);
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op); (*monop)( pop[i] );
eoUniform<unsigned> u(0, pop.size() ); break;
(*binop)(pop[i], pop[ u() ] ); }
break; case eoOp<Chrom>::binary:
} {
case Nary: eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
{ eoUniform<unsigned> u(0, pop.size() );
eoNaryOp<Chrom>* Nop = static_cast<eoNaryOp<Chrom>* >(op); (*binop)(pop[i], pop[ u() ] );
eoUniform<unsigned> u(0, pop.size() ); break;
eoPop<Chrom> inVec, outVec; }
inVec.push_back( pop[i] ); case eoOp<Chrom>::quadratic:
unsigned numberOfOperands = u(); {
for ( unsigned i = 0; i < numberOfOperands; i ++ ) { eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
inVec.push_back( pop[ u() ] );
} eoUniform<unsigned> u(0, pop.size() );
(*Nop)( inVec, outVec ); (*Qop)(pop[i], pop[ u() ] );
break; break;
} }
} case eoOp<Chrom>::general :
} {
}; eoGeneralOp<Chrom>* Gop = static_cast<eoGeneralOp<Chrom>* >(op);
/// The class name. eoRandomIndiSelector<Chrom> selector;
string classname() const { return "eoBreeder"; } eoBackInserter<Chrom> inserter;
private: (*Gop)(selector(pop, orgsize, i), inserter(pop));
eoOpSelector<Chrom>& opSel; break;
}
}; }
}
//----------------------------------------------------------------------------- };
#endif eoBreeder_h /// 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; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoDetTournament.h // eoDetTournament.h
// (c) GeNeura Team, 1998 - EEAAX 1999 // (c) GeNeura Team, 1998 - EEAAX 1999
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr Marc.Schoenauer@polytechnique.fr
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoDetTournament_h #ifndef eoDetTournament_h
#define eoDetTournament_h #define eoDetTournament_h
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <functional> // #include <functional> //
#include <numeric> // accumulate #include <numeric> // accumulate
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT #include <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h> #include "selectors.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** eoDetTournament: a selection method that selects ONE individual by /** eoDetTournament: a selection method that selects ONE individual by
deterministic tournament deterministic tournament
-MS- 24/10/99 */ -MS- 24/10/99 */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <class EOT> class eoDetTournament: public eoSelectOne<EOT> template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
{ {
public: public:
/// (Default) Constructor. /// (Default) Constructor.
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) {
// consistency check // consistency check
if (Tsize < 2) { if (Tsize < 2) {
cout << "Warning, Tournament size shoudl be >= 2\nAdjusted\n"; cout << "Warning, Tournament size should be >= 2\nAdjusted\n";
Tsize = 2; Tsize = 2;
} }
} }
/** DANGER: if you want to be able to minimize as well as maximizem virtual const EOT& operator()(const eoPop<EOT>& pop)
DON'T cast the fitness to a float, use the EOT comparator! */ {
virtual const EOT& operator()(const eoPop<EOT>& pop) { return deterministic_tournament(pop, Tsize)();
unsigned best = rng.random(pop.size()); // random individual }
for (unsigned i = 0; i<Tsize-1; i++) { /** Inherited from eoObject
unsigned tmp = rng.random(pop.size()); @see eoObject
if (pop[best] < pop[tmp]) */
best = tmp; string className() const {return "eoDetTournament";};
}
return pop[best] ; private:
} unsigned Tsize;
};
/** Inherited from eoObject
@see eoObject //-----------------------------------------------------------------------------
*/
string className() const {return "eoDetTournament";}; #endif eoDetTournament_h
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; -*- /** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
eoEvalFuncPtr.h eoEvalFuncPtr.h
Converts a classical C fitness evaluation function into a fitness Converts a classical C fitness evaluation function into a fitness
evaluation object evaluation object
(c) GeNeura Team, 2000 (c) GeNeura Team, 2000
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef EOEVALFUNCPTR_H #ifndef EOEVALFUNCPTR_H
#define EOEVALFUNCPTR_H #define EOEVALFUNCPTR_H
#include <eoEvalFunc.h> #include <eoEvalFunc.h>
/** EOEvalFuncPtr: This class /** EOEvalFuncPtr: This class
* takes an existing function pointer and converts it into a evaluation * 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 class. That way, old style C or C++ functions can be adapted to EO
* function classes. * function classes.
*/ */
template< class EOT > template< class EOT >
struct eoEvalFuncPtr: public eoEvalFunc<EOT> { struct eoEvalFuncPtr: public eoEvalFunc<EOT> {
/** Applies the function to the chromosome and sets the fitness of the /** Applies the function to the chromosome and sets the fitness of the
Chrom. Thus, the evaluation function need not be worried about that. Chrom. Thus, the evaluation function need not be worried about that.
@param _eval pointer to the evaluation function, takes a EOT as an @param _eval pointer to the evaluation function, takes a EOT as an
argument and returns the fitness argument and returns the fitness
@return the evaluated fitness for that object. @return the evaluated fitness for that object.
*/ */
eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) ) eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) )
: eoEvalFunc<EOT>(), evalFunc( _eval ) {}; : eoEvalFunc<EOT>(), evalFunc( _eval ) {};
/// Effectively applies the evaluation function to an EO /// Effectively applies the evaluation function to an EO
virtual void operator() ( EOT & _eo ) const { virtual void operator() ( EOT & _eo ) const {
_eo.fitness((*evalFunc)( _eo )); _eo.fitness((*evalFunc)( _eo ));
}; };
private: private:
EOFitT (* evalFunc )( EOT& ); EOFitT (* evalFunc )( const EOT& );
}; };
#endif #endif

View file

@ -1,139 +1,171 @@
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- /** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
eoGOpSelector.h eoGOpSelector.h
Base class for generalized (n-inputs, n-outputs) operator selectors. Base class for generalized (n-inputs, n-outputs) operator selectors.
Includes code and variables that contain operators and rates Includes code and variables that contain operators and rates.
Also included eoProportionalGOpSelector and eoSequentialGOpSelector, that offer
(c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000 a few concrete implementations.
This library is free software; you can redistribute it and/or (c) Maarten Keijzer, GeNeura Team 1998, 1999, 2000
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either This library is free software; you can redistribute it and/or
version 2 of the License, or (at your option) any later version. modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
This library is distributed in the hope that it will be useful, version 2 of the License, or (at your option) any later version.
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU This library is distributed in the hope that it will be useful,
Lesser General Public License for more details. but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
You should have received a copy of the GNU Lesser General Public Lesser General Public License for more details.
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Contact: todos@geneura.ugr.es, http://geneura.ugr.es 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
#ifndef eoGOpSelector_h
//----------------------------------------------------------------------------- #define eoGOpSelector_h
#include <vector> // vector //-----------------------------------------------------------------------------
#include <iterator>
#include <eoUniform.h> // eoUniform #include <list>
#include <eoGeneralOp.h> // eoOp, eoMonOp, eoBinOp #include "eoOpSelector.h"
#include <eoPop.h> // eoPop #include "eoWrappedOps.h" // for eoCombinedOp
#include <eoPopOps.h> // eoTransform #include "eoRNG.h"
#include <eoOpSelector.h> // eoOpSelector
#include <list> using namespace std;
#include "eoRNG.h"
/** Base class for alternative selectors, which use the generalized operator
using namespace std; interface. eoGOpBreeders expects this class */
template<class EOT>
/** Base class for alternative selectors, which use the generalized operator class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
interface */ {
public:
template<class EOT>
class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*> /// Dtor
{ virtual ~eoGOpSelector() {
public: for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
i != ownOpList.end(); i++ ) {
/// Dtor delete *i;
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 );
} // implementation can be found below
/// Add any kind of operator to the operator mix, with an argument /** Retrieve the operator using its integer handle
virtual ID addOp( eoOp<EOT>& _op, float _arg ) { @param _id The id number. Should be a valid id, or an exception
eoGeneralOp<EOT>* op = dynamic_cast<eoGeneralOp<EOT>*>(&_op); will be thrown
@return a reference of the operator corresponding to that id.
// 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 virtual const eoOp<EOT>& getOp( ID _id )
// will use auto_ptr when they're readily available {
if (op == 0) { return *operator[](_id);
switch(_op.readArity()) }
{
case unary : ///
op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op)); virtual void deleteOp( ID _id );
break; // implemented below
case binary :
op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op)); ///
break; virtual eoOp<EOT>* Op()
} {
ownOpList.push_back( op ); return &selectOp();
} }
iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer ///
virtual eoGeneralOp<EOT>& selectOp() = 0;
if (result == end())
{ ///
push_back(op); virtual string className() const { return "eoGOpSelector"; };
rates.push_back(_arg);
return size(); ///
} void printOn(ostream& _os) const {
// else _os << className() << endl;
for ( unsigned i=0; i!= rates.size(); i++ ) {
*result = op; _os << *(operator[](i)) << "\t" << rates[i] << endl;
ID id = result - begin(); }
rates[id] = _arg; }
return id;
}
const vector<float>& getRates(void) const { return rates; }
/** Retrieve the operator using its integer handle
@param _id The id number. Should be a valid id, or an exception private :
will be thrown vector<float> rates;
@return a reference of the operator corresponding to that id. list< eoGeneralOp<EOT>* > ownOpList;
*/ };
virtual const eoOp<EOT>& getOp( ID _id )
{ /* Implementation of longish functions defined above */
return *operator[](_id);
} template <class EOT>
inline eoOpSelector<EOT>::ID eoGOpSelector<EOT>::addOp( eoOp<EOT>& _op, float _arg )
/// {
virtual void deleteOp( ID _id )
{ eoGeneralOp<EOT>* op;
operator[](_id) = 0; // TODO, check oplist and clear it there too.
rates[_id] = 0.0; if (_op.getType() == eoOp<EOT>::general)
} {
op = static_cast<eoGeneralOp<EOT>*>(&_op);
/// }
virtual eoOp<EOT>* Op() else
{ {
return &selectOp(); // 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
///
virtual eoGeneralOp<EOT>& selectOp() = 0; switch(_op.getType())
{
/// case eoOp<EOT>::unary :
virtual string className() const { return "eoGOpSelector"; }; op= new eoWrappedMonOp<EOT>(static_cast<eoMonOp<EOT>&>(_op));
break;
/// case eoOp<EOT>::binary :
void printOn(ostream& _os) const { op = new eoWrappedBinOp<EOT>(static_cast<eoBinOp<EOT>&>(_op));
_os << className() << endl; case eoOp<EOT>::quadratic :
for ( unsigned i=0; i!= rates.size(); i++ ) { op = new eoWrappedQuadraticOp<EOT>(static_cast<eoQuadraticOp<EOT>&>(_op));
_os << *(operator[](i)) << "\t" << rates[i] << endl; break;
} }
} ownOpList.push_back( op );
}
protected : // Now 'op' is a general operator, either because '_op' was one or
vector<float> rates; // because we wrapped it in an appropriate wrapper in the code above.
list< eoGeneralOp<EOT>* > ownOpList;
}; iterator result = find(begin(), end(), (eoGeneralOp<EOT>*) 0); // search for nullpointer
#endif eoGOpSelector_h 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; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoLottery.h // eoLottery.h
// Implements the lottery procedure for selection // Implements the lottery procedure for selection
// (c) GeNeura Team, 1998 - Marc Schoenauer, 2000 // (c) GeNeura Team, 1998 - Marc Schoenauer, 2000
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoLottery_h #ifndef eoLottery_h
#define eoLottery_h #define eoLottery_h
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <functional> // #include <functional> //
#include <numeric> // accumulate #include <numeric> // accumulate
#include "eoPopOps.h" #include "selectors.h"
#include "eoRNG.h" #include <eo> // eoPop eoSelect MINFLOAT
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// eoLottery: a selection method. /** eoLottery: a selection method. Puts into the output a group of individuals
/// requires EOT::Fitness to be float castable selected using lottery; individuals with higher probability will have more
//----------------------------------------------------------------------------- chances of being selected.
Requires EOT::Fitness to be float castable
template<class EOT> class eoLottery: public eoBinPopOp<EOT> */
{ //-----------------------------------------------------------------------------
public:
/// (Default) Constructor. template<class EOT> class eoLottery: public eoBinPopOp<EOT>
eoLottery(const double & _rate = 1.0): rate(_rate) {} {
public:
/** actually selects individuals from pop and put them into breeders /// (Default) Constructor.
* until breeders has the right size: rate*pop.size() eoLottery(const float& _rate = 1.0): eoBinPopOp<EOT>(), rate(_rate)
* BUT what happens if breeders is already too big? {
*/ if (minimizing_fitness<EOT>())
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders) {
{ eoMinimizingFitnessException up(*this);
// scores of chromosomes throw up; // :-)
vector<double> score(pop.size()); }
}
// calculates total scores for chromosomes
double total = 0; /** actually selects individuals from pop and pushes them back them into breeders
for (unsigned i = 0; i < pop.size(); i++) { * until breeders has the right size: rate*pop.size()
score[i] = static_cast<double>(pop[i].fitness()); * BUT what happens if breeders is already too big?
total += score[i]; * Too big for what?
} */
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
// number of offspring needed {
int target = (int)rint(rate * pop.size()); int target = (int)(rate * pop.size());
// test of consistency // test of consistency
if (breeders.size() >= target) { if (breeders.size() >= target) {
throw("Problem in eoLottery: already too many offspring"); throw("Problem in eoLottery: already too many offspring");
} }
// selection of chromosomes double total;
while (breeders.size() < target) {
unsigned indloc = rng.roulette_wheel(score, total); try
breeders.push_back(pop[indloc]); {
} total = sum_fitness(pop);
} }
catch (eoNegativeFitnessException&)
/// accessor to private rate { // say where it occured...
double Rate() {return rate;} throw eoNegativeFitnessException(*this);
}
/** @name Methods from eoObject */
//@{ // selection of chromosomes
/** readFrom and printOn inherited from eoMerge */ while (breeders.size() < target)
{
/** Inherited from eoObject. Returns the class name. breeders.push_back(roulette_wheel(pop, total));
@see eoObject }
*/ }
virtual string className() const {return "eoLottery";};
//@} double Rate(void) const { return rate; }
private: private:
double rate; // selection rate double rate; // selection rate
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#endif eoLottery_h #endif eoLottery_h

View file

@ -1,99 +1,99 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoMultiBinOp.h // eoMultiBinOp.h
// Class that combines several binary or unary operators // Class that combines several binary or unary operators
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOMULTIBINOP_h #ifndef _EOMULTIBINOP_h
#define _EOMULTIBINOP_h #define _EOMULTIBINOP_h
#include <iterator> #include <iterator>
#include <eoOp.h> #include <eoOp.h>
/** MultiMonOp combines several monary operators. By itself, it does nothing to the /** MultiMonOp combines several monary operators. By itself, it does nothing to the
EO it´s handled*/ EO it´s handled*/
template <class EOT> template <class EOT>
class eoMultiBinOp: public eoBinOp<EOT> { class eoMultiBinOp: public eoBinOp<EOT> {
public: public:
/// Ctor from an already existing op /// Ctor from an already existing op
eoMultiBinOp( const eoBinOp<EOT>* _op ) eoMultiBinOp( const eoBinOp<EOT>* _op )
: eoBinOp< EOT >( ), vOp(){ : eoBinOp< EOT >( ), vOp(){
vOp.push_back( _op ); vOp.push_back( _op );
}; };
/// ///
eoMultiBinOp( ) eoMultiBinOp( )
: eoBinOp< EOT >( ), vOp(){}; : eoBinOp< EOT >( ), vOp(){};
/// Ads a new operator /// Ads a new operator
void adOp( const eoOp<EOT>* _op ){ void adOp( const eoOp<EOT>* _op ){
vOp.push_back( _op ); vOp.push_back( _op );
}; };
/// needed virtual dtor /// needed virtual dtor
virtual ~eoMultiBinOp() {}; virtual ~eoMultiBinOp() {};
/// ///
/// Applies all operators to the EO /// Applies all operators to the EO
virtual void operator()( EOT& _eo1, EOT& _eo2 ) const { virtual void operator()( EOT& _eo1, EOT& _eo2 ) const {
if ( vOp.begin() != vOp.end() ) { // which would mean it's empty if ( vOp.begin() != vOp.end() ) { // which would mean it's empty
for ( vector< const eoOp<EOT>* >::const_iterator i = vOp.begin(); for ( vector< const eoOp<EOT>* >::const_iterator i = vOp.begin();
i != vOp.end(); i++ ) { i != vOp.end(); i++ ) {
// Admits only unary or binary operator // Admits only unary or binary operator
switch ((*i)->readArity()) { switch ((*i)->readArity()) {
case unary: case unary:
{ {
const eoMonOp<EOT>* monop = static_cast<const eoMonOp<EOT>* >(*i); const eoMonOp<EOT>* monop = static_cast<const eoMonOp<EOT>* >(*i);
(*monop)( _eo1 ); (*monop)( _eo1 );
(*monop)( _eo2 ); (*monop)( _eo2 );
break; break;
} }
case binary: case binary:
{ {
const eoBinOp<EOT>* binop = static_cast<const eoBinOp<EOT>* >(*i); const eoBinOp<EOT>* binop = static_cast<const eoBinOp<EOT>* >(*i);
(*binop)( _eo1, _eo2 ); (*binop)( _eo1, _eo2 );
break; break;
} }
} }
} }
} }
} }
/** @name Methods from eoObject /** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp readFrom and printOn are directly inherited from eoOp
*/ */
//@{ //@{
/** Inherited from eoObject /** Inherited from eoObject
@see eoObject @see eoObject
*/ */
string className() const {return "eoMultiBinOp";}; string className() const {return "eoMultiBinOp";};
//@} //@}
private: private:
/// uses pointers to base class since operators can be unary or binary /// uses pointers to base class since operators can be unary or binary
vector< const eoOp<EOT>* > vOp; vector< const eoOp<EOT>* > vOp;
}; };
#endif #endif

View file

@ -1,69 +1,68 @@
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
eoObject.h // eoObject.h
This is the base class for most objects in EO. It basically defines an interf // (c) GeNeura Team, 1998
face for giving names to classes. /*
This library is free software; you can redistribute it and/or
(c) GeNeura Team, 1998, 1999, 2000 modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
This library is free software; you can redistribute it and/or version 2 of the License, or (at your option) any later version.
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either This library is distributed in the hope that it will be useful,
version 2 of the License, or (at your option) any later version. but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
This library is distributed in the hope that it will be useful, Lesser General Public License for more details.
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU You should have received a copy of the GNU Lesser General Public
Lesser General Public License for more details. License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software Contact: todos@geneura.ugr.es, http://geneura.ugr.es
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
#ifndef EOOBJECT_H //-----------------------------------------------------------------------------
#define EOOBJECT_H
#include <eoData.h> // For limits definition
//----------------------------------------------------------------------------- #include <iostream> // istream, ostream
#include <string> // string
#include <eoData.h> // For limits definition
#include <iostream> // istream, ostream #include "compatibility.h"
#include <string> // string
using namespace std;
using namespace std;
//-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- // eoObject
// eoObject //-----------------------------------------------------------------------------
//----------------------------------------------------------------------------- /**
/** This is the base class for the whole hierarchy; an eoObject defines
This is the base class for the whole hierarchy; an eoObject defines basically an interface for the whole hierarchy: each object should
basically an interface for the whole hierarchy: each object should know its name (#className#). Previously, this object defined a print and read
know its name (#className#). Previously, this object defined a print and read interface, but it´s been moved to eoPrintable and eoPersistent.
interface, but it´s been moved to eoPrintable and eoPersistent. */
*/ class eoObject
class eoObject {
{ public:
public:
/// Default Constructor.
/// Default Constructor. eoObject() {}
eoObject() {}
/// Copy constructor.
/// Copy constructor. eoObject( const eoObject& ) {}
eoObject( const eoObject& ) {}
/// Virtual dtor. They are needed in virtual class hierarchies.
/// Virtual dtor. They are needed in virtual class hierarchies. virtual ~eoObject() {}
virtual ~eoObject() {}
/** Return the class id. This should be redefined in each class; but
/** Return the class id. This should be redefined in each class; but it's got code as an example of implementation. Only "leaf" classes
it's got code as an example of implementation. Only "leaf" classes can be non-virtual.
can be non-virtual. */
*/ virtual string className() const { return "eoObject"; }
virtual string className() const { return "eoObject"; }
};
};
#endif EOOBJECT_H
#endif EOOBJECT_H

View file

@ -1,203 +1,235 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoOp.h // eoOp.h
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _eoOp_H #ifndef _eoOp_H
#define _eoOp_H #define _eoOp_H
#include <vector> #include <eoObject.h>
#include <eoObject.h> #include <eoPrintable.h>
#include <eoPrintable.h>
/** @name Genetic operators
/** @name Genetic operators
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with
What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with eoOp as father and eoMonOp (monary or unary operator) and eoBinOp and eoQuadraticOp (binary operators)
eoOp as father and eoMonOp (monary or unary operator) and eoBinOp (binary operator) as siblings. Nobody as siblings). Nobody should subclass eoOp, you should subclass eoGeneralOp, eoBinOp, eoQuadraticOp or eoMonOp,
should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\ 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 #eoOp#s are only printable objects, so if you want to build them from a file, it has to
factory, which know how to build them from a description in a file. be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own
@author GeNeura Team factory, which know how to build them from a description in a file.
@version 0.1 @author GeNeura Team
@see eoOpFactory @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
/** Abstract data types for EO operators. * be an eoObject, but in any case, they are type-specific; each kind of evolvable object
* Genetic operators act on chromosomes, changing them. The type to instantiate them should * can have its own operators
* 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 {
template<class EOType> public:
class eoOp: public eoObject, public eoPrintable {
public: //@{
enum OpType { unary = 0, binary = 1, quadratic = 2, general = 3};
/// Ctor ///
eoOp( Arity _arity = unary )
:arity( _arity ) {}; /// Ctor
eoOp(OpType _type)
/// Copy Ctor :opType( _type ) {};
eoOp( const eoOp& _eop )
:arity( _eop.arity ) {}; /// Copy Ctor
eoOp( const eoOp& _eop )
/// Needed virtual destructor :opType( _eop.opType ) {};
virtual ~eoOp(){};
/// Needed virtual destructor
/// Arity: number of operands virtual ~eoOp(){};
Arity readArity() const {return arity;};
/// getType: number of operands it takes and individuals it produces
/** @name Methods from eoObject */ 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. /**
*/ * Write object. It's called printOn since it prints the object _on_ a stream.
virtual void printOn(ostream& _os) const { * @param _os A ostream.
_os << className(); */
// _os << arity; virtual void printOn(ostream& _os) const {
}; _os << className();
// _os << arity;
/** Inherited from eoObject };
@see eoObject
*/ /** Inherited from eoObject
virtual string className() const {return "eoOp";}; @see eoObject
//@} */
virtual string className() const {return "eoOp";};
private: //@}
/// arity is the number of operands it takes
Arity arity; 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
*/ /** Binary genetic operator: subclasses eoOp, and defines
template<class EOType> basically the operator() with two operands
class eoBinOp: public eoOp<EOType> { */
public: template<class EOType>
class eoBinOp: public eoOp<EOType> {
/// Ctor public:
eoBinOp()
:eoOp<EOType>( binary ) {}; /// Ctor
eoBinOp()
/// Copy Ctor :eoOp<EOType>( binary ) {};
eoBinOp( const eoBinOp& _ebop )
: eoOp<EOType>( _ebop ){}; /// Copy Ctor
eoBinOp( const eoBinOp& _ebop )
/// Dtor : eoOp<EOType>( _ebop ){};
~eoBinOp () {};
/// Dtor
/** applies operator, to the object. Modifies both operands. ~eoBinOp () {};
*/
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0; /** applies operator, to the object. Modifies only the first operand.
*/
/** @name Methods from eoObject virtual void operator()( EOType& _eo1, const EOType& _eo2 ) const = 0;
readFrom and printOn are directly inherited from eoObject
*/ /** @name Methods from eoObject
//@{ readFrom and printOn are directly inherited from eoObject
/** Inherited from eoObject */
@see eoObject //@{
*/ /** Inherited from eoObject
virtual string className() const {return "eoBinOp";}; @see eoObject
//@} */
virtual string className() const {return "eoBinOp";};
}; //@}
/** eoMonOp is the monary operator: genetic operator that takes };
only one EO
*/ /** Quadratic genetic operator: subclasses eoOp, and defines
template <class EOType> basically the operator() with two operands
class eoMonOp: public eoOp<EOType> { */
public: template<class EOType>
class eoQuadraticOp: public eoOp<EOType> {
/// Ctor public:
eoMonOp( )
:eoOp<EOType>( unary ) {}; /// Ctor
eoQuadraticOp()
/// Copy Ctor :eoOp<EOType>( eoOp<EOType>::quadratic ) {};
eoMonOp( const eoMonOp& _emop )
: eoOp<EOType>( _emop ){}; /// Copy Ctor
eoQuadraticOp( const eoQuadraticOp& _ebop )
/// Dtor : eoOp<EOType>( _ebop ){};
~eoMonOp() {};
/// Dtor
/** applies randomly operator, to the object. If arity is more than 1, ~eoQuadraticOp() {};
* keeps a copy of the operand in a cache.
*/ /** applies operator, to the object. Modifies both operands.
virtual void operator()( EOType& _eo1) const = 0; */
virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0;
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoObject /** @name Methods from eoObject
*/ readFrom and printOn are directly inherited from eoObject
//@{ */
/** Inherited from eoObject //@{
@see eoObject /** Inherited from eoObject
*/ @see eoObject
virtual string className() const {return "eoMonOp";}; */
//@} virtual string className() const {return "eoBinOp";};
//@}
}; };
#include <eoPop.h> /** eoMonOp is the monary operator: genetic operator that takes
/** eoNaryOp is the N-ary operator: genetic operator that takes only one EO
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 eoMonOp: public eoOp<EOType> {
template <class EOType> public:
class eoNaryOp: public eoOp<EOType> {
public: /// Ctor
eoMonOp( )
/// Ctor : eoOp<EOType>( eoOp<EOType>::unary ) {};
eoNaryOp( )
:eoOp<EOType>( Nary ) {}; /// Copy Ctor
eoMonOp( const eoMonOp& _emop )
/// Copy Ctor : eoOp<EOType>( _emop ){};
eoNaryOp( const eoNaryOp& _emop )
: eoOp<EOType>( _emop ){}; /// Dtor
~eoMonOp() {};
/// Dtor
~eoNaryOp() {}; /** applies randomly operator, to the object. If arity is more than 1,
* keeps a copy of the operand in a cache.
/** applies randomly operator, to the object. */
*/ virtual void operator()( EOType& _eo1) const = 0;
virtual void operator()( const eoPop<EOType> & _in, eoPop<EOType> _out ) const = 0;
/** @name Methods from eoObject
/** @name Methods from eoObject readFrom and printOn are directly inherited from eoObject
readFrom and printOn are directly inherited from eoObject. */
*/ //@{
//@{ /** Inherited from eoObject
/** Inherited from eoObject @see eoObject
@see eoObject */
*/ virtual string className() const {return "eoMonOp";};
string className() const {return "eoNaryOp";}; //@}
//@} };
}; // some forward declarations
//@} template<class EOT>
class eoIndiSelector;
#endif
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; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoStochTournament.h // eoStochTournament.h
// (c) GeNeura Team, 1998 - EEAAX 1999 // (c) GeNeura Team, 1998 - EEAAX 1999
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr Marc.Schoenauer@polytechnique.fr
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoStochTournament_h #ifndef eoStochTournament_h
#define eoStochTournament_h #define eoStochTournament_h
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <functional> // #include <functional> //
#include <numeric> // accumulate #include <numeric> // accumulate
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT #include <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h> #include <eoRNG.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** eoStochTournament: a selection method that selects ONE individual by /** eoStochTournament: a selection method that selects ONE individual by
binary stochastic tournament binary stochastic tournament
-MS- 24/10/99 */ -MS- 24/10/99 */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <class EOT> class eoStochTournament: public eoSelectOne<EOT> template <class EOT> class eoStochTournament: public eoSelectOne<EOT>
{ {
public: public:
/// ///
eoStochTournament(float _Trate = 1.0 ):eoSelectOne(), Trate(_Trate) { eoStochTournament(float _Trate = 1.0 ):eoSelectOne<EOT>(), Trate(_Trate) {
// consistency check // consistency check
if (Trate < 0.5) { if (Trate < 0.5) {
cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n";
Trate = 0.55; Trate = 0.55;
} }
} }
/** DANGER: if you want to be able to minimize as well as maximizem /** DANGER: if you want to be able to minimize as well as maximizem
DON'T cast the fitness to a float, use the EOT comparator! */ DON'T cast the fitness to a float, use the EOT comparator! */
virtual const EOT& operator()(const eoPop<EOT>& pop) { virtual const EOT& operator()(const eoPop<EOT>& pop)
unsigned i1 = rng.random(pop.size()), {
i2 = rng.random(pop.size()); return stochastic_tournament(pop, Trate)();
}
bool ok = ( rng.flip(Trate) );
if (pop[i1] < pop[ i2 ] ) { private:
if (ok) return pop[ i2 ]; float Trate;
else return pop[ i1 ]; };
}
else { //-----------------------------------------------------------------------------
if (ok) return pop[ i1 ];
else return pop[ i2 ]; #endif eoDetTournament_h
}
}
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; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoUniformXOver.h // eoUniformXOver.h
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOUNIFORMXOVER_h #ifndef _EOUNIFORMXOVER_h
#define _EOUNIFORMXOVER_h #define _EOUNIFORMXOVER_h
// for swap // for swap
#if defined( __BORLANDC__ ) #if defined( __BORLANDC__ )
#include <algorith> #include <algorith>
#else #else
#include <algorithm> #include <algorithm>
#endif #endif
// EO includes // EO includes
#include <eoOp.h> #include <eoOp.h>
#include <eoUniform.h> #include <eoUniform.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* EOUniformCrossover: operator for binary chromosomes * EOUniformCrossover: operator for binary chromosomes
* implementation of uniform crossover for EO * implementation of uniform crossover for EO
* swaps ranges of bits between the parents * swaps ranges of bits between the parents
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<class EOT> template<class EOT>
class eoUniformXOver: public eoBinOp< EOT > class eoUniformXOver: public eoQuadraticOp< EOT >
{ {
public: public:
/// ///
eoUniformXOver( float _rate = 0.5 ): eoUniformXOver( float _rate = 0.5 ):
eoBinOp< EOT > ( ), rate( _rate ) { eoQuadraticOp< EOT > ( ), rate( _rate ) {
if (rate < 0 || rate > 1) if (rate < 0 || rate > 1)
runtime_error("UxOver --> invalid rate"); runtime_error("UxOver --> invalid rate");
} }
/// ///
void operator() ( EOT& chrom1, EOT& chrom2 ) const { void operator() ( EOT& chrom1, EOT& chrom2 ) const {
unsigned end = min(chrom1.length(),chrom2.length()) - 1; unsigned end = min(chrom1.length(),chrom2.length()) - 1;
// select bits to change // select bits to change
eoUniform<float> rnd(0, 1); eoUniform<float> rnd(0, 1);
// aply changes // aply changes
for (unsigned bit = 0; bit < end; bit++) for (unsigned bit = 0; bit < end; bit++)
if (rnd() < rate) if (rnd() < rate)
swap(chrom1[ bit], chrom2[ bit]); swap(chrom1[ bit], chrom2[ bit]);
} }
/** @name Methods from eoObject /** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp readFrom and printOn are directly inherited from eoOp
*/ */
//@{ //@{
/** Inherited from eoObject /** Inherited from eoObject
@see eoObject @see eoObject
*/ */
string className() const {return "eoUniformXOver";}; string className() const {return "eoUniformXOver";};
//@} //@}
private: private:
float rate; /// rate of uniform crossover float rate; /// rate of uniform crossover
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#endif #endif

View file

@ -1,106 +1,106 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoXOver2.h // eoXOver2.h
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOXOVER2_h #ifndef _EOXOVER2_h
#define _EOXOVER2_h #define _EOXOVER2_h
// for swap // for swap
#if defined( __BORLANDC__ ) #if defined( __BORLANDC__ )
#include <algorith> #include <algorith>
#else #else
#include <algorithm> #include <algorithm>
#endif #endif
// EO includes // EO includes
#include <eoOp.h> #include <eoOp.h>
#include <eoUniform.h> #include <eoUniform.h>
/** 2-point crossover: takes the genes in the central section of two EOs /** 2-point crossover: takes the genes in the central section of two EOs
and interchanges it and interchanges it
*/ */
template <class EOT> template <class EOT>
class eoXOver2: public eoBinOp<EOT> { class eoXOver2: public eoQuadraticOp<EOT> {
public: public:
/// ///
eoXOver2() eoXOver2()
: eoBinOp< EOT >(){}; : eoQuadraticOp< EOT >(){};
/// ///
virtual ~eoXOver2() {}; virtual ~eoXOver2() {};
/// ///
virtual void operator()( EOT& _eo1, virtual void operator()( EOT& _eo1,
EOT& _eo2 ) const { EOT& _eo2 ) const {
unsigned len1 = _eo1.length(), len2 = _eo2.length(), unsigned len1 = _eo1.length(), len2 = _eo2.length(),
len= (len1 > len2)?len2:len1; len= (len1 > len2)?len2:len1;
eoUniform<unsigned> uniform( 0, len ); eoUniform<unsigned> uniform( 0, len );
unsigned pos1 = uniform(), pos2 = uniform() ; unsigned pos1 = uniform(), pos2 = uniform() ;
applyAt( _eo1, _eo2, pos1, pos2 ); applyAt( _eo1, _eo2, pos1, pos2 );
} }
/** @name Methods from eoObject /** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp readFrom and printOn are directly inherited from eoOp
*/ */
//@{ //@{
/** Inherited from eoObject /** Inherited from eoObject
@see eoObject @see eoObject
*/ */
string className() const {return "eoXOver2";}; string className() const {return "eoXOver2";};
//@} //@}
private: private:
#ifdef _MSC_VER #ifdef _MSC_VER
typedef EOT::Type Type; typedef EOT::Type Type;
#else #else
typedef typename EOT::Type Type; typedef typename EOT::Type Type;
#endif #endif
/// applies operator to one gene in the EO /// applies operator to one gene in the EO
virtual void applyAt( EOT& _eo, EOT& _eo2, virtual void applyAt( EOT& _eo, EOT& _eo2,
unsigned _i, unsigned _j = 0) const { unsigned _i, unsigned _j = 0) const {
if ( _j < _i ) if ( _j < _i )
swap( _i, _j ); swap( _i, _j );
unsigned len1 = _eo.length(), len2 = _eo2.length(), unsigned len1 = _eo.length(), len2 = _eo2.length(),
len= (len1 > len2)?len2:len1; len= (len1 > len2)?len2:len1;
if ( (_j > len) || (_i> len ) ) if ( (_j > len) || (_i> len ) )
throw runtime_error( "xOver2: applying xOver past boundaries"); throw runtime_error( "xOver2: applying xOver past boundaries");
for ( unsigned i = _i; i < _j; i++ ) { for ( unsigned i = _i; i < _j; i++ ) {
Type tmp = _eo.gene( i ); Type tmp = _eo.gene( i );
_eo.gene( i ) = _eo2.gene( i ); _eo.gene( i ) = _eo2.gene( i );
_eo2.gene( i ) = tmp ; _eo2.gene( i ) = tmp ;
} }
} }
}; };
#endif #endif