-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbinary_map.hpp
66 lines (58 loc) · 1.83 KB
/
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
#ifndef BINARY_MAP_HPP_
#define BINARY_MAP_HPP_
#include <numeric>
#include <array>
#include <vector>
#include <string>
#include <fstream>
#include <boost/multi_array.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
// this is c++11!
// this header can be included in your project to load maps generated by map-elites in binary (whithout any loss of precision)
// it does not depends on sferes
// it works only for EvoFloat and Sampled genotypes!
namespace binary_map {
struct Elem {
std::vector<int> pos;
std::vector<float> phen;
float fit;
template <class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar& BOOST_SERIALIZATION_NVP(pos);
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;
}
};
#endif