10 #include <boost/property_tree/ptree.hpp>
39 auto mod = std::make_shared<UMBridgeModPiece>(
"http://localhost:4242",
"forward");
41 std::cout << mod->inputSizes << std::endl;
42 std::cout << mod->outputSizes << std::endl;
51 unsigned int numCells = 200;
54 Eigen::VectorXd nodeLocs = Eigen::VectorXd::LinSpaced(numCells+1,0,1);
57 Eigen::VectorXd cellLocs = 0.5*(nodeLocs.head(numCells) + nodeLocs.tail(numCells));
69 Eigen::VectorXd cond = (20.0*cellLocs.array()).cos().exp();
71 Eigen::VectorXd h = mod->Evaluate(cond).at(0);
73 std::cout <<
"Solution: \n" << h.transpose() << std::endl << std::endl;
88 auto objective = std::make_shared<Gaussian>(numCells+1)->AsDensity();
100 Eigen::VectorXd objSens = Eigen::VectorXd::Ones(1);
101 Eigen::VectorXd sens = objective->Gradient(0,0,h,objSens);
102 Eigen::VectorXd grad = mod->Gradient(0,0,cond,sens);
104 std::cout <<
"Gradient: \n" << grad.transpose() << std::endl << std::endl;
111 Eigen::VectorXd gradFD = mod->GradientByFD(0,0,std::vector<Eigen::VectorXd>{cond},sens);
112 std::cout <<
"Finite Difference Gradient:\n" << gradFD.transpose() << std::endl << std::endl;
118 Eigen::VectorXd jacDir = Eigen::VectorXd::Ones(numCells);
120 Eigen::VectorXd jacAct = mod->ApplyJacobian(0,0, cond, jacDir);
121 std::cout <<
"Jacobian Action: \n" << jacAct.transpose() << std::endl << std::endl;
123 Eigen::VectorXd jacActFD = mod->ApplyJacobianByFD(0,0, std::vector<Eigen::VectorXd>{cond}, jacDir);
124 std::cout <<
"Finite Difference Jacobian Action \n" << jacActFD.transpose() << std::endl << std::endl;
131 Eigen::VectorXd hessDir = Eigen::VectorXd::Ones(numCells);
133 Eigen::VectorXd hessAct = mod->ApplyHessian(0,0,0,cond,sens,hessDir);
134 std::cout <<
"Hessian Action: \n" << hessAct.transpose() << std::endl << std::endl;
136 Eigen::VectorXd hessActFD = mod->ApplyHessianByFD(0,0,0,std::vector<Eigen::VectorXd>{cond},sens,hessDir);
137 std::cout <<
"Finite Difference Hessian Action \n" << hessActFD.transpose() << std::endl << std::endl;
150 graph.
AddNode(objective,
"Objective");
151 graph.
AddEdge(
"Model",0,
"Objective",0);
162 hessAct = fullMod->ApplyHessian(0,0,0,cond,objSens,hessDir);
163 hessActFD = fullMod->ApplyHessianByFD(0,0,0,std::vector<Eigen::VectorXd>{cond},objSens,hessDir);
165 std::cout <<
"Hessian Action: \n" << hessAct.transpose() << std::endl << std::endl;
167 std::cout <<
"Finite Difference Hessian Action \n" << hessActFD.transpose() << std::endl << std::endl;
A graph of connected muq::Modeling::WorkPiece's.
void AddNode(std::shared_ptr< WorkPiece > input, std::string const &name)
Add a new node to the graph.
void AddEdge(std::string const &nameFrom, unsigned int const outputDim, std::string const &nameTo, unsigned int const inputDim)
Add a new edge to the graph.
std::shared_ptr< ModGraphPiece > CreateModPiece(std::string const &node, std::vector< std::string > const &inNames=std::vector< std::string >()) const
Create a muq::Modeling::ModPiece whose output matches a given node.