| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * testScheduler.cpp | 
					
						
							|  |  |  |  * @date March 25, 2011 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //#define ENABLE_TIMING
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/timing.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | #include <gtsam_unstable/discrete/Scheduler.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/optional.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create the expected graph of constraints
 | 
					
						
							|  |  |  | DiscreteFactorGraph createExpected() { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // Start building
 | 
					
						
							|  |  |  |   size_t nrFaculty = 4, nrTimeSlots = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // variables assigning a time to a student:
 | 
					
						
							|  |  |  |   // Akansel and Jake
 | 
					
						
							|  |  |  |   DiscreteKey A(6, nrTimeSlots), J(7, nrTimeSlots); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // variables assigning a faculty member to a student area
 | 
					
						
							|  |  |  |   // Akansel:AI,ME,PC and Jake:HR,CT,AI
 | 
					
						
							|  |  |  |   DiscreteKey A1(0, nrFaculty), J1(3, nrFaculty); | 
					
						
							|  |  |  |   DiscreteKey A2(1, nrFaculty), J2(4, nrFaculty); | 
					
						
							|  |  |  |   DiscreteKey A3(2, nrFaculty), J3(5, nrFaculty); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CSP expected; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Area constraints
 | 
					
						
							|  |  |  |   string faculty_in_A = "1 0 0 1"; | 
					
						
							|  |  |  |   string faculty_in_C = "0 0 1 0"; | 
					
						
							|  |  |  |   string faculty_in_H = "0 0 0 1"; | 
					
						
							|  |  |  |   string faculty_in_M = "0 1 0 0"; | 
					
						
							|  |  |  |   string faculty_in_P = "1 0 1 0"; | 
					
						
							|  |  |  |   string available = "1 1 1 0   1 1 1 1   0 1 1 1"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Akansel
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(A1, faculty_in_A);  // Area 1
 | 
					
						
							|  |  |  |   expected.add(A1, "1 1 1 0");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(A & A1, available); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(A2, faculty_in_M);  // Area 2
 | 
					
						
							|  |  |  |   expected.add(A2, "1 1 1 0");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(A & A2, available); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(A3, faculty_in_P);  // Area 3
 | 
					
						
							|  |  |  |   expected.add(A3, "1 1 1 0");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(A & A3, available); | 
					
						
							|  |  |  |   // Mutual exclusion for faculty
 | 
					
						
							|  |  |  |   expected.addAllDiff(A1 & A2 & A3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Jake
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(J1, faculty_in_H);  // Area 1
 | 
					
						
							|  |  |  |   expected.add(J1, "1 0 1 1");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(J & J1, available); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(J2, faculty_in_C);  // Area 2
 | 
					
						
							|  |  |  |   expected.add(J2, "1 0 1 1");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(J & J2, available); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   expected.add(J3, faculty_in_A);  // Area 3
 | 
					
						
							|  |  |  |   expected.add(J3, "1 0 1 1");     // Advisor
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   expected.add(J & J3, available); | 
					
						
							|  |  |  |   // Mutual exclusion for faculty
 | 
					
						
							|  |  |  |   expected.addAllDiff(J1 & J2 & J3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Mutual exclusion for students
 | 
					
						
							| 
									
										
										
										
											2013-10-12 01:42:30 +08:00
										 |  |  |   expected.addAllDiff(A, J); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 08:16:32 +08:00
										 |  |  |   return std::move(expected); | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | TEST(schedulingExample, test) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Scheduler s(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add faculty
 | 
					
						
							|  |  |  |   s.addFaculty("Frank"); | 
					
						
							|  |  |  |   s.addFaculty("Harvey"); | 
					
						
							|  |  |  |   s.addFaculty("Magnus"); | 
					
						
							|  |  |  |   s.addFaculty("Andrea"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add time slots
 | 
					
						
							|  |  |  |   s.addSlot("Mon"); | 
					
						
							|  |  |  |   s.addSlot("Wed"); | 
					
						
							|  |  |  |   s.addSlot("Fri"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add areas
 | 
					
						
							|  |  |  |   s.addArea("Frank", "AI"); | 
					
						
							|  |  |  |   s.addArea("Frank", "PC"); | 
					
						
							|  |  |  |   s.addArea("Harvey", "ME"); | 
					
						
							|  |  |  |   s.addArea("Magnus", "CT"); | 
					
						
							|  |  |  |   s.addArea("Magnus", "PC"); | 
					
						
							|  |  |  |   s.addArea("Andrea", "AI"); | 
					
						
							|  |  |  |   s.addArea("Andrea", "HR"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add availability, nrTimeSlots * nrFaculty
 | 
					
						
							|  |  |  |   string available = "1 1 1 0  1 1 1 1  0 1 1 1"; | 
					
						
							|  |  |  |   s.setAvailability(available); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add students
 | 
					
						
							|  |  |  |   s.addStudent("Akansel", "AI", "ME", "PC", "Andrea"); | 
					
						
							|  |  |  |   s.addStudent("Jake", "HR", "CT", "AI", "Harvey"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // BUILD THE GRAPH !
 | 
					
						
							|  |  |  |   s.buildGraph(); | 
					
						
							|  |  |  |   //  s.print();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check graph
 | 
					
						
							|  |  |  |   DiscreteFactorGraph expected = createExpected(); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, (DiscreteFactorGraph)s)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Do brute force product and output that to file
 | 
					
						
							|  |  |  |   DecisionTreeFactor product = s.product(); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   // product.dot("scheduling", false);
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Do exact inference
 | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttic(small); | 
					
						
							| 
									
										
										
										
											2022-01-22 03:47:28 +08:00
										 |  |  |   auto MPE = s.optimize(); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc(small); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // print MPE, commented out as unit tests don't print
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  |   //  s.printAssignment(MPE);
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Commented out as does not work yet
 | 
					
						
							|  |  |  |   // s.runArcConsistency(8,10,true);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // find the assignment of students to slots with most possible committees
 | 
					
						
							|  |  |  |   // Commented out as not implemented yet
 | 
					
						
							| 
									
										
										
										
											2021-11-21 05:15:05 +08:00
										 |  |  |   //  auto bestSchedule = s.bestSchedule();
 | 
					
						
							|  |  |  |   //  GTSAM_PRINT(bestSchedule);
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   //  find the corresponding most desirable committee assignment
 | 
					
						
							|  |  |  |   // Commented out as not implemented yet
 | 
					
						
							| 
									
										
										
										
											2021-11-21 05:15:05 +08:00
										 |  |  |   //  auto bestAssignment = s.bestAssignment(bestSchedule);
 | 
					
						
							|  |  |  |   //  GTSAM_PRINT(bestAssignment);
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2021-11-18 23:54:00 +08:00
										 |  |  | TEST(schedulingExample, smallFromFile) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string path(TOPSRCDIR "/gtsam_unstable/discrete/examples/"); | 
					
						
							|  |  |  |   Scheduler s(2, path + "small.csv"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // add areas
 | 
					
						
							|  |  |  |   s.addArea("Frank", "AI"); | 
					
						
							|  |  |  |   s.addArea("Frank", "PC"); | 
					
						
							|  |  |  |   s.addArea("Harvey", "ME"); | 
					
						
							|  |  |  |   s.addArea("Magnus", "CT"); | 
					
						
							|  |  |  |   s.addArea("Magnus", "PC"); | 
					
						
							|  |  |  |   s.addArea("Andrea", "AI"); | 
					
						
							|  |  |  |   s.addArea("Andrea", "HR"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   add students
 | 
					
						
							|  |  |  |   s.addStudent("Akansel", "AI", "ME", "PC", "Andrea"); | 
					
						
							|  |  |  |   s.addStudent("Jake", "HR", "CT", "AI", "Harvey"); | 
					
						
							|  |  |  |   //  s.print();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // BUILD THE GRAPH !
 | 
					
						
							|  |  |  |   s.buildGraph(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check graph
 | 
					
						
							|  |  |  |   DiscreteFactorGraph expected = createExpected(); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, (DiscreteFactorGraph)s)); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |