MUQ  0.4.3
UMBridgeModPiece.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
5 
6 UMBridgeModPiece::UMBridgeModPiece(const std::string host, std::string name, json config, httplib::Headers headers)
7 : config(config), client(host, name, headers),
8  ModPiece(read_input_size(host, name, headers), read_output_size(host, name, headers))
9 {
10  this->outputs.resize(this->numOutputs);
11 }
12 
13 
14 Eigen::VectorXi UMBridgeModPiece::read_input_size(const std::string host, std::string name, const httplib::Headers& headers){
15  // Would prefer to reuse the existing client, circular dependency in constructor though...
16  umbridge::HTTPModel client(host, name, headers);
18 }
19 
20 Eigen::VectorXi UMBridgeModPiece::read_output_size(const std::string host, std::string name, const httplib::Headers& headers){
21  umbridge::HTTPModel client(host, name, headers);
23 }
24 
26  if (!client.SupportsEvaluate())
27  throw std::runtime_error("Model does not support evaluation!");
28  std::vector<std::vector<double>> inputs_stdvec(this->numInputs);
29  for (int i = 0; i < this->numInputs; i++) {
30  inputs_stdvec[i] = EigenvectordToStdVector(inputs[i]);
31  }
32  std::vector<std::vector<double>> out = client.Evaluate(inputs_stdvec, config);
33  for (int i = 0; i < this->numOutputs; i++) {
34  outputs[i] = StdVectorToEigenvectord(out[i]);
35  }
36 }
37 
38 void UMBridgeModPiece::GradientImpl(unsigned int outWrt,
39  unsigned int inWrt,
41  Eigen::VectorXd const& sens) {
42  if (client.SupportsGradient()) {
43  gradient = StdVectorToEigenvectord(client.Gradient(outWrt, inWrt, EigenvectordsToStdVectors(inputs), EigenvectordToStdVector(sens), config));
44  } else
45  gradient = GradientByFD(outWrt, inWrt, inputs, sens);
46 }
47 
48 void UMBridgeModPiece::ApplyJacobianImpl(unsigned int outWrt,
49  unsigned int inWrt,
51  Eigen::VectorXd const& vec){
53  jacobianAction = StdVectorToEigenvectord(client.ApplyJacobian(outWrt, inWrt, EigenvectordsToStdVectors(inputs), EigenvectordToStdVector(vec), config));
54  } else
55  jacobianAction = ApplyJacobianByFD(outWrt, inWrt, inputs, vec);
56 }
57 
58 void UMBridgeModPiece::ApplyHessianImpl(unsigned int outWrt,
59  unsigned int inWrt1,
60  unsigned int inWrt2,
62  Eigen::VectorXd const& sens,
63  Eigen::VectorXd const& vec){
65  hessAction = StdVectorToEigenvectord(client.ApplyHessian(outWrt, inWrt1, inWrt2, EigenvectordsToStdVectors(inputs), EigenvectordToStdVector(sens), EigenvectordToStdVector(vec), config));
66  } else
67  hessAction = ApplyHessianByFD(outWrt, inWrt1, inWrt2, inputs, sens, vec);
68 }
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
virtual Eigen::VectorXd GradientByFD(unsigned int const outputDimWrt, unsigned int const inputDimWrt, std::vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity)
Definition: ModPiece.cpp:111
virtual Eigen::VectorXd ApplyHessianByFD(unsigned int const outWrt, unsigned int const inWrt1, unsigned int const inWrt2, std::vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec)
Definition: ModPiece.cpp:448
Eigen::MatrixXd ApplyJacobianByFD(unsigned int outWrt, unsigned int inWrt, Args const &... args)
Definition: ModPiece.h:303
std::vector< Eigen::VectorXd > outputs
Definition: ModPiece.h:503
Eigen::VectorXd gradient
Definition: ModPiece.h:504
Eigen::VectorXd hessAction
Definition: ModPiece.h:507
Eigen::VectorXd jacobianAction
Definition: ModPiece.h:505
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
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 Eigen::VectorXi StdVectorToEigenvectori(const std::vector< std::size_t > &vector)
int numOutputs
The number of outputs.
Definition: WorkPiece.h:504
std::string name
A unique name for this WorkPiece. Defaults to <ClassName>_<id>
Definition: WorkPiece.h:583
int numInputs
The number of inputs.
Definition: WorkPiece.h:501
a class to store JSON values
Definition: json.h:16658
bool SupportsApplyHessian() override
Definition: umbridge.h:269
bool SupportsEvaluate() override
Definition: umbridge.h:260
bool SupportsGradient() override
Definition: umbridge.h:263
bool SupportsApplyJacobian() override
Definition: umbridge.h:266
std::vector< std::vector< double > > Evaluate(const std::vector< std::vector< double >> &inputs, json config_json=json()) override
Definition: umbridge.h:149
std::vector< std::size_t > GetOutputSizes(const json &config_json=json()) const override
Definition: umbridge.h:132
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=json()) override
Definition: umbridge.h:174
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=json()) override
Definition: umbridge.h:229
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=json()) override
Definition: umbridge.h:202
std::vector< std::size_t > GetInputSizes(const json &config_json=json()) const override
Definition: umbridge.h:115
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37