schedulingQuals13.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 size_t NRSTUDENTS = 12;
27 
28 bool NonZero(size_t i) {
29  return i > 0;
30 }
31 
32 /* ************************************************************************* */
33 void addStudent(Scheduler& s, size_t i) {
34  switch (i) {
35  case 0:
36  s.addStudent("Young, Carol", "Controls", "Autonomy", "Mechanics", "Fumin Zhang");
37  break;
38  case 1:
39  s.addStudent("Erdogan, Can", "Controls", "AI", "Perception", "Mike Stilman");
40  break;
41  case 2:
42  s.addStudent("Arslan, Oktay", "Controls", "AI", "Mechanics", "Panos Tsiotras");
43  break;
44  case 3:
45  s.addStudent("Bhattacharjee, Tapomayukh", "Controls", "AI", "Mechanics", "Charlie Kemp");
46  break;
47  case 4:
48  s.addStudent("Grey, Michael", "Controls", "AI", "Mechanics", "Wayne Book");
49  break;
50  case 5:
51  s.addStudent("O'Flaherty, Rowland", "Controls", "AI", "Mechanics", "Magnus Egerstedt");
52  break;
53  case 6:
54  s.addStudent("Pickem, Daniel", "Controls", "AI", "Mechanics", "Jeff Shamma");
55  break;
56  case 7:
57  s.addStudent("Lee, Kimoon", "Controls", "Autonomy", "Mechanics", "Henrik Christensen");
58  break;
59  case 8:
60  s.addStudent("Melim, Andrew Lyon", "Controls", "AI", "Perception", "Frank Dellaert");
61  break;
62  case 9:
63  s.addStudent("Jensen, David", "Controls", "Autonomy", "HRI", "Andrea Thomaz");
64  break;
65  case 10:
66  s.addStudent("Nisbett, Jared", "Controls", "Perception", "Mechanics", "Magnus Egerstedt");
67  break;
68  case 11:
69  s.addStudent("Pan, Yunpeng", "Controls", "Perception", "Mechanics", "Wayne Book");
70  break;
71 // case 12:
72 // s.addStudent("Grice, Phillip", "Controls", "None", "None", "Wayne Book");
73 // break;
74 // case 13:
75 // s.addStudent("Robinette, Paul", "Controls", "None", "None", "Ayanna Howard");
76 // break;
77 // case 14:
78 // s.addStudent("Huaman, Ana", "Autonomy", "None", "None", "Mike Stilman");
79 // break;
80  }
81 }
82 
83 /* ************************************************************************* */
84 Scheduler largeExample(size_t nrStudents = NRSTUDENTS, bool addStudents=true) {
85  string path("../../../gtsam_unstable/discrete/examples/");
86  Scheduler s(nrStudents, path + "Doodle2013.csv");
87 
88  s.addArea("Harvey Lipkin", "Mechanics");
89  s.addArea("Jun Ueda", "Mechanics");
90  s.addArea("Mike Stilman", "Mechanics");
91 // s.addArea("Frank Dellaert", "Mechanics");
92  s.addArea("Wayne Book", "Mechanics");
93 // s.addArea("Charlie Kemp", "Mechanics");
94 
95  s.addArea("Patricio Vela", "Controls");
96  s.addArea("Magnus Egerstedt", "Controls");
97  s.addArea("Jun Ueda", "Controls");
98  s.addArea("Panos Tsiotras", "Controls");
99  s.addArea("Fumin Zhang", "Controls");
100  s.addArea("Ayanna Howard", "Controls");
101  s.addArea("Jeff Shamma", "Controls");
102 
103  s.addArea("Frank Dellaert", "Perception");
104  s.addArea("Henrik Christensen", "Perception");
105 
106  s.addArea("Mike Stilman", "AI");
107 // s.addArea("Henrik Christensen", "AI");
108 // s.addArea("Ayanna Howard", "AI");
109  s.addArea("Charles Isbell", "AI");
110 // s.addArea("Tucker Balch", "AI");
111  s.addArea("Andrea Thomaz", "AI");
112 
113  s.addArea("Ayanna Howard", "Autonomy");
114  s.addArea("Charlie Kemp", "Autonomy");
115 
116 // s.addArea("Andrea Thomaz", "HRI");
117  s.addArea("Karen Feigh", "HRI");
118 // s.addArea("Charlie Kemp", "HRI");
119 
120  // add students
121  if (addStudents)
122  for (size_t i = 0; i < nrStudents; i++)
123  addStudent(s, i);
124 
125  return s;
126 }
127 
128 /* ************************************************************************* */
130 
131  Scheduler scheduler = largeExample();
132  scheduler.print();
133 
134  // BUILD THE GRAPH !
135  size_t addMutex = 3;
136  SETDEBUG("Scheduler::buildGraph", true);
137  scheduler.buildGraph(addMutex);
138 
139  // Do brute force product and output that to file
140  if (scheduler.nrStudents() == 1) { // otherwise too slow
141  DecisionTreeFactor product = scheduler.product();
142  product.dot("scheduling-large", false);
143  }
144 
145  // Do exact inference
146  // SETDEBUG("timing-verbose", true);
147  SETDEBUG("DiscreteConditional::DiscreteConditional", true);
148 //#define SAMPLE
149 #ifdef SAMPLE
150  gttic(large);
151  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
152  gttoc(large);
154  tictoc_print();
155  for (size_t i=0;i<100;i++) {
156  DiscreteFactor::sharedValues assignment = sample(*chordal);
157  vector<size_t> stats(scheduler.nrFaculty());
158  scheduler.accumulateStats(assignment, stats);
159  size_t max = *max_element(stats.begin(), stats.end());
160  size_t min = *min_element(stats.begin(), stats.end());
161  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
162 // cout << min << ", " << max << ", " << nz << endl;
163  if (nz >= 13 && min >=1 && max <= 4) {
164  cout << "======================================================\n";
165  scheduler.printAssignment(assignment);
166  }
167  }
168 #else
169  gttic(large);
171  gttoc(large);
173  tictoc_print();
174  scheduler.printAssignment(MPE);
175 #endif
176 }
177 
178 /* ************************************************************************* */
179 // Solve a series of relaxed problems for maximum flexibility solution
180 void solveStaged(size_t addMutex = 2) {
181 
182  bool debug = false;
183 
184  // super-hack! just count...
185  SETDEBUG("DiscreteConditional::COUNT", true);
186  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
187 
188  // make a vector with slot availability, initially all 1
189  // Reads file to get count :-)
190  vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0);
191 
192  // now, find optimal value for each student, using relaxed mutex constraints
193  for (size_t s = 0; s < NRSTUDENTS; s++) {
194  // add all students first time, then drop last one second time, etc...
195  Scheduler scheduler = largeExample(NRSTUDENTS - s);
196 // scheduler.print(str(boost::format("Scheduler %d") % (NRSTUDENTS-s)));
197 
198  // only allow slots not yet taken
199  scheduler.setSlotsAvailable(slotsAvailable);
200 
201  // BUILD THE GRAPH !
202  scheduler.buildGraph(addMutex);
203 
204  // Do EXACT INFERENCE
205  gttic_(eliminate);
206  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
207  gttoc_(eliminate);
208 
209  // find root node
210  DiscreteConditional::shared_ptr root = chordal->back();
211  if (debug)
212  root->print(""/*scheduler.studentName(s)*/);
213 
214  // solve root node only
216  size_t bestSlot = root->solve(values);
217 
218  // get corresponding count
219  DiscreteKey dkey = scheduler.studentKey(NRSTUDENTS - 1 - s);
220  values[dkey.first] = bestSlot;
221  double count = (*root)(values);
222 
223  // remove this slot from consideration
224  slotsAvailable[bestSlot] = 0.0;
225  cout << boost::format("%s = %d (%d), count = %d") % scheduler.studentName(NRSTUDENTS-1-s)
226  % scheduler.slotName(bestSlot) % bestSlot % count << endl;
227  }
228  tictoc_print_();
229 }
230 
231 /* ************************************************************************* */
232 // Sample from solution found above and evaluate cost function
234  size_t slot, vector<Scheduler>& schedulers) {
235  Scheduler scheduler = largeExample(1,false);
236  addStudent(scheduler, i);
237  cout << " creating sampler for " << scheduler.studentName(0) << endl;
238  SETDEBUG("Scheduler::buildGraph", false);
239 // scheduler.print();
240  scheduler.addStudentSpecificConstraints(0, slot);
241  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
242  schedulers.push_back(scheduler);
243  return chordal;
244 }
245 
247 
248  size_t nrFaculty = 17; // Change to correct number !
249 
250  vector<Scheduler> schedulers;
251  vector<DiscreteBayesNet::shared_ptr> samplers(NRSTUDENTS);
252 
253  // Given the time-slots, we can create NRSTUDENTS independent samplers
254  vector<size_t> slots;
255  slots += 12,11,13, 21,16,1, 3,2,6, 7,22,4; // given slots
256  for (size_t i = 0; i < NRSTUDENTS; i++)
257  samplers[i] = createSampler(i, slots[i], schedulers);
258 
259  // now, sample schedules
260  for (size_t n = 0; n < 10000; n++) {
261  vector<size_t> stats(nrFaculty, 0);
262  vector<Scheduler::sharedValues> samples;
263  for (size_t i = 0; i < NRSTUDENTS; i++) {
264  samples.push_back(samplers[i]->sample());
265  schedulers[i].accumulateStats(samples[i], stats);
266  }
267  size_t max = *max_element(stats.begin(), stats.end());
268  size_t min = *min_element(stats.begin(), stats.end());
269  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
270  if (nz >= 16 && max <= 3) {
271  cout << boost::format(
272  "Sampled schedule %d, min = %d, nz = %d, max = %d\n") % (n + 1) % min
273  % nz % max;
274  for (size_t i = 0; i < NRSTUDENTS; i++) {
275  cout << schedulers[i].studentName(0) << " : " << schedulers[i].slotName(
276  slots[i]) << endl;
277  schedulers[i].printSpecial(samples[i]);
278  }
279  }
280  }
281 }
282 
283 /* ************************************************************************* */
284 int main() {
285 // runLargeExample();
286 // solveStaged(3);
287  sampleSolutions();
288  return 0;
289 }
290 /* ************************************************************************* */
291 
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
DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector< Scheduler > &schedulers)
#define gttic_(label)
Definition: timing.h:230
Global debugging flags.
#define min(a, b)
Definition: datatypes.h:19
bool NonZero(size_t i)
Scheduler largeExample(size_t nrStudents=NRSTUDENTS, bool addStudents=true)
int n
int main()
leaf::MyValues values
const std::string & studentName(size_t i) const
Definition: Scheduler.cpp:89
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
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 sampleSolutions()
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
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
void addStudent(Scheduler &s, size_t i)
std::vector< float > Values
void runLargeExample()
void print(const std::string &s="Scheduler", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: Scheduler.cpp:181
void solveStaged(size_t addMutex=2)
#define gttoc(label)
Definition: timing.h:281
boost::shared_ptr< Values > sharedValues
DiscreteBayesNet::shared_ptr eliminate() const
Definition: Scheduler.cpp:256
size_t nrFaculty() const
Definition: Scheduler.h:82
boost::shared_ptr< This > shared_ptr
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
size_t NRSTUDENTS
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
void accumulateStats(sharedValues assignment, std::vector< size_t > &stats) const
Definition: Scheduler.cpp:243


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