Turned bonary_value into the onemax (not pow that exploded for even only

moderately small size of genome!)
Turned it into a double (it was a float) as floats are dead and should dissappear
This commit is contained in:
evomarc 2001-11-17 07:54:12 +00:00
commit d686b37c1f
4 changed files with 11 additions and 19 deletions

View file

@ -1,25 +1,16 @@
#include <eo> #include <algorithm>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Just a simple function that takes binary value of a chromosome and sets /** Just the simple function that takes binary value of a chromosome and sets
the fitnes. the fitnes.
@param _chrom A binary chromosome @param _chrom A binary chromosome
*/ */
template <class Chrom> float binary_value(const Chrom& _chrom) template <class Chrom> double binary_value(const Chrom& _chrom)
{ {
float sum = 0; double sum = 0.0;
for (unsigned i = 0; i < _chrom.size(); i++) for (unsigned i=0; i<_chrom.size(); i++)
if (_chrom[i]) sum += _chrom[i];
sum += pow(2, _chrom.size() - i - 1); return sum;
return sum;
} }
struct BinaryValue
{
template <class Chrom> void operator()(Chrom& _chrom)
{
_chrom.fitness(binary_value(_chrom));
}
};

View file

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <ga/make_ga.h> #include <ga/make_ga.h>
#include <eoEvalFuncPtr.h>
#include "binary_value.h" #include "binary_value.h"
#include <apply.h> #include <apply.h>
@ -21,7 +22,7 @@ int main(int argc, char* argv[])
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// The evaluation fn - encapsulated into an eval counter for output // The evaluation fn - encapsulated into an eval counter for output
eoEvalFuncPtr<EOT, float> mainEval( binary_value<EOT> ); eoEvalFuncPtr<EOT, double> mainEval( binary_value<EOT> );
eoEvalFuncCounter<EOT> eval(mainEval); eoEvalFuncCounter<EOT> eval(mainEval);
// the genotype - through a genotype initializer // the genotype - through a genotype initializer

View file

@ -66,7 +66,7 @@ int main(int argc, char* argv[])
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// The evaluation fn - encapsulated into an eval counter for output // The evaluation fn - encapsulated into an eval counter for output
eoEvalFuncPtr<Indi, float> mainEval( binary_value<Indi>); eoEvalFuncPtr<Indi, double> mainEval( binary_value<Indi>);
eoEvalFuncCounter<Indi> eval(mainEval); eoEvalFuncCounter<Indi> eval(mainEval);
// COnstruction of the distribution // COnstruction of the distribution

View file

@ -32,7 +32,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
typedef eoBit<float> Chrom; typedef eoBit<double> Chrom;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------