Scheduler.cpp
Go to the documentation of this file.
1 /*
2  * Scheduler.h
3  * @brief an example how inference can be used for scheduling qualifiers
4  * @date Mar 26, 2011
5  * @author Frank Dellaert
6  */
7 
8 #include <gtsam/base/debug.h>
9 #include <gtsam/base/timing.h>
12 
13 #include <boost/tokenizer.hpp>
14 #include <cmath>
15 #include <fstream>
16 #include <iomanip>
17 #include <cassert>
18 
19 namespace gtsam {
20 
21 using namespace std;
22 
23 Scheduler::Scheduler(size_t maxNrStudents, const string& filename)
24  : maxNrStudents_(maxNrStudents) {
25  typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
26 
27  // open file
28  ifstream is(filename.c_str());
29  if (!is) {
30  cerr << "Scheduler: could not open file " << filename << endl;
31  throw runtime_error("Scheduler: could not open file " + filename);
32  }
33 
34  string line; // buffer
35 
36  // process first line with faculty
37  if (getline(is, line, '\r')) {
38  Tokenizer tok(line);
39  Tokenizer::iterator it = tok.begin();
40  for (++it; it != tok.end(); ++it) addFaculty(*it);
41  }
42 
43  // for all remaining lines
44  size_t count = 0;
45  while (getline(is, line, '\r')) {
46  if (count++ > 100) throw runtime_error("reached 100 lines, exiting");
47  Tokenizer tok(line);
48  Tokenizer::iterator it = tok.begin();
49  addSlot(*it++); // add slot
50  // add availability
51  for (; it != tok.end(); ++it) available_ += (it->empty()) ? "0 " : "1 ";
52  available_ += '\n';
53  }
54 } // constructor
55 
57 void Scheduler::addStudent(const string& studentName, const string& area1,
58  const string& area2, const string& area3,
59  const string& advisor) {
60  assert(nrStudents() < maxNrStudents_);
61  assert(facultyInArea_.count(area1));
62  assert(facultyInArea_.count(area2));
63  assert(facultyInArea_.count(area3));
64  size_t advisorIndex = facultyIndex_[advisor];
65  Student student(nrFaculty(), advisorIndex);
66  student.name_ = studentName;
67  // We fix the ordering by assigning a higher index to the student
68  // and numbering the areas lower
69  Key j = 3 * maxNrStudents_ + nrStudents();
70  student.key_ = DiscreteKey(j, nrTimeSlots());
71  Key base = 3 * nrStudents();
72  student.keys_[0] = DiscreteKey(base + 0, nrFaculty());
73  student.keys_[1] = DiscreteKey(base + 1, nrFaculty());
74  student.keys_[2] = DiscreteKey(base + 2, nrFaculty());
75  student.areaName_[0] = area1;
76  student.areaName_[1] = area2;
77  student.areaName_[2] = area3;
78  students_.push_back(student);
79 }
80 
83  std::optional<size_t> area) const {
84  return area ? students_[s].keys_[*area] : students_[s].key_;
85 }
86 
87 const string& Scheduler::studentName(size_t i) const {
88  assert(i < nrStudents());
89  return students_[i].name_;
90 }
91 
92 const DiscreteKey& Scheduler::studentKey(size_t i) const {
93  assert(i < nrStudents());
94  return students_[i].key_;
95 }
96 
97 const string& Scheduler::studentArea(size_t i, size_t area) const {
98  assert(i < nrStudents());
99  return students_[i].areaName_[area];
100 }
101 
104  std::optional<size_t> slot) {
105  bool debug = ISDEBUG("Scheduler::buildGraph");
106 
107  assert(i < nrStudents());
108  const Student& s = students_[i];
109 
110  if (!slot && !slotsAvailable_.empty()) {
111  if (debug) cout << "Adding availability of slots" << endl;
112  assert(slotsAvailable_.size() == s.key_.second);
113  CSP::add(s.key_, slotsAvailable_);
114  }
115 
116  // For all areas
117  for (size_t area = 0; area < 3; area++) {
118  DiscreteKey areaKey = s.keys_[area];
119  const string& areaName = s.areaName_[area];
120 
121  if (debug) cout << "Area constraints " << areaName << endl;
122  assert(facultyInArea_[areaName].size() == areaKey.second);
123  CSP::add(areaKey, facultyInArea_[areaName]);
124 
125  if (debug) cout << "Advisor constraint " << areaName << endl;
126  assert(s.advisor_.size() == areaKey.second);
127  CSP::add(areaKey, s.advisor_);
128 
129  if (debug) cout << "Availability of faculty " << areaName << endl;
130  if (slot) {
131  // get all constraints then specialize to slot
132  size_t dummyIndex = maxNrStudents_ * 3 + maxNrStudents_;
133  DiscreteKey dummy(dummyIndex, nrTimeSlots());
135  available_); // available_ is Doodle string
136  auto q = p.choose(dummyIndex, *slot);
137  CSP::add(areaKey, q);
138  } else {
139  DiscreteKeys keys {s.key_, areaKey};
140  CSP::add(keys, available_); // available_ is Doodle string
141  }
142  }
143 
144  // add mutex
145  if (debug) cout << "Mutex for faculty" << endl;
146  addAllDiff(s.keys_[0] & s.keys_[1] & s.keys_[2]);
147 }
148 
150 void Scheduler::buildGraph(size_t mutexBound) {
151  bool debug = ISDEBUG("Scheduler::buildGraph");
152 
153  if (debug) cout << "Adding student-specific constraints" << endl;
154  for (size_t i = 0; i < nrStudents(); i++) addStudentSpecificConstraints(i);
155 
156  // special constraint for MN
157  if (studentName(0) == "Michael N")
158  CSP::add(studentKey(0), "0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1");
159 
160  if (!mutexBound) {
161  DiscreteKeys dkeys;
162  for (const Student& s : students_) dkeys.push_back(s.key_);
163  addAllDiff(dkeys);
164  } else {
165  if (debug) cout << "Mutex for Students" << endl;
166  for (size_t i1 = 0; i1 < nrStudents(); i1++) {
167  // if mutexBound=1, we only mutex with next student
168  size_t bound = min((i1 + 1 + mutexBound), nrStudents());
169  for (size_t i2 = i1 + 1; i2 < bound; i2++) {
171  }
172  }
173  }
174 } // buildGraph
175 
177 void Scheduler::print(const string& s, const KeyFormatter& formatter) const {
178  cout << s << " Faculty:" << endl;
179  for (const string& name : facultyName_) cout << name << '\n';
180  cout << endl;
181 
182  cout << s << " Slots:\n";
183  size_t i = 0;
184  for (const string& name : slotName_) cout << i++ << " " << name << endl;
185  cout << endl;
186 
187  cout << "Availability:\n" << available_ << '\n';
188 
189  cout << s << " Area constraints:\n";
190  for (const FacultyInArea::value_type& it : facultyInArea_) {
191  cout << setw(12) << it.first << ": ";
192  for (double v : it.second) cout << v << " ";
193  cout << '\n';
194  }
195  cout << endl;
196 
197  cout << s << " Students:\n";
198  for (const Student& student : students_) student.print();
199  cout << endl;
200 
201  CSP::print(s + " Factor graph");
202  cout << endl;
203 } // print
204 
206 void Scheduler::printAssignment(const DiscreteValues& assignment) const {
207  // Not intended to be general! Assumes very particular ordering !
208  cout << endl;
209  for (size_t s = 0; s < nrStudents(); s++) {
210  Key j = 3 * maxNrStudents_ + s;
211  size_t slot = assignment.at(j);
212  cout << studentName(s) << " slot: " << slotName_[slot] << endl;
213  Key base = 3 * s;
214  for (size_t area = 0; area < 3; area++) {
215  size_t faculty = assignment.at(base + area);
216  cout << setw(12) << studentArea(s, area) << ": " << facultyName_[faculty]
217  << endl;
218  }
219  cout << endl;
220  }
221 }
222 
224 void Scheduler::printSpecial(const DiscreteValues& assignment) const {
225  DiscreteValues::const_iterator it = assignment.begin();
226  for (size_t area = 0; area < 3; area++, it++) {
227  size_t f = it->second;
228  cout << setw(12) << studentArea(0, area) << ": " << facultyName_[f] << endl;
229  }
230  cout << endl;
231 }
232 
235  vector<size_t>& stats) const {
236  for (size_t s = 0; s < nrStudents(); s++) {
237  Key base = 3 * s;
238  for (size_t area = 0; area < 3; area++) {
239  size_t f = assignment.at(base + area);
240  assert(f < stats.size());
241  stats[f]++;
242  } // area
243  } // s
244 }
245 
248  gttic(my_eliminate);
249  // TODO: fix this!!
250  size_t maxKey = keys().size();
251  Ordering defaultKeyOrdering;
252  for (size_t i = 0; i < maxKey; ++i) defaultKeyOrdering.push_back(i);
254  this->eliminateSequential(defaultKeyOrdering);
255  gttoc(my_eliminate);
256  return chordal;
257 }
258 
261  DiscreteValues best;
262  throw runtime_error("bestSchedule not implemented");
263  return best;
264 }
265 
268  DiscreteValues best;
269  throw runtime_error("bestAssignment not implemented");
270  return best;
271 }
272 
273 } // namespace gtsam
gttoc
#define gttoc(label)
Definition: timing.h:296
timing.h
Timing utilities.
gtsam::EliminateableFactorGraph< DiscreteFactorGraph >::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
gtsam::Scheduler::addFaculty
void addFaculty(const std::string &facultyName)
Definition: Scheduler.h:74
name
Annotation for function names.
Definition: attr.h:51
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::Scheduler::facultyInArea_
FacultyInArea facultyInArea_
Definition: Scheduler.h:55
gtsam::bound
double bound(double a, double min, double max)
Definition: PoseRTV.cpp:19
DiscreteFactorGraph.h
gtsam::DiscreteFactorGraph::keys
KeySet keys() const
Definition: DiscreteFactorGraph.cpp:45
gtsam::Scheduler::slotName_
std::vector< std::string > slotName_
Definition: Scheduler.h:51
gtsam::maxKey
Key maxKey(const PROBLEM &problem)
Definition: ActiveSetSolver.h:191
gtsam::Scheduler::Student::keys_
std::vector< DiscreteKey > keys_
Definition: Scheduler.h:28
gtsam::Scheduler::accumulateStats
void accumulateStats(const DiscreteValues &assignment, std::vector< size_t > &stats) const
Definition: Scheduler.cpp:234
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::DiscreteFactorGraph::print
void print(const std::string &s="DiscreteFactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: DiscreteFactorGraph.cpp:85
gtsam::Scheduler::students_
std::vector< Student > students_
Definition: Scheduler.h:47
gtsam::Scheduler::printSpecial
void printSpecial(const DiscreteValues &assignment) const
Definition: Scheduler.cpp:224
pybind_wrapper_test_script.dummy
dummy
Definition: pybind_wrapper_test_script.py:42
gtsam::Scheduler::Student::areaName_
std::vector< std::string > areaName_
Definition: Scheduler.h:29
gtsam::AlgebraicDecisionTree< Key >
gtsam::Scheduler::maxNrStudents_
size_t maxNrStudents_
Definition: Scheduler.h:44
ISDEBUG
#define ISDEBUG(S)
Definition: debug.h:60
gtsam::Scheduler::bestSchedule
DiscreteValues bestSchedule() const
Definition: Scheduler.cpp:260
gtsam::Scheduler::addSlot
void addSlot(const std::string &slotName)
Definition: Scheduler.h:84
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Scheduler::addStudentSpecificConstraints
void addStudentSpecificConstraints(size_t i, std::optional< size_t > slot={})
Definition: Scheduler.cpp:103
stats
bool stats
Definition: SolverComparer.cpp:100
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
relicense.filename
filename
Definition: relicense.py:57
gtsam::Scheduler::Student::key_
DiscreteKey key_
Definition: Scheduler.h:27
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::Scheduler::studentArea
const std::string & studentArea(size_t i, size_t area) const
Definition: Scheduler.cpp:97
debug
static constexpr bool debug
Definition: testDiscreteBayesTree.cpp:31
gtsam::CSP::addAllDiff
void addAllDiff(const DiscreteKey &key1, const DiscreteKey &key2)
Add a binary AllDiff constraint.
Definition: CSP.h:31
gtsam::Scheduler::eliminate
DiscreteBayesNet::shared_ptr eliminate() const
Definition: Scheduler.cpp:247
gtsam::Scheduler::facultyName_
std::vector< std::string > facultyName_
Definition: Scheduler.h:51
gtsam::Scheduler::slotsAvailable_
std::vector< double > slotsAvailable_
Definition: Scheduler.h:61
Scheduler.h
gtsam::Scheduler::facultyIndex_
std::map< std::string, size_t > facultyIndex_
Definition: Scheduler.h:50
gtsam::Scheduler::printAssignment
void printAssignment(const DiscreteValues &assignment) const
Definition: Scheduler.cpp:206
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::FactorGraph< DiscreteFactor >::size
size_t size() const
Definition: FactorGraph.h:297
gtsam
traits
Definition: SFMdata.h:40
i1
double i1(double x)
Definition: i1.c:150
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Scheduler::Scheduler
Scheduler(size_t maxNrStudents)
Definition: Scheduler.h:69
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::Scheduler::bestAssignment
DiscreteValues bestAssignment(const DiscreteValues &bestSchedule) const
Definition: Scheduler.cpp:267
std
Definition: BFloat16.h:88
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::Scheduler::nrStudents
size_t nrStudents() const
current number of students
Definition: Scheduler.h:119
gtsam::Scheduler::available_
std::string available_
Definition: Scheduler.h:58
gtsam::DiscreteFactorGraph::add
void add(Args &&... args)
Definition: DiscreteFactorGraph.h:139
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::DiscreteBayesNet::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: DiscreteBayesNet.h:43
gtsam::Scheduler::Student::name_
std::string name_
Definition: Scheduler.h:26
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::Scheduler::buildGraph
void buildGraph(size_t mutexBound=7)
Definition: Scheduler.cpp:150
gtsam::Scheduler::key
const DiscreteKey & key(size_t s, std::optional< size_t > area={}) const
Definition: Scheduler.cpp:82
gtsam::Scheduler::nrFaculty
size_t nrFaculty() const
Definition: Scheduler.h:79
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::Ordering
Definition: inference/Ordering.h:33
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
gtsam::Scheduler::Student
Definition: Scheduler.h:25
gttic
#define gttic(label)
Definition: timing.h:295
debug.h
Global debugging flags.


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:13:09