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


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