gtsam
Loading...
Searching...
No Matches
Scheduler.h
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#pragma once
9
10#include <gtsam_unstable/discrete/CSP.h>
12
13namespace gtsam {
14
22class GTSAM_UNSTABLE_EXPORT Scheduler : public CSP {
23 private:
25 struct Student {
26 std::string name_;
27 DiscreteKey key_; // key for student
28 std::vector<DiscreteKey> keys_; // key for areas
29 std::vector<std::string> areaName_;
30 std::vector<double> advisor_;
31 Student(size_t nrFaculty, size_t advisorIndex)
32 : keys_(3), areaName_(3), advisor_(nrFaculty, 1.0) {
33 advisor_[advisorIndex] = 0.0;
34 }
35 void print() const {
36 using std::cout;
37 cout << name_ << ": ";
38 for (size_t area = 0; area < 3; area++) cout << areaName_[area] << " ";
39 cout << std::endl;
40 }
41 };
42
44 size_t maxNrStudents_;
45
47 std::vector<Student> students_;
48
50 std::map<std::string, size_t> facultyIndex_;
51 std::vector<std::string> facultyName_, slotName_, areaName_;
52
54 typedef std::map<std::string, std::vector<double> > FacultyInArea;
55 FacultyInArea facultyInArea_;
56
58 std::string available_;
59
61 std::vector<double> slotsAvailable_;
62
63 public:
69 Scheduler(size_t maxNrStudents) : maxNrStudents_(maxNrStudents) {}
70
72 virtual ~Scheduler() {}
73
74 void addFaculty(const std::string& facultyName) {
75 facultyIndex_[facultyName] = nrFaculty();
76 facultyName_.push_back(facultyName);
77 }
78
79 size_t nrFaculty() const { return facultyName_.size(); }
80
82 void setAvailability(const std::string& available) { available_ = available; }
83
84 void addSlot(const std::string& slotName) { slotName_.push_back(slotName); }
85
86 size_t nrTimeSlots() const { return slotName_.size(); }
87
88 const std::string& slotName(size_t s) const { return slotName_[s]; }
89
91 void setSlotsAvailable(const std::vector<double>& slotsAvailable) {
92 slotsAvailable_ = slotsAvailable;
93 }
94
95 void addArea(const std::string& facultyName, const std::string& areaName) {
96 areaName_.push_back(areaName);
97 std::vector<double>& table =
98 facultyInArea_[areaName]; // will create if needed
99 if (table.empty()) table.resize(nrFaculty(), 0);
100 table[facultyIndex_[facultyName]] = 1;
101 }
102
107 Scheduler(size_t maxNrStudents, const std::string& filename);
108
110 const DiscreteKey& key(size_t s,
111 std::optional<size_t> area = {}) const;
112
114 void addStudent(const std::string& studentName, const std::string& area1,
115 const std::string& area2, const std::string& area3,
116 const std::string& advisor);
117
119 size_t nrStudents() const { return students_.size(); }
120
121 const std::string& studentName(size_t i) const;
122 const DiscreteKey& studentKey(size_t i) const;
123 const std::string& studentArea(size_t i, size_t area) const;
124
127 size_t i, std::optional<size_t> slot = {});
128
130 void buildGraph(size_t mutexBound = 7);
131
133 void print(
134 const std::string& s = "Scheduler",
135 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
136
138 void printAssignment(const DiscreteValues& assignment) const;
139
141 void printSpecial(const DiscreteValues& assignment) const;
142
144 void accumulateStats(const DiscreteValues& assignment,
145 std::vector<size_t>& stats) const;
146
148 DiscreteBayesNet::shared_ptr eliminate() const;
149
152
155
156}; // Scheduler
157
158} // namespace gtsam
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:38
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
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
Thread pool scheduler parameterized by a queue Policy.
Definition Scheduler.h:63
size_t nrStudents() const
current number of students
Definition Scheduler.h:119
Scheduler(size_t maxNrStudents, const std::string &filename)
Constructor that reads in faculty, slots, availibility.
Definition Scheduler.cpp:23
DiscreteValues bestAssignment(const DiscreteValues &bestSchedule) const
find the corresponding most desirable committee assignment
Definition Scheduler.cpp:267
const DiscreteKey & key(size_t s, std::optional< size_t > area={}) const
get key for student and area, 0 is time slot itself
Definition Scheduler.cpp:82
void addStudent(const std::string &studentName, const std::string &area1, const std::string &area2, const std::string &area3, const std::string &advisor)
addStudent has to be called after adding slots and faculty
Definition Scheduler.cpp:57
void print(const std::string &s="Scheduler", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition Scheduler.cpp:177
void printAssignment(const DiscreteValues &assignment) const
Print readable form of assignment.
Definition Scheduler.cpp:206
void accumulateStats(const DiscreteValues &assignment, std::vector< size_t > &stats) const
Accumulate faculty stats.
Definition Scheduler.cpp:234
void printSpecial(const DiscreteValues &assignment) const
Special print for single-student case.
Definition Scheduler.cpp:224
void setSlotsAvailable(const std::vector< double > &slotsAvailable)
slots available, boolean
Definition Scheduler.h:91
DiscreteValues bestSchedule() const
find the assignment of students to slots with most possible committees
Definition Scheduler.cpp:260
void setAvailability(const std::string &available)
boolean std::string of nrTimeSlots * nrFaculty
Definition Scheduler.h:82
Scheduler(size_t maxNrStudents)
Constructor We need to know the number of students in advance for ordering keys.
Definition Scheduler.h:69
virtual ~Scheduler()
Destructor.
Definition Scheduler.h:72
DiscreteBayesNet::shared_ptr eliminate() const
Eliminate, return a Bayes net.
Definition Scheduler.cpp:247
void buildGraph(size_t mutexBound=7)
Main routine that builds factor graph.
Definition Scheduler.cpp:150
void addStudentSpecificConstraints(size_t i, std::optional< size_t > slot={})
Add student-specific constraints to the graph.
Definition Scheduler.cpp:103
A map from keys to values.
Definition DiscreteValues.h:34
Constraint Satisfaction Problem class A specialization of a DiscreteFactorGraph.
Definition CSP.h:22