schedulingExample.cpp
Go to the documentation of this file.
1 /*
2  * schedulingExample.cpp
3  * @brief hard scheduling example
4  * @date March 25, 2011
5  * @author Frank Dellaert
6  */
7 
8 //#define ENABLE_TIMING
9 #define ADD_NO_CACHING
10 #define ADD_NO_PRUNING
12 #include <gtsam/base/debug.h>
13 #include <gtsam/base/timing.h>
14 
15 #include <algorithm>
16 
17 using namespace std;
18 using namespace gtsam;
19 
20 /* ************************************************************************* */
21 void addStudent(Scheduler& s, size_t i) {
22  switch (i) {
23  case 0:
24  s.addStudent("Michael N", "AI", "Autonomy", "Perception", "Tucker Balch");
25  break;
26  case 1:
27  s.addStudent("Tucker H", "Controls", "AI", "Perception", "Jim Rehg");
28  break;
29  case 2:
30  s.addStudent("Jake H", "Controls", "AI", "Perception", "Henrik Christensen");
31  break;
32  case 3:
33  s.addStudent("Tobias K", "Controls", "AI", "Autonomy", "Mike Stilman");
34  break;
35  case 4:
36  s.addStudent("Shu J", "Controls", "AI", "HRI", "N/A 1");
37  break;
38  case 5:
39  s.addStudent("Akansel C", "AI", "Autonomy", "Mechanics",
40  "Henrik Christensen");
41  break;
42  case 6:
43  s.addStudent("Tiffany C", "Controls", "N/A 1", "N/A 2", "Charlie Kemp");
44  break;
45  }
46 }
47 /* ************************************************************************* */
48 Scheduler largeExample(size_t nrStudents = 7) {
49 // char cCurrentPath[FILENAME_MAX];
50 // if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) return errno;
51 // cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
52 // printf ("The current working directory is %s", cCurrentPath);
53 
54  string path("../../../gtsam_unstable/discrete/examples/");
55  Scheduler s(nrStudents, path + "Doodle.csv");
56 
57  s.addArea("Harvey Lipkin", "Mechanics");
58  s.addArea("Wayne Book", "Mechanics");
59  s.addArea("Jun Ueda", "Mechanics");
60 
61  // s.addArea("Wayne Book", "Controls");
62  s.addArea("Patricio Vela", "Controls");
63  s.addArea("Magnus Egerstedt", "Controls");
64  s.addArea("Jun Ueda", "Controls");
65 
66  // s.addArea("Frank Dellaert", "Perception");
67  s.addArea("Jim Rehg", "Perception");
68  s.addArea("Irfan Essa", "Perception");
69  s.addArea("Aaron Bobick", "Perception");
70  s.addArea("Henrik Christensen", "Perception");
71 
72  s.addArea("Mike Stilman", "AI");
73  s.addArea("Henrik Christensen", "AI");
74  s.addArea("Frank Dellaert", "AI");
75  s.addArea("Ayanna Howard", "AI");
76  // s.addArea("Tucker Balch", "AI");
77 
78  s.addArea("Ayanna Howard", "Autonomy");
79  // s.addArea("Andrea Thomaz", "Autonomy");
80  s.addArea("Charlie Kemp", "Autonomy");
81  s.addArea("Tucker Balch", "Autonomy");
82  s.addArea("Ron Arkin", "Autonomy");
83 
84  s.addArea("Andrea Thomaz", "HRI");
85  s.addArea("Karen Feigh", "HRI");
86  s.addArea("Charlie Kemp", "HRI");
87 
88  // Allow students not to take three areas
89  s.addArea("N/A 1", "N/A 1");
90  s.addArea("N/A 2", "N/A 2");
91 
92  // add students
93  for (size_t i = 0; i < nrStudents; i++)
94  addStudent(s, i);
95 
96  return s;
97 }
98 
99 /* ************************************************************************* */
101 
102  Scheduler scheduler = largeExample();
103  scheduler.print();
104 
105  // BUILD THE GRAPH !
106  size_t addMutex = 2;
107  scheduler.buildGraph(addMutex);
108 
109  // Do brute force product and output that to file
110  if (scheduler.nrStudents() == 1) { // otherwise too slow
111  DecisionTreeFactor product = scheduler.product();
112  product.dot("scheduling-large", DefaultKeyFormatter, false);
113  }
114 
115  // Do exact inference
116  // SETDEBUG("timing-verbose", true);
117  SETDEBUG("DiscreteConditional::DiscreteConditional", true);
118  gttic(large);
119  auto MPE = scheduler.optimize();
120  gttoc(large);
122  tictoc_print();
123  scheduler.printAssignment(MPE);
124 }
125 
126 /* ************************************************************************* */
127 // Solve a series of relaxed problems for maximum flexibility solution
128 void solveStaged(size_t addMutex = 2) {
129 
130  // super-hack! just count...
131  bool debug = false;
132  SETDEBUG("DiscreteConditional::COUNT", true);
133  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
134 
135  // make a vector with slot availability, initially all 1
136  // Reads file to get count :-)
137  vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0);
138 
139  // now, find optimal value for each student, using relaxed mutex constraints
140  for (size_t s = 0; s < 7; s++) {
141  // add all students first time, then drop last one second time, etc...
142  Scheduler scheduler = largeExample(7 - s);
143  //scheduler.print(str(boost::format("Scheduler %d") % (7-s)));
144 
145  // only allow slots not yet taken
146  scheduler.setSlotsAvailable(slotsAvailable);
147 
148  // BUILD THE GRAPH !
149  scheduler.buildGraph(addMutex);
150 
151  // Do EXACT INFERENCE
152  gttic_(eliminate);
153  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
154  gttoc_(eliminate);
155 
156  // find root node
157  DiscreteConditional::shared_ptr root = chordal->back();
158  if (debug)
159  root->print(""/*scheduler.studentName(s)*/);
160 
161  // solve root node only
162  size_t bestSlot = root->argmax();
163 
164  // get corresponding count
165  DiscreteKey dkey = scheduler.studentKey(6 - s);
167  values[dkey.first] = bestSlot;
168  size_t count = (*root)(values);
169 
170  // remove this slot from consideration
171  slotsAvailable[bestSlot] = 0.0;
172  cout << scheduler.studentName(6 - s) << " = " << scheduler.slotName(bestSlot) << " (" << bestSlot
173  << "), count = " << count << endl;
174  }
175  tictoc_print_();
176 
177  // Solution with addMutex = 2: (20 secs)
178  // TC = Wed 2 (9), count = 96375041778
179  // AC = Tue 2 (5), count = 4076088090
180  // SJ = Mon 1 (0), count = 29596704
181  // TK = Mon 3 (2), count = 755370
182  // JH = Wed 4 (11), count = 12000
183  // TH = Fri 2 (17), count = 220
184  // MN = Fri 1 (16), count = 5
185  //
186  // Mutex does make a difference !!
187 
188 }
189 
190 /* ************************************************************************* */
191 // Sample from solution found above and evaluate cost function
192 bool NonZero(size_t i) {
193  return i > 0;
194 }
195 
197  size_t slot, vector<Scheduler>& schedulers) {
198  Scheduler scheduler = largeExample(0); // todo: wrong nr students
199  addStudent(scheduler, i);
200  SETDEBUG("Scheduler::buildGraph", false);
201  scheduler.addStudentSpecificConstraints(0, slot);
202  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
203  // chordal->print(scheduler[i].studentKey(0).name()); // large !
204  schedulers.push_back(scheduler);
205  return chordal;
206 }
207 
209 
210  vector<Scheduler> schedulers;
211  vector<DiscreteBayesNet::shared_ptr> samplers(7);
212 
213  // Given the time-slots, we can create 7 independent samplers
214  vector<size_t> slots{16, 17, 11, 2, 0, 5, 9}; // given slots
215  for (size_t i = 0; i < 7; i++)
216  samplers[i] = createSampler(i, slots[i], schedulers);
217 
218  // now, sample schedules
219  for (size_t n = 0; n < 500; n++) {
220  vector<size_t> stats(19, 0);
221  vector<DiscreteValues> samples;
222  for (size_t i = 0; i < 7; i++) {
223  samples.push_back(samplers[i]->sample());
224  schedulers[i].accumulateStats(samples[i], stats);
225  }
226  size_t max = *max_element(stats.begin(), stats.end());
227  size_t min = *min_element(stats.begin(), stats.end());
228  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
229  if (nz >= 15 && max <= 2) {
230  cout << "Sampled schedule " << (n + 1) << ", min = " << min << ", nz = " << nz
231  << ", max = " << max << endl;
232  for (size_t i = 0; i < 7; i++) {
233  cout << schedulers[i].studentName(0) << " : " << schedulers[i].slotName(
234  slots[i]) << endl;
235  schedulers[i].printSpecial(samples[i]);
236  }
237  }
238  }
239  // Output was
240  // Sampled schedule 359, min = 0, nz = 15, max = 2
241  // Michael N : Fri 9:00-10.30
242  // Michael N AI: Frank Dellaert
243  // Michael N Autonomy: Charlie Kemp
244  // Michael N Perception: Henrik Christensen
245  //
246  // Tucker H : Fri 10:30-12:00
247  // Tucker H AI: Ayanna Howard
248  // Tucker H Controls: Patricio Vela
249  // Tucker H Perception: Irfan Essa
250  //
251  // Jake H : Wed 3:00-4:30
252  // Jake H AI: Mike Stilman
253  // Jake H Controls: Magnus Egerstedt
254  // Jake H Perception: Jim Rehg
255  //
256  // Tobias K : Mon 1:30-3:00
257  // Tobias K AI: Ayanna Howard
258  // Tobias K Autonomy: Charlie Kemp
259  // Tobias K Controls: Magnus Egerstedt
260  //
261  // Shu J : Mon 9:00-10.30
262  // Shu J AI: Mike Stilman
263  // Shu J Controls: Jun Ueda
264  // Shu J HRI: Andrea Thomaz
265  //
266  // Akansel C : Tue 10:30-12:00
267  // Akansel C AI: Frank Dellaert
268  // Akansel C Autonomy: Tucker Balch
269  // Akansel C Mechanics: Harvey Lipkin
270  //
271  // Tiffany C : Wed 10:30-12:00
272  // Tiffany C Controls: Patricio Vela
273  // Tiffany C N/A 1: N/A 1
274  // Tiffany C N/A 2: N/A 2
275 
276 }
277 
278 /* ************************************************************************* */
280 
281  // super-hack! just count...
282  bool debug = false;
283  // SETDEBUG("DiscreteConditional::COUNT",true);
284  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
285 
286  Scheduler scheduler = largeExample(0);
287  // scheduler.addStudent("Victor E", "Autonomy", "HRI", "AI",
288  // "Henrik Christensen");
289  scheduler.addStudent("Carlos N", "Perception", "AI", "Autonomy",
290  "Henrik Christensen");
291  scheduler.print("scheduler");
292 
293  // rule out all occupied slots
294  vector<size_t> slots{16, 17, 11, 2, 0, 5, 9, 14};
295  vector<double> slotsAvailable(scheduler.nrTimeSlots(), 1.0);
296  for(size_t s: slots)
297  slotsAvailable[s] = 0;
298  scheduler.setSlotsAvailable(slotsAvailable);
299 
300  // BUILD THE GRAPH !
301  scheduler.buildGraph(1);
302 
303  // Do EXACT INFERENCE
304  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
305 
306  // find root node
307  DiscreteConditional::shared_ptr root = chordal->back();
308  if (debug)
309  root->print(""/*scheduler.studentName(s)*/);
310  // GTSAM_PRINT(*chordal);
311 
312  // solve root node only
313  size_t bestSlot = root->argmax();
314 
315  // get corresponding count
316  DiscreteKey dkey = scheduler.studentKey(0);
318  values[dkey.first] = bestSlot;
319  size_t count = (*root)(values);
320  cout << scheduler.studentName(0) << " = " << scheduler.slotName(bestSlot) << " (" << bestSlot
321  << "), count = " << count << endl;
322  // sample schedules
323  for (size_t n = 0; n < 10; n++) {
324  auto sample0 = chordal->sample();
325  scheduler.printAssignment(sample0);
326  }
327 }
328 
329 /* ************************************************************************* */
330 int main() {
331  runLargeExample();
332  solveStaged(3);
333 // sampleSolutions();
334  // accomodateStudent();
335  return 0;
336 }
337 /* ************************************************************************* */
338 
#define max(a, b)
Definition: datatypes.h:20
#define SETDEBUG(S, V)
Definition: debug.h:61
#define gttic_(label)
Definition: timing.h:245
void printAssignment(const DiscreteValues &assignment) const
Definition: Scheduler.cpp:205
void solveStaged(size_t addMutex=2)
Global debugging flags.
#define min(a, b)
Definition: datatypes.h:19
DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector< Scheduler > &schedulers)
DecisionTreeFactor product() const
const std::string & studentName(size_t i) const
Definition: Scheduler.cpp:86
int n
leaf::MyValues values
const std::string & slotName(size_t s) const
Definition: Scheduler.h:88
int main()
Definition: BFloat16.h:88
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.
void addStudentSpecificConstraints(size_t i, std::optional< size_t > slot={})
Definition: Scheduler.cpp:102
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
bool stats
static constexpr bool debug
const DiscreteKey & studentKey(size_t i) const
Definition: Scheduler.cpp:91
#define gttic(label)
Definition: timing.h:295
void addStudent(Scheduler &s, size_t i)
void runLargeExample()
void tictoc_print_()
Definition: timing.h:268
RealScalar s
void buildGraph(size_t mutexBound=7)
Definition: Scheduler.cpp:149
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const
void setSlotsAvailable(const std::vector< double > &slotsAvailable)
Definition: Scheduler.h:91
traits
Definition: chartTesting.h:28
size_t nrStudents() const
current number of students
Definition: Scheduler.h:119
void print(const std::string &s="Scheduler", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: Scheduler.cpp:176
void accomodateStudent()
#define gttoc(label)
Definition: timing.h:296
std::shared_ptr< This > shared_ptr
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
bool NonZero(size_t i)
DiscreteBayesNet::shared_ptr eliminate() const
Definition: Scheduler.cpp:246
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Scheduler largeExample(size_t nrStudents=7)
void addArea(const std::string &facultyName, const std::string &areaName)
Definition: Scheduler.h:95
#define gttoc_(label)
Definition: timing.h:250
#define tictoc_print()
Definition: timing.h:300
Timing utilities.
#define tictoc_finishedIteration()
Definition: timing.h:299
void sampleSolutions()
void product(const MatrixType &m)
Definition: product.h:20
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set samples
size_t nrTimeSlots() const
Definition: Scheduler.h:86


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:36