-
Notifications
You must be signed in to change notification settings - Fork 7
/
stat_map_binary.hpp
64 lines (57 loc) · 2.1 KB
/
stat_map_binary.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
#ifndef STAT_MAP_BINARY_HPP_
#define STAT_MAP_BINARY_HPP_
#include <numeric>
#include <boost/multi_array.hpp>
#include <sferes/stat/stat.hpp>
#include <boost/serialization/array.hpp>
#include "binary_map.hpp"
namespace sferes {
namespace stat {
// - write in generic binary format
// - work only for evofloat & sampled !
// (the binary map in sferes form is in gen_* if you have StatMap)
SFERES_STAT(MapBinary, Stat){
public :
template <typename E>
void refresh(const E& ea){
if (ea.gen() % Params::pop::dump_period != 0) return;
std::string fname = ea.res_dir() + "/"
+ "map_" + boost::lexical_cast<std::string>(ea.gen())
+ std::string(".bin");
auto archive = _multi_array_to_vector(ea.archive(), ea);
binary_map::write(archive, fname);
}
protected:
// convert a (possibly sparse) boost::multi_array of phenotypes
// to a list of vectors that can be used in other software
template <typename A, typename EA>
binary_map::BinaryMap _multi_array_to_vector(const A& array, const EA& ea)
{
binary_map::BinaryMap map;
map.dims.clear();
map.elems.clear();
for (size_t dim = 0; dim < array.num_dimensions(); ++dim)
map.dims.push_back(array.shape()[dim]);
size_t offset = 0;
for (auto i = array.data(); i < (array.data() + array.num_elements()); ++i) {
if (*i) {
auto posinarray = ea.getindexarray(array, i);
std::vector<int> pos;
for (size_t dim = 0; dim < array.num_dimensions(); ++dim) {
pos.push_back(posinarray[dim]);
}
auto phen = array(posinarray);
float fit = phen->fit().value();
std::vector<float> p;
for (size_t j = 0; j < phen->size(); ++j)
p.push_back(phen->data(j));
map.elems.push_back(binary_map::Elem{pos, p, fit});
}
++offset;
}
return map;
}
};
}
}
#endif