Added changes mainly by Marc
This commit is contained in:
parent
449ed17ff8
commit
91f5ddbdaa
28 changed files with 2400 additions and 1595 deletions
|
|
@ -11,13 +11,14 @@ libeoincdir = $(includedir)/eo
|
|||
libeoinc_HEADERS = EO.h eo eo1d.h eo2d.h eo2dVector.h eoAged.h eoAlgo.h\
|
||||
eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \
|
||||
eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\
|
||||
eoData.h eoDup.h eoESChrom.h eoESFullChrom.h eoESFullMut.h eoEasyEA.h\
|
||||
eoEvalFunc.h eoEvalFuncPtr.h eoEvaluator.h eoFitTerm.h eoFitness.h\
|
||||
eoData.h eoDetTournament.h eoDup.h eoESChrom.h eoESFullChrom.h eoESFullMut.h eoEasyEA.h\
|
||||
eoEvalFunc.h eoEvalFuncPtr.h eoFitTerm.h eoFitness.h\
|
||||
eoGenTerm.h eoGeneration.h eoID.h eoInclusion.h eoInsertion.h\
|
||||
eoKill.h eoLottery.h eoMultiMonOp.h eoMutation.h eoNegExp.h\
|
||||
eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h eoMutation.h eoNegExp.h\
|
||||
eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\
|
||||
eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\
|
||||
eoProblem.h eoProportionalOpSel.h eoRNG.h eoRnd.h eoString.h\
|
||||
eoTerm.h eoTranspose.h eoUniform.h eoVector.h eoXOver2.h
|
||||
eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \
|
||||
eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,21 +48,23 @@
|
|||
|
||||
#include <eoOp.h>
|
||||
#include <eoMultiMonOp.h>
|
||||
#include <eoMultiBinOp.h>
|
||||
|
||||
#include <eoDup.h>
|
||||
#include <eoKill.h>
|
||||
#include <eoTranspose.h>
|
||||
#include <eoXOver2.h>
|
||||
#include <eoUniformXOver.h>
|
||||
#include <eoMutation.h>
|
||||
|
||||
#include <eoPop.h>
|
||||
#include <eoPopOps.h>
|
||||
#include <eoMerge.h>
|
||||
#include <eoBitOp.h>
|
||||
|
||||
// Evaluation functions
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoEvalFuncPtr.h>
|
||||
#include <eoEvaluator.h>
|
||||
|
||||
// Terminators
|
||||
#include <eoTerm.h>
|
||||
|
|
@ -70,6 +72,9 @@
|
|||
#include <eoFitTerm.h>
|
||||
|
||||
// Selection and reproduction stuff
|
||||
#include <eoUniformSelect.h>
|
||||
#include <eoDetTournament.h>
|
||||
#include <eoStochTournament.h>
|
||||
#include <eoLottery.h>
|
||||
#include <eoBreeder.h>
|
||||
#include <eoInsertion.h>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoAtomCreep.h
|
||||
// eoAtomBitFlip.h
|
||||
// Increments or decrements by one a single element
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
|
|
@ -22,8 +22,8 @@
|
|||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef _EOATOMCREEP_H
|
||||
#define _EOATOMCREEP_H
|
||||
#ifndef _EOATOMBITFLIP_H
|
||||
#define _EOATOMBITFLIP_H
|
||||
|
||||
/** Flips a single bit
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -318,39 +318,6 @@ template<class Chrom> class eoBinGxOver: public eoBinOp<Chrom>
|
|||
};
|
||||
|
||||
|
||||
/** eoBinUxOver --> uniform crossover */
|
||||
|
||||
template<class Chrom> class eoBinUxOver: public eoBinOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoBinUxOver(const float _rate = 0.5): rate(_rate)
|
||||
{
|
||||
if (rate < 0 || rate > 1)
|
||||
runtime_error("UxOver --> invalid rate");
|
||||
}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoBinUxOver"; }
|
||||
|
||||
/**
|
||||
* Uniform crossover for binary chromosomes.
|
||||
* @param chrom1 The first chromosome.
|
||||
* @param chrom2 The first chromosome.
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2) const
|
||||
{
|
||||
unsigned min_size = min(chrom1.size(), chrom2.size());
|
||||
eoUniform<float> uniform(0, 1);
|
||||
|
||||
for (unsigned bit = 0; bit < min_size; bit++)
|
||||
if (uniform() < rate)
|
||||
swap(chrom1[bit], chrom2[bit]);
|
||||
}
|
||||
|
||||
public:
|
||||
float rate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//@}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,26 @@
|
|||
// -*- 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
|
||||
|
|
@ -21,7 +42,7 @@ using namespace std;
|
|||
* For every operator there is a rated to be applyed. *
|
||||
*****************************************************************************/
|
||||
|
||||
template<class Chrom> class eoBreeder: public eoTransform<Chrom>
|
||||
template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// Default constructor.
|
||||
|
|
|
|||
78
eo/src/eoDetTournament.h
Normal file
78
eo/src/eoDetTournament.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// -*- 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
|
||||
|
|
@ -119,9 +119,9 @@ class eoESFullChrom : public eoVector<double, fitT> {
|
|||
cout << "WARNING, Number of Standard Deviations > Number of Object Variables\nAdjusted!\n";
|
||||
num_sigma = num_genes;
|
||||
// modify the Param value - so .status is OK
|
||||
char sloc[20];
|
||||
sprintf(sloc, "%d", num_genes);
|
||||
parser.setParamValue("--NbSigma", sloc);
|
||||
ostrstream sloc;
|
||||
sloc << num_genes;
|
||||
parser.setParamValue("--NbSigma", sloc.str());
|
||||
}
|
||||
// adjust the sizes!!!
|
||||
resize(num_genes);
|
||||
|
|
|
|||
|
|
@ -41,9 +41,9 @@ template<class Chrom> class eoEasyEA: public eoAlgo<Chrom>
|
|||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoEasyEA(eoSelect<Chrom>& _select,
|
||||
eoTransform<Chrom>& _transform,
|
||||
eoMerge<Chrom>& _replace,
|
||||
eoEasyEA(eoBinPopOp<Chrom>& _select,
|
||||
eoMonPopOp<Chrom>& _transform,
|
||||
eoBinPopOp<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator,
|
||||
eoTerm<Chrom>& _terminator)
|
||||
:step(_select, _transform, _replace, _evaluator),
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ template<class EOT> struct eoEvalFunc {
|
|||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
|
||||
/// Effectively applies the evaluation function to an EO or urEO
|
||||
/// Effectively applies the evaluation function to an EO
|
||||
virtual void operator() ( EOT & _eo ) const = 0;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoEvaluator.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 _EOEVALUATOR_H
|
||||
#define _EOEVALUATOR_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoPopOps.h>
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Evaluator takes a vector of EOs and evaluates its fitness
|
||||
* returning void. Template instances must be of fitness and EO type
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoEvaluator: public eoTransform<EOT>{
|
||||
public:
|
||||
/// ctor
|
||||
eoEvaluator( const eoEvalFunc< EOT> & _ef )
|
||||
: eoTransform<EOT>(), repEF( _ef ){};
|
||||
|
||||
/// Needed virtual destructor
|
||||
virtual ~eoEvaluator() {};
|
||||
|
||||
/* Sets the evaluation function
|
||||
virtual void EF( const eoEvalFunc< EOT> & _ef ) { repEF= _ef;};*/
|
||||
|
||||
/// Gets the evaluation function
|
||||
virtual const eoEvalFunc< EOT>& EF() { return repEF;};
|
||||
|
||||
/** This is the actual function operator(); it is left without implementation.
|
||||
It takes a vector of pointers to eo
|
||||
* @param _vEO is a vector of pointers to eo, that will be evaluated
|
||||
*/
|
||||
|
||||
virtual void operator() ( EOT& _eot ) const = 0;
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
/** Return the class id */
|
||||
virtual string className() const { return "eoEvaluator"; }
|
||||
|
||||
/** Read and print are left without implementation */
|
||||
//@}
|
||||
|
||||
private:
|
||||
const eoEvalFunc< EOT> & repEF;
|
||||
};
|
||||
//@}
|
||||
|
||||
#endif
|
||||
|
|
@ -39,9 +39,9 @@ template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
|||
{
|
||||
public:
|
||||
/// Constructor.
|
||||
eoGeneration(eoSelect<Chrom>& _select,
|
||||
eoTransform<Chrom>& _transform,
|
||||
eoMerge<Chrom>& _replace,
|
||||
eoGeneration(eoBinPopOp<Chrom>& _select,
|
||||
eoMonPopOp<Chrom>& _transform,
|
||||
eoBinPopOp<Chrom>& _replace,
|
||||
eoEvalFunc<Chrom>& _evaluator):
|
||||
select(_select), transform(_transform),
|
||||
replace(_replace), evaluator( _evaluator) {};
|
||||
|
|
@ -69,9 +69,9 @@ template<class Chrom> class eoGeneration: public eoAlgo<Chrom>
|
|||
string className() const { return "eoGeneration"; }
|
||||
|
||||
private:
|
||||
eoSelect<Chrom>& select;
|
||||
eoTransform<Chrom>& transform;
|
||||
eoMerge<Chrom>& replace;
|
||||
eoBinPopOp<Chrom>& select;
|
||||
eoMonPopOp<Chrom>& transform;
|
||||
eoBinPopOp<Chrom>& replace;
|
||||
eoEvalFunc<Chrom>& evaluator;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,8 +7,11 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h>
|
||||
#include <eoMerge.h>
|
||||
|
||||
/*****************************************************************************
|
||||
* eoInclusion: A replacement algorithm. *
|
||||
|
|
@ -20,7 +23,13 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
|||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
eoInclusion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoInclusion( istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInclusion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
|
|
@ -38,6 +47,16 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
|
|||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
}
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMerge";};
|
||||
//@}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,5 +1,26 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion.h
|
||||
// Inserts new members into the population
|
||||
// (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 eoInsertion_h
|
||||
|
|
@ -7,8 +28,11 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
#include <eoMerge.h> // eoMerge
|
||||
|
||||
/******************************************************************************
|
||||
* eoInsertion: A replacement algorithm.
|
||||
|
|
@ -20,58 +44,51 @@ template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
|||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>(_rate) {}
|
||||
eoInsertion(const float& _rate = 1.0): eoMerge<Chrom>( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoInsertion( istream& _is): eoBinPopOp<Chrom>( _is ) {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoInsertion() {};
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
/*void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
void operator()( eoPop<Chrom>& _breeders, eoPop<Chrom>& _pop)
|
||||
{
|
||||
int new_size = static_cast<int>(pop.size() * rate());
|
||||
unsigned target = static_cast<unsigned>(rint(_pop.size() * rate()));
|
||||
|
||||
if (new_size == breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
}
|
||||
else if (new_size < breeders.size())
|
||||
{
|
||||
pop = breeders;
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(), pop.begin() - new_size + pop.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
sort(pop.begin(), pop.end());
|
||||
pop.erase(pop.begin(),
|
||||
pop.begin() + breeders.size() + pop.size() - new_size);
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
}
|
||||
}*/
|
||||
_pop.swap(_breeders);
|
||||
|
||||
void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(pop.size() * rate()));
|
||||
|
||||
pop.swap(breeders);
|
||||
|
||||
if (target < pop.size())
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
pop.erase(pop.begin() + target, pop.end());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - pop.size(), breeders.size());
|
||||
partial_sort(breeders.begin(), breeders.begin() + target,
|
||||
breeders.end(), greater<Chrom>());
|
||||
copy(breeders.begin(), breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn inherited from eoMerge */
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoInsertion";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,5 +1,26 @@
|
|||
// -*- 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
|
||||
/*
|
||||
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
|
||||
|
|
@ -16,14 +37,14 @@
|
|||
/// requires Chrom::Fitness to be float castable
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class eoLottery: public eoSelect<Chrom>
|
||||
template<class Chrom> class eoLottery: public eoBinPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoLottery(const float& _rate = 1.0): rate(_rate) {}
|
||||
|
||||
///
|
||||
void operator()(const eoPop<Chrom>& pop, eoPop<Chrom>& breeders) const
|
||||
void operator()( eoPop<Chrom>& pop, eoPop<Chrom>& breeders)
|
||||
{
|
||||
// scores of chromosomes
|
||||
vector<float> score(pop.size());
|
||||
|
|
|
|||
115
eo/src/eoMerge.h
Normal file
115
eo/src/eoMerge.h
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoMerge.h
|
||||
// Base class for population-merging classes
|
||||
// (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 eoMerge_h
|
||||
#define eoMerge_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// EO includes
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
/**
|
||||
* eoMerge: Base class for replacement algorithms
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoMerge: public eoBinPopOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoMerge(const float& _rate = 1.0): eoBinPopOp<Chrom>(), repRate( _rate ) {}
|
||||
|
||||
/// Ctor from istream
|
||||
eoMerge( istream& _is): eoBinPopOp<Chrom>() { readFrom( _is ); };
|
||||
|
||||
/**
|
||||
* Creates a new population based on breeders and original populations.
|
||||
* @param breeders The population of breeders. Should be sorted to work correctly
|
||||
* @param pop The original population.
|
||||
*/
|
||||
void operator()( eoPop<Chrom>& _breeders, eoPop<Chrom>& _pop)
|
||||
{
|
||||
unsigned target = static_cast<unsigned>(rint(_pop.size() * rate()));
|
||||
|
||||
_pop.swap(_breeders);
|
||||
|
||||
if (target < _pop.size())
|
||||
{
|
||||
partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(),
|
||||
greater<Chrom>());
|
||||
_pop.erase(_pop.begin() + target, _pop.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
target = min(target - _pop.size(), _breeders.size());
|
||||
partial_sort(_breeders.begin(), _breeders.begin() + target,
|
||||
_breeders.end(), greater<Chrom>());
|
||||
copy(_breeders.begin(), _breeders.begin() + target,
|
||||
back_insert_iterator<eoPop<Chrom> >(_pop));
|
||||
}
|
||||
};
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/**
|
||||
* Read object. The EOT class must have a ctor from a stream;
|
||||
in this case, a strstream is used.
|
||||
* @param _is A istream.
|
||||
|
||||
*/
|
||||
virtual void readFrom(istream& _is) {
|
||||
_is >> repRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. Prints relevant parameters to standard output
|
||||
* @param _os A ostream. In this case, prints the population to
|
||||
standard output. The EOT class must hav standard output with cout,
|
||||
but since it should be an eoObject anyways, it's no big deal.
|
||||
*/
|
||||
virtual void printOn(ostream& _os) const {
|
||||
_os << repRate;
|
||||
};
|
||||
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMerge";};
|
||||
//@}
|
||||
|
||||
protected:
|
||||
float rate() { return repRate;};
|
||||
|
||||
private:
|
||||
float repRate;
|
||||
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif eoInsertion_h
|
||||
99
eo/src/eoMultiBinOp.h
Normal file
99
eo/src/eoMultiBinOp.h
Normal file
|
|
@ -0,0 +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
|
||||
|
|
@ -833,7 +833,7 @@ void InitRandom( Parser & parser) {
|
|||
}
|
||||
|
||||
if (_seed == 0) { // use clock to get a "random" seed
|
||||
_seed = unsigned long( time( 0 ) );
|
||||
_seed = (unsigned long)( time( 0 ) );
|
||||
ostrstream s;
|
||||
s << _seed;
|
||||
parser.setParamValue("--seed", s.str()); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
|
||||
|
|
|
|||
|
|
@ -28,9 +28,7 @@
|
|||
#include <vector>
|
||||
#include <strstream>
|
||||
|
||||
using namespace std;
|
||||
// from file
|
||||
|
||||
// EO includes
|
||||
#include <eoRnd.h>
|
||||
#include <eoPersistent.h>
|
||||
|
||||
|
|
@ -62,8 +60,7 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
|
|||
/** Protected ctor. This is intended to avoid creation of void populations, except
|
||||
from sibling classes
|
||||
*/
|
||||
eoPop()
|
||||
:vector<EOT>() {};
|
||||
eoPop() :vector<EOT>() {};
|
||||
|
||||
|
||||
/** Ctor for fixed-size chromosomes, with variable content
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
// eoPopOps.h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eo1d.h
|
||||
// eoPopOps.h
|
||||
// (c) GeNeura Team, 1998
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
|
|
@ -31,6 +30,16 @@ using namespace std;
|
|||
/**
|
||||
@author Geneura Team
|
||||
@version 0.0
|
||||
|
||||
@version 0.1 : -MS- 22/10/99
|
||||
added the added the derived class eoSelectOne for which you only have
|
||||
to define the selection of 1 individual
|
||||
(e.g. for tournament, age selection in SSGA, ...)
|
||||
added at the BASE level (after all it's themost frequen case)
|
||||
the pure virtual operator that selects one single individual:
|
||||
EOT eoSelect::operator ( const eoPop<EOT>& _parents,
|
||||
const EOT& _first = 0)
|
||||
added the optional second parameter to transform::operator()
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -40,14 +49,14 @@ using namespace std;
|
|||
/** eoTransform is a class that transforms or does something on a population.
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoTransform: public eoObject{
|
||||
class eoMonPopOp: public eoObject{
|
||||
|
||||
public:
|
||||
/** ctor */
|
||||
eoTransform() {};
|
||||
eoMonPopOp() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoTransform(){};
|
||||
virtual ~eoMonPopOp(){};
|
||||
|
||||
/// Pure virtual transformation function. Does something on the population
|
||||
virtual void operator () ( eoPop<EOT>& _pop ) = 0;
|
||||
|
|
@ -59,7 +68,7 @@ class eoTransform: public eoObject{
|
|||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoTransform";};
|
||||
virtual string className() const {return "eoMonPopOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
|
@ -68,22 +77,22 @@ class eoTransform: public eoObject{
|
|||
|
||||
/** eoSelect usually takes elements from one population, with or without transformation, and transfers them to the other population */
|
||||
template<class EOT>
|
||||
class eoSelect: public eoObject{
|
||||
class eoBinPopOp: public eoObject{
|
||||
|
||||
public:
|
||||
/** ctor
|
||||
*/
|
||||
eoSelect() {};
|
||||
eoBinPopOp() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoSelect(){};
|
||||
virtual ~eoBinPopOp(){};
|
||||
|
||||
/** Pure virtual transformation function. Extracts something from the parents,
|
||||
and transfers it to the siblings
|
||||
@param _parents the initial generation. Will be kept constant
|
||||
@param _siblings the created offspring. Will be usually an empty population
|
||||
*/
|
||||
virtual void operator () ( const eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) const = 0;
|
||||
virtual void operator () ( eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
|
|
@ -92,52 +101,41 @@ class eoSelect: public eoObject{
|
|||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoSelect";};
|
||||
virtual string className() const {return "eoBinPopOp";};
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
/** eoMerge involves three populations, that can be merged and transformed to
|
||||
give a third
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** eoSelect usually takes elements from one population, with or without transformation, and transfers them to the other population */
|
||||
template<class EOT>
|
||||
class eoMerge: public eoObject{
|
||||
class eoSelectOne: public eoObject{
|
||||
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoMerge(const float& _rate = 1.0): rep_rate(_rate) {}
|
||||
/** ctor
|
||||
*/
|
||||
eoSelectOne() {};
|
||||
|
||||
/// Dtor
|
||||
virtual ~eoMerge() {}
|
||||
virtual ~eoSelectOne(){};
|
||||
|
||||
/** Pure virtual transformation function. Extracts something from breeders
|
||||
* and transfers it to the pop
|
||||
* @param breeders Tranformed individuals.
|
||||
* @param pop The original population at the begining, the result at the end
|
||||
*/
|
||||
virtual void operator () ( eoPop<EOT>& breeders, eoPop<EOT>& pop ) = 0;
|
||||
/** Pure virtual transformation function. Extracts something from the parents,
|
||||
and transfers it to the siblings
|
||||
@param _parents the initial generation. Will be kept constant
|
||||
@param _siblings the created offspring. Will be usually an empty population
|
||||
*/
|
||||
virtual const EOT& operator () ( const eoPop<EOT>& _parents ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn are not overriden
|
||||
*/
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
virtual string className() const {return "eoMerge";};
|
||||
virtual string className() const {return "eoSelectOne";};
|
||||
//@}
|
||||
|
||||
/// Return the rate to be selected from the original population
|
||||
float rate() const { return rep_rate; }
|
||||
|
||||
/// Set the rate to be obtained after replacement.
|
||||
/// @param _rate The rate.
|
||||
void rate(const float& _rate) { rep_rate = _rate; }
|
||||
|
||||
private:
|
||||
float rep_rate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
|
|
|||
360
eo/src/eoScheme.h
Normal file
360
eo/src/eoScheme.h
Normal file
|
|
@ -0,0 +1,360 @@
|
|||
// eoScheme.h
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoScheme.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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOSCHEME_H
|
||||
#define _EOSCHEME_H
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
@author Geneura Team -- EEAAX 99
|
||||
@version 0.0
|
||||
Evolution scheme
|
||||
|
||||
|
||||
It seems to me that some important conceptual object is missing
|
||||
(and God knows I hate turning everything into objects!
|
||||
|
||||
So here is the evolution scheme:
|
||||
|
||||
regroups selection (nb of offspring to generate AND selection method
|
||||
and replacement (from parents + offspring)
|
||||
|
||||
allows to include elitism, eugenism of the worst, ... more easily
|
||||
|
||||
a generation is then simply an evolution scheme and some operators
|
||||
and a full algo is a generation + a termination condition
|
||||
|
||||
this is mainly a container class
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPopOps.h>
|
||||
#include "eoStochTournament.h"
|
||||
#include "eoDetTournament.h"
|
||||
#include "eoLottery.h"
|
||||
#include "eoUniformSelect.h"
|
||||
#include "eoInclusion.h"
|
||||
#include "eoESReplace.h"
|
||||
#include "eoEPTournament.h"
|
||||
|
||||
/** eoScheme is a class that does more sophisticated evolution that eoEasyEA
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoScheme: public eoAlgo<EOT>{
|
||||
|
||||
public:
|
||||
|
||||
// Dtor
|
||||
virtual ~eoScheme() {};
|
||||
|
||||
// copy ctor is impossible because of pointers to pure virual types.
|
||||
// any idea???? --- leave the default copy ctor -- JJ
|
||||
|
||||
/** the Parser-based constructor
|
||||
these switch cases could be turned into as many subclasses
|
||||
- but how do you return the subclass to the caller of the constructor???
|
||||
*/
|
||||
eoScheme(Parser & parser) {
|
||||
// read the popsize
|
||||
parser.AddTitle("Description of evolution");
|
||||
string Evol;
|
||||
string SelectString;
|
||||
// temporary
|
||||
float rate_offspring;
|
||||
|
||||
try {
|
||||
Evol = parser.getString("-EE", "--evolution", "GGA",
|
||||
"Evolution scheme (GGA, SSGA, ESPlus, ESComma, EP, General)" );
|
||||
popsize = parser.getInt("-EP", "--population", "10",
|
||||
"Population size" );
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
// now the big switch
|
||||
if (! strcasecmp(Evol.c_str(), "GGA") ) {
|
||||
// GGA parameters: popsize, selection method (and its parameters)
|
||||
nb_offspring = 0;
|
||||
rate_offspring = 1.0; // generational replacement: #offspring=popsize
|
||||
try {
|
||||
// parser.AddTitle("GGA Parameters");
|
||||
SelectString = parser.getString("-ES", "--selection", "Tournament",
|
||||
"Selection method (Roulette, tournament)" );
|
||||
if (! strcasecmp(SelectString.c_str(), "roulette") ) {
|
||||
ptselect = new eoLottery<EOT> ();
|
||||
ptselect_mate = new eoLottery<EOT> ();
|
||||
}
|
||||
if (! strcasecmp(SelectString.c_str(), "tournament") ) {
|
||||
float rate = parser.getFloat("-Et", "--TselectSize", "2",
|
||||
"Tournament size or rate" );
|
||||
if (rate < 0.5)
|
||||
throw out_of_range("Invalid tournament rate");
|
||||
else if ( rate < 1 ) { // binary stochastic tournament
|
||||
ptselect = new eoStochTournament<EOT>(rate);
|
||||
ptselect_mate = new eoStochTournament<EOT>(rate);
|
||||
} else { // determeinistic tournament of size (int)rate
|
||||
ptselect = new eoDetTournament<EOT>((int)rate);
|
||||
ptselect_mate = new eoDetTournament<EOT>((int)rate);
|
||||
}
|
||||
}
|
||||
// end choice of selection
|
||||
ptreplace = new eoInclusion<EOT>();
|
||||
// put here the choice of elitism
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of GGA
|
||||
|
||||
// SSGA - standard, one offspring only at the moment
|
||||
if (! strcasecmp(Evol.c_str(), "SSGA") ) {
|
||||
// SSGA parameters: popsize, selection tournament size
|
||||
// the replacement is limited to repace_worst, though
|
||||
// it could be easy to add the anti-tournament replacement method
|
||||
nb_offspring = 1;
|
||||
// NOTE: of course it's a bit of a waste to use the standard loop
|
||||
// for one single offspring ...
|
||||
try {
|
||||
// parser.AddTitle("SSGA Parameters");
|
||||
float _rate = parser.getFloat("-ET", "--TSelectSize", "2",
|
||||
"Selection tournament size" );
|
||||
if ( _rate < 1 ) { // binary stochastic tournament
|
||||
ptselect = new eoStochTournament<EOT>(_rate);
|
||||
ptselect_mate = new eoStochTournament<EOT>(_rate);
|
||||
} else { // determeinistic tournament of size (int)rate
|
||||
ptselect = new eoDetTournament<EOT>((int)_rate);
|
||||
ptselect_mate = new eoDetTournament<EOT>((int)_rate);
|
||||
}
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
// end choice of selection
|
||||
ptreplace = new eoInclusion<EOT>();
|
||||
// put here the choice of elitism
|
||||
} // end of SSGA
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "ESPlus") ) {
|
||||
// ES evolution parameters: lambda = _nb_offspring
|
||||
|
||||
try {
|
||||
// parser.AddTitle("ES Scheme parameters");
|
||||
nb_offspring = parser.getInt("-EL", "--lambda", "50",
|
||||
"Lambda" );
|
||||
ptselect = new eoUniformSelect<EOT>();
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
ptreplace = new eoESPlus<EOT>();
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of ESPlus
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "ESComma") ) {
|
||||
// ES evolution parameters: lambda = _nb_offspring
|
||||
|
||||
try {
|
||||
// parser.AddTitle("ES Scheme parameters");
|
||||
nb_offspring = parser.getInt("-EL", "--lambda", "50",
|
||||
"Lambda" );
|
||||
ptselect = new eoUniformSelect<EOT>();
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
ptreplace = new eoESComma<EOT>();
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of ESCOmma
|
||||
|
||||
if (! strcasecmp(Evol.c_str(), "EP") ) {
|
||||
// EP evoltion scheme: only the EP-tournament replacement size is neede
|
||||
|
||||
try {
|
||||
// parser.AddTitle("EP Scheme parameters");
|
||||
nb_offspring = popsize;
|
||||
ptselect = new eoCopySelect<EOT>; /* no selection */
|
||||
ptselect_mate = new eoUniformSelect<EOT>();
|
||||
/* What, crossover in EP :-) */
|
||||
unsigned tsize = parser.getInt("-ET", "--TournamentSize", "6",
|
||||
"Size of stocahstic replacement tournament" );
|
||||
ptreplace = new eoEPTournament<EOT>(tsize);
|
||||
}
|
||||
catch (exception & e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
parser.printHelp();
|
||||
exit(1);
|
||||
}
|
||||
} // end of EP
|
||||
|
||||
|
||||
// everyting is read: now the consistency checks and other preliminary steps
|
||||
nb_offspring = (nb_offspring ? nb_offspring :
|
||||
(int) rint (rate_offspring * popsize) );
|
||||
if (!nb_offspring)
|
||||
nb_offspring = 1; /* al least one offspring */
|
||||
|
||||
}
|
||||
|
||||
|
||||
// accessors
|
||||
unsigned PopSize(){return popsize;}
|
||||
unsigned NbOffspring(){return nb_offspring ;}
|
||||
eoSelect<EOT>* PtSelect(){return ptselect;}
|
||||
eoSelect<EOT>* PtSelectMate(){return ptselect_mate;}
|
||||
eoMerge<EOT>* PtMerge() {return ptreplace;}
|
||||
// NOTE: need pointers otherwise you have many warnings when initializing
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoScheme";};
|
||||
//@}
|
||||
private:
|
||||
unsigned popsize; /* but should it be here ??? */
|
||||
unsigned nb_offspring; /* to generate through selection+operators */
|
||||
|
||||
// these are provisional for later use
|
||||
|
||||
// float rate_offspring; /* or rate */
|
||||
// unsigned nb_survive; /* the best guys that are transmitted anyway */
|
||||
// float rate_survive; /* or rate */
|
||||
// unsigned nb_die; /* the worst guys that do not enev enter selection */
|
||||
// float rate_die; /* or rate */
|
||||
|
||||
eoSelect<EOT> * ptselect;
|
||||
eoSelect<EOT>* ptselect_mate;
|
||||
eoMerge<EOT>* ptreplace;
|
||||
bool elitism; /* put back old best in the new population if necessary */
|
||||
};
|
||||
/* examples:
|
||||
for most populat schemes, nb_survive = nb_die = 0
|
||||
in GGA and EP, nb_offspring = pop.size()
|
||||
in ES, nb_offspring = lambda
|
||||
in SSGA, nb_offspring = 1 (usually)
|
||||
|
||||
elitism can be used anywhere - though stupid in ES, EP and SSGA who are
|
||||
elist by definition
|
||||
*/
|
||||
|
||||
#endif _EOSCHEME_H
|
||||
|
||||
/*
|
||||
/////////////////////////////////
|
||||
/// Applies one generation of evolution to the population.
|
||||
virtual void operator()(eoPop<EOT>& pop) {
|
||||
// Determine the number of offspring to create
|
||||
// either prescribed, or given as a rate
|
||||
unsigned nb_off_local = (nb_offspring ? nb_offspring :
|
||||
(int) rint (rate_offspring * pop.size()) );
|
||||
nb_off_local = (nb_off_local ? nb_off_local : 1); // in case it is rounded to 0!
|
||||
|
||||
// the worst die immediately
|
||||
unsigned nb_die_local = (nb_die ? nb_die :
|
||||
(int) rint (rate_die * pop.size()) );
|
||||
// and the best will survive without selection
|
||||
unsigned nb_survive_local = (nb_survive ? nb_survive :
|
||||
(int) rint (rate_survive * pop.size()) );
|
||||
|
||||
// before selection, erase the one to die
|
||||
// sort old pop - just in case!
|
||||
sort(pop.begin(), pop.end(), greater<EOT>());
|
||||
Fitness oldBest = pop[0].fitness(); // store for elitism
|
||||
eoPop<EOT> fertilepop = pop;
|
||||
if (nb_die_local)
|
||||
erase(fertilepop.end()-nb_die_local, fertilepop.end());
|
||||
|
||||
eoPop<EOT> offspring; // = select(fertilepop, nb_off_local);
|
||||
select(fertilepop, offspring, nb_off_local);
|
||||
|
||||
// now apply the operators to offspring
|
||||
for (unsigned i=0; i<nb_local; i++) {
|
||||
EOT tmp = offspring[i];
|
||||
unsigned id = 0; // first operator
|
||||
eoOp<EOT>* op = seqselop.Op(&id);
|
||||
while (op) { // NULL if no more operator
|
||||
EOT mate;
|
||||
if (op->readArity() == binary) // can eventually be skipped
|
||||
mate = select_mate(pop, tmp); // though useless ig mutation
|
||||
else
|
||||
mate = tmp; // assumed: mate will not be used!
|
||||
cout << op->className() << " for offspring " << i << endl;
|
||||
tmp = (*op)( tmp, mate, pop );
|
||||
op = seqselop.Op(&id);
|
||||
}
|
||||
offspring[i]=tmp; //* where it belongs
|
||||
}
|
||||
|
||||
eoPop<EOT>::iterator i;
|
||||
// Can't use foreach here since foreach takes the
|
||||
// parameter by reference
|
||||
for ( i = offspring.begin(); i != offspring.end(); i++)
|
||||
evaluator(*i);
|
||||
|
||||
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
||||
// not exact - later!!!
|
||||
// -MS-
|
||||
|
||||
// first, copy the ones that need to survive
|
||||
// assumed: pop is still sorted!
|
||||
eoPop<EOT> finalPop;
|
||||
// and the best survive without selection
|
||||
if (nb_survive_local) {
|
||||
finalPop.resize(nb_survive_local);
|
||||
copy( finalPop.begin(), fertilepop.begin(),
|
||||
fertilepop.begin()+nb_survive_local );
|
||||
}
|
||||
|
||||
// now call the replacement method
|
||||
replace(finalPop, tmpPop);
|
||||
|
||||
// handle elitlism
|
||||
sort(finalPop.begin(), finalPop.end(), greater<EOT>());
|
||||
if (elitism) {
|
||||
if ( finalPop[0].fitness() < oldBest ) // best fitness has decreased!
|
||||
copy(finalPop.end()-1, pop[0]);
|
||||
}
|
||||
// return finalPop;
|
||||
}
|
||||
*/
|
||||
23
eo/src/eoSelfEval.h
Normal file
23
eo/src/eoSelfEval.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// EO.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
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSimpleEval.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 _EOSimpleEval_H
|
||||
#define _EOSimpleEval_H
|
||||
|
||||
#include <eoEvaluator.h> // for evalFunc
|
||||
#include <algorithm> // For sort
|
||||
|
||||
/** Particular instantiation of the EOEvaluator class
|
||||
It takes each member in the population, and evaluates it, applying
|
||||
the evaluation function it´s been initialized with
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoSimpleEval: public eoEvaluator<EOT> {
|
||||
public:
|
||||
|
||||
/// Ctors/dtors
|
||||
eoSimpleEval( eoEvalFunc<EOT> & _ef ):eoEvaluator<EOT>(_ef) {};
|
||||
|
||||
///
|
||||
virtual ~eoSimpleEval() {};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
typedef EOT::Fitness EOFitT;
|
||||
#else
|
||||
typedef typename EOT::Fitness EOFitT;
|
||||
#endif
|
||||
/** Applies evaluation function to all members in the population, and sets
|
||||
their fitnesses
|
||||
Reference is non-const since it orders the population by any order
|
||||
it´s been defined
|
||||
@param _vEO the population whose fitness is going to be computed*/
|
||||
virtual void operator() ( eoPop< EOT >& _vEO ) {
|
||||
for ( eoPop<EOT>::iterator i = _vEO.begin(); i != _vEO.end(); i ++ ){
|
||||
i->fitness( EF().evaluate( *i ) );
|
||||
}
|
||||
sort( _vEO.begin(), _vEO.end() );
|
||||
};
|
||||
|
||||
///@name eoObject methods
|
||||
//@{
|
||||
///
|
||||
void printOn( ostream& _os ) const {};
|
||||
///
|
||||
void readFrom( istream& _is ){};
|
||||
|
||||
///
|
||||
string className() { return "eoSimpleEval";};
|
||||
|
||||
//@}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
78
eo/src/eoStochTournament.h
Normal file
78
eo/src/eoStochTournament.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// -*- 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
|
||||
64
eo/src/eoUniformSelect.h
Normal file
64
eo/src/eoUniformSelect.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoUniformSelect.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 eoUniformSelect_h
|
||||
#define eoUniformSelect_h
|
||||
// WARNING: 2 classes in this one - eoUniformSelect and eoCopySelect
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <functional> //
|
||||
#include <numeric> // accumulate
|
||||
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
|
||||
#include <eoRNG.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** eoUniformSelect: a selection method that selects ONE individual randomly
|
||||
-MS- 22/10/99 */
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <class EOT> class eoUniformSelect: public eoSelectOne<EOT>
|
||||
{
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoUniformSelect():eoSelectOne() {}
|
||||
|
||||
/// not a big deal!!!
|
||||
virtual const EOT& operator()(const eoPop<EOT>& pop) {
|
||||
return pop[rng.random(pop.size())] ;
|
||||
}
|
||||
|
||||
/// Methods inherited from eoObject
|
||||
//@{
|
||||
|
||||
/** Return the class id.
|
||||
* @return the class name as a string
|
||||
*/
|
||||
virtual string className() const { return "eoUniformSelect"; };
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
#endif eoUniformSelect_h
|
||||
90
eo/src/eoUniformXOver.h
Normal file
90
eo/src/eoUniformXOver.h
Normal file
|
|
@ -0,0 +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 ) {
|
||||
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
|
||||
|
|
@ -52,7 +52,7 @@ public:
|
|||
|
||||
///
|
||||
virtual void operator()( EOT& _eo1,
|
||||
EOT& _eo2 ) const {
|
||||
EOT& _eo2 ) const {
|
||||
unsigned len1 = _eo1.length(), len2 = _eo2.length(),
|
||||
len= (len1 > len2)?len2:len1;
|
||||
eoUniform<unsigned> uniform( 0, len );
|
||||
|
|
|
|||
Reference in a new issue