ECF 1.5
OneMaxEvalOp.cpp
1#include <ecf/ECF.h>
2#include "OneMaxEvalOp.h"
3
4// evaluate() receives a smart pointer to the individual to evaluate
5FitnessP OneMaxEvalOp::evaluate(IndividualP individual)
6{
7 // evaluation creates a new fitness object using a smart pointer
8 // in our case, we try to maximize the number of ones, so we use FitnessMax fitness (for maximization problems)
9 FitnessP fitness (new FitnessMax);
10
11 // Each individual is a vector of genotypes (defined in the configuration file).
12 // We'll use BitString, and put it as the first and only genotype
13 BitString::BitString* bitstr = (BitString::BitString*) individual->getGenotype().get();
14 //BitStringP bitstr = boost::static_pointer_cast<BitString::BitString> (individual->getGenotype(0)); // don't need zero for the first one
15
16 // count the ones; where are they?
17 // BitString genotype contains a std::vector of bool's named 'bits'
18 uint ones = 0;
19 for(uint i = 0; i<bitstr->bits.size(); i++){
20 if(bitstr->bits[i] == true)
21 ones++ ;
22 }
23 fitness->setValue(ones);
24
25 // return the smart pointer to new fitness
26 return fitness;
27}
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
Fitness for maximization problems.
Definition: FitnessMax.h:13
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
Definition: OneMaxEvalOp.cpp:5