ECF 1.5
StatCalc.cpp
1#include "ECF_base.h"
2#include "ECF_macro.h"
3#include <cmath>
4
5
6StatCalc::StatCalc()
7{
8 statNo = 0;
9 statsFileName_ = "";
10}
11
12
14{
15 state->getRegistry()->registerEntry("stats.file", (voidP) (new std::string("")), ECF::STRING);
16}
17
18
19bool StatCalc::initialize(StateP state)
20{
21 state_ = state;
22 average_.clear();
23 stdDev_.clear();
24 max_.clear();
25 min_.clear();
26 time_.clear();
27 sampleSize_.clear();
28 evaluations_.clear();
29 nEvaluations_ = 0;
30 lowest_ = highest_ = 0;
31
32 if(state->getRegistry()->isModified("stats.file")) {
33 voidP sptr = state->getRegistry()->getEntry("stats.file");
34 statsFileName_ = *((std::string*) sptr.get());
35
36 statsFile_.open(statsFileName_.c_str());
37 if(!statsFile_) {
38 throw std::string("Error: can't open stats file (") + statsFileName_ + ")";
39 }
40 }
41 return true;
42}
43
44
48double StatCalc::getFitnessMin(int generation)
49{
50 if(generation == -1)
51 generation = (int) statNo;
52 return min_[generation];
53}
54
55
59double StatCalc::getFitnessMax(int generation)
60{
61 if(generation == -1)
62 generation = (int) statNo;
63 return max_[generation];
64}
65
66
71bool StatCalc::operate(const std::vector<IndividualP>& pool)
72{
73 std::vector<double> fitnessTemp;
74
75 sampleSize_.push_back((uint)pool.size());
76 fitnessTemp.resize(pool.size());
77
78 for(uint i = 0; i < pool.size(); i++)
79 fitnessTemp[i] = pool[i]->fitness->getValue();
80
81 max_.push_back(fitnessTemp[0]);
82 min_.push_back(fitnessTemp[0]);
83 statNo = (uint) max_.size() - 1;
84 double sum = 0;
85
86 for(uint i = 0; i < fitnessTemp.size(); i++) {
87 if(fitnessTemp[i] > max_[statNo]) {
88 max_[statNo] = fitnessTemp[i];
89 }
90 if(fitnessTemp[i] < min_[statNo]) {
91 min_[statNo] = fitnessTemp[i];
92 }
93 sum += fitnessTemp[i];
94 }
95
96 average_.push_back(sum / fitnessTemp.size());
97
98 // check lowest and highest fitness values
99 if(min_.size() > 1) {
100 if(min_[statNo] < lowest_)
101 lowest_ = min_[statNo];
102 if(max_[statNo] > highest_)
103 highest_ = max_[statNo];
104 } else {
105 lowest_ = min_[0];
106 highest_ = max_[0];
107 }
108
109 // compute standard deviation
110 if(fitnessTemp.size() > 1) {
111 double numerator = 0, denominator;
112 for(uint i = 0; i < fitnessTemp.size(); i++) {
113 numerator += ((fitnessTemp[i] - average_[statNo]) * (fitnessTemp[i] - average_[statNo]));
114 }
115 denominator = (double) (fitnessTemp.size() - 1);
116 stdDev_.push_back(sqrt(numerator / denominator));
117 }
118 else
119 stdDev_.push_back(fitnessTemp[0]);
120
121 time_.push_back(state_->getElapsedTime());
122
123 evaluations_.push_back(nEvaluations_);
124
125 return true;
126}
127
128
132void StatCalc::copyStats(StatCalcP stats)
133{
134 std::vector<double> exStats = stats->getStats();
135 min_.push_back(exStats[0]);
136 max_.push_back(exStats[1]);
137 average_.push_back(exStats[2]);
138 stdDev_.push_back(exStats[3]);
139 sampleSize_.push_back((uint) exStats[4]);
140 time_.push_back((uint) exStats[5]);
141 evaluations_.push_back((uint) exStats[6]);
142 lowest_ = exStats[ECF::FIT_LOW];
143 highest_ = exStats[ECF::FIT_HIGH];
144
145 nEvaluations_ = evaluations_.back();
146 statNo = (uint) max_.size() - 1;
147}
148
149
153bool StatCalc::update(std::vector<double> stats)
154{
155 double outMin = stats[0];
156 double outMax = stats[1];
157 double outAvg = stats[2];
158 double outDev = stats[3];
159 uint outSize = (uint) stats[4];
160 uint outEval = (uint) stats[6];
161
162 if(outMin < min_.back())
163 min_.back() = outMin;
164
165 if(outMax > max_.back())
166 max_.back() = outMax;
167
168 evaluations_.back() += outEval;
169 nEvaluations_ = evaluations_.back();
170
171 uint mySize = sampleSize_.back();
172 if(mySize > 1) {
173 sampleSize_.back() = mySize + outSize;
174 double myAvg = average_.back();
175 double newAvg = (myAvg * mySize + outAvg * outSize) / sampleSize_.back();
176 average_.back() = newAvg;
177
178 uint totSize = sampleSize_.back();
179 double mySum = myAvg * mySize;
180 double outSum = outAvg * outSize;
181 double totSum = mySum + outSum;
182 double mySumSqr = pow(stdDev_.back(), 2) * mySize + mySum / mySize * mySum;
183 double outSumSqr = outDev * outDev * outSize + outSum / outSize * outSum;
184 double newDev = sqrt((mySumSqr + outSumSqr - (totSum / totSize * totSum)) / totSize);
185 stdDev_.back() = newDev;
186 }
187 else {
188 average_.back() = outAvg;
189 stdDev_.back() = outDev;
190 sampleSize_.back() = outSize;
191 }
192
193 if(stats[ECF::FIT_LOW] < lowest_)
194 lowest_ = stats[ECF::FIT_LOW];
195 if(stats[ECF::FIT_HIGH] > highest_)
196 highest_ = stats[ECF::FIT_HIGH];
197
198 return true;
199}
200
201
205std::vector<double> StatCalc::getStats(int gen)
206{
207 if(gen == -1)
208 gen = (int) statNo;
209
210 std::vector<double> stats;
211 stats.push_back(min_[gen]);
212 stats.push_back(max_[gen]);
213 stats.push_back(average_[gen]);
214 stats.push_back(stdDev_[gen]);
215 stats.push_back((double) sampleSize_[gen]);
216 stats.push_back((double) time_[gen]);
217 stats.push_back((double) evaluations_[gen]);
218 stats.push_back(lowest_);
219 stats.push_back(highest_);
220
221 return stats;
222}
223
224
228void StatCalc::log(int generation)
229{
230 if(generation == -1)
231 generation = (int) statNo;
232
233 ECF_LOG(state_, 3, "Evaluations: " + uint2str(evaluations_[generation]) +
234 "\nStats: fitness\n\tmax: " + dbl2str(max_[generation]) + "\n\tmin: " +
235 dbl2str(min_[generation]) + "\n\tavg: " + dbl2str(average_[generation]) + "\n\tstdev: " + dbl2str(stdDev_[generation]) + "\n");
236}
237
238
239// ispis tijeka min fitnesa - za potrebe mjerenja
240void StatCalc::output(uint step)
241{
242 std::stringstream log;
243 for(uint i = 0; i < min_.size(); i += step)
244 log << i << "\t" << min_[i] << std::endl;
245 ECF_LOG(state_, 2, log.str());
246}
bool initialize(StateP)
Perform initialization. Called before Operator::operate. By default, if the return value is false,...
Definition: StatCalc.cpp:19
double getFitnessMax(int generation=-1)
Definition: StatCalc.cpp:59
double getFitnessMin(int generation=-1)
Definition: StatCalc.cpp:48
void registerParameters(StateP)
Register parameters with the Registry. Called before Operator::initialize.
Definition: StatCalc.cpp:13
void log(int generation=-1)
Definition: StatCalc.cpp:228
bool update(std::vector< double >)
Definition: StatCalc.cpp:153
void copyStats(StatCalcP)
Definition: StatCalc.cpp:132
std::vector< double > getStats(int generation=-1)
Definition: StatCalc.cpp:205
bool operate(StateP)
perform the designated operation
Definition: StatCalc.h:40