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