-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_cmoea.hpp
143 lines (110 loc) · 4.35 KB
/
stat_cmoea.hpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* stat_cmoea.hpp
*
* Created on: Feb 26, 2015
* Author: Joost Huizinga
*/
#ifndef EXP_CMOEA_EXAMPLE_STAT_CMOEA_HPP_
#define EXP_CMOEA_EXAMPLE_STAT_CMOEA_HPP_
#include <numeric>
#include <iomanip>
#include <sstream>
#include <string>
#include <iostream>
#include <sferes/stat/stat.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/spirit/include/karma_real.hpp>
#include <sferes/dbg/dbg.hpp>
#include <modules/datatools/common_compare.hpp>
#include <modules/cmoea/cmoea_util.hpp>
namespace sferes
{
namespace stat
{
SFERES_STAT(StatCmoea, Stat)
{
public:
StatCmoea(){
_first_line = true;
}
static const size_t period = Params::stats::period;
typedef boost::shared_ptr<Phen> indiv_t;
typedef std::vector<indiv_t> pop_t;
template<typename E>
void refresh(const E& ea)
{
dbg::trace trace("stat", DBG_HERE);
typedef typename E::pop_t mixed_pop_t;
size_t categories = Params::cmoea::nb_of_bins;
size_t objectives = cmoea::getNrOfObjectives(categories);
size_t bin_size = Params::cmoea::bin_size;
dbg::out(dbg::info, "stat") << "Gen: " << ea.gen() << " period: " << period << std::endl;
if (ea.gen() % period != 0) return;
//Write header
dbg::out(dbg::info, "stat") << "Creating log file" << std::endl;
this->_create_log_file(ea, "innov_all_bin.dat");
dbg::out(dbg::info, "stat") << "Log file: " << this->_log_file << std::endl;
if(!this->_log_file) return;
if(this->firstLine()){
dbg::out(dbg::info, "stat") << "Writing header" << std::endl;
(*this->_log_file) << "#gen ";
for(size_t i=0; i<categories; ++i){
(*this->_log_file) << "obj_" << cmoea::getTaskIndicesStr(i, objectives) << " ";
}
(*this->_log_file) << "\n";
_first_line = false;
}
std::vector<size_t> best_fit_index(categories, 0);
std::vector<float> best_fit(categories, 0);
dbg::assertion(DBG_ASSERTION(categories == ea.archive().size()));
//Add them in this exact order so it is easy to put them back into the archive
_all.clear();
dbg::out(dbg::info, "stat") << "Archive size: " << ea.archive().size() << std::endl;
for(size_t i=0; i<ea.archive().size(); ++i){
dbg::check_bounds(dbg::error, 0, i, ea.archive().size(), DBG_HERE);
dbg::out(dbg::info, "stat") << "Bin: " << i << " size: " << ea.archive()[i].size() << std::endl;
best_fit[i]=ea.archive()[i][0]->fit().getBinFitness(i);
for(size_t j=0; j<ea.archive()[i].size(); ++j){
dbg::check_bounds(dbg::error, 0, j, ea.archive()[i].size(), DBG_HERE);
float fitness = ea.archive()[i][j]->fit().getBinFitness(i);
if(fitness > best_fit[i]){
dbg::check_bounds(dbg::error, 0, i, best_fit.size(), DBG_HERE);
dbg::check_bounds(dbg::error, 0, i, best_fit_index.size(), DBG_HERE);
best_fit[i] = fitness;
best_fit_index[i] = j;
}
_all.push_back(ea.archive()[i][j]);
}
}
dbg::assertion(DBG_ASSERTION(_all.size() == bin_size*categories));
dbg::check_bounds(dbg::error, 0, 0, ea.archive().size(), DBG_HERE);
dbg::check_bounds(dbg::error, 0, 0, best_fit_index.size(), DBG_HERE);
dbg::check_bounds(dbg::error, 0, best_fit_index[0], ea.archive()[0].size(), DBG_HERE);
_best_individual = ea.archive()[0][best_fit_index[0]];
//Start writing
dbg::out(dbg::info, "stat") << "Writing line" << std::endl;
(*this->_log_file) << ea.gen() << " "; // generation
for(size_t i=0; i<best_fit.size(); ++i){
(*this->_log_file) << best_fit[i] << " ";
}
(*this->_log_file) << std::endl;
std::cout << "Genenration: " << ea.gen() << " combined performance: " << best_fit [0] << std::endl;
}
bool firstLine(){
return _first_line;
}
pop_t& getPopulation(){
dbg::trace trace("stat", DBG_HERE);
return _all;
}
std::string name(){
return "StatCmoea";
}
protected:
indiv_t _best_individual;
pop_t _all;
bool _first_line;
};
}
}
#endif /* EXP_CMOEA_EXAMPLE_STAT_CMOEA_HPP_ */