Direct Graphical Models  v.1.7.0
IGraphPairwise.cpp
1 #include "IGraphPairwise.h"
2 
3 namespace DirectGraphicalModels
4 {
5  void IGraphPairwise::addEdge(size_t srcNode, size_t dstNode, const Mat &pot)
6  {
7  addEdge(srcNode, dstNode, 0, pot);
8  }
9 
10  bool IGraphPairwise::isEdgeArc(size_t srcNode, size_t dstNode) const
11  {
12  return isEdgeExists(dstNode, srcNode);
13  }
14 
15  // Add a new (undirected edge) ark to the graph with specified potentional
16  void IGraphPairwise::addArc(size_t Node1, size_t Node2, const Mat &pot)
17  {
18  addArc(Node1, Node2, 0, pot);
19  }
20 
21  // Add a new (undirected edge) ark to the graph with specified potentional
22  void IGraphPairwise::addArc(size_t Node1, size_t Node2, byte group, const Mat &pot)
23  {
24  if (pot.empty()) {
25  addEdge(Node1, Node2, group, Mat());
26  addEdge(Node2, Node1, group, Mat());
27  }
28  else {
29  Mat Pot;
30  sqrt(pot, Pot);
31  addEdge(Node1, Node2, group, Pot);
32  addEdge(Node2, Node1, group, Pot.t());
33  }
34  }
35 
36  // Add a new (undirected edge) arc to the graph with specified potentional
37  void IGraphPairwise::setArc(size_t Node1, size_t Node2, const Mat &pot)
38  {
39  Mat Pot;
40  sqrt(pot, Pot);
41  setEdge(Node1, Node2, Pot);
42  setEdge(Node2, Node1, Pot.t());
43  }
44 
45  void IGraphPairwise::setArcGroup(size_t Node1, size_t Node2, byte group)
46  {
47  setEdgeGroup(Node1, Node2, group);
48  setEdgeGroup(Node2, Node1, group);
49  }
50 
51  void IGraphPairwise::removeArc(size_t Node1, size_t Node2)
52  {
53  removeEdge(Node1, Node2);
54  removeEdge(Node2, Node1);
55  }
56 
57  bool IGraphPairwise::isArcExists(size_t Node1, size_t Node2) const
58  {
59  return (isEdgeExists(Node1, Node2) && isEdgeExists(Node2, Node1));
60  }
61 
62  void IGraphPairwise::marginalize(const vec_size_t &nodes)
63  {
64  Mat pot, pot1, pot2;
65 
66  for (size_t node : nodes) {
67  vec_size_t parentNodes, childNodes, managers;
68  getParentNodes(node, parentNodes);
69  getChildNodes(node, childNodes);
70 
71  // find all managers for the node
72  for (size_t child : childNodes) {
73  // Looking for those child nodes, which are managers
74  auto isArc = std::find(parentNodes.begin(), parentNodes.end(), child); // If there is a return edge => the child is a neighbor
75  if (isArc != parentNodes.end()) continue;
76  // Here the child is a manager
77  auto isInZ = std::find(nodes.begin(), nodes.end(), child); // If the manager is to be also marginalized
78  if (isInZ != nodes.end()) continue;
79 
80  managers.push_back(child);
81 
82  // Add new edges (from any other neighboring node to the manager)
83  for (size_t parent : parentNodes) {
84  auto isInZ = std::find(nodes.begin(), nodes.end(), parent); // If the parent is to be also marginalized
85  if (isInZ != nodes.end()) continue;
86 
87  getEdge(parent, node, pot1);
88  getEdge(node, child, pot2);
89  if (pot1.empty() && pot2.empty()) addEdge(parent, child);
90  else {
91  pot1.empty() ? pot = pot2 + pot1 : pot = pot1 + pot2;
92  addEdge(parent, child, pot);
93  }
94  }
95  }
96 
97  // Add new arcs (between two managers)
98  if (managers.size() >= 2)
99  for (size_t i = 0; i < managers.size() - 1; i++)
100  for (size_t j = i + 1; j < managers.size(); j++) {
101  getEdge(node, managers[i], pot1);
102  getEdge(node, managers[j], pot2);
103  if (pot1.empty() && pot2.empty()) addArc(managers[i], managers[j]);
104  else {
105  pot1.empty() ? pot = pot2 + pot1 : pot = pot1 + pot2;
106  addArc(managers[i], managers[j], pot);
107  }
108  }
109 
110  // Delete all
111  for (size_t &parent : parentNodes) removeEdge(parent, node);
112  for (size_t &child : childNodes) removeEdge(node, child);
113  } // n
114  }
115 }
virtual void setArcGroup(size_t Node1, size_t Node2, byte group)
Assigns an undirected edge (arc) (Node1) – (Node2) to the group group.
void addArc(size_t Node1, size_t Node2, const Mat &pot=EmptyMat)
Adds an additional udirected edge (arc) with specified potentional.
virtual bool isEdgeExists(size_t srcNode, size_t dstNode) const =0
Checks whether the edge exists.
virtual void setEdgeGroup(size_t srcNode, size_t dstNode, byte group)=0
Assigns a directed edge (srcNode) –> (dstNode) to the group group.
virtual void getChildNodes(size_t node, vec_size_t &vNodes) const =0
Returns the set of IDs of the child nodes of the argument node.
virtual void getParentNodes(size_t node, vec_size_t &vNodes) const =0
Returns the set of IDs of the parent nodes of the argument node.
void marginalize(const vec_size_t &nodes)
Marginalizes a set of nodes.
virtual void removeArc(size_t Node1, size_t Node2)
Removes the specified arc.
virtual bool isArcExists(size_t Node1, size_t Node2) const
Checks whether the arc exists.
void addEdge(size_t srcNode, size_t dstNode, const Mat &pot=EmptyMat)
Adds an additional directed edge with specified potentional.
virtual void getEdge(size_t srcNode, size_t dstNode, Mat &pot) const =0
Returns the edge potential.
virtual void setEdge(size_t srcNode, size_t dstNode, const Mat &pot)=0
Sets or changes the potentional of directed edge.
virtual void setArc(size_t Node1, size_t Node2, const Mat &pot)
Sets or changes the potentional of udirected edge (arc)
virtual void removeEdge(size_t srcNode, size_t dstNode)=0
Removes the specified edge.
virtual bool isEdgeArc(size_t srcNode, size_t dstNode) const
Checks whether the edge is a part of an arc.