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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:49:23