MUQ  0.4.3
WorkGraphPiece.h
Go to the documentation of this file.
1 #ifndef WORKGRAPHPIECE_H_
2 #define WORKGRAPHPIECE_H_
3 
4 #include <boost/graph/adjacency_list.hpp>
5 #include <boost/graph/filtered_graph.hpp>
6 
10 
11 #include <deque>
12 
13 namespace muq {
14  namespace Modeling {
15 
18  public:
19 
21 
22  UpstreamPredicate(boost::graph_traits<Graph>::vertex_descriptor const& baseNode, Graph const& graph);
23 
24  bool operator()(const boost::graph_traits<Graph>::vertex_descriptor& node) const;
25 
26  private:
28  std::vector<boost::graph_traits<Graph>::vertex_descriptor> doesDepend;
29 
34  void UpstreamNodes(const boost::graph_traits<Graph>::vertex_descriptor& baseNode, Graph const& graph);
35  };
36 
38  public:
39 
41 
43 
44  bool operator()(const boost::graph_traits<Graph>::edge_descriptor& edge) const;
45 
46  private:
47 
50 
52  const Graph* graph;
53 
54  };
55 
58  public:
61 
66  DependentPredicate(boost::graph_traits<Graph>::vertex_descriptor const& baseNode, Graph const& graph);
67 
72  bool operator()(const boost::graph_traits<Graph>::vertex_descriptor& node) const;
73 
74  private:
76  std::vector<boost::graph_traits<Graph>::vertex_descriptor> doesDepend;
77 
82  void DownstreamNodes(const boost::graph_traits<Graph>::vertex_descriptor& baseNode, Graph const& graph);
83  };
84 
87  public:
90 
96 
101  bool operator()(const boost::graph_traits<Graph>::edge_descriptor& edge) const;
102 
103  private:
106 
108  const Graph* graph;
109  };
110 
112  typedef boost::filtered_graph<Graph, DependentEdgePredicate, DependentPredicate> FilteredGraph;
113 
115  class WorkGraphPiece : public WorkPiece {
116  public:
117 
119 
128  WorkGraphPiece(std::shared_ptr<WorkGraph> wgraph,
129  std::vector<std::shared_ptr<ConstantPiece> > const& constantPieces,
130  std::vector<std::string> const& inputNames,
131  std::map<unsigned int, std::string> const& inTypes,
132  std::shared_ptr<WorkPiece> outputNode);
133 
135  virtual ~WorkGraphPiece();
136 
137 
138  std::shared_ptr<WorkGraph> GetGraph(){return wgraph;};
139 
140  private:
141 
143 
146  virtual void EvaluateImpl(ref_vector<boost::any> const& inputs) override;
147 
148  // /// Compute the Jacobian for this muq::Modeling::WorkGraphPiece using the chain rule
149  // /**
150  // @param[in] wrtIn We are taking the Jacobian with respect to this input
151  // @param[in] wrtOut We are taking the Jacobian of this output
152  // @param[in] inputs Inputs to the muq::Modeling::WorkGraphPiece
153  // */
154  // virtual void JacobianImpl(unsigned int const wrtIn, unsigned int const wrtOut, ref_vector<boost::any> const& inputs) override;
155  //
156  // /// Compute the action of the Jacobian for this muq::Modeling::WorkGraphPiece using the chain rule
157  // /**
158  // @param[in] wrtIn We are taking the Jacobian with respect to this input
159  // @param[in] wrtOut We are taking the Jacobian of this output
160  // @param[in] vec We are applying the Jacobian to this object
161  // @param[in] inputs Inputs to the muq::Modeling::WorkGraphPiece
162  // */
163  // virtual void JacobianActionImpl(unsigned int const wrtIn, unsigned int const wrtOut, boost::any const& vec, ref_vector<boost::any> const& inputs) override;
164  //
165  // /// Compute the action of the Jacobian transpose for this muq::Modeling::WorkGraphPiece using the chain rule
166  // /**
167  // @param[in] wrtIn We are taking the Jacobian with respect to this input
168  // @param[in] wrtOut We are taking the Jacobian of this output
169  // @param[in] vec We are applying the Jacobian transpose to this object
170  // @param[in] inputs Inputs to the muq::Modeling::WorkGraphPiece
171  // */
172  // virtual void JacobianTransposeActionImpl(unsigned int const wrtIn, unsigned int const wrtOut, boost::any const& vec, ref_vector<boost::any> const& inputs) override;
173 
175 
181  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;
182 
184 
189  std::vector<std::tuple<unsigned int, unsigned int, unsigned int> > RequiredInputs(boost::graph_traits<FilteredGraph>::vertex_descriptor const& node, unsigned int const wrtIn) const;
190 
192  void OutputMap();
193 
195 
198  void SetInputs(ref_vector<boost::any> const& inputs);
199 
201 
205  ref_vector<boost::any> Inputs(boost::graph_traits<Graph>::vertex_descriptor node) const;
206 
208 
212  std::map<unsigned int, std::vector<std::pair<unsigned int, unsigned int> > > InputNodes(boost::graph_traits<Graph>::vertex_descriptor const& node) const;
213 
215  std::deque<boost::graph_traits<Graph>::vertex_descriptor> runOrder;
216 
217  // Run order for computing the derivatives of this muq::Modeling::WorkGraphPiece
221  std::vector<std::deque<boost::graph_traits<Graph>::vertex_descriptor> > derivRunOrders;
222 
224  //std::shared_ptr<const Graph> graph;
225  std::shared_ptr<WorkGraph> wgraph;
226 
227  std::vector<std::shared_ptr<FilteredGraph> > filtered_graphs;
228 
230  std::unordered_map<unsigned int, ref_vector<boost::any> > valMap;
231 
233  unsigned int outputID;
234 
236  std::vector<std::shared_ptr<ConstantPiece> > constantPieces;
237 
238  };
239  } // namespace Modeling
240 } // namespace muq
241 
242 #endif
Determine if the source of an edge is downstream of an input.
bool operator()(const boost::graph_traits< Graph >::edge_descriptor &edge) const
DependentPredicate nodePred
The nodes that are downstream of the input.
DependentEdgePredicate()
Required default constructor.
const Graph * graph
The graph holding all the nodes.
This class keeps track of which nodes are downstream of a specified input.
bool operator()(const boost::graph_traits< Graph >::vertex_descriptor &node) const
DependentPredicate()
Required default constructor.
void DownstreamNodes(const boost::graph_traits< Graph >::vertex_descriptor &baseNode, Graph const &graph)
std::vector< boost::graph_traits< Graph >::vertex_descriptor > doesDepend
A vector of all the nodes downstream of the input node.
UpstreamPredicate nodePred
The nodes that are downstream of the input.
UpstreamEdgePredicate(UpstreamPredicate nodePred, Graph const &graph)
const Graph * graph
The graph holding all the nodes.
bool operator()(const boost::graph_traits< Graph >::edge_descriptor &edge) const
This class keeps track of which nodes are upstream (needed to evaluate) a given node.
bool operator()(const boost::graph_traits< Graph >::vertex_descriptor &node) const
std::vector< boost::graph_traits< Graph >::vertex_descriptor > doesDepend
A vector of all the nodes downstream of the input node.
void UpstreamNodes(const boost::graph_traits< Graph >::vertex_descriptor &baseNode, Graph const &graph)
A muq::Modeling::WorkPiece created from a muq::Modeling::WorkGraph.
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.
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.
void SetInputs(ref_vector< boost::any > const &inputs)
Set the inputs.
virtual void EvaluateImpl(ref_vector< boost::any > const &inputs) override
Evaluate each muq::Modeling::WorkPiece in the graph.
void OutputMap()
Fill the map from each node's muq::Modeling::WorkPiece::ID to its outputs.
virtual ~WorkGraphPiece()
Default destructor.
unsigned int outputID
The ID of the WorkPiece corresponding to the output node.
std::vector< std::shared_ptr< FilteredGraph > > filtered_graphs
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::unordered_map< unsigned int, ref_vector< boost::any > > valMap
A the map from each node's muq::Modeling::WorkPiece::ID to its outputs.
std::shared_ptr< WorkGraph > wgraph
The WorkGraph associated with this WorkGraphPiece.
std::deque< boost::graph_traits< Graph >::vertex_descriptor > runOrder
Run order computed during construction (input->output order)
std::shared_ptr< WorkGraph > GetGraph()
std::vector< std::shared_ptr< ConstantPiece > > constantPieces
The muq::Modeling::ConstantPiece's that store the inputs.
std::vector< std::deque< boost::graph_traits< Graph >::vertex_descriptor > > derivRunOrders
Base class for MUQ's modelling envronment.
Definition: WorkPiece.h:40
friend class WorkGraphPiece
Definition: WorkPiece.h:43
@ Inputs
The constructor fixes the input number and possibly the types.
Definition: WorkPiece.h:51
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
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, std::shared_ptr< WorkGraphNode >, std::shared_ptr< WorkGraphEdge > > Graph
Define a directed graph type.