MUQ  0.4.3
muq::Modeling::WorkGraph Class Reference

A graph of connected muq::Modeling::WorkPiece's. More...

#include <WorkGraph.h>

Detailed Description

A graph of connected muq::Modeling::WorkPiece's.

Definition at line 20 of file WorkGraph.h.

Public Member Functions

 WorkGraph ()
 
virtual ~WorkGraph ()
 
virtual std::shared_ptr< WorkGraphClone () const
 
unsigned int NumNodes () const
 Get the number of nodes in the graph. More...
 
unsigned int NumEdges () const
 Get the number of edgess in the graph. More...
 
void AddNode (std::shared_ptr< WorkPiece > input, std::string const &name)
 Add a new node to the graph. More...
 
void RemoveNode (std::string const &name)
 
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. More...
 
bool HasNode (std::string const &name) const
 Is the given node in the graph? More...
 
std::vector< std::pair< int, int > > GetEdges (std::string const &srcName, std::string const &tgtName)
 Returns all edges going from node name1 to node name2. More...
 
void Visualize (std::string const &filename) const
 Visualize the graph. More...
 
std::vector< std::string > GetParents (std::string const &name) const
 
std::string GetParent (std::string const &name, int inputIndex) const
 
std::vector< std::string > GetChildren (std::string const &name) const
 
std::shared_ptr< WorkGraphDependentCut (std::string const &nameOut) const
 Create a new graph cutting any of the nodes that do not affect the output node. More...
 
std::shared_ptr< WorkGraphPieceCreateWorkPiece (std::string const &node) const
 Create a muq::Modeling::WorkPiece whose output matches a given node. More...
 
