2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/**
							 | 
						
					
						
							
								
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * @file    testLoopyBelief.cpp
							 | 
						
					
						
							
								
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * @brief
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * @author Duy-Nguyen Ta
							 | 
						
					
						
							
								
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * @date    Oct 11, 2013
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <CppUnitLite/TestHarness.h>
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <gtsam/discrete/DecisionTreeFactor.h>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <gtsam/discrete/DiscreteFactorGraph.h>
							 | 
						
					
						
							
								
									
										
										
										
											2022-01-10 04:59:40 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <gtsam/discrete/DiscreteConditional.h>
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <gtsam/inference/VariableIndex.h>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <fstream>
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <iostream>
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								using namespace std;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								using namespace boost;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								using namespace gtsam;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								/**
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * Loopy belief solver for graphs with only binary and unary factors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class LoopyBelief {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /** Star graph struct for each node, containing
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * - the star graph itself
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   * - the product of original unary factors so we don't have to recompute it
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * later, and
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * - the factor indices of the corrected belief factors of the neighboring
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * nodes
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   */
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  typedef std::map<Key, size_t> CorrectedBeliefIndices;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  struct StarGraph {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    DiscreteFactorGraph::shared_ptr star;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    CorrectedBeliefIndices correctedBeliefIndices;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    DecisionTreeFactor::shared_ptr unary;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    VariableIndex varIndex_;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    StarGraph(const DiscreteFactorGraph::shared_ptr& _star,
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								              const CorrectedBeliefIndices& _beliefIndices,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								              const DecisionTreeFactor::shared_ptr& _unary)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        : star(_star),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          correctedBeliefIndices(_beliefIndices),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          unary(_unary),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          varIndex_(*_star) {}
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    void print(const std::string& s = "") const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      cout << s << ":" << endl;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      star->print("Star graph: ");
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (const auto& [key, _] : correctedBeliefIndices) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cout << "Belief factor index for " << key << ": "
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								             << correctedBeliefIndices.at(key) << endl;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (unary) unary->print("Unary: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  typedef std::map<Key, StarGraph> StarGraphs;
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  StarGraphs starGraphs_;  ///< star graph at each variable
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 public:
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /** Constructor
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   * Need all discrete keys to access node's cardinality for creating belief
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * factors
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								   * TODO: so troublesome!!
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  LoopyBelief(const DiscreteFactorGraph& graph,
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								              const std::map<Key, DiscreteKey>& allDiscreteKeys)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      : starGraphs_(buildStarGraphs(graph, allDiscreteKeys)) {}
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  /// print
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  void print(const std::string& s = "") const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cout << s << ":" << endl;
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    for (const auto& [key, _] : starGraphs_) {
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      starGraphs_.at(key).print("Node " + std::to_string(key) + ":");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /// One step of belief propagation
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  DiscreteFactorGraph::shared_ptr iterate(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const std::map<Key, DiscreteKey>& allDiscreteKeys) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    static const bool debug = false;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    DiscreteFactorGraph::shared_ptr beliefs(new DiscreteFactorGraph());
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    std::map<Key, std::map<Key, DiscreteFactor::shared_ptr> > allMessages;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // Eliminate each star graph
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    for (const auto& [key, _] : starGraphs_) {
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      //      cout << "***** Node " << key << "*****" << endl;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // initialize belief to the unary factor from the original graph
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      DecisionTreeFactor::shared_ptr beliefAtKey;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // keep intermediate messages to divide later
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      std::map<Key, DiscreteFactor::shared_ptr> messages;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // eliminate each neighbor in this star graph one by one
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (const auto& [neighbor, _] : starGraphs_.at(key).correctedBeliefIndices) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        DiscreteFactorGraph subGraph;
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for (size_t factor : starGraphs_.at(key).varIndex_[neighbor]) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          subGraph.push_back(starGraphs_.at(key).star->at(factor));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (debug) subGraph.print("------- Subgraph:");
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-05 01:42:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        const auto [dummyCond, message] =
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-08 15:59:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            EliminateDiscrete(subGraph, Ordering{neighbor});
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // store the new factor into messages
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        messages.insert(make_pair(neighbor, message));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (debug) message->print("------- Message: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // Belief is the product of all messages and the unary factor
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // Incorporate new the factor to belief
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (!beliefAtKey)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          beliefAtKey =
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:39:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								              std::dynamic_pointer_cast<DecisionTreeFactor>(message);
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        else
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          beliefAtKey = std::make_shared<DecisionTreeFactor>(
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								              (*beliefAtKey) *
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:39:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								              (*std::dynamic_pointer_cast<DecisionTreeFactor>(message)));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (starGraphs_.at(key).unary)
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        beliefAtKey = std::make_shared<DecisionTreeFactor>(
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            (*beliefAtKey) * (*starGraphs_.at(key).unary));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (debug) beliefAtKey->print("New belief at key: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // normalize belief
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      double sum = 0.0;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (size_t v = 0; v < allDiscreteKeys.at(key).second; ++v) {
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        DiscreteValues val;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        val[key] = v;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        sum += (*beliefAtKey)(val);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // TODO(kartikarcot): Check if this makes sense
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      string sumFactorTable;
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (size_t v = 0; v < allDiscreteKeys.at(key).second; ++v) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        sumFactorTable = sumFactorTable + " " + std::to_string(sum);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      DecisionTreeFactor sumFactor(allDiscreteKeys.at(key), sumFactorTable);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      if (debug) sumFactor.print("denomFactor: ");
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      beliefAtKey =
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          std::make_shared<DecisionTreeFactor>((*beliefAtKey) / sumFactor);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (debug) beliefAtKey->print("New belief at key normalized: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      beliefs->push_back(beliefAtKey);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      allMessages[key] = messages;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // Update corrected beliefs
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    VariableIndex beliefFactors(*beliefs);
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    for (const auto& [key, _] : starGraphs_) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      std::map<Key, DiscreteFactor::shared_ptr> messages = allMessages[key];
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (const auto& [neighbor, _] : starGraphs_.at(key).correctedBeliefIndices) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								              DecisionTreeFactor correctedBelief =
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:39:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            (*std::dynamic_pointer_cast<DecisionTreeFactor>(
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                beliefs->at(beliefFactors[key].front()))) /
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:39:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            (*std::dynamic_pointer_cast<DecisionTreeFactor>(
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                messages.at(neighbor)));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if (debug) correctedBelief.print("correctedBelief: ");
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        size_t beliefIndex =
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            starGraphs_.at(neighbor).correctedBeliefIndices.at(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        starGraphs_.at(neighbor).star->replace(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            beliefIndex,
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            std::make_shared<DecisionTreeFactor>(correctedBelief));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (debug) print("After update: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return beliefs;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 private:
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /**
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   * Build star graphs for each node.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								   */
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  StarGraphs buildStarGraphs(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const DiscreteFactorGraph& graph,
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const std::map<Key, DiscreteKey>& allDiscreteKeys) const {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    StarGraphs starGraphs;
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    VariableIndex varIndex(graph);  ///< access to all factors of each node
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-20 07:58:17 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    for (const auto& [key, _] : varIndex) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // initialize to multiply with other unary factors later
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      DecisionTreeFactor::shared_ptr prodOfUnaries;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // collect all factors involving this key in the original graph
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      DiscreteFactorGraph::shared_ptr star(new DiscreteFactorGraph());
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (size_t factorIndex : varIndex[key]) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-09 06:29:31 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        star->push_back(graph.at(factorIndex));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // accumulate unary factors
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-09 06:29:31 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (graph.at(factorIndex)->size() == 1) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          if (!prodOfUnaries)
							 | 
						
					
						
							
								
									
										
										
										
											2023-10-07 00:34:49 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            prodOfUnaries = graph.at<DecisionTreeFactor>(factorIndex);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          else
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            prodOfUnaries = std::make_shared<DecisionTreeFactor>(
							 | 
						
					
						
							
								
									
										
										
										
											2023-10-07 00:34:49 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                *prodOfUnaries * (*graph.at<DecisionTreeFactor>(factorIndex)));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // add the belief factor for each neighbor variable to this star graph
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // also record the factor index for later modification
							 | 
						
					
						
							
								
									
										
										
										
											2015-06-21 09:38:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      KeySet neighbors = star->keys();
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      neighbors.erase(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      CorrectedBeliefIndices correctedBeliefIndices;
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      for (Key neighbor : neighbors) {
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // TODO: default table for keys with more than 2 values?
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        string initialBelief;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for (size_t v = 0; v < allDiscreteKeys.at(neighbor).second - 1; ++v) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          initialBelief = initialBelief + "0.0 ";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        initialBelief = initialBelief + "1.0";
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        star->push_back(
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            DecisionTreeFactor(allDiscreteKeys.at(neighbor), initialBelief));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        correctedBeliefIndices.insert(make_pair(neighbor, star->size() - 1));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      starGraphs.insert(make_pair(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          key, StarGraph(star, correctedBeliefIndices, prodOfUnaries)));
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return starGraphs;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* ************************************************************************* */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								TEST_UNSAFE(LoopyBelief, construction) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Variables: Cloudy, Sprinkler, Rain, Wet
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  DiscreteKey C(0, 2), S(1, 2), R(2, 2), W(3, 2);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Map from key to DiscreteKey for building belief factor.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // TODO: this is bad!
							 | 
						
					
						
							
								
									
										
										
										
											2023-01-08 15:59:39 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  std::map<Key, DiscreteKey> allKeys{{0, C}, {1, S}, {2, R}, {3, W}};
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Build graph
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  DecisionTreeFactor pC(C, "0.5 0.5");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  DiscreteConditional pSC(S | C = "0.5/0.5 0.9/0.1");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  DiscreteConditional pRC(R | C = "0.8/0.2 0.2/0.8");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-16 02:25:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  DecisionTreeFactor pSR(S & R, "0.0 0.9 0.9 0.99");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  DiscreteFactorGraph graph;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  graph.push_back(pC);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  graph.push_back(pSC);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  graph.push_back(pRC);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  graph.push_back(pSR);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  graph.print("graph: ");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  LoopyBelief solver(graph, allKeys);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  solver.print("Loopy belief: ");
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Main loop
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  for (size_t iter = 0; iter < 20; ++iter) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    cout << "==================================" << endl;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cout << "iteration: " << iter << endl;
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-13 04:06:02 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    DiscreteFactorGraph::shared_ptr beliefs = solver.iterate(allKeys);
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 23:27:37 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    beliefs->print();
							 | 
						
					
						
							
								
									
										
										
										
											2013-10-12 13:49:11 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* ************************************************************************* */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								int main() {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  TestResult tr;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return TestRegistry::runAllTests(tr);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* ************************************************************************* */
							 |