1#include "GPSymbRegEvalOp.h"
4#include <boost/math/special_functions/fpclassify.hpp>
6#include "MeanSquareErrorMetric.h"
7#include "MeanAbsoluteErrorMetric.h"
8#include "MeanAbsolutePercentageErrorMetric.h"
11#include "IATerminal.h"
16 state->getRegistry()->registerEntry(
"input_file", (voidP)(
new std::string), ECF::STRING);
17 state->getRegistry()->registerEntry(
"linear_scaling", (voidP) (
new std::string), ECF::STRING);
18 state->getRegistry()->registerEntry(
"error_weights.file", (voidP) (
new std::string), ECF::STRING);
19 state->getRegistry()->registerEntry(
"error_metric", (voidP) (
new std::string), ECF::STRING);
27 voidP sptr = state->getRegistry()->getEntry(
"linear_scaling");
28 ss << *((std::string*) sptr.get());
29 std::string ls = ss.str();
30 if (ls.compare(
"true") == 0) {
33 linearScaling =
false;
46 if (state->getRegistry()->isModified(
"input_file")) {
47 std::string dataFile = *((std::string*) state->getRegistry()->getEntry(
"input_file").get());
50 if (!readDataFromFile(eval.data, dataFile)) {
54 nSamples = eval.data.size();
55 nVariables = eval.data[0].size() - 1;
59 for(uint i = 0; i < nVariables; i++) {
60 varNames.push_back (
"x" + uint2str(i + 1));
63 GenotypeP genotype = state->getGenotypes()[0];
64 Tree::IATreeP iaTree = boost::dynamic_pointer_cast<Tree::IATree>(genotype);
67 for(uint i = 0; i < nVariables; i++) {
68 IATerminalP terminal = boost::dynamic_pointer_cast<IATerminal>(iaTree->primitiveSet_->getTerminalByName(
"x" + uint2str(i + 1)));
69 assert(terminal != 0);
70 double low = eval.data[0][i];
72 for (uint j = 1; j < nSamples; j++) {
73 double d = eval.data[j][i];
82 terminal->lowerBound = low;
83 terminal->upperBound = up;
88 errorWeightsDefined =
false;
90 if (state->getRegistry()->isModified(
"error_weights.file")) {
91 std::string dataFile = *((std::string*) state->getRegistry()->getEntry(
"error_weights.file").get());
93 std::vector< std::vector<double> > error_weights;
96 if (!readDataFromFile(error_weights, dataFile)) {
100 errorWeightsDefined =
true;
102 unsigned int size = error_weights.size();
103 for (uint i = 0; i < size; i++) {
104 errorWeights.push_back(error_weights[i][0]);
107 if (size < nSamples) {
108 std::cout <<
"Error weights count less than samples count." << std::endl;
109 errorWeightsDefined =
false;
114 if (state->getRegistry()->isModified(
"error_metric")) {
115 std::string errorMetricStr = *((std::string*) state->getRegistry()->getEntry(
"error_metric").get());
117 if (errorMetricStr.compare(
"mean_square_error") == 0) {
119 }
else if (errorMetricStr.compare(
"mean_absolute_error") == 0) {
121 }
else if (errorMetricStr.compare(
"mean_absolute_percentage_error") == 0) {
124 std::cout <<
"Error metric unknown." << std::endl;
136bool GPSymbRegEvalOp::csvRead(StateP state, std::string entry, std::vector<double>* vec)
138 std::ifstream stream;
140 voidP sptr = state->getRegistry()->getEntry(entry);
141 std::string fname = *((std::string*) sptr.get());
142 stream.open(fname.c_str());
143 if (!stream.is_open())
return false;
144 while (getline(stream, line)){
145 vec->push_back(atof(line.c_str()));
151FitnessP GPSymbRegEvalOp::evaluateUsingLinearScaling(IndividualP individual)
162 double mean_y = 0, mean_t = 0, sum_yt = 0, sum_sqr_y = 0;
164 for (uint i = 0 ; i < nSamples ; i++)
167 for(uint term = 0; term < nVariables; term++) {
175 double t = eval.data[i][nVariables];
185 double b = (sum_yt - nSamples*mean_y*mean_t)/(sum_sqr_y - nSamples*mean_y*mean_y);
186 double a = mean_t - b*mean_y;
188 bool isNaN = boost::math::isnan(a) || boost::math::isnan(b);
189 bool isInf = boost::math::isinf(a) || boost::math::isinf(b);
190 if (isNaN || isInf) {
191 fitness->setValue(1e14);
198 for(uint i = 0; i < nSamples; i++) {
200 for(uint term = 0; term < nVariables; term++) {
202 dataWriter->writeData(eval.data[i][term]);
203 dataWriter->writeData(
'\t');
209 var_of_y += (y-mean_y)*(y-mean_y);
211 double t = eval.data[i][nVariables];
212 double result = a + b*y;
213 dataWriter->writeData(result);
214 dataWriter->writeData(
'\n');
216 double error = errorMetric->calculateError(result, t);
218 if (errorWeightsDefined) {
219 error *= errorWeights[i];
222 value += error*error;
226 var_of_y /= nSamples-1;
234 std::cout <<
"\nLinear scaling parameters: scale=" << b <<
" offset=" << a << std::endl;
237 fitness->setValue(value);
243FitnessP GPSymbRegEvalOp::evaluateWithoutLinearScaling(IndividualP individual)
258 for (uint i = 0; i < nSamples; i++) {
260 for(uint term = 0; term < nVariables; term++) {
262 dataWriter->writeData(eval.data[i][term]);
263 dataWriter->writeData(
'\t');
268 dataWriter->writeData(result);
269 dataWriter->writeData(
'\n');
276 double error = errorMetric->calculateError(result, eval.data[i][nVariables]);
278 if (errorWeightsDefined) {
279 error *= errorWeights[i];
286 value /= pow(1.*nSamples, 2);
287 fitness->setValue(value);
295 return evaluateUsingLinearScaling(individual);
297 return evaluateWithoutLinearScaling(individual);
303void GPSymbRegEvalOp::readIndividual(IndividualP individual)
305 TreeP tree = boost::dynamic_pointer_cast<Tree::Tree> (individual->getGenotype());
306 static std::string strPrimitive;
308 uint nTrees = (uint) individual->size();
309 eval.parsedExpression.resize(nTrees);
312 for(uint iTree = 0; iTree < nTrees; iTree++) {
313 TreeP pTree = boost::dynamic_pointer_cast<Tree::Tree> (individual->getGenotype(iTree));
314 nTreeSize = (uint) pTree->size();
315 eval.parsedExpression[iTree].resize(nTreeSize);
318 for(uint i = 0; i < nTreeSize; i++) {
319 strPrimitive = (*pTree)[i]->primitive_->getName();
322 for(uint iPrim = 0; iPrim < eval.funcNames.size(); iPrim++)
326 if(strPrimitive == eval.funcNames[iPrim]) {
327 eval.parsedExpression[iTree][i] = iPrim;
333 for(uint iPrim = 0; iPrim < eval.termNames.size(); iPrim++)
336 if(strPrimitive == eval.termNames[iPrim]) {
337 eval.parsedExpression[iTree][i] = iPrim + Evaluator::TERMINALS;
Fitness for minimization problems.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
Tree class - implements genotype as a tree.
void setTerminalValue(std::string, void *)
Set a terminal's value.
void execute(void *)
Execute current tree.