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
112  *std::dynamic_pointer_cast<DecisionTreeFactor>(scheduler.product());
113  product.dot("scheduling-large", DefaultKeyFormatter, false);
114  }
115 
116  // Do exact inference
117  // SETDEBUG("timing-verbose", true);
118  SETDEBUG("DiscreteConditional::DiscreteConditional", true);
119  gttic(large);
120  auto MPE = scheduler.optimize();
121  gttoc(large);
123  tictoc_print();
124  scheduler.printAssignment(MPE);
125 }
126 
127 /* ************************************************************************* */
128 // Solve a series of relaxed problems for maximum flexibility solution
129 void solveStaged(size_t addMutex = 2) {
130 
131  // super-hack! just count...
132  bool debug = false;
133  SETDEBUG("DiscreteConditional::COUNT", true);
134  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
135 
136  // make a vector with slot availability, initially all 1
137  // Reads file to get count :-)
138  vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0);
139 
140  // now, find optimal value for each student, using relaxed mutex constraints
141  for (size_t s = 0; s < 7; s++) {
142  // add all students first time, then drop last one second time, etc...
143  Scheduler scheduler = largeExample(7 - s);
144  //scheduler.print(str(boost::format("Scheduler %d") % (7-s)));
145 
146  // only allow slots not yet taken
147  scheduler.setSlotsAvailable(slotsAvailable);
148 
149  // BUILD THE GRAPH !
150  scheduler.buildGraph(addMutex);
151 
152  // Do EXACT INFERENCE
153  gttic_(eliminate);
154  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
155  gttoc_(eliminate);
156 
157  // find root node
158  DiscreteConditional::shared_ptr root = chordal->back();
159  if (debug)
160  root->print(""/*scheduler.studentName(s)*/);
161 
162  // solve root node only
163  size_t bestSlot = root->argmax();
164 
165  // get corresponding count
166  DiscreteKey dkey = scheduler.studentKey(6 - s);
168  values[dkey.first] = bestSlot;
169  size_t count = (*root)(values);
170 
171  // remove this slot from consideration
172  slotsAvailable[bestSlot] = 0.0;
173  cout << scheduler.studentName(6 - s) << " = " << scheduler.slotName(bestSlot) << " (" << bestSlot
174  << "), count = " << count << endl;
175  }
176  tictoc_print_();
177 
178  // Solution with addMutex = 2: (20 secs)
179  // TC = Wed 2 (9), count = 96375041778
180  // AC = Tue 2 (5), count = 4076088090
181  // SJ = Mon 1 (0), count = 29596704
182  // TK = Mon 3 (2), count = 755370
183  // JH = Wed 4 (11), count = 12000
184  // TH = Fri 2 (17), count = 220
185  // MN = Fri 1 (16), count = 5
186  //
187  // Mutex does make a difference !!
188 
189 }
190 
191 /* ************************************************************************* */
192 // Sample from solution found above and evaluate cost function
193 bool NonZero(size_t i) {
194  return i > 0;
195 }
196 
198  size_t slot, vector<Scheduler>& schedulers) {
199  Scheduler scheduler = largeExample(0); // todo: wrong nr students
200  addStudent(scheduler, i);
201  SETDEBUG("Scheduler::buildGraph", false);
202  scheduler.addStudentSpecificConstraints(0, slot);
203  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
204  // chordal->print(scheduler[i].studentKey(0).name()); // large !
205  schedulers.push_back(scheduler);
206  return chordal;
207 }
208 
210 
211  vector<Scheduler> schedulers;
212  vector<DiscreteBayesNet::shared_ptr> samplers(7);
213 
214  // Given the time-slots, we can create 7 independent samplers
215  vector<size_t> slots{16, 17, 11, 2, 0, 5, 9}; // given slots
216  for (size_t i = 0; i < 7; i++)
217  samplers[i] = createSampler(i, slots[i], schedulers);
218 
219  // now, sample schedules
220  for (size_t n = 0; n < 500; n++) {
221  vector<size_t> stats(19, 0);
222  vector<DiscreteValues> samples;
223  for (size_t i = 0; i < 7; i++) {
224  samples.push_back(samplers[i]->sample());
225  schedulers[i].accumulateStats(samples[i], stats);
226  }
227  size_t max = *max_element(stats.begin(), stats.end());
228  size_t min = *min_element(stats.begin(), stats.end());
229  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
230  if (nz >= 15 && max <= 2) {
231  cout << "Sampled schedule " << (n + 1) << ", min = " << min << ", nz = " << nz
232  << ", max = " << max << endl;
233  for (size_t i = 0; i < 7; i++) {
234  cout << schedulers[i].studentName(0) << " : " << schedulers[i].slotName(
235  slots[i]) << endl;
236  schedulers[i].printSpecial(samples[i]);
237  }
238  }
239  }
240  // Output was
241  // Sampled schedule 359, min = 0, nz = 15, max = 2
242  // Michael N : Fri 9:00-10.30
243  // Michael N AI: Frank Dellaert
244  // Michael N Autonomy: Charlie Kemp
245  // Michael N Perception: Henrik Christensen
246  //
247  // Tucker H : Fri 10:30-12:00
248  // Tucker H AI: Ayanna Howard
249  // Tucker H Controls: Patricio Vela
250  // Tucker H Perception: Irfan Essa
251  //
252  // Jake H : Wed 3:00-4:30
253  // Jake H AI: Mike Stilman
254  // Jake H Controls: Magnus Egerstedt
255  // Jake H Perception: Jim Rehg
256  //
257  // Tobias K : Mon 1:30-3:00
258  // Tobias K AI: Ayanna Howard
259  // Tobias K Autonomy: Charlie Kemp
260  // Tobias K Controls: Magnus Egerstedt
261  //
262  // Shu J : Mon 9:00-10.30
263  // Shu J AI: Mike Stilman
264  // Shu J Controls: Jun Ueda
265  // Shu J HRI: Andrea Thomaz
266  //
267  // Akansel C : Tue 10:30-12:00
268  // Akansel C AI: Frank Dellaert
269  // Akansel C Autonomy: Tucker Balch
270  // Akansel C Mechanics: Harvey Lipkin
271  //
272  // Tiffany C : Wed 10:30-12:00
273  // Tiffany C Controls: Patricio Vela
274  // Tiffany C N/A 1: N/A 1
275  // Tiffany C N/A 2: N/A 2
276 
277 }
278 
279 /* ************************************************************************* */
281 
282  // super-hack! just count...
283  bool debug = false;
284  // SETDEBUG("DiscreteConditional::COUNT",true);
285  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
286 
287  Scheduler scheduler = largeExample(0);
288  // scheduler.addStudent("Victor E", "Autonomy", "HRI", "AI",
289  // "Henrik Christensen");
290  scheduler.addStudent("Carlos N", "Perception", "AI", "Autonomy",
291  "Henrik Christensen");
292  scheduler.print("scheduler");
293 
294  // rule out all occupied slots
295  vector<size_t> slots{16, 17, 11, 2, 0, 5, 9, 14};
296  vector<double> slotsAvailable(scheduler.nrTimeSlots(), 1.0);
297  for(size_t s: slots)
298  slotsAvailable[s] = 0;
299  scheduler.setSlotsAvailable(slotsAvailable);
300 
301  // BUILD THE GRAPH !
302  scheduler.buildGraph(1);
303 
304  // Do EXACT INFERENCE
305  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
306 
307  // find root node
308  DiscreteConditional::shared_ptr root = chordal->back();
309  if (debug)
310  root->print(""/*scheduler.studentName(s)*/);
311  // GTSAM_PRINT(*chordal);
312 
313  // solve root node only
314  size_t bestSlot = root->argmax();
315 
316  // get corresponding count
317  DiscreteKey dkey = scheduler.studentKey(0);
319  values[dkey.first] = bestSlot;
320  size_t count = (*root)(values);
321  cout << scheduler.studentName(0) << " = " << scheduler.slotName(bestSlot) << " (" << bestSlot
322  << "), count = " << count << endl;
323  // sample schedules
324  for (size_t n = 0; n < 10; n++) {
325  auto sample0 = chordal->sample();
326  scheduler.printAssignment(sample0);
327  }
328 }
329 
330 /* ************************************************************************* */
331 int main() {
332  runLargeExample();
333  solveStaged(3);
334 // sampleSolutions();
335  // accomodateStudent();
336  return 0;
337 }
338 /* ************************************************************************* */
339 
gttoc
#define gttoc(label)
Definition: timing.h:327
timing.h
Timing utilities.
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
SETDEBUG
#define SETDEBUG(S, V)
Definition: debug.h:61
solveStaged
void solveStaged(size_t addMutex=2)
Definition: schedulingExample.cpp:129
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::Scheduler::slotName
const std::string & slotName(size_t s) const
Definition: Scheduler.h:88
tictoc_finishedIteration
#define tictoc_finishedIteration()
Definition: timing.h:330
createSampler
DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector< Scheduler > &schedulers)
Definition: schedulingExample.cpp:197
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
main
int main()
Definition: schedulingExample.cpp:331
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
n
int n
Definition: BiCGSTAB_simple.cpp:1
samples
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
Definition: gnuplot_common_settings.hh:32
gttoc_
#define gttoc_(label)
Definition: timing.h:273
gtsam::Scheduler::addStudentSpecificConstraints
void addStudentSpecificConstraints(size_t i, std::optional< size_t > slot={})
Definition: Scheduler.cpp:103
stats
bool stats
Definition: SolverComparer.cpp:100
gttic_
#define gttic_(label)
Definition: timing.h:268
tictoc_print
#define tictoc_print()
Definition: timing.h:331
debug
static constexpr bool debug
Definition: testDiscreteBayesTree.cpp:31
gtsam::tictoc_print_
void tictoc_print_()
Definition: timing.h:291
gtsam::Scheduler::eliminate
DiscreteBayesNet::shared_ptr eliminate() const
Definition: Scheduler.cpp:247
addStudent
void addStudent(Scheduler &s, size_t i)
Definition: schedulingExample.cpp:21
runLargeExample
void runLargeExample()
Definition: schedulingExample.cpp:100
test_cases::large
std::vector< Vector3 > large
Definition: testPose3.cpp:897
gtsam::DiscreteFactorGraph::product
DiscreteFactor::shared_ptr product() const
Definition: DiscreteFactorGraph.cpp:67
Scheduler.h
matlab_wrap.path
path
Definition: matlab_wrap.py:66
gtsam::Scheduler::printAssignment
void printAssignment(const DiscreteValues &assignment) const
Definition: Scheduler.cpp:206
gtsam::DiscreteConditional::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:43
gtsam::Scheduler
Definition: Scheduler.h:22
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::Scheduler::nrStudents
size_t nrStudents() const
current number of students
Definition: Scheduler.h:119
accomodateStudent
void accomodateStudent()
Definition: schedulingExample.cpp:280
NonZero
bool NonZero(size_t i)
Definition: schedulingExample.cpp:193
gtsam::Scheduler::setSlotsAvailable
void setSlotsAvailable(const std::vector< double > &slotsAvailable)
Definition: Scheduler.h:91
gtsam::DiscreteBayesNet::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: DiscreteBayesNet.h:43
product
void product(const MatrixType &m)
Definition: product.h:20
min
#define min(a, b)
Definition: datatypes.h:19
gtsam::Scheduler::print
void print(const std::string &s="Scheduler", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: Scheduler.cpp:177
gtsam::Scheduler::studentName
const std::string & studentName(size_t i) const
Definition: Scheduler.cpp:87
gtsam::DiscreteFactorGraph::optimize
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
Definition: DiscreteFactorGraph.cpp:187
gtsam::Scheduler::buildGraph
void buildGraph(size_t mutexBound=7)
Definition: Scheduler.cpp:150
sampleSolutions
void sampleSolutions()
Definition: schedulingExample.cpp:209
max
#define max(a, b)
Definition: datatypes.h:20
largeExample
Scheduler largeExample(size_t nrStudents=7)
Definition: schedulingExample.cpp:48
gtsam::Scheduler::addStudent
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:57
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::Scheduler::studentKey
const DiscreteKey & studentKey(size_t i) const
Definition: Scheduler.cpp:92
gtsam::Scheduler::nrTimeSlots
size_t nrTimeSlots() const
Definition: Scheduler.h:86
gttic
#define gttic(label)
Definition: timing.h:326
debug.h
Global debugging flags.


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:03:21