-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcvt_binary_map.hpp
68 lines (60 loc) · 2.08 KB
/
cvt_binary_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
#ifndef CVT_MAP_ELITES_BINARY_MAP_HPP_
#define CVT_MAP_ELITES_BINARY_MAP_HPP_
#include <array>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/multi_array.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <numeric>
#include <string>
#include <vector>
// this is c++11!
// this header can be included in your project to load maps generated by cvt-map-elites in binary (without any loss of precision)
// it does not depends on sferes
// it works only for EvoFloat and Sampled genotypes!
namespace cvt {
namespace binary_map {
struct Elem {
std::vector<float> desc;
std::vector<float> phen;
float fit;
template <class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar& BOOST_SERIALIZATION_NVP(desc);
ar& BOOST_SERIALIZATION_NVP(phen);
ar& BOOST_SERIALIZATION_NVP(fit);
}
};
struct BinaryMap {
std::vector<Elem> elems;
std::vector<float> dims;
template <class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar& BOOST_SERIALIZATION_NVP(elems);
ar& BOOST_SERIALIZATION_NVP(dims);
}
};
static void write(const BinaryMap& m, const std::string& filename)
{
std::ofstream ofs(filename.c_str());
assert(ofs.good());
std::cout << "writing : " << filename << std::endl;
boost::archive::binary_oarchive oa(ofs);
oa& BOOST_SERIALIZATION_NVP(m);
std::cout << "done" << std::endl;
};
static BinaryMap load(const std::string& filename)
{
std::ifstream ifs(filename.c_str());
assert(ifs.good());
boost::archive::binary_iarchive ia(ifs);
BinaryMap data;
ia& BOOST_SERIALIZATION_NVP(data);
return data;
}
} // namespace binary_map
} // namespace cvt
#endif