std::shared_ptr< ModGraphPieceCreateModPiece (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. More...
 
bool Constant (std::string const &node) const
 Check to see if a node is constant? More...
 
std::vector< boost::any > const & GetConstantOutputs (std::string const &node) const
 Get the output values for a constant node. More...
 
std::shared_ptr< WorkPieceGetPiece (std::string const &name)
 
std::string GetName (std::shared_ptr< WorkPiece > piece) const
 
void Print (std::ostream &fout=std::cout) const
 
std::vector< std::pair< std::string, int > > GetInputNames () const
 Find the inputs to the graph. More...
 
std::vector< std::pair< std::string, int > > GetOutputNames () const
 Find the outputs to the graph. More...
 
void BindNode (std::string const &nodeName, std::vector< boost::any > const &x)
 
void BindEdge (std::string const &nodeName, unsigned int inputDim, boost::any const &x)
 

Friends

class WorkGraphPiece
 
class ModGraphPiece
 

Constructor & Destructor Documentation

◆ WorkGraph()

WorkGraph::WorkGraph ( )

Definition at line 50 of file WorkGraph.cpp.

◆ ~WorkGraph()

WorkGraph::~WorkGraph ( )
virtual

Definition at line 52 of file WorkGraph.cpp.

Member Function Documentation

◆ AddEdge()

void WorkGraph::AddEdge ( std::string const &  nameFrom,
unsigned int const  outputDim,
std::string const &  nameTo,
unsigned int const  inputDim 
)

Add a new edge to the graph.

Parameters
[in]nameFromThe name of the upstream node.
[in]outputDimThe output dimension of "nameFrom" that will be given to "nameTo"
[in]nameToThe name of the downstream node.
[in]inputDimThe input dimension of "nameTo" that will be given the output of "nameFrom"

Definition at line 206 of file WorkGraph.cpp.

References GetNodeIterator(), graph, and nlohmann::to_string().

Referenced by BindEdge(), muq::SamplingAlgorithms::DILIKernel::CreateLikelihood(), muq::Modeling::ModGraphPiece::GradientGraph(), muq::Modeling::ModGraphPiece::JacobianGraph(), and main().

◆ AddNode()

void WorkGraph::AddNode ( std::shared_ptr< WorkPiece input,
std::string const &  name 
)

Add a new node to the graph.

Parameters
[in]inputA pointer to a muq::Modelling::Core::WorkPiece that will be called when this node is evaluated.
[in]nameA string representing a unique name for this node.

Definition at line 195 of file WorkGraph.cpp.

References graph, and HasNode().

Referenced by BindEdge(), muq::SamplingAlgorithms::DILIKernel::CreateLikelihood(), muq::Modeling::ModGraphPiece::GradientGraph(), muq::Modeling::ModGraphPiece::JacobianGraph(), and main().

◆ BindEdge()

void WorkGraph::BindEdge ( std::string const &  nodeName,
unsigned int  inputDim,
boost::any const &  x 
)

Like BindNode, but only for an edge. This function actually creates a new node in the system and fixes the value of that node. The new node name is constructed using

nodeName+"_FixedInput"+std::to_string(inputDim)
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.h:25172

As a naming example, if nodeName is "x" and inputDim is 2, this function will create a new node with name "x_FixedInput2"

Parameters
[in]nodeNameThe name of the existing node whose input we wish to set
[in]inputDimThe input of "nodeName" to fix
[in]xThe value the input should be set to

Definition at line 777 of file WorkGraph.cpp.

References AddEdge(), AddNode(), nlohmann::detail::dtoa_impl::e, GetNodeIterator(), graph, and nlohmann::to_string().

◆ BindNode()

void WorkGraph::BindNode ( std::string const &  nodeName,
std::vector< boost::any > const &  x 
)

Fix the value of a particular node to a constant value and remove any input edges from that node. After binding a node or edge, if DependentCut is called, any group of constant nodes will be grouped into a single node. This is a useful feature when offline computations are required for a model.

Parameters
[in]nodeNameThe node to bind
[in]xThe value that "nodeName" will be fixed at. Note that x must be the same size of the existing output of "nodeName"

Definition at line 754 of file WorkGraph.cpp.

References GetNodeIterator(), and graph.

◆ Clone()

std::shared_ptr< WorkGraph > WorkGraph::Clone ( ) const
virtual

Definition at line 54 of file WorkGraph.cpp.

References graph.

◆ Constant() [1/2]

bool WorkGraph::Constant ( boost::graph_traits< Graph >::vertex_descriptor const &  node) const
private

Check to see if a node is constant?

A constant node is nonrandom, has no random dependencies, and either has no inputs or all of its inputs are given by const ant upstream nodes

Parameters
[in]nodeWe want to know if this node is constant.
Returns
true: the node is constant, false: the node is not constant

Definition at line 1006 of file WorkGraph.cpp.

References Constant(), nlohmann::detail::dtoa_impl::e, and graph.

◆ Constant() [2/2]

bool WorkGraph::Constant ( std::string const &  node) const

Check to see if a node is constant?

A constant node is nonrandom, has no random dependencies, and either has no inputs or all of its inputs are given by const ant upstream nodes

Parameters
[in]nodeThe name of the node
Returns
true: the node is constant, false: the node is not constant

Definition at line 1002 of file WorkGraph.cpp.

References GetNodeIterator().

Referenced by Constant(), DependentCut(), GetConstantOutputs(), and RecursiveCut().

◆ CreateModPiece()

std::shared_ptr< ModGraphPiece > WorkGraph::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.

Parameters
[in]nodeThe name of the output node
[in]inNamesThe name (and order) of the input nodes—defaults to the order of the graph
Returns
A muq::Utilities::WorkPiece whose outputs are the same as the output node

Definition at line 591 of file WorkGraph.cpp.

References DependentCut(), nlohmann::detail::dtoa_impl::e, graph, and HasNode().

Referenced by muq::SamplingAlgorithms::DILIKernel::CreateLikelihood(), muq::Modeling::ModGraphPiece::GradientGraph(), muq::Modeling::ModGraphPiece::JacobianGraph(), and main().

◆ CreateWorkPiece()

std::shared_ptr< WorkGraphPiece > WorkGraph::CreateWorkPiece ( std::string const &  node) const

Create a muq::Modeling::WorkPiece whose output matches a given node.

Parameters
[in]nodeThe name of the output node
Returns
A muq::Utilities::WorkPiece whose outputs are the same as the output node

Definition at line 523 of file WorkGraph.cpp.

References DependentCut(), and HasNode().

◆ DependentCut()

std::shared_ptr< WorkGraph > WorkGraph::DependentCut ( std::string const &  nameOut) const

Create a new graph cutting any of the nodes that do not affect the output node.

Parameters
[in]nameOutThe output node we care about
Returns
A new graph where all of the nodes that do not affect the output node are cut

Definition at line 477 of file WorkGraph.cpp.

References Constant(), GetConstantOutputs(), GetNodeIterator(), graph, and RecursiveCut().

Referenced by CreateModPiece(), and CreateWorkPiece().

◆ GetChildren()

std::vector< std::string > WorkGraph::GetChildren ( std::string const &  name) const

Returns a list of the downstream nodes.

Definition at line 137 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, GetNodeIterator(), graph, and nlohmann::detail::dtoa_impl::v.

◆ GetConstantOutputs() [1/2]

std::vector< boost::any > & WorkGraph::GetConstantOutputs ( boost::graph_traits< Graph >::vertex_descriptor const &  node) const
private

Get the output values for a constant node.

Parameters
[out]outsThe output values for the node (returned by reference)
[in]nodeWe want the output values of this node

Definition at line 943 of file WorkGraph.cpp.

References Constant(), nlohmann::detail::dtoa_impl::e, GetConstantOutputs(), and graph.

◆ GetConstantOutputs() [2/2]

std::vector< boost::any > const & WorkGraph::GetConstantOutputs ( std::string const &  node) const

Get the output values for a constant node.

Parameters
[out]outsThe output values for the node (returned by reference)
[in]nodeThe name of the node

Definition at line 936 of file WorkGraph.cpp.

References Constant(), and GetNodeIterator().

Referenced by DependentCut(), GetConstantOutputs(), and RecursiveCut().

◆ GetEdges()

std::vector< std::pair< int, int > > WorkGraph::GetEdges ( std::string const &  srcName,
std::string const &  tgtName 
)

Returns all edges going from node name1 to node name2.

Parameters
[in]srcNameThe name of the source node
[in]tgtNameThe name of the target node
Returns
A vector of pairs holding the edge information. The first component of the pair is the output index of the source node and the second component of the pair is the input index of the target ndoe.

Definition at line 69 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, GetNodeIterator(), and graph.

◆ GetInputNames()

std::vector< std::pair< std::string, int > > WorkGraph::GetInputNames ( ) const

Find the inputs to the graph.

Returns
The node name and unused input index for each output

Definition at line 153 of file WorkGraph.cpp.

References graph, and GraphInputs().

◆ GetName()

std::string WorkGraph::GetName ( std::shared_ptr< WorkPiece piece) const

Given a WorkPiece, return the name of the node containing that piece.

Definition at line 749 of file WorkGraph.cpp.

References GetNodeIterator(), and graph.

◆ GetNodeIterator() [1/2]

boost::graph_traits< Graph >::vertex_iterator WorkGraph::GetNodeIterator ( std::shared_ptr< WorkPiece piece) const
private

Definition at line 269 of file WorkGraph.cpp.

References graph, and nlohmann::detail::dtoa_impl::v.

◆ GetNodeIterator() [2/2]

boost::graph_traits< Graph >::vertex_iterator WorkGraph::GetNodeIterator ( std::string const &  name) const
private

Get a vertex_iterator to the node with name "name".

Parameters
[in]nameThe name of a node in the graph
Returns
An iterator to that node (returns the "end" iterator if that node does not exist

Definition at line 258 of file WorkGraph.cpp.

References graph, and nlohmann::detail::dtoa_impl::v.

Referenced by AddEdge(), BindEdge(), BindNode(), Constant(), DependentCut(), GetChildren(), GetConstantOutputs(), GetEdges(), GetName(), GetParent(), GetParents(), GetPiece(), HasNode(), and RemoveNode().

◆ GetOutputNames()

std::vector< std::pair< std::string, int > > WorkGraph::GetOutputNames ( ) const

Find the outputs to the graph.

Returns
The node name and unused output index for each output

Definition at line 164 of file WorkGraph.cpp.

References graph, and GraphOutputs().

◆ GetParent()

std::string WorkGraph::GetParent ( std::string const &  name,
int  inputIndex 
) const

Get the upstream node with an edge that comes input input inputIndex.

Definition at line 120 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, GetNodeIterator(), graph, and nlohmann::detail::dtoa_impl::v.

◆ GetParents()

std::vector< std::string > WorkGraph::GetParents ( std::string const &  name) const

Returns a list of the upstream nodes.

Definition at line 98 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, GetNodeIterator(), graph, and nlohmann::detail::dtoa_impl::v.

◆ GetPiece() [1/2]

std::shared_ptr< WorkPiece > WorkGraph::GetPiece ( boost::graph_traits< Graph >::vertex_descriptor  it)
private

Definition at line 736 of file WorkGraph.cpp.

References graph.

◆ GetPiece() [2/2]

std::shared_ptr< WorkPiece > WorkGraph::GetPiece ( std::string const &  name)

Returns the work piece at a particular node in the graph.

Definition at line 720 of file WorkGraph.cpp.

References GetNodeIterator(), graph, and nlohmann::detail::dtoa_impl::v.

◆ GraphInputs()

std::vector< std::pair< boost::graph_traits< Graph >::vertex_descriptor, int > > WorkGraph::GraphInputs ( ) const
private

Find the inputs to the graph.

Returns
The inputs to the graph, each input is the vertex and the input dimension that has not be set

Definition at line 335 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, graph, and nlohmann::detail::dtoa_impl::v.

Referenced by GetInputNames(), and Visualize().

◆ GraphOutputs()

std::vector< std::pair< boost::graph_traits< Graph >::vertex_descriptor, int > > WorkGraph::GraphOutputs ( ) const
private

Find the outputs to the graph.

Returns
The outputs to the graph, each input is the vertex and the output dimension that has not be set

Definition at line 282 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, graph, and nlohmann::detail::dtoa_impl::v.

Referenced by GetOutputNames(), and Visualize().

◆ HasEdge()

bool WorkGraph::HasEdge ( boost::graph_traits< Graph >::vertex_descriptor const &  vOut,
boost::graph_traits< Graph >::vertex_descriptor const &  vIn,
int const  inputDim 
) const
private

Is there an edge between two vertices?

Parameters
[in]vOutThe node whose output is going to vIn
[in]vInThe node whose is recieving output from vOut
[in]inputDimThe dimension of the vIn that the output is going to
Returns
true if there is an edge between vOut and vIn, false otherwise

Definition at line 388 of file WorkGraph.cpp.

References graph.

◆ HasNode() [1/2]

bool WorkGraph::HasNode ( boost::graph_traits< Graph >::vertex_iterator &  iter,
std::string const &  name 
) const
private

Is the given node in the graph?

Parameters
[out]iterIf the node exists, this is an iterator to it. If the node does not exist, this is the end iterator
[in]nameThe name of the node
Returns
true if the graph has a node named 'name', false otherwise

Definition at line 186 of file WorkGraph.cpp.

References GetNodeIterator(), and graph.

◆ HasNode() [2/2]

bool WorkGraph::HasNode ( std::string const &  name) const

Is the given node in the graph?

Parameters
[in]nameThe name of the node
Returns
true if the graph has a node named 'name', false otherwise

Definition at line 177 of file WorkGraph.cpp.

Referenced by AddNode(), CreateModPiece(), CreateWorkPiece(), and muq::Modeling::ModGraphPiece::JacobianGraph().

◆ NumEdges()

unsigned int WorkGraph::NumEdges ( ) const

Get the number of edgess in the graph.

Returns
The number of edges

Definition at line 92 of file WorkGraph.cpp.

References graph.

◆ NumNodes()

unsigned int WorkGraph::NumNodes ( ) const

Get the number of nodes in the graph.

Returns
The number of nodes

Definition at line 86 of file WorkGraph.cpp.

References graph.

◆ Print()

void WorkGraph::Print ( std::ostream &  fout = std::cout) const

Print the nodes and edges of this graph to std::cout.

Definition at line 697 of file WorkGraph.cpp.

References nlohmann::detail::dtoa_impl::e, graph, and nlohmann::detail::dtoa_impl::v.

◆ RecursiveCut()

void WorkGraph::RecursiveCut ( const boost::graph_traits< Graph >::vertex_descriptor &  vOld,
const boost::graph_traits< Graph >::vertex_descriptor &  vNew,
std::shared_ptr< WorkGraph > &  newGraph 
) const
private

Recursively go upstream from a node, copying nodes.

Copies the upstream nodes into a new graph. If a node is constant it evaluates it and lumps all of the upstream nodes from that node into a muq::Modeling::ConstantParameters

Parameters
[in]vOldThe old vertex, where we beging copying from
[in]vNewThe new vertex, a copy of the old one on the new graph
[in,out]newGraphAll of the (nonconstant) upstreams nodes from vOld will be copied are copied to this new graph

Definition at line 402 of file WorkGraph.cpp.

References Constant(), nlohmann::detail::dtoa_impl::e, GetConstantOutputs(), graph, and nlohmann::detail::dtoa_impl::v.

Referenced by DependentCut().

◆ RemoveNode()

void WorkGraph::RemoveNode ( std::string const &  name)

Definition at line 61 of file WorkGraph.cpp.

References GetNodeIterator(), and graph.

◆ Visualize()

void WorkGraph::Visualize ( std::string const &  filename) const

Visualize the graph.

Parameters
[in]filenameThe name of the file where the graph visualization is to be stored

Definition at line 857 of file WorkGraph.cpp.

References graph, GraphInputs(), GraphOutputs(), and nlohmann::detail::dtoa_impl::v.

Friends And Related Function Documentation

◆ ModGraphPiece

friend class ModGraphPiece
friend

Definition at line 23 of file WorkGraph.h.

◆ WorkGraphPiece

friend class WorkGraphPiece
friend

Definition at line 22 of file WorkGraph.h.

Member Data Documentation

◆ graph


The documentation for this class was generated from the following files: