00001 #include <ecf/ECF.h> 00002 #include "OneMaxEvalOp.h" 00003 00004 // evaluate() receives a smart pointer to the individual to evaluate 00005 FitnessP OneMaxEvalOp::evaluate(IndividualP individual) 00006 { 00007 // evaluation creates a new fitness object using a smart pointer 00008 // in our case, we try to maximize the number of ones, so we use FitnessMax fitness (for maximization problems) 00009 FitnessP fitness (new FitnessMax); 00010 00011 // Each individual is a vector of genotypes (defined in the configuration file). 00012 // We'll use BitString, and put it as the first and only genotype 00013 BitString::BitString* bitstr = (BitString::BitString*) individual->getGenotype().get(); 00014 //BitStringP bitstr = boost::static_pointer_cast<BitString::BitString> (individual->getGenotype(0)); // don't need zero for the first one 00015 00016 // count the ones; where are they? 00017 // BitString genotype contains a std::vector of bool's named 'bits' 00018 uint ones = 0; 00019 for(uint i = 0; i<bitstr->bits.size(); i++){ 00020 if(bitstr->bits[i] == true) 00021 ones++ ; 00022 } 00023 fitness->setValue(ones); 00024 00025 // return the smart pointer to new fitness 00026 return fitness; 00027 }