1 #ifndef MODGRAPHPIECE_H_
2 #define MODGRAPHPIECE_H_
4 #include <boost/graph/adjacency_list.hpp>
5 #include <boost/graph/filtered_graph.hpp>
20 typedef boost::filtered_graph<Graph, DependentEdgePredicate, DependentPredicate>
FilteredGraph;
35 std::vector<std::shared_ptr<ConstantVector> >
const&
constantPieces,
36 std::vector<std::string>
const& inputNames,
37 std::shared_ptr<ModPiece> outputNode);
59 std::shared_ptr<ModGraphPiece>
GetSubModel(std::string
const& nodeName)
const;
75 std::vector<int>
MatchInputs(std::shared_ptr<ModGraphPiece> otherPiece)
const;
82 std::shared_ptr<ModGraphPiece>
GradientGraph(
unsigned int const outputDimWrt,
83 unsigned int const inputDimWrt);
90 std::shared_ptr<ModGraphPiece>
JacobianGraph(
unsigned int const outputDimWrt,
91 unsigned int const inputDimWrt);
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;
112 int GetInputIndex(std::shared_ptr<WorkPiece>
const& piece)
const;
114 static Eigen::VectorXi
ConstructInputSizes(std::vector<std::shared_ptr<ConstantVector> >
const& constantPiecesIn);
129 virtual void GradientImpl(
unsigned int const outputDimWrt,
130 unsigned int const inputDimWrt,
132 Eigen::VectorXd
const& sensitivity)
override;
140 virtual void JacobianImpl(
unsigned int const outputDimWrt,
141 unsigned int const inputDimWrt,
152 unsigned int const inputDimWrt,
154 Eigen::VectorXd
const& vec)
override;
158 unsigned int const inWrt1,
159 unsigned int const inWrt2,
161 Eigen::VectorXd
const& sens,
162 Eigen::VectorXd
const& vec)
override;
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;
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;
203 std::map<unsigned int, std::vector<std::pair<unsigned int, unsigned int> > >
InputNodes(boost::graph_traits<Graph>::vertex_descriptor
const& node)
const;
206 std::deque<boost::graph_traits<Graph>::vertex_descriptor>
runOrder;
220 std::unordered_map<unsigned int, ref_vector<Eigen::VectorXd> >
valMap;
A muq::Modeling::ModPiece created from a muq::Modeling::WorkGraph.
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()
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()
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
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
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
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.
boost::filtered_graph< Graph, DependentEdgePredicate, DependentPredicate > FilteredGraph
A filtered graph that only has nodes downstream of a specified input.
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...