25 #ifndef PAGMO_SERIALIZATION_H
26 #define PAGMO_SERIALIZATION_H
28 #include "adj_list_serialize_bug_fix.hpp"
31 #include <boost/archive/binary_iarchive.hpp>
32 #include <boost/archive/binary_oarchive.hpp>
33 #include <boost/archive/text_iarchive.hpp>
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/graph/adjacency_list.hpp>
36 #include <boost/lexical_cast.hpp>
37 #include <boost/math/special_functions/fpclassify.hpp>
38 #include <boost/serialization/assume_abstract.hpp>
39 #include <boost/serialization/base_object.hpp>
40 #include <boost/serialization/export.hpp>
41 #include <boost/serialization/serialization.hpp>
42 #include <boost/serialization/shared_ptr.hpp>
43 #include <boost/serialization/split_free.hpp>
44 #include <boost/serialization/split_member.hpp>
45 #include <boost/serialization/string.hpp>
46 #include <boost/serialization/utility.hpp>
47 #include <boost/serialization/vector.hpp>
48 #include <boost/serialization/version.hpp>
53 #include <boost/circular_buffer.hpp>
54 #include <boost/unordered_map.hpp>
55 #include <boost/random/normal_distribution.hpp>
56 #include <boost/random/uniform_real_distribution.hpp>
61 #include "Eigen/Dense"
66 template <
class Archive>
69 const std::vector<double>::size_type size = v.size();
74 for (std::vector<double>::size_type i = 0; i < size; ++i) {
75 if (boost::math::isnan(v[i])) {
77 }
else if (boost::math::isinf(v[i])) {
84 tmp = boost::lexical_cast<std::string>(v[i]);
91 template <
class Archive>
94 std::vector<double>::size_type size = 0;
99 for (std::vector<double>::size_type i = 0; i < size; ++i) {
102 v[i] = std::numeric_limits<double>::quiet_NaN();
103 }
else if (tmp ==
"inf") {
104 v[i] = std::numeric_limits<double>::infinity();
105 }
else if (tmp ==
"-inf") {
106 v[i] = -std::numeric_limits<double>::infinity();
108 v[i] = boost::lexical_cast<
double>(tmp);
115 namespace boost {
namespace serialization {
117 template <
class Archive>
118 void save(Archive &ar,
const boost::circular_buffer<std::vector<double> > &cb,
unsigned int version)
121 typedef typename boost::circular_buffer<std::vector<double> >::capacity_type capacity_type;
122 typedef typename boost::circular_buffer<std::vector<double> >::size_type size_type;
123 capacity_type capacity = cb.capacity();
125 size_type size = cb.size();
128 for (size_type i = 0; i < size; ++i) {
133 template <
class Archive>
134 void load(Archive &ar, boost::circular_buffer<std::vector<double> > &cb,
unsigned int version)
136 typedef typename boost::circular_buffer<std::vector<double> >::capacity_type capacity_type;
137 typedef typename boost::circular_buffer<std::vector<double> >::size_type size_type;
139 capacity_type capacity;
141 cb.set_capacity(capacity);
147 for (size_type i = 0; i < size; ++i) {
152 template <
class Archive,
class T>
153 void serialize(Archive &ar, boost::circular_buffer<T> &cb,
const unsigned int version)
155 split_free(ar, cb, version);
158 template <
class Archive,
class Key,
class Mapped,
class Hash,
class Pred,
class Alloc>
159 void save(Archive &ar,
const boost::unordered_map<Key,Mapped,Hash,Pred,Alloc> &um,
unsigned int)
161 typedef typename boost::unordered_map<Key,Mapped,Hash,Pred,Alloc>::size_type size_type;
162 typedef typename boost::unordered_map<Key,Mapped,Hash,Pred,Alloc>::const_iterator const_iterator;
164 const size_type size = um.size();
166 for (const_iterator it = um.begin(); it != um.end(); ++it) {
171 template <
class Archive,
class Key,
class Mapped,
class Hash,
class Pred,
class Alloc>
172 void load(Archive &ar, boost::unordered_map<Key,Mapped,Hash,Pred,Alloc> &um,
unsigned int)
174 typedef typename boost::unordered_map<Key,Mapped,Hash,Pred,Alloc>::size_type size_type;
180 for (size_type i = 0; i < size; ++i) {
181 std::pair<Key, Mapped> value;
183 um[value.first] = value.second;
188 template <
class Archive,
class Key,
class Mapped,
class Hash,
class Pred,
class Alloc>
189 void serialize(Archive &ar, boost::unordered_map<Key,Mapped,Hash,Pred,Alloc> &um,
unsigned int version)
191 split_free(ar, um, version);
194 template <
class Archive>
195 void save(Archive &ar,
const Eigen::MatrixXd &cb,
unsigned int)
198 std::size_t nrows = cb.rows(), ncols = cb.cols();
202 for (
unsigned int i = 0; i < (
unsigned int)cb.rows(); ++i) {
203 for (
unsigned int j = 0; j < (
unsigned int)cb.cols(); ++j) {
209 template <
class Archive>
210 void load(Archive &ar, Eigen::MatrixXd &cb,
unsigned int)
212 std::size_t rows, cols;
216 cb.resize(rows,cols);
218 for (
unsigned int i = 0; i < (
unsigned int)cb.rows(); ++i) {
219 for (std::size_t j = 0; j < (
unsigned int)cb.cols(); ++j) {
225 template <
class Archive>
226 void serialize(Archive &ar, Eigen::MatrixXd &cb,
const unsigned int version)
228 split_free(ar, cb, version);
231 template <
class Archive>
232 void save(Archive &ar,
const Eigen::VectorXd &cb,
unsigned int)
234 std::size_t nrows = cb.rows();
238 for (
unsigned int i = 0; i < (
unsigned int)cb.rows(); ++i) {
243 template <
class Archive>
244 void load(Archive &ar, Eigen::VectorXd &cb,
unsigned int)
251 for (
unsigned int i = 0; i < (
unsigned int)cb.rows(); ++i) {
256 template <
class Archive>
257 void serialize(Archive &ar, Eigen::VectorXd &cb,
const unsigned int version)
259 split_free(ar, cb, version);
262 template <
class Archive>
263 void serialize(Archive &ar, boost::vecS &cb,
const unsigned int version)
274 template <
class Archive>
275 void save(Archive &ar,
const boost::normal_distribution<double> &cb,
const unsigned int)
277 std::stringstream ss;
279 std::string tmp(ss.str());
283 template <
class Archive>
284 void load(Archive &ar, boost::normal_distribution<double> &cb,
const unsigned int)
288 std::stringstream ss(tmp);
292 template <
class Archive>
293 void serialize(Archive &ar, boost::normal_distribution<double> &cb,
const unsigned int version)
295 split_free(ar, cb, version);
301 template <
class Archive>
302 void save(Archive &ar,
const boost::random::uniform_real_distribution<double> &cb,
const unsigned int)
304 std::stringstream ss;
306 std::string tmp(ss.str());
310 template <
class Archive>
311 void load(Archive &ar, boost::random::uniform_real_distribution<double> &cb,
const unsigned int)
315 std::stringstream ss(tmp);
319 template <
class Archive>
320 void serialize(Archive &ar, boost::random::uniform_real_distribution<double> &cb,
const unsigned int version)
322 split_free(ar, cb, version);
void custom_vector_double_save(Archive &ar, const std::vector< double > &v, const unsigned int)
Custom save function for the serialization of vector of doubles that handle also inf and NaN...
void custom_vector_double_load(Archive &ar, std::vector< double > &v, const unsigned int)
Custom load function for the serialization of vector of doubles that handle also inf and NaN...