From 83eff969c5d2e3c1cc34848bdb19d3aa25f9cf07 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Mon, 15 Jul 2024 17:46:26 -0400 Subject: [PATCH] add tie-breaking test --- .../tests/testDiscreteConditional.cpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/gtsam/discrete/tests/testDiscreteConditional.cpp b/gtsam/discrete/tests/testDiscreteConditional.cpp index 2abb67fca..172dd0fa1 100644 --- a/gtsam/discrete/tests/testDiscreteConditional.cpp +++ b/gtsam/discrete/tests/testDiscreteConditional.cpp @@ -290,26 +290,32 @@ TEST(DiscreteConditional, choose) { } /* ************************************************************************* */ -// Check argmax on P(C|D) and P(D) +// Check argmax on P(C|D) and P(D), plus tie-breaking for P(B) TEST(DiscreteConditional, Argmax) { - DiscreteKey C(2, 2), D(4, 2); + DiscreteKey B(2, 2), C(2, 2), D(4, 2); + DiscreteConditional B_prior(D, "1/1"); DiscreteConditional D_prior(D, "1/3"); DiscreteConditional C_given_D((C | D) = "1/4 1/1"); - // Case 1: No parents - size_t actual1 = D_prior.argmax(); - EXPECT_LONGS_EQUAL(1, actual1); + // Case 1: Tie breaking + size_t actual1 = B_prior.argmax(); + // In the case of ties, the first value is chosen. + EXPECT_LONGS_EQUAL(0, actual1); + // Case 2: No parents + size_t actual2 = D_prior.argmax(); + // Selects 1 since it has 0.75 probability + EXPECT_LONGS_EQUAL(1, actual2); - // Case 2: Given parent values + // Case 3: Given parent values DiscreteValues given; given[D.first] = 1; - size_t actual2 = C_given_D.argmax(given); + size_t actual3 = C_given_D.argmax(given); // Should be 0 since D=1 gives 0.5/0.5 - EXPECT_LONGS_EQUAL(0, actual2); + EXPECT_LONGS_EQUAL(0, actual3); given[D.first] = 0; - size_t actual3 = C_given_D.argmax(given); - EXPECT_LONGS_EQUAL(1, actual3); + size_t actual4 = C_given_D.argmax(given); + EXPECT_LONGS_EQUAL(1, actual4); } /* ************************************************************************* */