ECF 1.5
SymbRegEvalOp.cpp
1#include "SymbRegEvalOp.h"
2#include "./cartesian/Cartesian.h"
3
4void SymbRegEvalOp::addConstants(uint nConstants)
5{
6 for(uint i = 0; i < inputs.size(); i++) {
7 for(uint j = 0; j < nConstants; j++) {
8 inputs[i].push_back(cartesian::Cartesian::static_random_double(-10.0,10.0));
9 }
10 }
11}
12
13
15{
16 stateP->getRegistry()->registerEntry("training.infile", (voidP) new (std::string), ECF::STRING);
17 stateP->getRegistry()->registerEntry("expression.infile", (voidP) new(std::string), ECF::STRING);
18 stateP->getRegistry()->registerEntry("softtarget", (voidP) new(std::string), ECF::STRING);
19 stateP->getRegistry()->registerEntry("softtarget.gamma", (voidP) new(std::string), ECF::STRING);
20 stateP->getRegistry()->registerEntry("softtarget.beta", (voidP) new(std::string), ECF::STRING);
21}
22
23bool SymbRegEvalOp::initialize(StateP stateP)
24{
25 if(!stateP->getRegistry()->isModified("expression.infile")) {
26 ECF_LOG_ERROR(stateP, "Expression is not registered as a parameter. Register a path to a expression in parameters.txt file.");
27 return false;
28 }
29 voidP vp1 = stateP->getRegistry()->getEntry("expression.infile");
30 std::string path1 = *((std::string*)vp1.get());
31 std::ifstream in_file(path1.c_str());
32 if(!in_file) {
33 std::cerr << "Parser could not load a expression file: " << path1 << '\n';
34 exit(-1);
35 }
36 std::string line;
37 getline(in_file,line);
38 in_file.close();
39 this->equation = line;
40 if(!stateP->getRegistry()->isModified("training.infile")) {
41 ECF_LOG_ERROR(stateP, "Error: no input file defined for arguments and function values.");
42 return false;
43 }
44 vp1 = stateP->getRegistry()->getEntry("training.infile");
45 path1 = *((std::string*)vp1.get());
46 std::ifstream in_file2(path1.c_str());
47 if(!in_file2) {
48 std::cerr << "Could not load a file which defines arguments / function values for given function.\n";
49 exit(-1);
50 }
51 for(std::string line2; getline(in_file2,line2);) {
52 std::pair<std::vector<double>,double> result = utility::parseArgumentsAndFunctionValues(line2);
53 this->inputs.push_back(result.first);
54 this->output.push_back(result.second);
55 }
56 if(stateP->getRegistry()->isModified("softtarget")) {
57 voidP vpS = stateP->getRegistry()->getEntry("softtarget");
58 std::string sa = *((std::string*)vpS.get());
59 if(sa == "1" || sa == "yes" || sa == "Yes" || sa == "true") {
60 softTarget = true;
61 ECF_LOG(stateP,0,"Soft target regularization is used.");
62 if(!stateP->getRegistry()->isModified("softtarget.beta")) {
63 ECF_LOG_ERROR(stateP,"Could not find beta factor for softtarget. Define softtarget.beta entry in registry.\n");
64 exit(-1);
65 }
66 else {
67 voidP vpSBeta = stateP->getRegistry()->getEntry("softtarget.beta");
68 std::string vbeta = *((std::string*)vpSBeta.get());
69 try{
70 softTargetBeta = str2dbl(vbeta);
71 }catch(std::exception& ex) {
72 ECF_LOG_ERROR(stateP,"Soft target beta is not a number convertible to double.\n");
73 exit(-1);
74 }
75 }
76 if(!stateP->getRegistry()->isModified("softtarget.gamma")) {
77 ECF_LOG_ERROR(stateP,"Could not find gamma factor for softtarget. Define softtarget.gamma entry in registry.\n");
78 exit(-1);
79 }
80 else {
81 voidP vpSGamma = stateP->getRegistry()->getEntry("softtarget.gamma");
82 std::string vgamma = *((std::string*)vpSGamma.get());
83 try{
84 softTargetGamma = str2dbl(vgamma);
85 }catch(std::exception& ex) {
86 ECF_LOG_ERROR(stateP, "Soft target gamma is not a number convertible to double.\n");
87 exit(-1);
88 }
89 }
90 }
91 }
92 return true;
93}
94
95FitnessP SymbRegEvalOp::evaluate(IndividualP individual) {
96 FitnessP fitness(new FitnessMin);
97 cartesian::Cartesian* cartesian = (cartesian::Cartesian *) individual->getGenotype().get();
98 //nInputs = nVariables + nConstants, constants are added to each example in a random fashion
99 addConstants(cartesian->nConstants);
100 std::vector<double> calculatedOutputs;
101 for(uint i = 0; i < inputs.size(); i++) {
102 std::vector<double> results;
103 cartesian->evaluate(inputs[i], results);
104 calculatedOutputs.push_back(results[0]);
105 }
106 double MSE;
107 if (!softTarget) {
108 MSE = utility::rootMeanSquareError(output, calculatedOutputs);
109 } else {
110 if (cartesian->state_->getGenerationNo() > 0) {
111 if (kappaOutput.size() != calculatedOutputs.size()) {
112 std::cout << "Theta output i calculated output nemaju jednake dimenzije.\n";
113 }
114 for (uint j = 0; j < kappaOutput.size(); j++) {
115 kappaOutput[j] = softTargetBeta * kappaOutput[j] + (1 - softTargetBeta) * calculatedOutputs[j];
116 }
117 if (kappaOutput.size() != thetaOutput.size()) {
118 std::cout << "Kappa output i Theta output nemaju jednake dimenzije.\n";
119 }
120 for (uint j = 0; j < thetaOutput.size(); j++) {
121 thetaOutput[j] = softTargetGamma * kappaOutput[j] + (1 - softTargetGamma) * output[j];
122 }
123 } else {
124 kappaOutput = calculatedOutputs;
125 thetaOutput = output;
126 }
127 MSE = utility::rootMeanSquareError(thetaOutput, calculatedOutputs);
128 }
129 fitness->setValue(MSE);
130 return fitness;
131}
Fitness for minimization problems.
Definition: FitnessMin.h:12
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
void registerParameters(StateP stateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
void evaluate(const vector< double > &inputData, vector< double > &results)
StateP state_
local copy of state
uint nConstants
number of input constants