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
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 #define SAMPLE
120 #ifdef SAMPLE
121  gttic(large);
122  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
123  gttoc(large);
125  tictoc_print();
126  for (size_t i=0;i<100;i++) {
127  auto assignment = chordal->sample();
128  vector<size_t> stats(scheduler.nrFaculty());
129  scheduler.accumulateStats(assignment, stats);
130  size_t max = *max_element(stats.begin(), stats.end());
131  size_t min = *min_element(stats.begin(), stats.end());
132  size_t nz = count_if(stats.begin(), stats.end(), NonZero);
133 // cout << min << ", " << max << ", " << nz << endl;
134  if (nz >= 13 && min >=1 && max <= 4) {
135  cout << "======================================================\n";
136  scheduler.printAssignment(assignment);
137  }
138  }
139 #else
140  gttic(large);
141  auto MPE = scheduler.optimize();
142  gttoc(large);
144  tictoc_print();
145  scheduler.printAssignment(MPE);
146 #endif
147 }
148 
149 /* ************************************************************************* */
150 // Solve a series of relaxed problems for maximum flexibility solution
151 void solveStaged(size_t addMutex = 2) {
152 
153  // super-hack! just count...
154  bool debug = false;
155  SETDEBUG("DiscreteConditional::COUNT", true);
156  SETDEBUG("DiscreteConditional::DiscreteConditional", debug); // progress
157 
158  // make a vector with slot availability, initially all 1
159  // Reads file to get count :-)
160  vector<double> slotsAvailable(largeExample(0).nrTimeSlots(), 1.0);
161 
162  // now, find optimal value for each student, using relaxed mutex constraints
163  for (size_t s = 0; s < NRSTUDENTS; s++) {
164  // add all students first time, then drop last one second time, etc...
165  Scheduler scheduler = largeExample(NRSTUDENTS - s);
166  //scheduler.print(str(boost::format("Scheduler %d") % (NRSTUDENTS-s)));
167 
168  // only allow slots not yet taken
169  scheduler.setSlotsAvailable(slotsAvailable);
170 
171  // BUILD THE GRAPH !
172  scheduler.buildGraph(addMutex);
173 
174  // Do EXACT INFERENCE
175  gttic_(eliminate);
176  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
177  gttoc_(eliminate);
178 
179  // find root node
180  DiscreteConditional::shared_ptr root = chordal->back();
181  if (debug)
182  root->print(""/*scheduler.studentName(s)*/);
183 
184  // solve root node only
185  size_t bestSlot = root->argmax();
186 
187  // get corresponding count
188  DiscreteKey dkey = scheduler.studentKey(NRSTUDENTS - 1 - s);
190  values[dkey.first] = bestSlot;
191  size_t count = (*root)(values);
192 
193  // remove this slot from consideration
194  slotsAvailable[bestSlot] = 0.0;
195  cout << scheduler.studentName(NRSTUDENTS - 1 - s) << " = " <<
196  scheduler.slotName(bestSlot) << " (" << bestSlot
197  << "), count = " << count << endl;
198  }
199  tictoc_print_();
200 }
201 
202 /* ************************************************************************* */
203 // Sample from solution found above and evaluate cost function
205  size_t slot, vector<Scheduler>& schedulers) {
206  Scheduler scheduler = largeExample(0); // todo: wrong nr students
207  addStudent(scheduler, i);
208  SETDEBUG("Scheduler::buildGraph", false);
209  scheduler.addStudentSpecificConstraints(0, slot);
210  DiscreteBayesNet::shared_ptr chordal = scheduler.eliminate();
211  schedulers.push_back(scheduler);
212  return chordal;
213 }
214 
216 
217  vector<Scheduler> schedulers;
218  vector<DiscreteBayesNet::shared_ptr> samplers(NRSTUDENTS);
219 
220  // Given the time-slots, we can create NRSTUDENTS independent samplers
221  vector<size_t> slots{3, 20, 2, 6, 5, 11, 1, 4}; // given slots
222  for (size_t i = 0; i < NRSTUDENTS; 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<DiscreteValues> samples;
229  for (size_t i = 0; i < NRSTUDENTS; 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 << "Sampled schedule " << (n + 1) << ", min = " << min
238  << ", nz = " << nz << ", max = " << max << endl;
239  for (size_t i = 0; i < NRSTUDENTS; i++) {
240  cout << schedulers[i].studentName(0) << " : " << schedulers[i].slotName(
241  slots[i]) << endl;
242  schedulers[i].printSpecial(samples[i]);
243  }
244  }
245  }
246 }
247 
248 /* ************************************************************************* */
249 int main() {
250 // runLargeExample();
251  solveStaged(3);
252 // sampleSolutions();
253  return 0;
254 }
255 /* ************************************************************************* */
256 
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
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
NonZero
bool NonZero(size_t i)
Definition: schedulingQuals12.cpp:22
gtsam::Scheduler::accumulateStats
void accumulateStats(const DiscreteValues &assignment, std::vector< size_t > &stats) const
Definition: Scheduler.cpp:234
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
createSampler
DiscreteBayesNet::shared_ptr createSampler(size_t i, size_t slot, vector< Scheduler > &schedulers)
Definition: schedulingQuals12.cpp:204
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
runLargeExample
void runLargeExample()
Definition: schedulingQuals12.cpp:99
n
int n
Definition: BiCGSTAB_simple.cpp:1
sampleSolutions
void sampleSolutions()
Definition: schedulingQuals12.cpp:215
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
main
int main()
Definition: schedulingQuals12.cpp:249
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
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
largeExample
Scheduler largeExample(size_t nrStudents=NRSTUDENTS)
Definition: schedulingQuals12.cpp:60
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
addStudent
void addStudent(Scheduler &s, size_t i)
Definition: schedulingQuals12.cpp:27
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
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
NRSTUDENTS
size_t NRSTUDENTS
Definition: schedulingQuals12.cpp:20
gtsam::Scheduler::nrFaculty
size_t nrFaculty() const
Definition: Scheduler.h:79
max
#define max(a, b)
Definition: datatypes.h:20
solveStaged
void solveStaged(size_t addMutex=2)
Definition: schedulingQuals12.cpp:151
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::Scheduler::studentKey
const DiscreteKey & studentKey(size_t i) const
Definition: Scheduler.cpp:92
gttic
#define gttic(label)
Definition: timing.h:326
debug.h
Global debugging flags.


gtsam
Author(s):
autogenerated on Fri Mar 28 2025 03:03:21