Commit fb42bef3 authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Add serialization to all objections transmitted via MPI

Node and Unit need to be serialized
parent 071cbecf
......@@ -5,6 +5,8 @@
#include <utils/enum.hpp>
#include <memory>
#include <boost/serialization/base_object.hpp>
#include <feature_creation/node/Node.hpp>
typedef std::function<double(double)> unary_op_func;
......@@ -19,6 +21,12 @@ public:
inline std::string expr(){return _expr;}
inline Unit unit(){return _unit;}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<Node>(*this);
}
};
#endif
......@@ -34,27 +34,3 @@ Node::Node(const Node &o) :
std::copy_n(o._value_ptr.get(), o._n_samp, value_ptr());
}
Node::Node(const Node &&o) :
_n_samp(o._n_samp),
_tag(o._tag),
_feat_ind(o._feat_ind),
_expr(o._expr),
_unit(o._unit),
_value_ptr(new double[o._n_samp])
{
std::copy_n(o._value_ptr.get(), o._n_samp, value_ptr());
}
// bool Node::equal(Node node_2)
// {
// if(!_value_ptr[0] || !node_2.value_ptr())
// return false;
// double norm_1 = util_funcs::norm(value_ptr(), _n_samp);
// double norm_2 = util_funcs::norm(value_ptr(), _n_samp);
// std::vector<double> difference(_n_samp, 0.0);
// std::transform(value_ptr(), value_ptr() + _n_samp, node_2.value_ptr(), difference.data(), [&norm_1, &norm_2](double n1, double n2){return n1 / norm_1 - n2/norm_2;});
// return !std::any_of(difference.begin(), difference.end(), [](double d){return std::abs(d) > 1e-12;});
// }
......@@ -9,6 +9,10 @@
#include <cmath>
#include <memory>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/string.hpp>
typedef std::function<double(double)> unary_op_func;
typedef std::function<double(double, double)> binary_op_func;
......@@ -32,7 +36,6 @@ public:
Node(int feat_ind, std::string expr, std::vector<double> value, Unit unit, NODE_TYPE tag = NODE_TYPE::FEAT);
Node(int feat_ind, std::string expr, int n_samp, NODE_TYPE tag = NODE_TYPE::FEAT);
Node(const Node &o);
Node(const Node &&o);
// bool equal(Node node_2);
......@@ -53,6 +56,17 @@ public:
inline double* value_ptr(){return _value_ptr.get();}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & _n_samp;
ar & _tag;
ar & _feat_ind;
ar & _expr;
ar & _unit;
ar & _value_ptr;
}
// inline void set_value_ptr(double* value_ptr){_value_ptr = value_ptr;}
};
......
#include <feature_creation/node/operator_nodes/OperatorNode.hpp>
OperatorNode::OperatorNode(op_func func_in, std::vector<feat_ptr> feats, int feat_ind, std::string expr, NODE_TYPE tag) :
OperatorNode::OperatorNode(op_func func_in, std::vector<node_ptr> feats, int feat_ind, std::string expr, NODE_TYPE tag) :
Node(feat_ind, expr, feats[0]->n_samp(), tag),
_func(func_in),
_feats(feats),
......@@ -32,3 +32,4 @@ std::string OperatorNode::expr()
return expression.substr(0, expression.size() - 2) + ")";
}
......@@ -4,17 +4,17 @@
#include <feature_creation/node/Node.hpp>
#include <feature_creation/node/operator_nodes/functions.hpp>
typedef std::shared_ptr<Node> feat_ptr;
#include <boost/serialization/base_object.hpp>
class OperatorNode: public Node
{
protected:
op_func _func;
std::vector<feat_ptr> _feats;
std::vector<node_ptr> _feats;
std::vector<double*> _feat_values;
public:
OperatorNode(op_func func, std::vector<feat_ptr> feats, int feat_ind, std::string expr, NODE_TYPE tag = NODE_TYPE::FXN);
OperatorNode(op_func func, std::vector<node_ptr> feats, int feat_ind, std::string expr, NODE_TYPE tag = NODE_TYPE::FXN);
OperatorNode(const OperatorNode &o);
inline op_func func(){return _func;};
......@@ -24,6 +24,15 @@ public:
virtual Unit unit() = 0;
inline void set_value() {_func(_n_samp, _feat_values, value_ptr());}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<Node>(*this);
ar & _func;
ar & _feats;
ar & _feat_values;
}
};
#endif
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/absolute_difference.hpp>
AbsDiffNode::AbsDiffNode(std::vector<feat_ptr> feats, int feat_ind) :
AbsDiffNode::AbsDiffNode(std::vector<node_ptr> feats, int feat_ind) :
OperatorNode(allowed_op_funcs::abs_diff, feats, feat_ind, "abs_diff", NODE_TYPE::ABS_DIFF)
{
if(feats[0]->unit() != feats[1]->unit())
throw InvalidFeatureException();
}
AbsDiffNode::AbsDiffNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind) :
AbsDiffNode::AbsDiffNode(node_ptr feat_1, node_ptr feat_2, int feat_ind) :
OperatorNode(allowed_op_funcs::abs_diff, {feat_1, feat_2}, feat_ind, "abs_diff", NODE_TYPE::ABS_DIFF)
{
if(feat_1->unit() != feat_2->unit())
......
......@@ -7,13 +7,19 @@ class AbsDiffNode: public OperatorNode
{
public:
AbsDiffNode(std::vector<feat_ptr> feats, int feat_ind);
AbsDiffNode(std::vector<node_ptr> feats, int feat_ind);
AbsDiffNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind);
AbsDiffNode(node_ptr feat_1, node_ptr feat_2, int feat_ind);
inline Unit unit(){return _feats[0]->unit();}
inline std::string expr(){return "|" + _feats[0]->expr() + " - (" + _feats[1]->expr() + ")|";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/absolute_value.hpp>
AbsNode::AbsNode(std::vector<feat_ptr> feats, int feat_ind):
AbsNode::AbsNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::abs, feats, feat_ind, "abs", NODE_TYPE::ABS)
{}
AbsNode::AbsNode(feat_ptr feat, int feat_ind):
AbsNode::AbsNode(node_ptr feat, int feat_ind):
OperatorNode(allowed_op_funcs::abs, {feat}, feat_ind, "abs", NODE_TYPE::ABS)
{}
......@@ -7,14 +7,18 @@ class AbsNode: public OperatorNode
{
public:
AbsNode(std::vector<feat_ptr> feats, int feat_ind);
AbsNode(std::vector<node_ptr> feats, int feat_ind);
AbsNode(feat_ptr feat, int feat_ind);
AbsNode(node_ptr feat, int feat_ind);
inline Unit unit(){return _feats[0]->unit();}
inline std::string expr(){return "|" + _feats[0]->expr() + "|";}
};
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/add.hpp>
AddNode::AddNode(std::vector<feat_ptr> feats, int feat_ind):
AddNode::AddNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::add, feats, feat_ind, "add", NODE_TYPE::ADD)
{
if(feats[0]->unit() != feats[1]->unit())
throw InvalidFeatureException();
}
AddNode::AddNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind):
AddNode::AddNode(node_ptr feat_1, node_ptr feat_2, int feat_ind):
OperatorNode(allowed_op_funcs::add, {feat_1, feat_2}, feat_ind, "add", NODE_TYPE::ADD)
{
if(feat_1->unit() != feat_2->unit())
......
......@@ -7,13 +7,19 @@ class AddNode: public OperatorNode
{
public:
AddNode(std::vector<feat_ptr> feats, int feat_ind);
AddNode(std::vector<node_ptr> feats, int feat_ind);
AddNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind);
AddNode(node_ptr feat_1, node_ptr feat_2, int feat_ind);
inline Unit unit(){return _feats[0]->unit();}
inline std::string expr(){return "(" + _feats[0]->expr() + " + " + _feats[1]->expr() + ")";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/cos.hpp>
CosNode::CosNode(std::vector<feat_ptr> feats, int feat_ind):
CosNode::CosNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::cos, feats, feat_ind, "cos", NODE_TYPE::COS)
{
if(feats[0]->unit() != Unit())
throw InvalidFeatureException();
}
CosNode::CosNode(feat_ptr feat, int feat_ind):
CosNode::CosNode(node_ptr feat, int feat_ind):
OperatorNode(allowed_op_funcs::cos, {feat}, feat_ind, "cos", NODE_TYPE::COS)
{
if(feat->unit() != Unit())
......
......@@ -2,19 +2,23 @@
#define COS_NODE
#include <feature_creation/node/operator_nodes/OperatorNode.hpp>
#include <feature_creation/units/Unit.hpp>
class CosNode: public OperatorNode
{
public:
CosNode(std::vector<feat_ptr> feats, int feat_ind);
CosNode(std::vector<node_ptr> feats, int feat_ind);
CosNode(feat_ptr feat, int feat_ind);
CosNode(node_ptr feat, int feat_ind);
inline Unit unit(){return Unit();}
inline std::string expr(){return "cos(" + _feats[0]->expr() + ")";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/cube.hpp>
CbNode::CbNode(std::vector<feat_ptr> feats, int feat_ind):
CbNode::CbNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::cb, feats, feat_ind, "cb", NODE_TYPE::CB)
{}
CbNode::CbNode(feat_ptr feat, int feat_ind):
CbNode::CbNode(node_ptr feat, int feat_ind):
OperatorNode(allowed_op_funcs::cb, {feat}, feat_ind, "cb", NODE_TYPE::CB)
{}
......@@ -7,13 +7,18 @@ class CbNode: public OperatorNode
{
public:
CbNode(std::vector<feat_ptr> feats, int feat_ind);
CbNode(std::vector<node_ptr> feats, int feat_ind);
CbNode(feat_ptr feat, int feat_ind);
CbNode(node_ptr feat, int feat_ind);
inline Unit unit(){return _feats[0]->unit()^(3.0);}
inline std::string expr(){return "(" + _feats[0]->expr() + ")^3";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/cube_root.hpp>
CbrtNode::CbrtNode(std::vector<feat_ptr> feats, int feat_ind):
CbrtNode::CbrtNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::cbrt, feats, feat_ind, "cbrt", NODE_TYPE::CBRT)
{}
CbrtNode::CbrtNode(feat_ptr feat, int feat_ind):
CbrtNode::CbrtNode(node_ptr feat, int feat_ind):
OperatorNode(allowed_op_funcs::cbrt, {feat}, feat_ind, "cbrt", NODE_TYPE::CBRT)
{}
......@@ -7,12 +7,18 @@ class CbrtNode: public OperatorNode
{
public:
CbrtNode(std::vector<feat_ptr> feats, int feat_ind);
CbrtNode(std::vector<node_ptr> feats, int feat_ind);
CbrtNode(feat_ptr feat, int feat_ind);
CbrtNode(node_ptr feat, int feat_ind);
inline Unit unit(){return _feats[0]->unit()^(1.0 / 3.0);}
inline std::string expr(){return "cbrt(" + _feats[0]->expr() + ")";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/divide.hpp>
DivNode::DivNode(std::vector<feat_ptr> feats, int feat_ind):
DivNode::DivNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::div, feats, feat_ind, "div", NODE_TYPE::DIV)
{}
DivNode::DivNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind):
DivNode::DivNode(node_ptr feat_1, node_ptr feat_2, int feat_ind):
OperatorNode(allowed_op_funcs::div, {feat_1, feat_2}, feat_ind, "div", NODE_TYPE::DIV)
{}
......@@ -7,13 +7,19 @@ class DivNode: public OperatorNode
{
public:
DivNode(std::vector<feat_ptr> feats, int feat_ind);
DivNode(std::vector<node_ptr> feats, int feat_ind);
DivNode(feat_ptr feat_1, feat_ptr feat_2, int feat_ind);
DivNode(node_ptr feat_1, node_ptr feat_2, int feat_ind);
inline Unit unit(){return _feats[0]->unit() / _feats[1]->unit();}
inline std::string expr(){return "[(" + _feats[0]->expr() + ") / (" + _feats[1]->expr() + ")]";}
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<OperatorNode>(*this);
}
};
#endif
\ No newline at end of file
#include <feature_creation/node/operator_nodes/allowed_operator_nodes/exponential.hpp>
ExpNode::ExpNode(std::vector<feat_ptr> feats, int feat_ind):
ExpNode::ExpNode(std::vector<node_ptr> feats, int feat_ind):
OperatorNode(allowed_op_funcs::exp, feats, feat_ind, "exp", NODE_TYPE::EXP)
{
if(feats[0]->unit() != Unit())
throw InvalidFeatureException();
}
ExpNode::ExpNode(feat_ptr feat, int feat_ind):
ExpNode::ExpNode(node_ptr feat, int feat_ind):
OperatorNode(allowed_op_funcs::exp, {feat}, feat_ind, "exp", NODE_TYPE::EXP)
{
if(feat->unit() != Unit())
throw InvalidFeatureException();
}
\ No newline at end of file
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment