gtsam 4.1.1
gtsam
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>
11
12namespace gtsam {
13
21class GTSAM_UNSTABLE_EXPORT Scheduler : public CSP {
22 private:
24 struct Student {
25 std::string name_;
26 DiscreteKey key_; // key for student
27 std::vector<DiscreteKey> keys_; // key for areas
28 std::vector<std::string> areaName_;
29 std::vector<double> advisor_;
30 Student(size_t nrFaculty, size_t advisorIndex)
31 : keys_(3), areaName_(3), advisor_(nrFaculty, 1.0) {
32 advisor_[advisorIndex] = 0.0;
33 }
34 void print() const {
35 using std::cout;
36 cout << name_ << ": ";
37 for (size_t area = 0; area < 3; area++) cout << areaName_[area] << " ";
38 cout << std::endl;
39 }
40 };
41
43 size_t maxNrStudents_;
44
46 std::vector<Student> students_;
47
49 std::map<std::string, size_t> facultyIndex_;
50 std::vector<std::string> facultyName_, slotName_, areaName_;
51
53 typedef std::map<std::string, std::vector<double> > FacultyInArea;
54 FacultyInArea facultyInArea_;
55
57 std::string available_;
58
60 std::vector<double> slotsAvailable_;
61
62 public:
68 Scheduler(size_t maxNrStudents) : maxNrStudents_(maxNrStudents) {}
69
71 virtual ~Scheduler() {}
72
73 void addFaculty(const std::string& facultyName) {
74 facultyIndex_[facultyName] = nrFaculty();
75 facultyName_.push_back(facultyName);
76 }
77
78 size_t nrFaculty() const { return facultyName_.size(); }
79
81 void setAvailability(const std::string& available) { available_ = available; }
82
83 void addSlot(const std::string& slotName) { slotName_.push_back(slotName); }
84
85 size_t nrTimeSlots() const { return slotName_.size(); }
86
87 const std::string& slotName(size_t s) const { return slotName_[s]; }
88
90 void setSlotsAvailable(const std::vector<double>& slotsAvailable) {
91 slotsAvailable_ = slotsAvailable;
92 }
93
94 void addArea(const std::string& facultyName, const std::string& areaName) {
95 areaName_.push_back(areaName);
96 std::vector<double>& table =
97 facultyInArea_[areaName]; // will create if needed
98 if (table.empty()) table.resize(nrFaculty(), 0);
99 table[facultyIndex_[facultyName]] = 1;
100 }
101
106 Scheduler(size_t maxNrStudents, const std::string& filename);
107
109 const DiscreteKey& key(size_t s,
110 boost::optional<size_t> area = boost::none) const;
111
113 void addStudent(const std::string& studentName, const std::string& area1,
114 const std::string& area2, const std::string& area3,
115 const std::string& advisor);
116
118 size_t nrStudents() const { return students_.size(); }
119
120 const std::string& studentName(size_t i) const;
121 const DiscreteKey& studentKey(size_t i) const;
122 const std::string& studentArea(size_t i, size_t area) const;
123
125 void addStudentSpecificConstraints(
126 size_t i, boost::optional<size_t> slot = boost::none);
127
129 void buildGraph(size_t mutexBound = 7);
130
132 void print(
133 const std::string& s = "Scheduler",
134 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
135
137 void printAssignment(sharedValues assignment) const;
138
140 void printSpecial(sharedValues assignment) const;
141
143 void accumulateStats(sharedValues assignment,
144 std::vector<size_t>& stats) const;
145
147 DiscreteBayesNet::shared_ptr eliminate() const;
148
150 sharedValues optimalAssignment() const;
151
153 sharedValues bestSchedule() const;
154
156 sharedValues bestAssignment(sharedValues bestSchedule) const;
157
158}; // Scheduler
159
160} // namespace gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
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
Constraint Satisfaction Problem class A specialization of a DiscreteFactorGraph.
Definition: CSP.h:21
Scheduler class Creates one variable for each student, and three variables for each of the student's ...
Definition: Scheduler.h:21
void setSlotsAvailable(const std::vector< double > &slotsAvailable)
slots available, boolean
Definition: Scheduler.h:90
Scheduler(size_t maxNrStudents)
Constructor We need to know the number of students in advance for ordering keys.
Definition: Scheduler.h:68
size_t nrStudents() const
current number of students
Definition: Scheduler.h:118
void setAvailability(const std::string &available)
boolean std::string of nrTimeSlots * nrFaculty
Definition: Scheduler.h:81
virtual ~Scheduler()
Destructor.
Definition: Scheduler.h:71