testScheduler.cpp
Go to the documentation of this file.
1 /*
2  * testScheduler.cpp
3  * @date March 25, 2011
4  * @author Frank Dellaert
5  */
6 
7 //#define ENABLE_TIMING
9 #include <gtsam/base/Testable.h>
10 #include <gtsam/base/timing.h>
12 
13 
14 using namespace std;
15 using namespace gtsam;
16 
17 /* ************************************************************************* */
18 // Create the expected graph of constraints
20  // Start building
21  size_t nrFaculty = 4, nrTimeSlots = 3;
22 
23  // variables assigning a time to a student:
24  // Akansel and Jake
25  DiscreteKey A(6, nrTimeSlots), J(7, nrTimeSlots);
26 
27  // variables assigning a faculty member to a student area
28  // Akansel:AI,ME,PC and Jake:HR,CT,AI
29  DiscreteKey A1(0, nrFaculty), J1(3, nrFaculty);
30  DiscreteKey A2(1, nrFaculty), J2(4, nrFaculty);
31  DiscreteKey A3(2, nrFaculty), J3(5, nrFaculty);
32 
33  CSP expected;
34 
35  // Area constraints
36  string faculty_in_A = "1 0 0 1";
37  string faculty_in_C = "0 0 1 0";
38  string faculty_in_H = "0 0 0 1";
39  string faculty_in_M = "0 1 0 0";
40  string faculty_in_P = "1 0 1 0";
41  string available = "1 1 1 0 1 1 1 1 0 1 1 1";
42 
43  // Akansel
44  expected.add(A1, faculty_in_A); // Area 1
45  expected.add(A1, "1 1 1 0"); // Advisor
46  expected.add(A & A1, available);
47  expected.add(A2, faculty_in_M); // Area 2
48  expected.add(A2, "1 1 1 0"); // Advisor
49  expected.add(A & A2, available);
50  expected.add(A3, faculty_in_P); // Area 3
51  expected.add(A3, "1 1 1 0"); // Advisor
52  expected.add(A & A3, available);
53  // Mutual exclusion for faculty
54  expected.addAllDiff(A1 & A2 & A3);
55 
56  // Jake
57  expected.add(J1, faculty_in_H); // Area 1
58  expected.add(J1, "1 0 1 1"); // Advisor
59  expected.add(J & J1, available);
60  expected.add(J2, faculty_in_C); // Area 2
61  expected.add(J2, "1 0 1 1"); // Advisor
62  expected.add(J & J2, available);
63  expected.add(J3, faculty_in_A); // Area 3
64  expected.add(J3, "1 0 1 1"); // Advisor
65  expected.add(J & J3, available);
66  // Mutual exclusion for faculty
67  expected.addAllDiff(J1 & J2 & J3);
68 
69  // Mutual exclusion for students
70  expected.addAllDiff(A, J);
71 
72  return std::move(expected);
73 }
74 
75 /* ************************************************************************* */
76 TEST(schedulingExample, test) {
77  Scheduler s(2);
78 
79  // add faculty
80  s.addFaculty("Frank");
81  s.addFaculty("Harvey");
82  s.addFaculty("Magnus");
83  s.addFaculty("Andrea");
84 
85  // add time slots
86  s.addSlot("Mon");
87  s.addSlot("Wed");
88  s.addSlot("Fri");
89 
90  // add areas
91  s.addArea("Frank", "AI");
92  s.addArea("Frank", "PC");
93  s.addArea("Harvey", "ME");
94  s.addArea("Magnus", "CT");
95  s.addArea("Magnus", "PC");
96  s.addArea("Andrea", "AI");
97  s.addArea("Andrea", "HR");
98 
99  // add availability, nrTimeSlots * nrFaculty
100  string available = "1 1 1 0 1 1 1 1 0 1 1 1";
101  s.setAvailability(available);
102 
103  // add students
104  s.addStudent("Akansel", "AI", "ME", "PC", "Andrea");
105  s.addStudent("Jake", "HR", "CT", "AI", "Harvey");
106 
107  // BUILD THE GRAPH !
108  s.buildGraph();
109  // s.print();
110 
111  // Check graph
113  EXPECT(assert_equal(expected, (DiscreteFactorGraph)s));
114 
115  // Do brute force product and output that to file
117  // product.dot("scheduling", false);
118 
119  // Do exact inference
120  gttic(small);
121  auto MPE = s.optimize();
122  gttoc(small);
123 
124  // print MPE, commented out as unit tests don't print
125  // s.printAssignment(MPE);
126 
127  // Commented out as does not work yet
128  // s.runArcConsistency(8,10,true);
129 
130  // find the assignment of students to slots with most possible committees
131  // Commented out as not implemented yet
132  // auto bestSchedule = s.bestSchedule();
133  // GTSAM_PRINT(bestSchedule);
134 
135  // find the corresponding most desirable committee assignment
136  // Commented out as not implemented yet
137  // auto bestAssignment = s.bestAssignment(bestSchedule);
138  // GTSAM_PRINT(bestAssignment);
139 }
140 
141 /* ************************************************************************* */
142 TEST(schedulingExample, smallFromFile) {
143  string path(TOPSRCDIR "/gtsam_unstable/discrete/examples/");
144  Scheduler s(2, path + "small.csv");
145 
146  // add areas
147  s.addArea("Frank", "AI");
148  s.addArea("Frank", "PC");
149  s.addArea("Harvey", "ME");
150  s.addArea("Magnus", "CT");
151  s.addArea("Magnus", "PC");
152  s.addArea("Andrea", "AI");
153  s.addArea("Andrea", "HR");
154 
155  // add students
156  s.addStudent("Akansel", "AI", "ME", "PC", "Andrea");
157  s.addStudent("Jake", "HR", "CT", "AI", "Harvey");
158  // s.print();
159 
160  // BUILD THE GRAPH !
161  s.buildGraph();
162 
163  // Check graph
165  EXPECT(assert_equal(expected, (DiscreteFactorGraph)s));
166 }
167 
168 /* ************************************************************************* */
169 int main() {
170  TestResult tr;
171  return TestRegistry::runAllTests(tr);
172 }
173 /* ************************************************************************* */
static Matrix A1
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
Matrix expected
Definition: testMatrix.cpp:971
int main()
DecisionTreeFactor product() const
Definition: test.py:1
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
void setAvailability(const std::string &available)
Definition: Scheduler.h:82
Definition: BFloat16.h:88
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
void addStudent(const std::string &studentName, const std::string &area1, const std::string &area2, const std::string &area3, const std::string &advisor)
Definition: Scheduler.cpp:56
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
DiscreteFactorGraph createExpected()
TEST(schedulingExample, test)
#define gttic(label)
Definition: timing.h:295
#define EXPECT(condition)
Definition: Test.h:150
RealScalar s
void addSlot(const std::string &slotName)
Definition: Scheduler.h:84
JacobiRotation< float > J
void buildGraph(size_t mutexBound=7)
Definition: Scheduler.cpp:149
traits
Definition: chartTesting.h:28
#define gttoc(label)
Definition: timing.h:296
void addAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
Add a binary AllDiff constraint.
Definition: CSP.h:31
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Definition: CSP.h:21
void addArea(const std::string &facultyName, const std::string &areaName)
Definition: Scheduler.h:95
void addFaculty(const std::string &facultyName)
Definition: Scheduler.h:74
Timing utilities.
void product(const MatrixType &m)
Definition: product.h:20


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:18