InferenceΒΆ

Here is an example of inference with a single evidence.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "Variable.h"
#include "BbnNode.h"
#include "Bbn.h"
#include "InferenceController.h"

using namespace com::ooc::bbn;

int main(int argc, char *argv[]) {
  // create vector of nodes
  std::vector<graph::_bbnNode> nodes{
      graph::BbnNode::instance(graph::Variable::instance("0", "a", {"on", "off"}), {0.5, 0.5}),
      graph::BbnNode::instance(graph::Variable::instance("1", "b", {"on", "off"}), {0.5, 0.5, 0.5, 0.5})
  };

  // create vector of edges
  std::vector<graph::_edge> edges{
      graph::Edge::instance("0", "1", graph::EdgeType::DIRECTED)
  };

  // create bbn
  auto bbn = graph::Bbn::instance(nodes, edges);

  // create junction tree
  auto jt = pptc::InferenceController::apply(bbn);

  // set observation
  auto evidence = graph::Evidence::instance(jt->getBbnNode("0"), graph::EvidenceType::OBSERVATION, {{"on", 1.0}});
  jt->setObservation(evidence);

  // gets the posteriors
  auto posteriors = jt->getPosteriors();

  // loop through the posteriors and print them
  for (auto const&[id, map] : posteriors) {
    for (auto const&[value, posterior] : map) {
      std::cout << id << " " << value << " " << posterior << std::endl;
    }
  }
  return 0;
}

When you have multiple evidences, do not pass them one at a time by using the setObservation method. Each call to setObservation will trigger a propagation cycle in the join tree. Instead, pass in a vector of evidences and use the updateEvidence method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "Variable.h"
#include "BbnNode.h"
#include "Bbn.h"
#include "InferenceController.h"

using namespace com::ooc::bbn;

int main(int argc, char *argv[]) {
  // create vector of nodes
  std::vector<graph::_bbnNode> nodes{
      graph::BbnNode::instance(graph::Variable::instance("0", "a", {"on", "off"}), {0.5, 0.5}),
      graph::BbnNode::instance(graph::Variable::instance("1", "b", {"on", "off"}), {0.5, 0.5, 0.5, 0.5})
  };

  // create vector of edges
  std::vector<graph::_edge> edges{
      graph::Edge::instance("0", "1", graph::EdgeType::DIRECTED)
  };

  // create bbn
  auto bbn = graph::Bbn::instance(nodes, edges);

  // create junction tree
  auto jt = pptc::InferenceController::apply(bbn);

  // set multiple observations
  std::vector<graph::_evidence> evidences{
      graph::Evidence::instance(jt->getBbnNode("0"), graph::EvidenceType::OBSERVATION, {{"on", 1.0}}),
      graph::Evidence::instance(jt->getBbnNode("1"), graph::EvidenceType::OBSERVATION, {{"on", 1.0}})
  };
  jt->updateEvidence(evidences);

  // gets the posteriors
  auto posteriors = jt->getPosteriors();

  // loop through the posteriors and print them
  for (auto const&[id, map] : posteriors) {
    for (auto const&[value, posterior] : map) {
      std::cout << id << " " << value << " " << posterior << std::endl;
    }
  }
  return 0;
}