| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * testCSP.cpp | 
					
						
							|  |  |  |  * @brief develop code for CSP solver | 
					
						
							|  |  |  |  * @date Feb 5, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 08:47:19 +08:00
										 |  |  | #include <gtsam_unstable/discrete/CSP.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/discrete/Domain.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | #include <fstream>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | TEST(CSP, SingleValue) { | 
					
						
							|  |  |  |   // Create keys for Idaho, Arizona, and Utah, allowing two colors for each:
 | 
					
						
							|  |  |  |   size_t nrColors = 3; | 
					
						
							|  |  |  |   DiscreteKey ID(0, nrColors), AZ(1, nrColors), UT(2, nrColors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check that a single value is equal to a decision stump with only one "1":
 | 
					
						
							|  |  |  |   SingleValue singleValue(AZ, 2); | 
					
						
							|  |  |  |   DecisionTreeFactor f1(AZ, "0 0 1"); | 
					
						
							|  |  |  |   EXPECT(assert_equal(f1, singleValue.toDecisionTreeFactor())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   // Create domains
 | 
					
						
							|  |  |  |   Domains domains; | 
					
						
							|  |  |  |   domains.emplace(0, Domain(ID)); | 
					
						
							|  |  |  |   domains.emplace(1, Domain(AZ)); | 
					
						
							|  |  |  |   domains.emplace(2, Domain(UT)); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Ensure arc-consistency: just wipes out values in AZ domain:
 | 
					
						
							|  |  |  |   EXPECT(singleValue.ensureArcConsistency(1, &domains)); | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   LONGS_EQUAL(3, domains.at(0).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(1, domains.at(1).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(3, domains.at(2).nrValues()); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(CSP, BinaryAllDif) { | 
					
						
							|  |  |  |   // Create keys for Idaho, Arizona, and Utah, allowing 2 colors for each:
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   size_t nrColors = 2; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   DiscreteKey ID(0, nrColors), AZ(1, nrColors), UT(2, nrColors); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check construction and conversion
 | 
					
						
							|  |  |  |   BinaryAllDiff c1(ID, UT); | 
					
						
							|  |  |  |   DecisionTreeFactor f1(ID & UT, "0 1 1 0"); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f1, c1.toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check construction and conversion
 | 
					
						
							|  |  |  |   BinaryAllDiff c2(UT, AZ); | 
					
						
							|  |  |  |   DecisionTreeFactor f2(UT & AZ, "0 1 1 0"); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f2, c2.toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // Check multiplication of factors with constraint:
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   DecisionTreeFactor f3 = f1 * f2; | 
					
						
							|  |  |  |   EXPECT(assert_equal(f3, c1 * f2)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(f3, c2 * f1)); | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | TEST(CSP, AllDiff) { | 
					
						
							|  |  |  |   // Create keys for Idaho, Arizona, and Utah, allowing two colors for each:
 | 
					
						
							|  |  |  |   size_t nrColors = 3; | 
					
						
							|  |  |  |   DiscreteKey ID(0, nrColors), AZ(1, nrColors), UT(2, nrColors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check construction and conversion
 | 
					
						
							|  |  |  |   vector<DiscreteKey> dkeys{ID, UT, AZ}; | 
					
						
							|  |  |  |   AllDiff alldiff(dkeys); | 
					
						
							|  |  |  |   DecisionTreeFactor actual = alldiff.toDecisionTreeFactor(); | 
					
						
							|  |  |  |   // GTSAM_PRINT(actual);
 | 
					
						
							|  |  |  |   actual.dot("actual"); | 
					
						
							|  |  |  |   DecisionTreeFactor f2( | 
					
						
							|  |  |  |       ID & AZ & UT, | 
					
						
							|  |  |  |       "0 0 0  0 0 1  0 1 0   0 0 1  0 0 0  1 0 0   0 1 0  1 0 0  0 0 0"); | 
					
						
							|  |  |  |   EXPECT(assert_equal(f2, actual)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create domains.
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   Domains domains; | 
					
						
							|  |  |  |   domains.emplace(0, Domain(ID)); | 
					
						
							|  |  |  |   domains.emplace(1, Domain(AZ)); | 
					
						
							|  |  |  |   domains.emplace(2, Domain(UT)); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // First constrict AZ domain:
 | 
					
						
							|  |  |  |   SingleValue singleValue(AZ, 2); | 
					
						
							|  |  |  |   EXPECT(singleValue.ensureArcConsistency(1, &domains)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Arc-consistency
 | 
					
						
							|  |  |  |   EXPECT(alldiff.ensureArcConsistency(0, &domains)); | 
					
						
							|  |  |  |   EXPECT(!alldiff.ensureArcConsistency(1, &domains)); | 
					
						
							|  |  |  |   EXPECT(alldiff.ensureArcConsistency(2, &domains)); | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   LONGS_EQUAL(2, domains.at(0).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(1, domains.at(1).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(2, domains.at(2).nrValues()); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(CSP, allInOne) { | 
					
						
							|  |  |  |   // Create keys for Idaho, Arizona, and Utah, allowing 3 colors for each:
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   size_t nrColors = 2; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   DiscreteKey ID(0, nrColors), AZ(1, nrColors), UT(2, nrColors); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the CSP
 | 
					
						
							|  |  |  |   CSP csp; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   csp.addAllDiff(ID, UT); | 
					
						
							|  |  |  |   csp.addAllDiff(UT, AZ); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check an invalid combination, with ID==UT==AZ all same color
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   DiscreteValues invalid; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   invalid[ID.first] = 0; | 
					
						
							|  |  |  |   invalid[UT.first] = 0; | 
					
						
							|  |  |  |   invalid[AZ.first] = 0; | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(0, csp(invalid), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check a valid combination
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   DiscreteValues valid; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   valid[ID.first] = 0; | 
					
						
							|  |  |  |   valid[UT.first] = 1; | 
					
						
							|  |  |  |   valid[AZ.first] = 0; | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(1, csp(valid), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Just for fun, create the product and check it
 | 
					
						
							|  |  |  |   DecisionTreeFactor product = csp.product(); | 
					
						
							|  |  |  |   // product.dot("product");
 | 
					
						
							|  |  |  |   DecisionTreeFactor expectedProduct(ID & AZ & UT, "0 1 0 0 0 0 1 0"); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(expectedProduct, product)); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Solve
 | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  |   auto mpe = csp.optimize(); | 
					
						
							| 
									
										
										
										
											2022-12-31 14:53:10 +08:00
										 |  |  |   DiscreteValues expected {{ID.first, 1}, {UT.first, 0}, {AZ.first, 1}}; | 
					
						
							| 
									
										
										
										
											2021-11-21 05:15:05 +08:00
										 |  |  |   EXPECT(assert_equal(expected, mpe)); | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | TEST(CSP, WesternUS) { | 
					
						
							|  |  |  |   // Create keys for all states in Western US, with 4 color possibilities.
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   size_t nrColors = 4; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   DiscreteKey WA(0, nrColors), OR(3, nrColors), CA(1, nrColors), | 
					
						
							|  |  |  |       NV(2, nrColors), ID(8, nrColors), UT(9, nrColors), AZ(10, nrColors), | 
					
						
							|  |  |  |       MT(4, nrColors), WY(5, nrColors), CO(7, nrColors), NM(6, nrColors); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the CSP
 | 
					
						
							|  |  |  |   CSP csp; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   csp.addAllDiff(WA, ID); | 
					
						
							|  |  |  |   csp.addAllDiff(WA, OR); | 
					
						
							|  |  |  |   csp.addAllDiff(OR, ID); | 
					
						
							|  |  |  |   csp.addAllDiff(OR, CA); | 
					
						
							|  |  |  |   csp.addAllDiff(OR, NV); | 
					
						
							|  |  |  |   csp.addAllDiff(CA, NV); | 
					
						
							|  |  |  |   csp.addAllDiff(CA, AZ); | 
					
						
							|  |  |  |   csp.addAllDiff(ID, MT); | 
					
						
							|  |  |  |   csp.addAllDiff(ID, WY); | 
					
						
							|  |  |  |   csp.addAllDiff(ID, UT); | 
					
						
							|  |  |  |   csp.addAllDiff(ID, NV); | 
					
						
							|  |  |  |   csp.addAllDiff(NV, UT); | 
					
						
							|  |  |  |   csp.addAllDiff(NV, AZ); | 
					
						
							|  |  |  |   csp.addAllDiff(UT, WY); | 
					
						
							|  |  |  |   csp.addAllDiff(UT, CO); | 
					
						
							|  |  |  |   csp.addAllDiff(UT, NM); | 
					
						
							|  |  |  |   csp.addAllDiff(UT, AZ); | 
					
						
							|  |  |  |   csp.addAllDiff(AZ, CO); | 
					
						
							|  |  |  |   csp.addAllDiff(AZ, NM); | 
					
						
							|  |  |  |   csp.addAllDiff(MT, WY); | 
					
						
							|  |  |  |   csp.addAllDiff(WY, CO); | 
					
						
							|  |  |  |   csp.addAllDiff(CO, NM); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-31 14:53:10 +08:00
										 |  |  |   DiscreteValues mpe{{0, 2}, {1, 3}, {2, 2}, {3, 1}, {4, 1}, {5, 3}, | 
					
						
							|  |  |  |                      {6, 3}, {7, 2}, {8, 0}, {9, 1}, {10, 0}}; | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // Create ordering according to example in ND-CSP.lyx
 | 
					
						
							| 
									
										
										
										
											2013-10-12 01:48:44 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   ordering += Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7), | 
					
						
							|  |  |  |       Key(8), Key(9), Key(10); | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // Solve using that ordering:
 | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  |   auto actualMPE = csp.optimize(ordering); | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  |   EXPECT(assert_equal(mpe, actualMPE)); | 
					
						
							| 
									
										
										
										
											2021-11-21 05:15:05 +08:00
										 |  |  |   EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Write out the dual graph for hmetis
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | #ifdef DUAL
 | 
					
						
							| 
									
										
										
										
											2013-07-30 07:55:40 +08:00
										 |  |  |   VariableIndexOrdered index(csp); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   index.print("index"); | 
					
						
							|  |  |  |   ofstream os("/Users/dellaert/src/hmetis-1.5-osx-i686/US-West-dual.txt"); | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  |   index.outputMetisFormat(os); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | TEST(CSP, ArcConsistency) { | 
					
						
							|  |  |  |   // Create keys for Idaho, Arizona, and Utah, allowing three colors for each:
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   size_t nrColors = 3; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   DiscreteKey ID(0, nrColors), AZ(1, nrColors), UT(2, nrColors); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // Create the CSP using just one all-diff constraint, plus constrain Arizona.
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   CSP csp; | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   vector<DiscreteKey> dkeys{ID, UT, AZ}; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   csp.addAllDiff(dkeys); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   csp.addSingleValue(AZ, 2); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // GTSAM_PRINT(csp);
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check an invalid combination, with ID==UT==AZ all same color
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   DiscreteValues invalid; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   invalid[ID.first] = 0; | 
					
						
							|  |  |  |   invalid[UT.first] = 1; | 
					
						
							|  |  |  |   invalid[AZ.first] = 0; | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(0, csp(invalid), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check a valid combination
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   DiscreteValues valid; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   valid[ID.first] = 0; | 
					
						
							|  |  |  |   valid[UT.first] = 1; | 
					
						
							|  |  |  |   valid[AZ.first] = 2; | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(1, csp(valid), 1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Solve
 | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  |   auto mpe = csp.optimize(); | 
					
						
							| 
									
										
										
										
											2022-12-31 14:53:10 +08:00
										 |  |  |   DiscreteValues expected {{ID.first, 1}, {UT.first, 0}, {AZ.first, 2}}; | 
					
						
							| 
									
										
										
										
											2021-11-21 05:15:05 +08:00
										 |  |  |   EXPECT(assert_equal(expected, mpe)); | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // ensure arc-consistency, i.e., narrow domains...
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   Domains domains; | 
					
						
							|  |  |  |   domains.emplace(0, Domain(ID)); | 
					
						
							|  |  |  |   domains.emplace(1, Domain(AZ)); | 
					
						
							|  |  |  |   domains.emplace(2, Domain(UT)); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   SingleValue singleValue(AZ, 2); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   AllDiff alldiff(dkeys); | 
					
						
							|  |  |  |   EXPECT(singleValue.ensureArcConsistency(1, &domains)); | 
					
						
							|  |  |  |   EXPECT(alldiff.ensureArcConsistency(0, &domains)); | 
					
						
							|  |  |  |   EXPECT(!alldiff.ensureArcConsistency(1, &domains)); | 
					
						
							|  |  |  |   EXPECT(alldiff.ensureArcConsistency(2, &domains)); | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   LONGS_EQUAL(2, domains.at(0).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(1, domains.at(1).nrValues()); | 
					
						
							|  |  |  |   LONGS_EQUAL(2, domains.at(2).nrValues()); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Parial application, version 1
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   DiscreteValues known; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   known[AZ.first] = 2; | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr reduced1 = alldiff.partiallyApply(known); | 
					
						
							|  |  |  |   DecisionTreeFactor f3(ID & UT, "0 1 1  1 0 1  1 1 0"); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f3, reduced1->toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   DiscreteFactor::shared_ptr reduced2 = singleValue.partiallyApply(known); | 
					
						
							|  |  |  |   DecisionTreeFactor f4(AZ, "0 0 1"); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f4, reduced2->toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Parial application, version 2
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr reduced3 = alldiff.partiallyApply(domains); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f3, reduced3->toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   DiscreteFactor::shared_ptr reduced4 = singleValue.partiallyApply(domains); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   EXPECT(assert_equal(f4, reduced4->toDecisionTreeFactor())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // full arc-consistency test
 | 
					
						
							|  |  |  |   csp.runArcConsistency(nrColors); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  |   // GTSAM_PRINT(csp);
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |