schedulingQuals12.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 size_t NRSTUDENTS = 9;
21 
22 bool NonZero(size_t i) {
23  return i > 0;
24 }
25 
26 /* ************************************************************************* */
27 void addStudent(Scheduler& s, size_t i) {
28  switch (i) {
29  case 0:
30  s.addStudent("Pan, Yunpeng", "Controls", "Perception", "Mechanics", "Eric Johnson");
31  break;
32  case 1:
33  s.addStudent("Sawhney, Rahul", "Controls", "AI", "Perception", "Henrik Christensen");
34  break;
35  case 2:
36  s.addStudent("Akgun, Baris", "Controls", "AI", "HRI", "Andrea Thomaz");
37  break;
38  case 3:
39  s.addStudent("Jiang, Shu", "Controls", "AI", "Perception", "Ron Arkin");
40  break;
41  case 4:
42  s.addStudent("Grice, Phillip", "Controls", "Perception", "HRI", "Charlie Kemp");
43  break;
44  case 5:
45  s.addStudent("Huaman, Ana", "Controls", "AI", "Perception", "Mike Stilman");
46  break;
47  case 6:
48  s.addStudent("Levihn, Martin", "AI", "Autonomy", "Perception", "Mike Stilman");
49  break;
50  case 7:
51  s.addStudent("Nieto, Carlos", "AI", "Autonomy", "Perception", "Henrik Christensen");
52  break;
53  case 8:
54  s.addStudent("Robinette, Paul", "Controls", "AI", "HRI", "Ayanna Howard");
55  break;
56  }
57 }
58 
59 /* ************************************************************************* */
60 Scheduler largeExample(size_t nrStudents = NRSTUDENTS) {
61  string path("../../../gtsam_unstable/discrete/examples/");
62  Scheduler s(nrStudents, path + "Doodle2012.csv");
63 
64  s.addArea("Harvey Lipkin", "Mechanics");
65  s.addArea("Jun Ueda", "Mechanics");
66 
67  s.addArea("Patricio Vela", "Controls");
68  s.addArea("Magnus Egerstedt", "Controls");
69  s.addArea("Jun Ueda", "Controls");
70  s.addArea("Panos Tsiotras", "Controls");
71  s.addArea("Fumin Zhang", "Controls");
72 
73  s.addArea("Henrik Christensen", "Perception");
74  s.addArea("Aaron Bobick", "Perception");
75 
76  s.addArea("Mike Stilman", "AI");
77 // s.addArea("Henrik Christensen", "AI");
78  s.addArea("Ayanna Howard", "AI");
79  s.addArea("Charles Isbell", "AI");
80  s.addArea("Tucker Balch", "AI");
81 
82  s.addArea("Ayanna Howard", "Autonomy");
83  s.addArea("Charlie Kemp", "Autonomy");
84  s.addArea("Tucker Balch", "Autonomy");
85  s.addArea("Ron Arkin", "Autonomy");
86 
87  s.addArea("Andrea Thomaz", "HRI");
88  s.addArea("Karen Feigh", "HRI");
89  s.addArea("Charlie Kemp", "HRI");
90 
91  // add students
92  for (size_t i = 0; i < nrStudents; i++)
93  addStudent(s, i);
94 
95  return s;
96 }
97 
98 /* ************************************************************************* */
100 
101  Scheduler scheduler = largeExample();
102  scheduler.print();
103 
104  // BUILD THE GRAPH !
105  size_t addMutex = 3;
106  // SETDEBUG("Scheduler::buildGraph", true);
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 #define SAMPLE
119 #ifdef SAMPLE
120  gttic(large);
121  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
122  gttoc(large);
124  tictoc_print();
125  for (size_t i=0;i<100;i++) {
126  auto assignment = chordal->sample();
127  vector<size_t> stats(scheduler.nrFaculty());
128  scheduler.accumulateStats(assignment, stats);
129  size_t max = *max_element(stats.begin(), stats.end());
130  size_t min = *min_element(stats.begin(), stats.end());
131  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
132 // cout << min << ", " << max << ", " << nz << endl;
133  if (nz >= 13 && min >=1 && max <= 4) {
134  cout << "======================================================\n";
135  scheduler.printAssignment(assignment);
136  }
137  }
138 #else
139  gttic(large);
140  auto MPE = scheduler.optimize();
141  gttoc(large);
143  tictoc_print();
144  scheduler.printAssignment(MPE);
145 #endif
146 }
147 
148 /* ************************************************************************* */
149 // Solve a series of relaxed problems for maximum flexibility solution
150 void solveStaged(size_t addMutex = 2) {
151 
152  // super-hack! just count...
153  bool debug = false;
154  SETDEBUG("DiscreteConditional::COUNT", true);
155  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
156 
157  // make a vector with slot availability, initially all 1
158  // Reads file to get count :-)
159  vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0);
160 
161  // now, find optimal value for each student, using relaxed mutex constraints
162  for (size_t s = 0; s < NRSTUDENTS; s++) {
163  // add all students first time, then drop last one second time, etc...
164  Scheduler scheduler = largeExample(NRSTUDENTS - s);
165  //scheduler.print(str(boost::format("Scheduler %d") % (NRSTUDENTS-s)));
166 
167  // only allow slots not yet taken
168  scheduler.setSlotsAvailable(slotsAvailable);
169 
170  // BUILD THE GRAPH !
171  scheduler.buildGraph(addMutex);
172 
173  // Do EXACT INFERENCE
174  gttic_(eliminate);
175  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
176  gttoc_(eliminate);
177 
178  // find root node
179  DiscreteConditional::shared_ptr root = chordal->back();
180  if (debug)
181  root->print(""/*scheduler.studentName(s)*/);
182 
183  // solve root node only
184  size_t bestSlot = root->argmax();
185 
186  // get corresponding count
187  DiscreteKey dkey = scheduler.studentKey(NRSTUDENTS - 1 - s);
189  values[dkey.first] = bestSlot;
190  size_t count = (*root)(values);
191 
192  // remove this slot from consideration
193  slotsAvailable[bestSlot] = 0.0;
194  cout << scheduler.studentName(NRSTUDENTS - 1 - s) << " = " <<
195  scheduler.slotName(bestSlot) << " (" << bestSlot
196  << "), count = " << count << endl;
197  }
198  tictoc_print_();
199 }
200 
201 /* ************************************************************************* */
202 // Sample from solution found above and evaluate cost function
204  size_t slot, vector<Scheduler>& schedulers) {
205  Scheduler scheduler = largeExample(0); // todo: wrong nr students
206  addStudent(scheduler, i);
207  SETDEBUG("Scheduler::buildGraph", false);
208  scheduler.addStudentSpecificConstraints(0, slot);
209  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
210  schedulers.push_back(scheduler);
211  return chordal;
212 }
213 
215 
216  vector<Scheduler> schedulers;
217  vector<DiscreteBayesNet::shared_ptr> samplers(NRSTUDENTS);
218 
219  // Given the time-slots, we can create NRSTUDENTS independent samplers
220  vector<size_t> slots{3, 20, 2, 6, 5, 11, 1, 4}; // given slots
221  for (size_t i = 0; i < NRSTUDENTS; i++)
222  samplers[i] = createSampler(i, slots[i], schedulers);
223 
224  // now, sample schedules
225  for (size_t n = 0; n < 500; n++) {
226  vector<size_t> stats(19, 0);
227  vector<DiscreteValues> samples;
228  for (size_t i = 0; i < NRSTUDENTS; i++) {
229  samples.push_back(samplers[i]->sample());
230  schedulers[i].accumulateStats(samples[i], stats);
231  }
232  size_t max = *max_element(stats.begin(), stats.end());
233  size_t min = *min_element(stats.begin(), stats.end());
234  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
235  if (nz >= 15 && max <= 2) {
236  cout << "Sampled schedule " << (n + 1) << ", min = " << min
237  << ", nz = " << nz << ", max = " << max << endl;
238  for (size_t i = 0; i < NRSTUDENTS; i++) {
239  cout << schedulers[i].studentName(0) << " : " << schedulers[i].slotName(
240  slots[i]) << endl;
241  schedulers[i].printSpecial(samples[i]);
242  }
243  }
244  }
245 }
246 
247 /* ************************************************************************* */
248 int main() {
249 // runLargeExample();
250  solveStaged(3);
251 // sampleSolutions();
252  return 0;
253 }
254 /* ************************************************************************* */
255 
#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
Global debugging flags.
#define min(a, b)
Definition: datatypes.h:19
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
Definition: BFloat16.h:88
void sampleSolutions()
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
DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector< Scheduler > &schedulers)
bool stats
static constexpr bool debug
const DiscreteKey & studentKey(size_t i) const
Definition: Scheduler.cpp:91
int main()
#define gttic(label)
Definition: timing.h:295
void accumulateStats(const DiscreteValues &assignment, std::vector< size_t > &stats) const
Definition: Scheduler.cpp:233
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
Scheduler largeExample(size_t nrStudents=NRSTUDENTS)
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
#define gttoc(label)
Definition: timing.h:296
size_t NRSTUDENTS
std::shared_ptr< This > shared_ptr
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
DiscreteBayesNet::shared_ptr eliminate() const
Definition: Scheduler.cpp:246
std::shared_ptr< This > shared_ptr
shared_ptr to this class
void addStudent(Scheduler &s, size_t i)
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
size_t nrFaculty() const
Definition: Scheduler.h:79
void solveStaged(size_t addMutex=2)
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
bool NonZero(size_t i)


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