Direct Graphical Models  v.1.7.0
InferExact.cpp
1 #include "InferExact.h"
2 #include "GraphPairwise.h"
3 #include <numeric>
4 
5 namespace DirectGraphicalModels
6 {
7  void CInferExact::infer(unsigned int)
8  {
9  size_t nNodes = CInfer::getGraph().getNumNodes();
10  byte nStates = CInfer::getGraph().getNumStates();
11  vec_byte_t state(nNodes);
12 
13  // Calculating the potentials for every possible configuration
14  vec_float_t P = calculatePotentials();
15 
16  // Calculating the partition function
17  float Z = std::accumulate(P.cbegin(), P.cend(), 0.0f);
18 
19  // Filling node potentials with marginal probabilities
20  for (size_t n = 0; n < nNodes; n++)
21  CInfer::getGraph().setNode(n, Mat::zeros(nStates, 1, CV_32FC1));
22  setState(state, 0);
23  Mat nPot;
24  for(float &p : P) {
25  for (size_t n = 0; n < nNodes; n++) {
26  CInfer::getGraph().getNode(n, nPot);
27  nPot.at<float>(state[n], 0) += p / Z;
28  CInfer::getGraph().setNode(n, nPot);
29  }
30  incState(state);
31  }
32 
33  // Old implementation wich directly operates with the private member-variables of the CGraphPairwise class
34 /* for (ptr_node_t &node : getGraphPairwise().m_vNodes)
35  node->Pot.setTo(0);
36  setState(state, 0);
37  for(float &p : P) {
38  for (ptr_node_t &node: getGraphPairwise().m_vNodes)
39  node->Pot.at<float>(state[node->id], 0) += p / Z;
40  incState(state);
41  }*/
42  }
43 }
virtual void setNode(size_t node, const Mat &pot)=0
Sets or changes the potential of node.
byte getNumStates(void) const
Returns number of states (classes)
Definition: Graph.h:99
vec_float_t calculatePotentials(void) const
Calculates potentials for all possible configurations.
Definition: DecodeExact.cpp:58
virtual size_t getNumNodes(void) const =0
Returns the number of nodes in the graph.
void incState(vec_byte_t &state) const
Increases the state by one, i.e. switches the state array to the consequent configuration.
Definition: DecodeExact.cpp:49
virtual void getNode(size_t node, Mat &pot) const =0
Returns the node potential.
CGraph & getGraph(void) const
Returns the reference to the graph.
Definition: Infer.h:82
void setState(vec_byte_t &state, qword configuration) const
Sets the state according to the configuration index configuration.
Definition: DecodeExact.cpp:39
virtual void infer(unsigned int nIt=0)
Exact inference.
Definition: InferExact.cpp:7