MUQ  0.4.3
ModGraphPiece.h
Go to the documentation of this file.
1 #ifndef MODGRAPHPIECE_H_
2 #define MODGRAPHPIECE_H_
3 
4 #include <boost/graph/adjacency_list.hpp>
5 #include <boost/graph/filtered_graph.hpp>
6 
7 #include <map>
8 
14 #include "MUQ/Modeling/WorkGraph.h"
15 
16 namespace muq {
17  namespace Modeling {
18 
20  typedef boost::filtered_graph<Graph, DependentEdgePredicate, DependentPredicate> FilteredGraph;
21 
23  class ModGraphPiece : public ModPiece {
24  public:
25 
27 
34  ModGraphPiece(std::shared_ptr<WorkGraph> graph,
35  std::vector<std::shared_ptr<ConstantVector> > const& constantPieces,
36  std::vector<std::string> const& inputNames,
37  std::shared_ptr<ModPiece> outputNode);
38 
40  virtual ~ModGraphPiece() = default;
41 
42  std::shared_ptr<WorkGraph> GetGraph(){return wgraph;};
43 
44  std::vector<std::shared_ptr<ConstantVector> > GetConstantPieces(){return constantPieces;};
45 
59  std::shared_ptr<ModGraphPiece> GetSubModel(std::string const& nodeName) const;
60 
75  std::vector<int> MatchInputs(std::shared_ptr<ModGraphPiece> otherPiece) const;
76 
82  std::shared_ptr<ModGraphPiece> GradientGraph(unsigned int const outputDimWrt,
83  unsigned int const inputDimWrt);
84 
90  std::shared_ptr<ModGraphPiece> JacobianGraph(unsigned int const outputDimWrt,
91  unsigned int const inputDimWrt);
92 
94  std::string GetOutputName() const{return wgraph->GetName(outputPiece);};
95 
97  std::shared_ptr<ModPiece> GetOutputPiece() const{return outputPiece;};
98 
99  //virtual void SetWarnLevel(unsigned int newLevel) override{};
100 
101  private:
102 
103  // Indices are [outWrt][inWrt]
104  std::map<std::pair<unsigned int, unsigned int>, std::shared_ptr<ModGraphPiece>> gradientPieces;
105  std::map<std::pair<unsigned int, unsigned int>, std::shared_ptr<ModGraphPiece>> jacobianPieces;
106  std::map<std::tuple<unsigned int, unsigned int, unsigned int>, std::shared_ptr<ModGraphPiece>> hessianPieces;
107 
112  int GetInputIndex(std::shared_ptr<WorkPiece> const& piece) const;
113 
114  static Eigen::VectorXi ConstructInputSizes(std::vector<std::shared_ptr<ConstantVector> > const& constantPiecesIn);
115 
117 
120  virtual void EvaluateImpl(ref_vector<Eigen::VectorXd> const& inputs) override;
121 
123 
129  virtual void GradientImpl(unsigned int const outputDimWrt,
130  unsigned int const inputDimWrt,
131  ref_vector<Eigen::VectorXd> const& input,
132  Eigen::VectorXd const& sensitivity) override;
133 
135 
140  virtual void JacobianImpl(unsigned int const outputDimWrt,
141  unsigned int const inputDimWrt,
142  ref_vector<Eigen::VectorXd> const& input) override;
143 
145 
151  virtual void ApplyJacobianImpl(unsigned int const outputDimWrt,
152  unsigned int const inputDimWrt,
153  ref_vector<Eigen::VectorXd> const& input,
154  Eigen::VectorXd const& vec) override;
155 
156 
157  virtual void ApplyHessianImpl(unsigned int const outWrt,
158  unsigned int const inWrt1,
159  unsigned int const inWrt2,
160  ref_vector<Eigen::VectorXd> const& input,
161  Eigen::VectorXd const& sens,
162  Eigen::VectorXd const& vec) override;
163 
164 
166 
172  std::vector<std::tuple<unsigned int, unsigned int, unsigned int> > RequiredOutputs(boost::graph_traits<FilteredGraph>::vertex_descriptor const& node, unsigned int const wrtIn, unsigned int wrtOut) const;
173 
175 
180  std::vector<std::tuple<unsigned int, unsigned int, unsigned int> > RequiredInputs(boost::graph_traits<FilteredGraph>::vertex_descriptor const& node, unsigned int const wrtIn) const;
181 
183  void FillOutputMap();
184 
186 
189  void SetInputs(ref_vector<Eigen::VectorXd> const& inputs);
190 
192 
196  ref_vector<Eigen::VectorXd> GetNodeInputs(boost::graph_traits<Graph>::vertex_descriptor node) const;
197 
199 
203  std::map<unsigned int, std::vector<std::pair<unsigned int, unsigned int> > > InputNodes(boost::graph_traits<Graph>::vertex_descriptor const& node) const;
204 
206  std::deque<boost::graph_traits<Graph>::vertex_descriptor> runOrder;
207 
208  // Run order for computing the derivatives of this muq::Modeling::WorkGraphPiece
212  std::vector<std::deque<boost::graph_traits<Graph>::vertex_descriptor> > adjointRunOrders;
213 
215  std::shared_ptr<WorkGraph> wgraph;
216 
217  std::vector<std::shared_ptr<FilteredGraph> > filtered_graphs;
218 
220  std::unordered_map<unsigned int, ref_vector<Eigen::VectorXd> > valMap;
221 
223  unsigned int outputID;
224 
225  std::shared_ptr<ModPiece> outputPiece;
226 
228  std::vector<std::shared_ptr<ConstantVector> > constantPieces;
229 
230  };
231  } // namespace Modeling
232 } // namespace muq
233 
234 #endif
A muq::Modeling::ModPiece created from a muq::Modeling::WorkGraph.
Definition: ModGraphPiece.h:23
std::unordered_map< unsigned int, ref_vector< Eigen::VectorXd > > valMap
A the map from each node's muq::Modeling::WorkPiece::ID to its outputs.
virtual void ApplyHessianImpl(unsigned int const outWrt, unsigned int const inWrt1, unsigned int const inWrt2, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec) override
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input) override
Compute the Jacobian for this muq::Modeling::WorkGraphPiece using the chain rule.
void SetInputs(ref_vector< Eigen::VectorXd > const &inputs)
Set the inputs.
std::vector< std::shared_ptr< ConstantVector > > GetConstantPieces()
Definition: ModGraphPiece.h:44
unsigned int outputID
The ID of the WorkPiece corresponding to the output node.
std::vector< std::tuple< unsigned int, unsigned int, unsigned int > > RequiredOutputs(boost::graph_traits< FilteredGraph >::vertex_descriptor const &node, unsigned int const wrtIn, unsigned int wrtOut) const
Get the required outputs for a node in one of the filtered graphs.
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Evaluate each muq::Modeling::WorkPiece in the graph.
std::vector< std::shared_ptr< ConstantVector > > constantPieces
The muq::Modeling::ConstantVector's that store the inputs.
std::shared_ptr< WorkGraph > GetGraph()
Definition: ModGraphPiece.h:42
void FillOutputMap()
Fill the map from each node's muq::Modeling::WorkPiece::ID to its outputs.
std::shared_ptr< WorkGraph > wgraph
The WorkGraph associated with this WorkGraphPiece.
virtual ~ModGraphPiece()=default
Default destructor.
virtual void GradientImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity) override
Compute the action of the Jacobian transpose for this muq::Modeling::WorkGraphPiece using the chain r...
int GetInputIndex(std::shared_ptr< WorkPiece > const &piece) const
std::shared_ptr< ModGraphPiece > JacobianGraph(unsigned int const outputDimWrt, unsigned int const inputDimWrt)
std::map< std::pair< unsigned int, unsigned int >, std::shared_ptr< ModGraphPiece > > gradientPieces
Definition: ModGraphPiece.h:97
std::deque< boost::graph_traits< Graph >::vertex_descriptor > runOrder
Run order computed during construction (input->output order)
std::shared_ptr< ModPiece > outputPiece
static Eigen::VectorXi ConstructInputSizes(std::vector< std::shared_ptr< ConstantVector > > const &constantPiecesIn)
std::shared_ptr< ModPiece > GetOutputPiece() const
Definition: ModGraphPiece.h:97
std::vector< std::tuple< unsigned int, unsigned int, unsigned int > > RequiredInputs(boost::graph_traits< FilteredGraph >::vertex_descriptor const &node, unsigned int const wrtIn) const
Get the required inputs for a node in one of the filtered graphs.
std::string GetOutputName() const
Definition: ModGraphPiece.h:94
std::map< std::pair< unsigned int, unsigned int >, std::shared_ptr< ModGraphPiece > > jacobianPieces
std::vector< std::deque< boost::graph_traits< Graph >::vertex_descriptor > > adjointRunOrders
ref_vector< Eigen::VectorXd > GetNodeInputs(boost::graph_traits< Graph >::vertex_descriptor node) const
Get the inputs from muq::Modeling::WorkGraphPiece::valMap to a specified node in the graph.
virtual void ApplyJacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec) override
Compute the action of the Jacobian for this muq::Modeling::WorkGraphPiece using the chain rule.
std::shared_ptr< ModGraphPiece > GetSubModel(std::string const &nodeName) const
Returns another ModGraphPiece containing only part of the graph making up this model.
std::vector< int > MatchInputs(std::shared_ptr< ModGraphPiece > otherPiece) const
Matches inputs with another ModGraphPiece by looking at node names and edges.
std::shared_ptr< ModGraphPiece > GradientGraph(unsigned int const outputDimWrt, unsigned int const inputDimWrt)
std::map< unsigned int, std::vector< std::pair< unsigned int, unsigned int > > > InputNodes(boost::graph_traits< Graph >::vertex_descriptor const &node) const
Get a the input nodes for a node.
ModGraphPiece(std::shared_ptr< WorkGraph > graph, std::vector< std::shared_ptr< ConstantVector > > const &constantPieces, std::vector< std::string > const &inputNames, std::shared_ptr< ModPiece > outputNode)
Construct a muq::Modeling::ModGraphPiece.
std::vector< std::shared_ptr< FilteredGraph > > filtered_graphs
std::map< std::tuple< unsigned int, unsigned int, unsigned int >, std::shared_ptr< ModGraphPiece > > hessianPieces
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
boost::filtered_graph< Graph, DependentEdgePredicate, DependentPredicate > FilteredGraph
A filtered graph that only has nodes downstream of a specified input.
Definition: ModGraphPiece.h:20
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37