tests/testDCSAM.cpp
TEST(TestSuite, discrete_prior_factor) {
  // Make an empty discrete factor graph
  gtsam::DiscreteFactorGraph dfg;

  // We'll make a variable with 2 possible assignments
  const size_t cardinality = 2;
  gtsam::DiscreteKey dk(gtsam::Symbol('d', 1), cardinality);
  const std::vector<double> probs{0.1, 0.9};

  // Make a discrete prior factor and add it to the graph
  DiscretePriorFactor dpf(dk, probs);
  dfg.push_back(dpf);

  // Solve
  gtsam::DiscreteFactor::sharedValues mostProbableEstimate = dfg.optimize();

  // Get the most probable estimate
  size_t mpeD = (*mostProbableEstimate).at(dk.first);

  // Get the marginals
  gtsam::DiscreteMarginals discreteMarginals(dfg);
  gtsam::Vector margProbs = discreteMarginals.marginalProbabilities(dk);

  // Verify that each marginal probability is within `tol` of the true marginal
  for (size_t i = 0; i < dk.second; i++) {
    bool margWithinTol = (abs(margProbs[i] - probs[i]) < tol);
    EXPECT_EQ(margWithinTol, true);
  }

  // Ensure that the most probable estimate is correct
  EXPECT_EQ(mpeD, 1);
}
/*
 * Test simple DiscretePriorFactor. We construct a gtsam::DiscreteFactor factor
 * graph with a single binary variable d1 and a single custom
 * DiscretePriorFactor p(d1) specifying the distribution:
 * p(d1 = 0) = 0.1, p(d1 = 1) = 0.9.
 *
 * After constructing the factor graph, we validate that the marginals are
 * correct (i.e. they match the input p(d1)) and the most probable estimate
 * (computed by maximizing p(d1) over possible values of d1) is 1.
 */


After constructing the factor graph, we validate that the marginals are
correct ) and the most probable estimate
(computed by maximizing p(d1) over possible values of d1) is 1.
After constructing the factor graph we validate that the marginals are correct ) and the most probable estimate ( computed by maximizing p ( d1 ) over possible values of d1 ) is 1 .
TEST(TestSuite, discrete_prior_factor) {
  gtsam::DiscreteFactorGraph dfg;
  const size_t cardinality = 2;
  gtsam::DiscreteKey dk(gtsam::Symbol('d', 1), cardinality);
  const std::vector<double> probs{0.1, 0.9};
  DiscretePriorFactor dpf(dk, probs);
  dfg.push_back(dpf);
  gtsam::DiscreteFactor::sharedValues mostProbableEstimate = dfg.optimize();
  size_t mpeD = (*mostProbableEstimate).at(dk.first);
  gtsam::DiscreteMarginals discreteMarginals(dfg);
  gtsam::Vector margProbs = discreteMarginals.marginalProbabilities(dk);
  for (size_t i = 0; i < dk.second; i++) {
    bool margWithinTol = (abs(margProbs[i] - probs[i]) < tol);
    EXPECT_EQ(margWithinTol, true);
  }
  EXPECT_EQ(mpeD, 1);
}
TEST ( TestSuite , discrete_prior_factor ) { gtsam :: DiscreteFactorGraph dfg ; const size_t cardinality = 2 ; gtsam :: DiscreteKey dk ( gtsam :: Symbol ( ' ' , 1 ) , cardinality ) ; const std :: vector < double > probs { 0.1 , 0.9 } ; DiscretePriorFactor dpf ( dk , probs ) ; dfg . push_back ( dpf ) ; gtsam :: DiscreteFactor :: sharedValues mostProbableEstimate = dfg . optimize ( ) ; size_t mpeD = ( * mostProbableEstimate ) . at ( dk . first ) ; gtsam :: DiscreteMarginals discreteMarginals ( dfg ) ; gtsam :: Vector margProbs = discreteMarginals . marginalProbabilities ( dk ) ; for ( size_t i = 0 ; i < dk . second ; i ++ ) { bool margWithinTol = ( abs ( margProbs [ i ] - probs [ i ] ) < tol ) ; EXPECT_EQ ( margWithinTol , true ) ; } EXPECT_EQ ( mpeD , 1 ) ; }
Test simple DiscretePriorFactor.
Test simple DiscretePriorFactor .
// Make an empty discrete factor graph // We'll make a variable with 2 possible assignments // Make a discrete prior factor and add it to the graph // Solve // Get the most probable estimate // Get the marginals // Verify that each marginal probability is within `tol` of the true marginal // Ensure that the most probable estimate is correct