This commit is contained in:
evomarc 2003-08-02 06:55:47 +00:00
commit 4238ee1bd3
2 changed files with 28 additions and 22 deletions

View file

@ -18,10 +18,8 @@
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: Marc.Schoenauer@polytechnique.fr
Marc.Schoenauer@polytechnique.fr mkeijzer@cs.vu.nl
mak@dhi.dk
CVS Info: $Date: 2003-03-21 02:38:57 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/eoVariableLengthCrossover.h,v 1.9 2003-03-21 02:38:57 maartenkeijzer Exp $ $Author: maartenkeijzer $
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -43,20 +41,23 @@ template <class Atom>
class eoAtomExchange : public eoBF<unsigned, Atom &, bool> class eoAtomExchange : public eoBF<unsigned, Atom &, bool>
{ {
public: public:
// a function to initlialize - to be called before every crossover /** a function to initlialize - to be called before every crossover */
virtual void randomize(unsigned int, unsigned int){} virtual void randomize(unsigned int, unsigned int){}
/** the inherited className() */
virtual std::string className() const=0; virtual std::string className() const=0;
}; };
/** Uniform crossover - well, not really for FixedLength */ /** Uniform crossover - well, not really, efficient for FixedLength */
template <class Atom> template <class Atom>
class eoUniformAtomExchange: public eoAtomExchange<Atom> class eoUniformAtomExchange: public eoAtomExchange<Atom>
{ {
public: public:
eoUniformAtomExchange(double _rate=0.5):rate(_rate){} eoUniformAtomExchange(double _rate=0.5):rate(_rate){}
// randomize: fill the mask: the exchange will be simulated first /** randomize: fill the mask: the exchange will be simulated first
// to see if sizes are OK, so it must be repeatable * to see if sizes are OK, so it must be repeatable :
* the mask has to be a private data, cannot be computed on the fly
*/
void randomize(unsigned _size1, unsigned _size2) void randomize(unsigned _size1, unsigned _size2)
{ {
mask.resize(_size1 + _size2); mask.resize(_size1 + _size2);
@ -64,12 +65,13 @@ public:
mask[i]=eo::rng.flip(rate); mask[i]=eo::rng.flip(rate);
} }
// the operator() simply returns the mask booleans in turn /** the operator() simply returns the mask booleans in turn */
bool operator()(unsigned _i, Atom & ) bool operator()(unsigned _i, Atom & )
{ {
return mask[_i]; return mask[_i];
} }
/** inherited className() */
virtual std::string className() const {return "eoUniformAtomExchange";} virtual std::string className() const {return "eoUniformAtomExchange";}
private: private:
@ -77,10 +79,12 @@ private:
std::vector<bool> mask; std::vector<bool> mask;
}; };
/////////////////////////////////////////////////////////////////////
////// Now the operators themsleves
/////////////////////////////////////////////////////////////////////
/** Exchange Crossover using an AtomExchange /** Exchange Crossover using an AtomExchange
*/ */
template <class EOT> template <class EOT>
class eoVlAtomExchangeQuadOp : public eoQuadOp<EOT> class eoVlAtomExchangeQuadOp : public eoQuadOp<EOT>
{ {
@ -88,7 +92,7 @@ public :
typedef typename EOT::AtomType AtomType; typedef typename EOT::AtomType AtomType;
// default ctor: requires bounds on number of genes + a rate /** default ctor: requires bounds on number of genes + a rate */
eoVlAtomExchangeQuadOp(unsigned _Min, unsigned _Max, eoVlAtomExchangeQuadOp(unsigned _Min, unsigned _Max,
eoAtomExchange<AtomType>& _atomExchange): eoAtomExchange<AtomType>& _atomExchange):
Min(_Min), Max(_Max), atomExchange(_atomExchange) {} Min(_Min), Max(_Max), atomExchange(_atomExchange) {}
@ -148,6 +152,7 @@ public :
return true; // should we test that? Yes, but no time now return true; // should we test that? Yes, but no time now
} }
/** the inherited className */
virtual std::string className() const virtual std::string className() const
{ {
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
@ -155,7 +160,6 @@ public :
os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")"; os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")";
return os.str() return os.str()
#else #else
char s[1024]; char s[1024];
std::ostrstream os(s, 1022); std::ostrstream os(s, 1022);
os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")" << std::ends; os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")" << std::ends;
@ -168,9 +172,9 @@ private:
eoAtomExchange<AtomType> & atomExchange; eoAtomExchange<AtomType> & atomExchange;
}; };
/** Crossover using an AtomCrossover (probably irrelevant in Variable Length) /** Crossover using an AtomCrossover. Probably irrelevant in Variable Length -
see eoFlOrBinOp.h and eoFlOrQuadOp.h for the similar Fixed Length operators
*/ */
template <class EOT> template <class EOT>
class eoInnerExchangeQuadOp : public eoQuadOp<EOT> class eoInnerExchangeQuadOp : public eoQuadOp<EOT>
{ {
@ -178,10 +182,11 @@ public :
typedef typename EOT::AtomType AtomType; typedef typename EOT::AtomType AtomType;
// default ctor: requires bounds on number of genes + a rate /** default ctor: requires bounds on number of genes + a rate */
eoInnerExchangeQuadOp( eoQuadOp<AtomType>& _op, float _rate = 0.5): eoInnerExchangeQuadOp( eoQuadOp<AtomType>& _op, float _rate = 0.5):
op(_op), rate( _rate ) {} op(_op), rate( _rate ) {}
/** performs the Atom crossover */
bool operator()(EOT & _eo1, EOT & _eo2) bool operator()(EOT & _eo1, EOT & _eo2)
{ {
unsigned size1 = _eo1.size(), size2 = _eo2.size(), minsize = ( size1 > size2)?size2:size1; unsigned size1 = _eo1.size(), size2 = _eo2.size(), minsize = ( size1 > size2)?size2:size1;
@ -210,7 +215,7 @@ private:
/** Direct Uniform Exchange of genes (obsolete, already :-) /** Direct Uniform Exchange of genes (obsolete, already :-) stays there for historical reasons
A very primitive version, that does no verification at all!!! A very primitive version, that does no verification at all!!!
NEEDS to be improved - but no time now :-((( NEEDS to be improved - but no time now :-(((

View file

@ -2,7 +2,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoVariableLengthMutation.h // eoVariableLengthMutation.h
// (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 2000 // (c) Marc Schoenauer 1999 - Maarten Keijzer 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
@ -18,10 +18,8 @@
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: Marc.Schoenauer@polytechnique.fr
Marc.Schoenauer@polytechnique.fr mkeijzer@cs.vu.nl
mak@dhi.dk
CVS Info: $Date: 2003-03-21 02:38:57 $ $Version$ $Author: maartenkeijzer $
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -61,6 +59,7 @@ public :
eoVlAddMutation(unsigned _nMax, eoInit<AtomType> & _atomInit) : eoVlAddMutation(unsigned _nMax, eoInit<AtomType> & _atomInit) :
nMax(_nMax), atomInit(_atomInit) {} nMax(_nMax), atomInit(_atomInit) {}
/** operator: actually adds an Atom */
bool operator()(EOT & _eo) bool operator()(EOT & _eo)
{ {
if (_eo.size() >= nMax) if (_eo.size() >= nMax)
@ -72,6 +71,7 @@ public :
return true; return true;
} }
/** inherited className */
virtual std::string className() const { return "eoVlAddMutation"; } virtual std::string className() const { return "eoVlAddMutation"; }
private: private:
@ -79,6 +79,7 @@ private:
eoInit<AtomType> & atomInit; eoInit<AtomType> & atomInit;
}; };
/** A helper class for choosing which site to delete */ /** A helper class for choosing which site to delete */
template <class EOT> template <class EOT>
class eoGeneDelChooser : public eoUF<EOT &, unsigned int> class eoGeneDelChooser : public eoUF<EOT &, unsigned int>
@ -120,7 +121,7 @@ public :
eoVlDelMutation(unsigned _nMin, eoGeneDelChooser<EOT> & _chooser) : eoVlDelMutation(unsigned _nMin, eoGeneDelChooser<EOT> & _chooser) :
nMin(_nMin), uChooser(), chooser(_chooser) {} nMin(_nMin), uChooser(), chooser(_chooser) {}
/** ctor with uniform gene chooser /** ctor with uniform gene chooser - the default
* @param nMin min number of atoms to leave in the individual * @param nMin min number of atoms to leave in the individual
*/ */