MUQ  0.4.3
UMBridgeModPieceServer.h
Go to the documentation of this file.
2 
3 namespace muq {
4  namespace Modeling {
5 
12  public:
13 
19  UMBridgeModPieceWrapper(std::shared_ptr<muq::Modeling::ModPiece> modPiece, std::string name)
20  : umbridge::Model(name),
22  {}
23 
24  std::vector<std::size_t> GetInputSizes(const json& config_json = json()) const override {
25  return UMBridgeModPiece::EigenvectoriToStdVector(modPiece->inputSizes);
26  }
27 
28  std::vector<std::size_t> GetOutputSizes(const json& config_json = json()) const override {
29  return UMBridgeModPiece::EigenvectoriToStdVector(modPiece->outputSizes);
30  }
31 
32  std::vector<std::vector<double>> Evaluate(const std::vector<std::vector<double>>& inputs, json config) override {
33  return UMBridgeModPiece::EigenvectordsToStdVectors(
35  }
36 
37  std::vector<double> Gradient(unsigned int outWrt,
38  unsigned int inWrt,
39  const std::vector<std::vector<double>>& inputs,
40  const std::vector<double>& sens,
41  json config = json()) override {
42  return UMBridgeModPiece::EigenvectordToStdVector(
43  modPiece->Gradient(outWrt,
44  inWrt,
47  }
48 
49  std::vector<double> ApplyJacobian(unsigned int outWrt,
50  unsigned int inWrt,
51  const std::vector<std::vector<double>>& inputs,
52  const std::vector<double>& vec,
53  json config = json()) override {
54  return UMBridgeModPiece::EigenvectordToStdVector(
55  modPiece->ApplyJacobian(outWrt,
56  inWrt,
59  }
60 
61  std::vector<double> ApplyHessian(unsigned int outWrt,
62  unsigned int inWrt1,
63  unsigned int inWrt2,
64  const std::vector<std::vector<double>>& inputs,
65  const std::vector<double>& sens,
66  const std::vector<double>& vec,
67  json config = json()) override {
68  return UMBridgeModPiece::EigenvectordToStdVector(
69  modPiece->ApplyHessian(outWrt,
70  inWrt1,
71  inWrt2,
75  }
76 
77  bool SupportsEvaluate() override {return true;}
78  bool SupportsGradient() override {return true;} // Expose derivative information as well,
79  bool SupportsApplyJacobian() override {return true;} // since we fall back to finite differences automatically
80  bool SupportsApplyHessian() override {return true;}
81 
82  private:
83  std::shared_ptr<muq::Modeling::ModPiece> modPiece;
84  };
85 
93  void serveModPiece(std::shared_ptr<ModPiece> modPiece, std::string name, std::string host, int port) {
94  UMBridgeModPieceWrapper wrapper(modPiece, name);
95  umbridge::serveModels({&wrapper}, host, port);
96  }
97 
98  }
99 }
Wrap a ModPiece in an UM-Bridge Model.
std::vector< double > ApplyHessian(unsigned int outWrt, unsigned int inWrt1, unsigned int inWrt2, const std::vector< std::vector< double >> &inputs, const std::vector< double > &sens, const std::vector< double > &vec, json config=json()) override
std::vector< double > Gradient(unsigned int outWrt, unsigned int inWrt, const std::vector< std::vector< double >> &inputs, const std::vector< double > &sens, json config=json()) override
UMBridgeModPieceWrapper(std::shared_ptr< muq::Modeling::ModPiece > modPiece, std::string name)
Constructor.
std::vector< std::size_t > GetInputSizes(const json &config_json=json()) const override
std::vector< std::size_t > GetOutputSizes(const json &config_json=json()) const override
std::vector< std::vector< double > > Evaluate(const std::vector< std::vector< double >> &inputs, json config) override
std::shared_ptr< muq::Modeling::ModPiece > modPiece
std::vector< double > ApplyJacobian(unsigned int outWrt, unsigned int inWrt, const std::vector< std::vector< double >> &inputs, const std::vector< double > &vec, json config=json()) override
static Eigen::VectorXd StdVectorToEigenvectord(std::vector< double > &vector)
static std::vector< Eigen::VectorXd > StdVectorsToEigenvectords(std::vector< std::vector< double >> vector)
a class to store JSON values
Definition: json.h:16658
std::string name
Definition: umbridge.h:69
Model(std::string name)
Definition: umbridge.h:21
void serveModPiece(std::shared_ptr< ModPiece > modPiece, std::string name, std::string host, int port)
Serve a ModPiece via network using UM-Bridge.
void serveModels(std::vector< Model * > models, std::string host, int port)
Definition: umbridge.h:425