-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstat_map.hpp
154 lines (133 loc) · 6.03 KB
/
stat_map.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
144
145
146
147
148
149
150
151
152
153
154
#ifndef STAT_MAP_HPP_
#define STAT_MAP_HPP_
#include <numeric>
#include <boost/multi_array.hpp>
#include <boost/serialization/array.hpp>
#include <sferes/stat/stat.hpp>
// #define MAP_WRITE_PARENTS
namespace sferes {
namespace stat {
SFERES_STAT(Map, Stat)
{
public:
typedef boost::shared_ptr<Phen> phen_t;
typedef boost::multi_array<phen_t, Params::ea::behav_dim> array_t;
typedef std::array<float, Params::ea::behav_dim> point_t;
typedef std::array<typename array_t::index, Params::ea::behav_dim> behav_index_t;
size_t behav_dim;
behav_index_t behav_shape;
behav_index_t behav_strides;
behav_index_t behav_indexbase;
Map() : behav_dim(Params::ea::behav_dim)
{
for (size_t i = 0; i < Params::ea::behav_shape_size(); ++i)
behav_shape[i] = Params::ea::behav_shape(i);
}
template <typename E>
void refresh(const E& ea)
{
_archive.clear();
for (size_t i = 0; i < behav_dim; ++i) {
assert(ea.archive().shape()[i] == behav_shape[i]);
behav_strides[i] = ea.archive().strides()[i];
behav_indexbase[i] = ea.archive().index_bases()[i];
}
for (const phen_t* i = ea.archive().data(); i < (ea.archive().data() + ea.archive().num_elements()); ++i) {
phen_t p = *i;
_archive.push_back(p);
}
if (ea.gen() % Params::pop::dump_period == 0) {
_write_archive(ea.archive(), std::string("archive_"), ea);
#ifdef MAP_WRITE_PARENTS
_write_parents(ea.archive(), ea.parents(), std::string("parents_"), ea);
#endif
}
}
void show(std::ostream & os, size_t k)
{
std::cerr << "loading ";
for (size_t i = 0; i < behav_dim; ++i)
std::cerr << (k / behav_strides[i] % behav_shape[i] + behav_indexbase[i]) << ",";
std::cerr << std::endl;
if (_archive[k]) {
_archive[k]->develop();
_archive[k]->show(os);
_archive[k]->fit().set_mode(fit::mode::view);
_archive[k]->fit().eval(*_archive[k]);
}
else
std::cerr << "Warning, no point here" << std::endl;
}
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar& BOOST_SERIALIZATION_NVP(_archive);
ar& BOOST_SERIALIZATION_NVP(behav_dim);
ar& BOOST_SERIALIZATION_NVP(behav_shape);
ar& BOOST_SERIALIZATION_NVP(behav_strides);
ar& BOOST_SERIALIZATION_NVP(behav_indexbase);
}
protected:
std::vector<phen_t> _archive;
template <typename EA>
void _write_parents(const array_t& array,
const array_t& p_array,
const std::string& prefix,
const EA& ea) const
{
std::cout << "writing..." << prefix << ea.gen() << std::endl;
std::string fname = ea.res_dir() + "/"
+ prefix
+ boost::lexical_cast<std::string>(ea.gen())
+ std::string(".dat");
std::ofstream ofs(fname.c_str());
for (const phen_t* i = array.data(); i < (array.data() + array.num_elements()); ++i) {
if (*i) {
behav_index_t idx = ea.getindexarray(array, i);
assert(array(idx)->fit().value() == (*i)->fit().value());
if (p_array(idx)) {
for (size_t dim = 0; dim < behav_dim; ++dim)
ofs << idx[dim] / (float)behav_shape[dim] << " ";
ofs << " " << p_array(idx)->fit().value() << " ";
point_t p = ea.get_point(p_array(idx));
behav_index_t posinparent;
for (size_t dim = 0; dim < behav_dim; ++dim) {
posinparent[dim] = round(p[dim] * behav_shape[dim]);
ofs << posinparent[dim] / (float)behav_shape[dim] << " ";
}
ofs << " " << array(idx)->fit().value() << std::endl;
}
}
}
}
template <typename EA>
void _write_archive(const array_t& array,
const std::string& prefix,
const EA& ea) const
{
std::cout << "writing..." << prefix << ea.gen() << std::endl;
std::string fname = ea.res_dir() + "/"
+ prefix
+ boost::lexical_cast<std::string>(ea.gen())
+ std::string(".dat");
std::ofstream ofs(fname.c_str());
size_t offset = 0;
for (const phen_t* i = array.data(); i < (array.data() + array.num_elements()); ++i) {
if (*i) {
behav_index_t posinarray = ea.getindexarray(array, i);
assert(array(posinarray)->fit().value() == (*i)->fit().value());
ofs << offset << " ";
for (size_t dim = 0; dim < behav_dim; ++dim)
ofs << posinarray[dim] / (float)behav_shape[dim] << " ";
ofs << " " << array(posinarray)->fit().value() << " ";
for (size_t k = 0; k < array(posinarray)->gen().size(); ++k)
ofs << array(posinarray)->gen().data(k) << " ";
ofs << std::endl;
}
++offset;
}
}
};
}
}
#endif