00001 #include <ecf/ECF.h> 00002 #include "FunctionMinEvalOp.h" 00003 00004 00005 FitnessP FunctionMinEvalOp::evaluate(IndividualP individual) 00006 { 00007 // evaluation creates a new fitness object using a smart pointer 00008 // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems) 00009 FitnessP fitness (new FitnessMin); 00010 00011 // we define Binary as the only genotype (in the configuration file) 00012 Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get(); 00013 // (you can also use boost smart pointers:) 00014 //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype()); 00015 00016 // alternative encoding: FloatingPoint Genotype 00017 //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype()); 00018 //FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get(); 00019 00020 // we implement the fitness function 'as is', without any translation 00021 // the number of variables we read from the genotype itself (size of 'realValue' vactor) 00022 double realTemp = 0, value = 0; 00023 for (uint i = 0; i < gen->realValue.size(); i++){ 00024 realTemp = pow((gen->realValue.at(i) - (i + 1)), 2.); 00025 value += realTemp; 00026 } 00027 00028 fitness->setValue(value); 00029 return fitness; 00030 }
1.7.1