| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |  * @file UGM_small.cpp | 
					
						
							| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  |  * @brief UGM (undirected graphical model) examples: small | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.di.ens.fr/~mschmidt/Software/UGM/small.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  | #include <gtsam/base/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteMarginals.h>
 | 
					
						
							| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char** argv) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // We will assume 2-state variables, where, to conform to the "small" example
 | 
					
						
							|  |  |  |   // we have 0 == "right answer" and 1 == "wrong answer"
 | 
					
						
							|  |  |  |   size_t nrStates = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // define variables
 | 
					
						
							|  |  |  |   DiscreteKey Cathy(1, nrStates), Heather(2, nrStates), Mark(3, nrStates), | 
					
						
							|  |  |  |       Allison(4, nrStates); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create graph
 | 
					
						
							|  |  |  |   DiscreteFactorGraph graph; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add node potentials
 | 
					
						
							|  |  |  |   graph.add(Cathy,   "1 3"); | 
					
						
							|  |  |  |   graph.add(Heather, "9 1"); | 
					
						
							|  |  |  |   graph.add(Mark,    "1 3"); | 
					
						
							|  |  |  |   graph.add(Allison, "9 1"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add edge potentials
 | 
					
						
							|  |  |  |   graph.add(Cathy & Heather, "2 1 1 2"); | 
					
						
							|  |  |  |   graph.add(Heather & Mark,  "2 1 1 2"); | 
					
						
							|  |  |  |   graph.add(Mark & Allison,  "2 1 1 2"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Print the UGM distribution
 | 
					
						
							|  |  |  |   cout << "\nUGM distribution:" << endl; | 
					
						
							| 
									
										
										
										
											2022-01-10 03:46:23 +08:00
										 |  |  |   auto allPosbValues = | 
					
						
							|  |  |  |       DiscreteValues::CartesianProduct(Cathy & Heather & Mark & Allison); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   for (size_t i = 0; i < allPosbValues.size(); ++i) { | 
					
						
							|  |  |  |     DiscreteFactor::Values values = allPosbValues[i]; | 
					
						
							|  |  |  |     double prodPot = graph(values); | 
					
						
							|  |  |  |     cout << values[Cathy.first] << " " << values[Heather.first] << " " | 
					
						
							|  |  |  |         << values[Mark.first] << " " << values[Allison.first] << " :\t" | 
					
						
							|  |  |  |         << prodPot << "\t" << prodPot / 3790 << endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // "Decoding", i.e., configuration with largest value (MPE)
 | 
					
						
							| 
									
										
										
										
											2022-01-22 07:12:38 +08:00
										 |  |  |   // Uses max-product
 | 
					
						
							|  |  |  |   auto optimalDecoding = graph.optimize(); | 
					
						
							| 
									
										
										
										
											2021-11-21 05:34:53 +08:00
										 |  |  |   GTSAM_PRINT(optimalDecoding); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // "Inference" Computing marginals
 | 
					
						
							|  |  |  |   cout << "\nComputing Node Marginals .." << endl; | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |   DiscreteMarginals marginals(graph); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |   Vector margProbs = marginals.marginalProbabilities(Cathy); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   print(margProbs, "Cathy's Node Marginal:"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |   margProbs = marginals.marginalProbabilities(Heather); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   print(margProbs, "Heather's Node Marginal"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |   margProbs = marginals.marginalProbabilities(Mark); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   print(margProbs, "Mark's Node Marginal"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 08:46:12 +08:00
										 |  |  |   margProbs = marginals.marginalProbabilities(Allison); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   print(margProbs, "Allison's Node Marginal"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2012-05-03 13:09:22 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |