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
114 
115  // Do brute force product and output that to file
116  DecisionTreeFactor product = s.product();
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
166 }
167 
168 /* ************************************************************************* */
169 int main() {
170  TestResult tr;
171  return TestRegistry::runAllTests(tr);
172 }
173 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gttoc
#define gttoc(label)
Definition: timing.h:296
timing.h
Timing utilities.
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
A3
static const double A3[]
Definition: expn.h:8
s
RealScalar s
Definition: level1_cplx_impl.h:126
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
test
Definition: test.py:1
J
JacobiRotation< float > J
Definition: Jacobi_makeJacobi.cpp:3
A2
static const double A2[]
Definition: expn.h:7
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
Scheduler.h
TestResult
Definition: TestResult.h:26
matlab_wrap.path
path
Definition: matlab_wrap.py:66
gtsam::Scheduler
Definition: Scheduler.h:22
main
int main()
Definition: testScheduler.cpp:169
TEST
TEST(schedulingExample, test)
Definition: testScheduler.cpp:76
gtsam
traits
Definition: SFMdata.h:40
gtsam::CSP
Definition: CSP.h:21
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
A1
static const double A1[]
Definition: expn.h:6
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
product
void product(const MatrixType &m)
Definition: product.h:20
createExpected
DiscreteFactorGraph createExpected()
Definition: testScheduler.cpp:19
gttic
#define gttic(label)
Definition: timing.h:295


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:08:24