Direct Graphical Models  v.1.7.0
GraphPairwise.h
1 // (pairwise) Graph class interface;
2 // Written by Sergey G. Kosov in 2015 for Project X
3 #pragma once
4 
5 #include "IGraphPairwise.h"
6 
7 namespace DirectGraphicalModels
8 {
9  // =============================== Node Structure ==============================
14  struct Node {
15  size_t id;
16  Mat Pot;
17  byte sol;
18  vec_size_t to;
19  vec_size_t from;
20 
21  Node(void) = delete;
22  Node(size_t _id, const Mat &p = EmptyMat) : id(_id), Pot(p.empty() ? Mat() : p.clone()), sol(0) {}
23  };
24  using ptr_node_t = std::unique_ptr<Node>;
25  using vec_node_t = std::vector<ptr_node_t>;
26 
27  // =============================== Edge Structure ==============================
32  struct Edge {
33  size_t node1;
34  size_t node2;
35  Mat Pot;
36  float * msg;
37  float * msg_temp;
38  byte group_id;
39  bool suspend;
40 
41  Edge(void) = delete;
42  Edge(size_t n1, size_t n2, byte group = 0, const Mat &p = EmptyMat) : node1(n1), node2(n2), Pot(p.empty() ? Mat() : p.clone()), msg(NULL), msg_temp(NULL), group_id(group), suspend(false) {}
43 
44  ~Edge(void) {
45  if (msg) delete msg;
46  if (msg_temp) delete msg_temp;
47  }
48 
49  void msg_swap(void) {
50  float *tmp = msg;
51  msg = msg_temp;
52  msg_temp = tmp;
53  }
54  };
55  using ptr_edge_t = std::unique_ptr<Edge>;
56  using vec_edge_t = std::vector<ptr_edge_t>;
57 
58  // ================================ Graph Class ================================
65  {
66  friend class CMessagePassing;
67  friend class CInferChain;
68  friend class CInferTree;
69  friend class CInferLBP;
70  friend class CInferViterbi;
71  friend class CInferTRW;
72 
73 
74  public:
79  DllExport CGraphPairwise(byte nStates) : IGraphPairwise(nStates), m_IDx(0) {}
80  DllExport virtual ~CGraphPairwise(void) = default;
81 
82  // CGraph
83  DllExport void reset(void) override;
84  DllExport size_t addNode (const Mat &pot = EmptyMat) override;
85  DllExport void setNode (size_t node, const Mat &pot) override;
86  DllExport void getNode (size_t node, Mat &pot) const override;
87  DllExport void getChildNodes (size_t node, vec_size_t &vNodes) const override;
88  DllExport void getParentNodes(size_t node, vec_size_t &vNodes) const override;
89  DllExport size_t getNumNodes(void) const override { return m_vNodes.size(); }
90  DllExport size_t getNumEdges(void) const override { return m_vEdges.size(); }
91 
92 // DllExport virtual void marginalize(const vec_size_t &nodes);
93 
94  DllExport void addEdge (size_t srcNode, size_t dstNode, byte group, const Mat &pot) override;
95  DllExport void setEdge (size_t srcNode, size_t dstNode, const Mat &pot) override;
96  DllExport void setEdges (std::optional<byte> group, const Mat& pot) override;
97  DllExport void getEdge (size_t srcNode, size_t dstNode, Mat &pot) const override;
98  DllExport void setEdgeGroup(size_t srcNode, size_t dstNode, byte group) override;
99  DllExport byte getEdgeGroup(size_t srcNode, size_t dstNode) const override;
100  DllExport void removeEdge (size_t srcNode, size_t dstNode) override;
101  DllExport bool isEdgeExists(size_t srcNode, size_t dstNode) const override;
102 
103 
104  private:
109  DllExport void removeEdge(size_t edge);
110 
111 
112  private:
113  size_t m_IDx; // = 0; Primary Key
114  vec_node_t m_vNodes; // Nodes container
115  vec_edge_t m_vEdges; // Edges container
116  };
117 }
118 
Mat Pot
Node potentials: Mat(size: nStates x 1; type: CV_32FC1)
Definition: GraphPairwise.h:16
void setEdges(std::optional< byte > group, const Mat &pot) override
Sets the potential pot to all edges belonging to group group.
vec_size_t to
Array of edge ids, pointing to the Child vertices.
Definition: GraphPairwise.h:18
virtual ~CGraphPairwise(void)=default
void removeEdge(size_t srcNode, size_t dstNode) override
Removes the specified edge.
void addEdge(size_t srcNode, size_t dstNode, byte group, const Mat &pot) override
Adds an additional directed edge with specified potentional.
void setNode(size_t node, const Mat &pot) override
Sets or changes the potential of node.
bool suspend
Flag, indicating weather the message calculation must be postponed (used in message-passing algorithm...
Definition: GraphPairwise.h:39
size_t node2
Second (destination) node in edge.
Definition: GraphPairwise.h:34
Abstract base class for message passing inference algorithmes.
CGraphPairwise(byte nStates)
Constructor.
Definition: GraphPairwise.h:79
std::vector< ptr_node_t > vec_node_t
Definition: GraphPairwise.h:25
float * msg
Message (used in message-passing algorithms): Mat(size: nStates x 1; type: CV_32FC1) ...
Definition: GraphPairwise.h:36
byte getEdgeGroup(size_t srcNode, size_t dstNode) const override
Returns the group of the edge.
std::vector< ptr_edge_t > vec_edge_t
Definition: GraphPairwise.h:56
size_t node1
First (source) node in edge.
Definition: GraphPairwise.h:33
float * msg_temp
Temp Message (used in message-passing algorithms): Mat(size: nStates x 1; type: CV_32FC1) ...
Definition: GraphPairwise.h:37
Inference for chain graphs.
Definition: InferChain.h:16
void setEdgeGroup(size_t srcNode, size_t dstNode, byte group) override
Assigns a directed edge (srcNode) –> (dstNode) to the group group.
byte group_id
ID of the group, to which the edge belongs.
Definition: GraphPairwise.h:38
Tree-reweighted inference class.
Definition: InferTRW.h:19
void reset(void) override
Resets the graph.
bool isEdgeExists(size_t srcNode, size_t dstNode) const override
Checks whether the edge exists.
size_t getNumEdges(void) const override
Returns the number of edges in the graph.
Definition: GraphPairwise.h:90
size_t getNumNodes(void) const override
Returns the number of nodes in the graph.
Definition: GraphPairwise.h:89
Edge(size_t n1, size_t n2, byte group=0, const Mat &p=EmptyMat)
Definition: GraphPairwise.h:42
std::unique_ptr< Node > ptr_node_t
Definition: GraphPairwise.h:24
void getChildNodes(size_t node, vec_size_t &vNodes) const override
Returns the set of IDs of the child nodes of the argument node.
void getParentNodes(size_t node, vec_size_t &vNodes) const override
Returns the set of IDs of the parent nodes of the argument node.
void getNode(size_t node, Mat &pot) const override
Returns the node potential.
Inference for tree graphs (undirected graphs without loops)
Definition: InferTree.h:16
void setEdge(size_t srcNode, size_t dstNode, const Mat &pot) override
Sets or changes the potentional of directed edge.
Interface class for graphical models.
Mat Pot
The edge potentials: Mat(size: nStates x nStates; type: CV_32FC1)
Definition: GraphPairwise.h:35
std::unique_ptr< Edge > ptr_edge_t
Definition: GraphPairwise.h:55
Max product Viterbi inference class.
Definition: InferViterbi.h:15
Sum product Loopy Belief Propagation inference class.
Definition: InferLBP.h:16
void getEdge(size_t srcNode, size_t dstNode, Mat &pot) const override
Returns the edge potential.
vec_size_t from
Array of edge ids, coming from the Parent vertices.
Definition: GraphPairwise.h:19
Node(size_t _id, const Mat &p=EmptyMat)
Definition: GraphPairwise.h:22
size_t addNode(const Mat &pot=EmptyMat) override
Adds an additional node (with specified potentional)