MUQ  0.4.3
UMBridgeModPiece.h
Go to the documentation of this file.
1 #ifndef UMBRIDGEMODPIECE
2 #define UMBRIDGEMODPIECE
4 #include "umbridge.h"
5 
6 namespace muq {
7  namespace Modeling {
8 
50  public:
51 
60  UMBridgeModPiece(const std::string host, std::string name, json config = json(), httplib::Headers headers = httplib::Headers());
61 
62  static const std::vector<double> EigenvectordToStdVector(const Eigen::VectorXd& vector) {
63  const std::vector<double> vec(vector.data(), vector.data() + vector.rows());
64  return vec;
65  }
66  static Eigen::VectorXd StdVectorToEigenvectord(std::vector<double>& vector) {
67  double* ptr_data = &vector[0];
68  Eigen::VectorXd vec = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(ptr_data, vector.size());
69  return vec;
70  }
71  static const Eigen::VectorXd StdVectorToEigenvectord(const std::vector<double>& vector) {
72  const double* ptr_data = &vector[0];
73  const Eigen::VectorXd vec = Eigen::Map<const Eigen::VectorXd, Eigen::Unaligned>(ptr_data, vector.size());
74  return vec;
75  }
76 
77  static std::vector<std::vector<double>> EigenvectordsToStdVectors(std::vector<Eigen::VectorXd> const& inputs) {
78  std::vector<std::vector<double>> vecs(inputs.size());
79  for (int i = 0; i < inputs.size(); i++)
80  vecs[i] = EigenvectordToStdVector(inputs[i]);
81  return vecs;
82  }
83  static std::vector<std::vector<double>> EigenvectordsToStdVectors(muq::Modeling::ref_vector<Eigen::VectorXd> const& inputs) {
84  std::vector<std::vector<double>> vecs(inputs.size());
85  for (int i = 0; i < inputs.size(); i++)
86  vecs[i] = EigenvectordToStdVector(inputs[i]);
87  return vecs;
88  }
89  static std::vector<Eigen::VectorXd> StdVectorsToEigenvectords(std::vector<std::vector<double>> vector) {
90  std::vector<Eigen::VectorXd> vec(vector.size());
91  for (int i = 0; i < vector.size(); i++) {
92  vec[i] = StdVectorToEigenvectord(vector[i]);
93  }
94  return vec;
95  }
96 
97  static std::vector<std::size_t> EigenvectoriToStdVector(const Eigen::VectorXi& vector) {
98  std::vector<std::size_t> vec(vector.data(), vector.data() + vector.rows());
99  return vec;
100  }
101  static Eigen::VectorXi StdVectorToEigenvectori(const std::vector<std::size_t>& vector) {
102  Eigen::VectorXi vec(vector.size());
103  for (int i = 0; i < vector.size(); i++)
104  vec[i] = vector[i];
105  return vec;
106  }
107 
108  private:
109 
110  Eigen::VectorXi read_input_size(const std::string host, std::string name, const httplib::Headers& headers);
111 
112  Eigen::VectorXi read_output_size(const std::string host, std::string name, const httplib::Headers& headers);
113 
114  void EvaluateImpl(muq::Modeling::ref_vector<Eigen::VectorXd> const& inputs) override;
115 
116  void GradientImpl(unsigned int outWrt,
117  unsigned int inWrt,
119  Eigen::VectorXd const& sens) override;
120 
121  void ApplyJacobianImpl(unsigned int outWrt,
122  unsigned int inWrt,
124  Eigen::VectorXd const& vec) override;
125 
126  void ApplyHessianImpl(unsigned int outWrt,
127  unsigned int inWrt1,
128  unsigned int inWrt2,
130  Eigen::VectorXd const& sens,
131  Eigen::VectorXd const& vec) override;
132 
135  };
136 
137  }
138 }
139 
140 #endif
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
A ModPiece connecting to a model via the UM-Bridge HTTP protocol.
void GradientImpl(unsigned int outWrt, unsigned int inWrt, muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &sens) override
Eigen::VectorXi read_input_size(const std::string host, std::string name, const httplib::Headers &headers)
static Eigen::VectorXd StdVectorToEigenvectord(std::vector< double > &vector)
UMBridgeModPiece(const std::string host, std::string name, json config=json(), httplib::Headers headers=httplib::Headers())
Construct UMBridgeModPiece, connecting to model server.
void ApplyJacobianImpl(unsigned int outWrt, unsigned int inWrt, muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &vec) override
void EvaluateImpl(muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs) override
static std::vector< Eigen::VectorXd > StdVectorsToEigenvectords(std::vector< std::vector< double >> vector)
void ApplyHessianImpl(unsigned int outWrt, unsigned int inWrt1, unsigned int inWrt2, muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec) override
Eigen::VectorXi read_output_size(const std::string host, std::string name, const httplib::Headers &headers)
static const Eigen::VectorXd StdVectorToEigenvectord(const std::vector< double > &vector)
static Eigen::VectorXi StdVectorToEigenvectori(const std::vector< std::size_t > &vector)
std::string name
A unique name for this WorkPiece. Defaults to <ClassName>_<id>
Definition: WorkPiece.h:583
a class to store JSON values
Definition: json.h:16658
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37