Skip to content
Snippets Groups Projects
Commit ebdcbaf1 authored by Martin Beseda's avatar Martin Beseda
Browse files

[FIX] Rewritten serialization of ExprtkWrapper, so that Boost.serialization...

[FIX] Rewritten serialization of ExprtkWrapper, so that Boost.serialization doesn't have to be friended anymore
parent 607acf2f
No related branches found
No related tags found
No related merge requests found
......@@ -13,54 +13,54 @@
#include "exceptions.h"
#include <string>
BOOST_CLASS_EXPORT_IMPLEMENT(ExprtkWrapper);
//BOOST_CLASS_EXPORT_IMPLEMENT(ExprtkWrapper);
ExprtkWrapper::ExprtkWrapper(std::string expression_string) {
this->p_impl = new ExprtkWrapperImpl();
// this = new ExprtkWrapperImpl();
this->p_impl->expression_str = expression_string;
this->expression_str = expression_string;
this->p_impl->symbol_table = new symbol_table_t();
this->symbol_table = new symbol_table_t();
this->p_impl->symbol_table->add_variable("x",
this->p_impl->x);
this->p_impl->symbol_table->add_variable("y",
this->p_impl->y);
this->p_impl->symbol_table->add_variable("z",
this->p_impl->z);
this->p_impl->symbol_table->add_variable("t",
this->p_impl->t);
this->p_impl->symbol_table->add_variable("f",
this->p_impl->z);
this->symbol_table->add_variable("x",
this->x);
this->symbol_table->add_variable("y",
this->y);
this->symbol_table->add_variable("z",
this->z);
this->symbol_table->add_variable("t",
this->t);
this->symbol_table->add_variable("f",
this->z);
this->p_impl->expression = new expression_t();
this->p_impl->expression->register_symbol_table(*this->p_impl->symbol_table);
this->expression = new expression_t();
this->expression->register_symbol_table(*this->symbol_table);
this->p_impl->parser = new parser_t();
this->p_impl->parser->compile(this->p_impl->expression_str,
*this->p_impl->expression);
this->parser = new parser_t();
this->parser->compile(this->expression_str,
*this->expression);
}
ExprtkWrapper::~ExprtkWrapper() {
if (this->p_impl->expression) {
delete this->p_impl->expression;
this->p_impl->expression = nullptr;
if (this->expression) {
delete this->expression;
this->expression = nullptr;
}
if (this->p_impl->symbol_table) {
delete this->p_impl->symbol_table;
this->p_impl->symbol_table = nullptr;
if (this->symbol_table) {
delete this->symbol_table;
this->symbol_table = nullptr;
}
if (this->p_impl->parser) {
delete this->p_impl->parser;
this->p_impl->parser = nullptr;
if (this->parser) {
delete this->parser;
this->parser = nullptr;
}
delete this->p_impl;
this->p_impl = nullptr;
delete this;
// this = nullptr;
}
......@@ -69,12 +69,12 @@ double ExprtkWrapper::eval(double x1,
double x3,
double x4) {
this->p_impl->x = x1;
this->p_impl->y = x2;
this->p_impl->z = x3;
this->p_impl->t = x4;
this->x = x1;
this->y = x2;
this->z = x3;
this->t = x4;
return this->p_impl->expression->value();
return this->expression->value();
}
......@@ -82,19 +82,39 @@ double ExprtkWrapper::eval(std::vector<double>& p) {
if (p.size() > 0) {
this->p_impl->x = p[0];
this->x = p[0];
}
if (p.size() > 1) {
this->p_impl->y = p[1];
this->y = p[1];
}
if (p.size() > 2) {
this->p_impl->z = p[2];
this->z = p[2];
}
if (p.size() > 3) {
this->p_impl->t = p[3];
this->t = p[3];
}
double result = this->p_impl->expression->value();
double result = this->expression->value();
return result;
}
\ No newline at end of file
}
ExprtkWrapper::ExprtkWrapper(expression_t* expression,
symbol_table_t* symbol_table,
parser_t* parser,
double x,
double y,
double z,
double t,
double f,
std::string expression_str) {
this->expression = expression;
this->symbol_table = symbol_table;
this->parser = parser;
this->x = x;
this->y = y;
this->z = z;
this->t = t;
this->f = f;
this->expression_str = expression_str;
}
......@@ -12,12 +12,50 @@
#include <vector>
#include <string>
#include <serialization/access.hpp>
#include "exprtk.hpp"
typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
//#include <serialization/access.hpp>
#include "../settings.h"
class ExprtkWrapper {
friend boost::serialization::access;
// friend boost::serialization::access;
private:
//TODO remove PIMPL - Exprtk is NOT part of API!
// class ExprtkWrapperImpl;
//
// ExprtkWrapperImpl* p_impl;
/**
*
*/
expression_t* expression = nullptr;
/**
*
*/
symbol_table_t* symbol_table = nullptr;
/**
*
*/
parser_t* parser = nullptr;
/**
* variables
*/
double x, y, z, t, f;
/**
* referential expression string
*/
std::string expression_str;
public:
......@@ -34,6 +72,16 @@ public:
*/
LIB4NEURO_API explicit ExprtkWrapper(std::string expression_string);
LIB4NEURO_API ExprtkWrapper(expression_t* expression,
symbol_table_t* symbol_table,
parser_t* parser,
double x,
double y,
double z,
double t,
double f,
std::string expression_str);
/**
*
*/
......@@ -58,23 +106,6 @@ public:
* @return
*/
LIB4NEURO_API double eval(std::vector<double>& p);
private:
/**
* Private properties
*
* They are hidden in .cpp file
* to isolate Exprtk dependency from header
*/
class ExprtkWrapperImpl;
ExprtkWrapperImpl* p_impl;
/**
*
*/
ExprtkWrapper() = default;
};
#endif //LIB4NEURO_EXPRTKWRAPPER_H
......@@ -5,87 +5,128 @@
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/serialization.hpp>
//TODO check different order of headers - possible bug
#include "ExprtkWrapper.h"
#include "exprtk.hpp"
BOOST_CLASS_EXPORT_KEY(ExprtkWrapper);
typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
//BOOST_CLASS_EXPORT_KEY(ExprtkWrapper);
/**
* Class implementing the private properties
* of ExprtkWrapper class.
*/
class ExprtkWrapper::ExprtkWrapperImpl {
public:
/**
* Struct used to access private properties from
* the serialization function
*/
struct access;
/**
*
*/
expression_t* expression = nullptr;
/**
*
*/
symbol_table_t* symbol_table = nullptr;
/**
*
*/
parser_t* parser = nullptr;
/**
* variables
*/
double x, y, z, t, f;
/**
* referential expression string
*/
std::string expression_str;
};
//class ExprtkWrapper::ExprtkWrapperImpl {
//
//public:
//
// /**
// * Struct used to access private properties from
// * the serialization function
// */
// struct access;
//
// /**
// *
// */
// expression_t* expression = nullptr;
//
// /**
// *
// */
// symbol_table_t* symbol_table = nullptr;
//
// /**
// *
// */
// parser_t* parser = nullptr;
//
// /**
// * variables
// */
// double x, y, z, t, f;
//
// /**
// * referential expression string
// */
// std::string expression_str;
//};
struct ExprtkWrapper::access {
template<class Archive>
static void serialize(Archive& ar,
ExprtkWrapper& n,
const unsigned int version) {
ar & n.p_impl->expression_str;
ar & n.p_impl->x & n.p_impl->y & n.p_impl->z & n.p_impl->t & n.p_impl->f;
ExprtkWrapper& w,
const unsigned int version) {}
template<class Archive>
static void save_construct_data(Archive & ar,
const ExprtkWrapper* w,
const unsigned long int file_version){
ar << w->expression_str;
ar << w->x;
ar << w->y;
ar << w->z;
ar << w->t;
ar << w->f;
}
template<class Archive>
static void load_construct_data(Archive & ar,
ExprtkWrapper* w,
const unsigned long int file_version) {
expression_t* expression;
symbol_table_t* symbol_table;
parser_t* parser;
double x, y, z, t, f;
std::string expression_str;
ar >> expression_str;
ar >> x;
ar >> y;
ar >> z;
ar >> t;
ar >> f;
::new(w)ExprtkWrapper(expression,
symbol_table,
parser,
x,
y,
z,
t,
f,
expression_str);
}
};
namespace boost {
namespace serialization {
namespace boost::serialization {
template<class Archive>
inline void load_construct_data(Archive & ar,
ExprtkWrapper* w,
const unsigned long int file_version) {
ExprtkWrapper::access::load_construct_data(ar, w, file_version);
}
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param n ExprtkWrapper instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
ExprtkWrapper& n,
const unsigned int version) {
ExprtkWrapper::access::serialize(ar,
n,
version);
}
template<class Archive>
inline void save_construct_data(Archive & ar,
const ExprtkWrapper* w,
const unsigned long int file_version) {
ExprtkWrapper::access::save_construct_data(ar, w, file_version);
}
} // namespace serialization
} // namespace boost
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param n ExprtkWrapper instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
ExprtkWrapper& n,
const unsigned int version) {}
} // namespace boost::serialization
#endif //LIB4NEURO_EXPRTKWRAPPERSERIALIZATION_H
......@@ -19,6 +19,7 @@
#include <fstream>
#include <memory>
#include "../DataSet/DataSet.h"
#include "../settings.h"
#include "../Neuron/Neuron.h"
#include "../Neuron/NeuronConstant.h"
......@@ -282,6 +283,9 @@ namespace lib4neuro {
std::vector<double>& output,
std::vector<double>* custom_weights_and_biases = nullptr);
//TODO implement
// LIB4NEURO_API virtual std::shared_ptr<DataSet> eval_on_dataset(const DataSet& ds);
/**
*
* @param error_derivative
......
......@@ -11,7 +11,7 @@
#include "NeuralNetworkSumSerialization.h"
#include "General/ExprtkWrapperSerialization.h"
BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NeuralNetworkSum);
//BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NeuralNetworkSum);
namespace lib4neuro {
......
......@@ -12,7 +12,7 @@
#include "NeuralNetworkSum.h"
#include "NeuralNetworkSerialization.h"
BOOST_CLASS_EXPORT_KEY(lib4neuro::NeuralNetworkSum);
//BOOST_CLASS_EXPORT_KEY(lib4neuro::NeuralNetworkSum);
namespace lib4neuro {
class NeuralNetworkSum::NeuralNetworkSumImpl {
......
......@@ -8,7 +8,7 @@
#include "NormalizationStrategyACSFSerialization.h"
#include "exceptions.h"
BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NormalizationStrategyACSF)
//BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::NormalizationStrategyACSF)
lib4neuro::NormalizationStrategyACSF::NormalizationStrategyACSF(
......@@ -59,7 +59,7 @@ lib4neuro::NormalizationStrategyACSF::NormalizationStrategyACSF(
}
}
void lib4neuro::NormalizationStrategyACSF::normalize_input(std::vector<double> &inp){
void lib4neuro::NormalizationStrategyACSF::normalize_input(std::vector<double>& inp){
unsigned int first_input_idx = 0;
double len, dist2min;
......@@ -76,7 +76,7 @@ void lib4neuro::NormalizationStrategyACSF::normalize_input(std::vector<double> &
}
}
void lib4neuro::NormalizationStrategyACSF::de_normalize_input(std::vector<double> &inp){
void lib4neuro::NormalizationStrategyACSF::de_normalize_input(std::vector<double>& inp){
unsigned int first_input_idx = 0;
double len, dist2min;
......@@ -106,4 +106,18 @@ void lib4neuro::NormalizationStrategyACSF::de_normalize_output(std::vector<doubl
len = this->outputs_max - this->outputs_min;
out[ 0 ] = (out[ 0 ] + 1.0) * len * 0.5 + this->outputs_min;
}
\ No newline at end of file
}
lib4neuro::NormalizationStrategyACSF::NormalizationStrategyACSF(const std::vector<ELEMENT_SYMBOL>& element_order,
std::unordered_map<ELEMENT_SYMBOL, std::vector<double>> inputs_min,
std::unordered_map<ELEMENT_SYMBOL, std::vector<double>> inputs_max,
std::unordered_map<ELEMENT_SYMBOL, unsigned int> number_of_inputs_per_element,
double outputs_min,
double outputs_max) {
this->order_of_elements = element_order;
this->inputs_min = inputs_min;
this->inputs_max = inputs_max;
this->number_of_inputs_per_element = number_of_inputs_per_element;
this->outputs_min = outputs_min;
this->outputs_max = outputs_max;
}
......@@ -5,7 +5,6 @@
#include <limits>
#include <vector>
#include <unordered_map>
#include <serialization/access.hpp>
#include "../SymmetryFunction/SymmetryFunction.h"
#include "../DataSet/DataSet.h"
......@@ -13,15 +12,15 @@ namespace lib4neuro {
/**
*
*/
class NormalizationStrategyACSF: public NormalizationStrategy {
friend boost::serialization::access;
class NormalizationStrategyACSF : public NormalizationStrategy {
protected:
/**
* information about the real range of input values for each element
*/
std::unordered_map<ELEMENT_SYMBOL, std::vector<double> > inputs_min, inputs_max;
std::unordered_map<ELEMENT_SYMBOL, std::vector<double> > inputs_min;
std::unordered_map<ELEMENT_SYMBOL, std::vector<double> > inputs_max;
/**
* information about the range of the output
......@@ -33,12 +32,10 @@ namespace lib4neuro {
* information about the various elements
*/
std::unordered_map<ELEMENT_SYMBOL, unsigned int > number_of_inputs_per_element;
std::unordered_map<ELEMENT_SYMBOL, unsigned int> number_of_inputs_per_element;
std::vector<ELEMENT_SYMBOL> order_of_elements;
NormalizationStrategyACSF() = default;
public:
/**
......@@ -48,20 +45,29 @@ namespace lib4neuro {
NormalizationStrategyACSF(
const std::unordered_map<ELEMENT_SYMBOL, Element*>& element_description,
const std::vector<ELEMENT_SYMBOL> &element_order,
const std::vector<std::pair<std::vector<double>, std::vector<double>>> &data
const std::vector<ELEMENT_SYMBOL>& element_order,
const std::vector<std::pair<std::vector<double>, std::vector<double>>>& data
);
NormalizationStrategyACSF(
const std::vector<ELEMENT_SYMBOL>& element_order,
std::unordered_map<ELEMENT_SYMBOL, std::vector<double> > inputs_min,
std::unordered_map<ELEMENT_SYMBOL, std::vector<double> > inputs_max,
std::unordered_map<ELEMENT_SYMBOL, unsigned int> number_of_inputs_per_element,
double outputs_min,
double outputs_max
);
virtual ~NormalizationStrategyACSF( ) = default;
~NormalizationStrategyACSF() override = default;
void normalize_input(std::vector<double> &inp);
void normalize_input(std::vector<double>& inp);
void de_normalize_input(std::vector<double> &inp);
void de_normalize_input(std::vector<double>& inp);
void normalize_output(std::vector<double> &out);
void normalize_output(std::vector<double>& out);
void de_normalize_output(std::vector<double> &out);
void de_normalize_output(std::vector<double>& out);
/**
*
......@@ -70,11 +76,9 @@ namespace lib4neuro {
* @param min
* @return
*/
virtual double normalize(double n,
double max,
double min){
return 0.0;
};
double normalize(double n,
double max,
double min) override;
/**
*
......@@ -83,9 +87,7 @@ namespace lib4neuro {
* @param min
* @return
*/
virtual double de_normalize(double n){
return 0.0;
}
double de_normalize(double n) override;
};
}//end of namespace lib4neuro
......
......@@ -5,6 +5,7 @@
#ifndef LIB4NEURO_NORMALIZATIONSTRATEGYACSFSERIALIZATION_H
#define LIB4NEURO_NORMALIZATIONSTRATEGYACSFSERIALIZATION_H
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
......@@ -15,40 +16,76 @@
#include "NormalizationStrategyACSF.h"
#include "NormalizationStrategySerialization.h"
BOOST_CLASS_EXPORT_KEY(lib4neuro::NormalizationStrategyACSF)
//BOOST_CLASS_EXPORT_KEY(lib4neuro::NormalizationStrategyACSF)
struct lib4neuro::NormalizationStrategyACSF::access {
template<class Archive>
static void serialize(Archive& ar,
NormalizationStrategyACSF& ns,
const unsigned int version) {
static void save_construct_data(
Archive & ar, const lib4neuro::NormalizationStrategyACSF* ns, const unsigned long int file_version) {
//TODO check if it's possible to call base_object in this function
ar & boost::serialization::base_object<NormalizationStrategy>(ns);
ar & ns.inputs_min;
ar & ns.inputs_max;
ar & ns.outputs_min;
ar & ns.outputs_max;
ar & ns.number_of_inputs_per_element;
ar & ns.order_of_elements;
// ar << ns->max_min_inp_val;
ar << ns->inputs_min;
ar << ns->inputs_max;
ar << ns->outputs_min;
ar << ns->outputs_max;
ar << ns->number_of_inputs_per_element;
ar << ns->order_of_elements;
}
template<class Archive>
static void load_construct_data(Archive & ar,
lib4neuro::NormalizationStrategyACSF* ns,
const unsigned long int file_version) {
//TODO check if it's possible to call base_object in this function
ar & boost::serialization::base_object<NormalizationStrategy>(ns);
// std::vector<double> max_min_inp_val;
std::unordered_map<ELEMENT_SYMBOL, std::vector<double>> inputs_min;
std::unordered_map<ELEMENT_SYMBOL, std::vector<double>> inputs_max;
double outputs_min;
double outputs_max;
std::unordered_map<ELEMENT_SYMBOL, unsigned int> number_of_inputs_per_element;
std::vector<ELEMENT_SYMBOL> element_order;
// ar >> max_min_inp_val;
ar >> inputs_min;
ar >> inputs_max;
ar >> outputs_min;
ar >> outputs_max;
ar >> number_of_inputs_per_element;
ar >> element_order;
::new(ns)lib4neuro::NormalizationStrategyACSF(element_order,
inputs_min,
inputs_max,
number_of_inputs_per_element,
outputs_min,
outputs_max);
}
};
namespace boost::serialization {
template<class Archive>
inline void load_construct_data(Archive & ar,
lib4neuro::NormalizationStrategyACSF* ns,
const unsigned long int file_version) {
lib4neuro::NormalizationStrategyACSF::access::load_construct_data(ar, ns, file_version);
}
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param ns NormalizationStrategyACSF instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
lib4neuro::NormalizationStrategyACSF& ns,
const unsigned int version) {
lib4neuro::NormalizationStrategyACSF::access::serialize(ar,
ns,
version);
}
template<class Archive>
inline void save_construct_data(Archive & ar,
const lib4neuro::NormalizationStrategyACSF* ns,
const unsigned long int file_version) {
lib4neuro::NormalizationStrategyACSF::access::save_construct_data(ar, ns, file_version);
}
template<class Archive>
void serialize(Archive& ar,
lib4neuro::NormalizationStrategyACSF& ns,
const unsigned int version) {}
} // namespace boost::serialization
#endif //LIB4NEURO_NORMALIZATIONSTRATEGYACSFSERIALIZATION_H
......@@ -21,6 +21,20 @@ struct lib4neuro::NormalizationStrategy::access {
const unsigned int version) {
ar & ns.max_min_inp_val;
}
// template<class Archive>
// static void save_construct_data(
// Archive & ar, const lib4neuro::NormalizationStrategy* s, const unsigned long int file_version) {
// ar & s->max_min_inp_val;
// }
//
// template<class Archive>
// inline void load_construct_data(Archive & ar,
// lib4neuro::NormalizationStrategy* s,
// const unsigned long int file_version) {
// ar & s->max_min_inp_val;
// ::new(s)lib4neuro::NormalizationStrategy(m);
// }
};
struct lib4neuro::DoubleUnitStrategy::access {
......@@ -32,41 +46,55 @@ struct lib4neuro::DoubleUnitStrategy::access {
}
};
namespace boost {
namespace serialization {
namespace boost::serialization {
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param ns lib4neuro::NormalizationStrategy instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
lib4neuro::NormalizationStrategy& ns,
const unsigned int version) {
lib4neuro::NormalizationStrategy::access::serialize(ar,
ns,
version);
}
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param ns lib4neuro::NormalizationStrategy instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
lib4neuro::NormalizationStrategy& ns,
const unsigned int version) {
lib4neuro::NormalizationStrategy::access::serialize(ar,
ns,
version);
}
// template<class Archive>
// inline void save_construct_data(
// Archive & ar, const lib4neuro::NormalizationStrategy* s, const unsigned long int file_version
// ){
// lib4neuro::NormalizationStrategy::access::save_construct_data(ar, s , file_version);
// }
//
// template<class Archive>
// inline void load_construct_data(
// Archive & ar, lib4neuro::NormalizationStrategy* t, const unsigned long int file_version
// ){
// ar & ns.max_min_inp_val;
//
// ::new(t)my_class(m);
// }
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param s lib4neuro::DoubleUnitStrategy instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
lib4neuro::DoubleUnitStrategy& s,
const unsigned int version) {
lib4neuro::DoubleUnitStrategy::access::serialize(ar,
s,
version);
}
} // namespace serialization
} // namespace boost
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param s lib4neuro::DoubleUnitStrategy instance
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive& ar,
lib4neuro::DoubleUnitStrategy& s,
const unsigned int version) {
lib4neuro::DoubleUnitStrategy::access::serialize(ar,
s,
version);
}
} // namespace boost::serialization
#endif //LIB4NEURO_NORMALIZATIONSTRATEGYSERIALIZATION_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment