ECF 1.5
MultipleClassEvalOp.cpp
1#include "MultipleClassEvalOp.h"
2#include "./cartesian/Cartesian.h"
3#include "utility/fileparser.h"
4#include "./utility/measures.h"
5#include "./utility/utility.h"
6#include <boost/algorithm/string.hpp>
7
9{
10 stateP->getRegistry()->registerEntry("training.infile", (voidP) new (std::string), ECF::STRING);
11 stateP->getRegistry()->registerEntry("testing.infile", (voidP) new (std::string), ECF::STRING);
12 stateP->getRegistry()->registerEntry("measure", (voidP) new (std::string), ECF::STRING);
13 stateP->getRegistry()->registerEntry("hasID", (voidP) new (std::string), ECF::STRING);
14}
15
17{
18 if(!stateP->getRegistry()->isModified("training.infile")) {
19 ECF_LOG_ERROR(stateP, "Error: no input file defined for training");
20 return false;
21 }
22 if(!stateP->getRegistry()->isModified("testing.infile")) {
23 ECF_LOG_ERROR(stateP, "Error: no input file defined for testing");
24 return false;
25 }
26 if(!stateP->getRegistry()->isModified("measure")) {
27 ECF_LOG(stateP,0,"Default measure of accuracy is used.");
28 measureUsed = "accuracy";
29 }
30 else {
31 voidP vpM = stateP->getRegistry()->getEntry("measure");
32 measureUsed = *((std::string*)vpM.get());
33 boost::algorithm::to_lower(measureUsed);
34 }
35 if(!stateP->getRegistry()->isModified("hasID")) {
36 leadsWithId = true;
37 }
38 //Load training from file.
39 voidP vp1 = stateP->getRegistry()->getEntry("training.infile");
40 std::string path1 = *((std::string*)vp1.get());
41 std::vector<std::string> parsedTrainingCSV = utility::parseCSVFromFile(path1);
42 PairAllFeaturesAllLabels ioTrainingPair = utility::parseStringIntoFeaturesAndLabels(parsedTrainingCSV, leadsWithId);
43 trainingInput = ioTrainingPair.first;
44 trainingOutputs = ioTrainingPair.second;
45
46 //Load testing from file.
47 voidP vp2 = stateP->getRegistry()->getEntry("testing.infile");
48 std::string path2 = *((std::string*)vp2.get());
49 std::vector<std::string> parsedTestCSV = utility::parseCSVFromFile(path2);
50 PairAllFeaturesAllLabels ioTestPair = utility::parseStringIntoFeaturesAndLabels(parsedTestCSV, leadsWithId);
51 testingInput = ioTestPair.first;
52 testingOutputs = ioTestPair.second;
53
54 std::set<uint> distinct;
55 for(uint i = 0; i < trainingOutputs.size(); i++) {
56 distinct.insert(trainingOutputs[i]);
57 }
58 numberOfDifferentClasses = distinct.size();
59
60 if(stateP->getRegistry()->isModified("softtarget")) {
61 voidP vpS = stateP->getRegistry()->getEntry("softtarget");
62 std::string sa = *((std::string*)vpS.get());
63 if(sa == "1" || sa == "yes" || sa == "Yes" || sa == "true") {
64 softTarget = true;
65 ECF_LOG(stateP,0,"Soft target regularization is used.");
66 if(!stateP->getRegistry()->isModified("softtarget.beta")) {
67 ECF_LOG_ERROR(stateP,"Could not find beta factor for softtarget. Define softtarget.beta entry in registry.\n");
68 exit(-1);
69 }
70 else {
71 voidP vpSBeta = stateP->getRegistry()->getEntry("softtarget.beta");
72 std::string vbeta = *((std::string*)vpSBeta.get());
73 try{
74 softTargetBeta = str2dbl(vbeta);
75 }catch(std::exception& ex) {
76 ECF_LOG_ERROR(stateP,"Soft target beta is not a number convertible to double.\n");
77 exit(-1);
78 }
79 }
80 if(!stateP->getRegistry()->isModified("softtarget.gamma")) {
81 ECF_LOG_ERROR(stateP,"Could not find gamma factor for softtarget. Define softtarget.gamma entry in registry.\n");
82 exit(-1);
83 }
84 else {
85 voidP vpSGamma = stateP->getRegistry()->getEntry("softtarget.gamma");
86 std::string vgamma = *((std::string*)vpSGamma.get());
87 try{
88 softTargetGamma = str2dbl(vgamma);
89 }catch(std::exception& ex) {
90 ECF_LOG_ERROR(stateP, "Soft target gamma is not a number convertible to double.\n");
91 exit(-1);
92 }
93 }
94 }
95 }
96 return true;
97}
98
99FitnessP MultipleClassEvalOp::evaluate(IndividualP individual)
100{
101 FitnessP fitness(new FitnessMax);
102 cartesian::Cartesian* cartesian = (cartesian::Cartesian*) individual->getGenotype().get();
103
104 std::vector<std::vector<uint> > confusionMatrix(numberOfDifferentClasses, std::vector<uint>(numberOfDifferentClasses,0));
105
106 for(uint i = 0; i < trainingInput.size(); i++) {
107 std::vector<double> results;
108 cartesian->evaluate(trainingInput[i], results);
109 confusionMatrix[utility::vectorArgmax(results)][trainingOutputs[i]]++;
110 }
111 double valueOfFitness = utility::returnConfusionMatrixResult(confusionMatrix,trainingInput.size(),measureUsed);
112 fitness->setValue(valueOfFitness);
113 return fitness;
114}
Fitness for maximization problems.
Definition: FitnessMax.h:13
bool initialize(StateP stateP)
Initialize the evaluator. Called before first evaluation occurs.
void registerParameters(StateP 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.
void evaluate(const vector< double > &inputData, vector< double > &results)