32 using namespace gtsam;
49 CHECK(bayesNet.
at(0)->asDiscrete());
81 auto fg = bn.toFactorGraph(
vv);
85 std::vector<double>
ratio(2);
86 for (
size_t mode : {0, 1}) {
96 const auto continuousConditional = GaussianConditional::sharedMeanAndStddev(
97 X(0), 2 * I_1x1,
X(1),
Vector1(-4.0), 5.0);
102 const auto conditional0 = std::make_shared<GaussianConditional>(
103 X(1), Vector1::Constant(5), I_1x1, model0),
104 conditional1 = std::make_shared<GaussianConditional>(
105 X(1), Vector1::Constant(2), I_1x1,
model1);
109 bayesNet.
push_back(continuousConditional);
110 bayesNet.emplace_back(
120 const double conditionalProbability =
121 continuousConditional->evaluate(values.
continuous());
122 const double mixtureProbability = conditional0->evaluate(values.
continuous());
124 bayesNet.evaluate(values), 1
e-9);
134 const auto [hybridBayesNet, remainingFactorGraph] =
138 assignment[
M(0)] = 1;
139 assignment[
M(1)] = 1;
140 assignment[
M(2)] = 0;
163 const auto [hybridBayesNet, remainingFactorGraph] =
167 assignment[
M(0)] = 1;
168 assignment[
M(1)] = 1;
169 assignment[
M(2)] = 1;
177 expected_delta.
insert(make_pair(
X(0), -Vector1::Ones()));
178 expected_delta.
insert(make_pair(
X(1), -Vector1::Ones()));
179 expected_delta.
insert(make_pair(
X(2), -Vector1::Ones()));
180 expected_delta.
insert(make_pair(
X(3), -Vector1::Ones()));
188 Switching s(4, 1.0, 0.1, {0, 1, 2, 3},
"1/1 1/1");
191 s.linearizedFactorGraph.eliminateSequential();
197 expectedAssignment[
M(0)] = 1;
198 expectedAssignment[
M(1)] = 1;
199 expectedAssignment[
M(2)] = 1;
203 expectedValues.
insert(
X(0), -Vector1::Ones());
204 expectedValues.
insert(
X(1), -Vector1::Ones());
205 expectedValues.
insert(
X(2), -Vector1::Ones());
206 expectedValues.
insert(
X(3), -Vector1::Ones());
221 auto actualTree = posterior->evaluate(delta.continuous());
224 std::vector<DiscreteKey> discrete_keys = {{
M(0), 2}, {
M(1), 2}};
225 std::vector<double> leaves = {6.1112424, 20.346113, 17.785849, 19.738098};
230 auto prunedBayesNet = posterior->prune(2);
231 auto prunedTree = prunedBayesNet.evaluate(delta.continuous());
234 std::vector<double> pruned_leaves = {0.0, 20.346113, 0.0, 19.738098};
241 double logProbability = 0;
242 logProbability += posterior->
at(0)->asMixture()->logProbability(hybridValues);
243 logProbability += posterior->at(1)->asMixture()->logProbability(hybridValues);
244 logProbability += posterior->at(2)->asMixture()->logProbability(hybridValues);
247 posterior->at(3)->asDiscrete()->logProbability(hybridValues);
249 posterior->at(4)->asDiscrete()->logProbability(hybridValues);
269 auto prunedBayesNet = posterior->prune(2);
285 size_t maxNrLeaves = 3;
286 auto discreteConditionals = posterior->discreteConditionals();
288 std::make_shared<DecisionTreeFactor>(
289 discreteConditionals->prune(maxNrLeaves));
292 prunedDecisionTree->nrLeaves());
294 auto original_discrete_conditionals = *(posterior->at(4)->asDiscrete());
297 posterior->prune(maxNrLeaves);
301 double probability) ->
double {
304 if (prunedDecisionTree->operator()(choices) == 0) {
314 auto pruned_discrete_conditionals = posterior->at(4)->asDiscrete();
315 auto discrete_conditional_tree =
317 pruned_discrete_conditionals);
320 discrete_conditional_tree->
apply(checker);
328 auto noise_model = noiseModel::Diagonal::Sigmas(
Vector1(1.0));
330 std::make_shared<BetweenFactor<double>>(
X(0),
X(1), 0, noise_model);
332 std::make_shared<BetweenFactor<double>>(
X(0),
X(1), 1, noise_model);
333 std::vector<NonlinearFactor::shared_ptr>
factors = {zero_motion, one_motion};
342 double z0 = 0.0,
z1 = 1.0;
343 initial.insert<
double>(
X(0), z0);
344 initial.insert<
double>(
X(1),
z1);
352 std::mt19937_64 gen(11);
355 vector<double> discrete_samples;
358 size_t num_samples = 1000;
359 for (
size_t i = 0;
i < num_samples;
i++) {
363 discrete_samples.push_back(sample.
discrete().at(
M(0)));
Matrix< RealScalar, Dynamic, Dynamic > M
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
static int runAllTests(TestResult &result)
Factor Graph consisting of non-linear factors.
Eigen::Matrix< double, 1, 1 > Vector1
bool equals(const This &fg, double tol=1e-9) const
GTSAM-style equals.
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
std::pair< std::shared_ptr< BayesNetType >, std::shared_ptr< FactorGraphType > > eliminatePartialSequential(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
const GaussianFactorGraph factors
static const GaussianBayesNet gbn
double evaluate(const HybridValues &values) const
Evaluate hybrid probability density for given HybridValues.
iterator insert(const std::pair< Key, Vector > &key_value)
const VectorValues & continuous() const
Return the multi-dimensional vector values.
static enum @1107 ordering
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
noiseModel::Isotropic::shared_ptr model1
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
A Bayes net of Gaussian Conditionals indexed by discrete keys.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size ratio
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
static const VectorValues vv
HybridGaussianFactorGraph linearizedFactorGraph
TEST(HybridBayesNet, Creation)
A conditional of gaussian mixtures indexed by discrete variables, as part of a Bayes Network...
Hybrid Bayes Tree, the result of eliminating a HybridJunctionTree.
#define EXPECT(condition)
static const DiscreteKey Asia(asiaKey, 2)
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
std::shared_ptr< This > shared_ptr
shared_ptr to This
void push_back(std::shared_ptr< HybridConditional > conditional)
Add a hybrid conditional using a shared_ptr.
DecisionTree apply(const Unary &op) const
void emplace_back(Conditional *conditional)
noiseModel::Diagonal::shared_ptr SharedDiagonal
std::shared_ptr< DecisionTreeFactor > shared_ptr
Values linearizationPoint
#define EXPECT_LONGS_EQUAL(expected, actual)
const sharedFactor at(size_t i) const
const DiscreteValues & discrete() const
Return the discrete values.
std::pair< Key, size_t > DiscreteKey
std::shared_ptr< HybridGaussianFactorGraph > linearize(const Values &continuousValues) const
Linearize all the continuous factors in the HybridNonlinearFactorGraph.
void insert(Key j, const Vector &value)
HybridBayesNet createHybridBayesNet(size_t num_measurements=1, bool manyModes=false)
static const DiscreteKey mode(modeKey, 2)
Implementation of a discrete conditional mixture factor.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
std::uint64_t Key
Integer nonlinear key type.
std::shared_ptr< HybridBayesNet > shared_ptr
DiscreteKeys is a set of keys that can be assembled using the & operator.