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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:54