eoLottery finished

This commit is contained in:
gustavo 1999-02-05 16:34:00 +00:00
commit 5247c976ce
3 changed files with 11 additions and 11 deletions

View file

@ -11,9 +11,9 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <stdexcept> // runtime_error #include <stdexcept> // runtime_error
#include <eoObject.h> #include <eoObject.h>
#include <eoPersistent.h> #include <eoPersistent.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** EO is a base class for evolvable objects, that is, the subjects of evolution. /** EO is a base class for evolvable objects, that is, the subjects of evolution.
@ -40,7 +40,7 @@ public:
*/ */
EO( istream& _is ) { EO( istream& _is ) {
_is >> repFitness; _is >> repFitness;
validFitness = true; invalidFitness = false;
}; };
/// Copy ctor /// Copy ctor
@ -53,7 +53,8 @@ public:
Fitness fitness() const Fitness fitness() const
{ {
if (invalid()) if (invalid())
throw runtime_error("invalid fitness"); //throw runtime_error("invalid fitness");
cout << "invalid fitness" << endl;
return repFitness; return repFitness;
} }

View file

@ -7,4 +7,4 @@
lib_LTLIBRARIES = libeo.la lib_LTLIBRARIES = libeo.la
libeo_la_SOURCES = eoPrintable.cpp eoPersistent.cpp libeo_la_SOURCES = eoPrintable.cpp eoPersistent.cpp
libeoincdir = $(includedir)/eo libeoincdir = $(includedir)/eo
libeoinc_HEADERS = eo EO.h eoDup.h eoMultiMonOp.h eoPop.h eoUniform.h eoESChrom.h eoNegExp.h eoProblem.h eoVector.h eoFitness.h eoNormal.h eoRnd.h eoXOver2.h eo1d.h eoID.h eoObject.h eoString.h eoAged.h eoKill.h eoOp.h eoTranspose.h eoBin.h libeoinc_HEADERS = eo EO.h eoDup.h eoMultiMonOp.h eoPop.h eoUniform.h eoESChrom.h eoNegExp.h eoProblem.h eoVector.h eoFitness.h eoNormal.h eoRnd.h eoXOver2.h eo1d.h eoID.h eoObject.h eoString.h eoAged.h eoKill.h eoOp.h eoTranspose.h eoBin.h eoPrintable.h eoPersistent.h eoLottery.h eoMutation.h eoPopOps.h eoUniform.h

View file

@ -13,8 +13,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// eoLottery: a selection method. /// eoLottery: a selection method.
/// requires that the fitness type of the chromosome inherits from eoFitness /// requires Chrom::Fitness to be float castable
/// or have a cast to float implemented
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<class Chrom> class eoLottery: public eoSelect<Chrom> template<class Chrom> class eoLottery: public eoSelect<Chrom>
@ -28,11 +27,11 @@ template<class Chrom> class eoLottery: public eoSelect<Chrom>
{ {
// scores of chromosomes // scores of chromosomes
vector<float> score(pop.size()); vector<float> score(pop.size());
// calculates accumulated scores for chromosomes // calculates accumulated scores for chromosomes
for (unsigned i = 0; i < pop.size(); i++) for (unsigned i = 0; i < pop.size(); i++)
score[i] = pop[i].fitness(); score[i] = static_cast<float>(pop[i].fitness());
float sum = accumulate(score.begin(), score.end(), MINFLOAT); float sum = accumulate(score.begin(), score.end(), MINFLOAT);
transform(score.begin(), score.end(), score.begin(), transform(score.begin(), score.end(), score.begin(),
bind2nd(divides<float>(), sum)); bind2nd(divides<float>(), sum));