-
Notifications
You must be signed in to change notification settings - Fork 7
/
Fitness.cpp
83 lines (72 loc) · 2.86 KB
/
Fitness.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/***************************************************************************
* (c) 2008-2011 Aleksandar Topuzović *
* <aleksandar.topuzovic@fer.hr>, <aleksandar.topuzovic@gmail.com> *
***************************************************************************/
#include "Fitness.hpp"
#include <cfloat>
#include <cmath>
#include <algorithm>
using namespace trading;
Fitness::Fitness() :
Beagle::FitnessSimple(false)
{ }
Fitness::Fitness(float inFitness) :
Beagle::FitnessSimple(true)
{
setValue(inFitness);
}
Fitness::Fitness(float inFitness1, float inFitness2) :
Beagle::FitnessSimple(true)
{
setValue(inFitness1, inFitness2);
}
void Fitness::setValue(float inFitness)
{
setValue(inFitness, 0);
}
void Fitness::setValue(float inFitness1, float inFitness2)
{
// Training set fitness
if(Beagle::isFinite(inFitness1)) mFitness = inFitness1;
else mFitness = FLT_MIN;
// Validation set fitness
if(Beagle::isFinite(inFitness2)) vsFitness = inFitness2;
else vsFitness = FLT_MIN;
setValid();
}
void Fitness::read(PACC::XML::ConstIterator inIter)
{
if((inIter->getType()!=PACC::XML::eData) || (inIter->getValue()!="Fitness"))
throw Beagle_IOExceptionNodeM(*inIter, "tag <Fitness> expected!");
std::string lType = inIter->getAttribute("type").c_str();
if(lType != "trading") throw Beagle_IOExceptionNodeM(*inIter, "fitness type mismatch!");
for(PACC::XML::ConstIterator lChild=inIter->getFirstChild(); lChild; ++lChild) {
if(lChild->getType() == PACC::XML::eData) {
if(lChild->getValue() == "FitnessTS") { // Fitness on training set
PACC::XML::ConstIterator lChild2 = lChild->getFirstChild();
if(!lChild2) throw Beagle_IOExceptionNodeM(*lChild, "no normalized fitness value present!");
if(lChild2->getType() != PACC::XML::eString)
throw Beagle_IOExceptionNodeM(*lChild2, "no normalized fitness value present!");
mFitness = Beagle::str2dbl(lChild2->getValue().c_str());
} else if(lChild->getValue() == "FitnessVS") { // Fitness on validation set
PACC::XML::ConstIterator lChild2 = lChild->getFirstChild();
if(!lChild2) throw Beagle_IOExceptionNodeM(*lChild, "no adjusted fitness value present!");
if(lChild2->getType() != PACC::XML::eString)
throw Beagle_IOExceptionNodeM(*lChild2, "no adjusted fitness value present!");
vsFitness = Beagle::str2dbl(lChild2->getValue().c_str());
}
}
}
}
void Fitness::write(PACC::XML::Streamer& ioStreamer, bool inIndent) const
{
ioStreamer.openTag("Fitness", inIndent);
ioStreamer.insertAttribute("type", "trading");
ioStreamer.openTag("FitnessTS", false); // Fitness on training set
ioStreamer.insertStringContent(Beagle::dbl2str(mFitness).c_str());
ioStreamer.closeTag();
ioStreamer.openTag("FitnessVS", false); // Fitness on validation set
ioStreamer.insertStringContent(Beagle::dbl2str(vsFitness).c_str());
ioStreamer.closeTag();
ioStreamer.closeTag();
}