Class Scheduling to Boolean satisfiability [Polynomial-time reduction] part 2 - algorithm

I asked few days ago, a question about how to transform a University Class Scheduling Problem into a Boolean Satisfiability Problem.
(Class Scheduling to Boolean satisfiability [Polynomial-time reduction])
I got an answer by #Amit who was very elegant and easy to code.
Basically, his answer was like this : instead of considering courses, he considered time-intervals.
So for the i-th course, he just indicted all the possible intervals for this course. And we obtain a solution when there is at least 1 true-interval for every course and when no interval overlap an other.
This methods works very well when we consider only courses and nothing else. I generalize it by encoding the room inside the interval.
for example, instead of [8-10] to say that a course can happen between 8am and 10am.
I used [0.00801 - 0.01001] to say that a course can happen between 8am and 10am in the room 1.
I'm sure that you are currently wandering "why use double ?" well, because here come my problem :
To continue to generalize this method, I encode also the n° of the teacher in this interval.
I used [1.00801 - 1.01001] to say that a course can happen between 8am and 10am in the room 1 and be taught by the teacher n°1.
Here is what I got for now :
like this [1.008XX - 1.010XX] can happen in the same time as [2.008YY - 2.010YY], which is true, if the teacher 1 is teaching in the room X between 8am and 10am, the teacher 2 can teach also in Y between 8am and 10am, if and only if the room is available.
The problem is : with this method I cannot assure that XX and YY will be different and that YY will be available, because [1.008XX - 1.010XX] don't overlap [2.008XX - 2.010XX], so for now, the solver consider this possible.
And I still don't have any clue on how to assure this, by using this interval-method...
I need a way to encode {Interval, room and teacher-id} in order that :
a teacher cannot be in 2 places in the same interval.
there cannot be 2 teachers in the same room for the same interval.
there is a least 1 interval true by course.
Thanks in advance for your help,
Best regards !
Follow up question: Class Scheduling to Boolean satisfiability [Polynomial-time reduction] Final Part

This answer is extension of Part 1's answer, and uses the same notations when possible.
Ok, assume each interval is assigned to one teacher (if more than one teacher can take the interval, just have multiple instances of it, with different teachers per instance), so to indicate teacher t teaches in a classroom p at time x to y, we can use the old variable that this class is given - V_{i,j} - for the class and interval.
For each teacher t , and for each pair of intervals c=(x1,y1), d=(x2,y2) in classes (a,b) the teacher might participate in, add the clause:
Q_{t,i,j} = Not(V_ac) OR Not(V_bd) OR Smaller(y1,x2) OR Smaller(y2,x1)
Intuitively, the above clause guarantees a teacher cannot be in the same time in two places - no intervals overlap that the same teacher is assigned to them.
By chaining each pair (i,j) for each teacher t with AND to the original formula, it satisfies your first constraint - a teacher cannot be in 2 places in the same interval. - since each teacher cannot be in two places at the same time.
Your second constraint there cannot be 2 teachers in the same room for the same interval. is also satisfied by the fact that there cannot be two classes that overlap the time and class.
The 3rd constraint there is a least 1 interval true by course. is satisfied by the F1 clause, since you have to choose at least one interval (with one teacher assigned) for each course.

Related

How to design an algorithm to put elements into groups with constraints?

I was given a task of putting students into groups (to prepare a coding camp), but with several constraints. Though I've finished the task by hand, I'd like to know is there already exist some algorithms for tasks like this, or how can I design such an algorithm.
Background: 40 students in total, with these attributes:
gender: F/M
grade: Year 1/2
school: School 1/School 2/...
early assessment result: Rank from 1 to 40
Constraints: All of them needs to be satisfied.
Exactly 4 people per group
Each group needs to have at least a girl
Each group needs to have at least a Year 2 student
4 group members needs to come from 4 different schools
Each group needs to have at least a student who ranked top 10 in early assessment
What I'm expecting:
The Best: An existing algorithm/program for these kind of problems
Or, An algorithm for this specific problem
Or at least, Some ideas of creating an algorithm for this specific problem
My thoughts:
Since I've successed in making groups by hand, I know that such a solution indeed exists for my current dataset. But if I need an algorithm to find a solution for me, it should first try to check whether a solution even exists, by check if the number of girl / Year 2 students is greater than 10 (with pigeonhole principle), and some other conditions. And obviously, Constraint 5 is the easiest, and can provide a base solution for the rest. However, I still can not find a systematic way of doing it. Perhaps bruteforce and randomization can help? I'm not sure.
And sorry, since the data is confidential, I can not post it.
Update: After consulting a friend, here is a possible method:
First put the top 1 to 10 into 10 different groups.
Then iterate through groups. If the only person in the group is a boy/girl, try to add a girl/boy from a different school.
Then the problem size is reduced from 2^40 to 2^20, making bruthforce a viable solution.

A specific scheduling algorithm

The question I have concerns a hobby project that I'm working on to help out my wife with her work.
I realize that the problem I'm facing is quite similar to what's been answered on SO in numerous threads. However, I can't seem to find any thread that would address one little specific difference that I need to count in.
Here's a detailed description of the problem:
A teacher has numerous students that she teaches. Each student needs to have two lessons every week at least two days apart (i.e. Monday + Wednesday, Tuesday + Thursday etc.). The lessons are individual meaning there's only one student and the teacher in a class at a time. The teacher needs to have exactly one lunch break every day. The lunch break can be any time between two classes - the only condition is: a day cannot start or end in a lunch break. Each student provides their availability during a week (a list of possible time slots they can attend the class).
So far, this looks like a "regular" scheduling problem that there's bunch of material available online on.
But here's the catch: students are in different age meaning their lesson takes either 30 or 45 minutes. Younger students' lesson takes 30 minutes and olders' - 45 minutes. The goal of the algorithm is to find the optimal weekly schedule for the teacher. By optimal I mean a schedule where the teacher needs to spend least amount of time in school - all lessons back to back with no need for the teacher to wait for another student.
My first attempt was to come with all possible permutations of students' classes, lunch breaks and week days but for a 3 student schedule I came up with 13! permutations (that's roughly around 6 bln permutations). Here's why 13:
3 students - 2 classes each
4 lunch breaks (a 4 day week to keep the numbers small)
3 day breaks (a "night" so to speak)
The above gave me 13 elements to try in different permutations. However, after calculating the factorial I gave that up - 6 bln permutations will not be tried in any reasonable time on a "regular" machine. And that's just counting 3 students, the real-life data will be closer to 9 or 10. Obviously, the nonsense permutations (2 "nights" in a row, 2 classes for the same student on the same day and so on) would be thrown right away but still...
That was when I realized I'm not going to do this inventing the wheel by myself.
I did some research and came across Hopcroft-Karp algorithm. However I don't think it can be applied in a mixed timespan slots scenario (30/45 minute lessons). Am I wrong? Then I found some info on genetic algorithms. However, again I am not sure if they can take the mixed timespan condition into account?
I would greatly appreciate any pointers as to which directions my research should go.

scheduling/ matching/ preference algorithm

I have this problem but not sure what algorithm it belongs to.
We are trying to make a scheduling system where the users can choose the time preferences and then they are grouped into classes with their most preferred time.
Let say I have 100 users. Those users have their time preferences. We want to divide them into 4 -> 6 class with about 20 -> 25 students in each class. My question is
How to schedule them into the class time that they most preferred with the least amount of classes used ?
(Another constraint factor we have is the amount of teachers we have and the maximum hours a week they can teach. Also we want to be able to have makeup class. For eg: students who miss class this week can be reschedule for next week. )
One way to go about a multi-objective optimization problem like this is to find a solution that satisfies one objective, and then use local search to attempt to satisfy the remaining constraints.
For example, if you ignore the constraint that you would like to minimize the number of classes used, then you can treat this as a variant on the Stable Marriage Problem (or weighted bipartite graph matching problem) - this has the nice property that it can be solved in polynomial time. Your variant on the problem is most similar to the "hospitals/residents" problem (assigning many residents to a few hospitals based on preference).
This will leave you with several classes with only a few students in them, so you next perform a local search to satisfy the "minimize the number of classes" constraint (if you formulated the Stable Marriage algorithm correctly then you should have already satisfied the "no class exceeds 25 students" constraint) - from there you have two options:
Sort the classes from fewest to most students and close the classes with the fewest students, reassigning the evicted students to the remaining classes
Continue to take the students' preferences into account and sort the classes from least-preferred to most-preferred (so if you have 5 students in a class who assigned it a weight of 10 then you would first close a class with 10 students who assigned it a weight of 2).
You would then perform another local search to satisfy the teachers' hours constraints - you would perform the "teachers can't teach more than X hours" search after you perform the "minimize the number of classes" search, since the latter optimization will make it easier to perform the former optimization.
If the resulting algorithm is fast enough then you can randomize it and run it a few dozen times, saving the best result. For example, rather than closing the class with the fewest students first, randomly select a class to close (weight the selection so that it usually selects the smallest class - a completely random search won't perform well)
It's possible that you'll find that one of your constraints is causing a lot of conflicts, e.g. when satisfying the teachers' hours constraint you discover that you're having to rearrange a large percentage of the students. If this occurs then change the constraint ordering, so that satisfying the teachers' hours is done on the second or even the first pass rather than on the third pass.
The makeup-class-constraint might actually belong at the top of the constraint list (even though it's got a low priority), depending on its specifications. For example, you may have the requirement that the makeup class occur before any other class (so that e.g. a student with class on Tuesday can have the makeup class on Monday and get caught up prior to his regular class); even though the makeup class constraint has a low priority, it has the tightest requirements, and so it needs to get ordered first.

On-call night scheduling algorithm

I work in a residence hall at my college as an RA, and each night we need two RAs to be on call (able to respond to incidents and emergencies). Each month, RAs submit the nights they cannot be on call (due to a conflict of some sort). There are both male and female RAs. I am trying to come up with an effective algorithm to find the best arrangement of on-call nights for any particular month that satisfies the following requirements:
No RA is scheduled for a night he or she has listed as unavailable.
No RA is on-call more than a specified number of nights per month.
No RA is on-call twice within any given three-night span.
Each night has exactly two RAs on call.
Each night has, if possible, one male and one female RA on call.
Doing some research, I've found that this is a resource-constrained scheduling problem, and it is considered NP-complete. Therefore, I don't need to find the optimal solution, just any solution that works.
So far, I've considered the following approaches:
A simple 2d array of RAs x nights where each cell holds a boolean value answering whether or not that RA is on call for that night. I could hold the RAs in two priority queues (one male and one female) sorted by how many nights they've had so far, how long it has been since their last on call night, and simply iterate through the nights, selecting one RA from each queue, and then backtracking if I hit a dead end where nobody can work a particular night.
A tripartite graph with male RAs on one side, female RAs on another, and nights on the last side. In the solution, each night would have an edge to one male and one female RA. I would start with all edges connected and then remove edges until a solution is found.
Some sort of genetic approach using the above structures.
What sort of approach do you suggest I use? Is there something I haven't thought of that might work better? This is not a homework question; I am trying to develop a website that would make it easy for my staff and possibly other staffs on campus to submit date preferences and then generate a schedule that works for everybody.
I recommend the 2d array, but instead of iterating through the nights in order from first to last you iterate through the nights in order from most to least constrained - in other words, start with the night that has the fewest RAs available due to conflicts. This heuristic could also apply to the tripartite graph if you go with that approach - it's basically a question of domain representation, the algorithm itself isn't affected at this point.
The question is what you do when you reach the last few nights and find that no RAs are available. At this point you would do a local search to attempt to reach a viable solution, for example you've selected RA1 for NightA but RA2 and RA3 were also available on that night, so you select RA2 or RA3 to see if that frees up RA1 for the night on which you did not have an available RA.
Using the binary variables mentioned, your problem can be formulated as an Integer Linear Program (ILP). If there are not more than a few thousand variables, this can probably be solved optimally by a solver such as GLPK. All constraints but the last one are already linear. For "Each night has, if possible, one male and one female RA on call", you can introduce another Boolean variable "x" for each night, have a constraint "sum of female RAs on call <= 1 + x", and then minimize the sum of x's, thereby allowing the constraint to be violated at a cost. If you provide the data to the solver in text format, this should also be not too hard to implement.

Algorithm to find the best possible available times

Here is my scenario,
I run a Massage Place which offers various type of massages. Say 30 min Massage, 45 min massage, 1 hour massage, etc. I have 50 rooms, 100 employees and 30 pieces of equipment.When a customer books a massage appointment, the appointment requires 1 room, 1 employee and 1 piece of equipment to be available.
What is a good algorithm to find available resources for 10 guests for a given day
Resources:
Room – 50
Staff – 100
Equipment – 30
Business Hours : 9AM - 6PM
Staff Hours: 9AM- 6PM
No of guests: 10
Services
5 Guests- (1 hour massages)
3 Guests - (45mins massages)
2 Guests - (1 hour massage).
They are coming around the same time. Assume there are no other appointment on that day
What is the best way to get ::
Top 10 result - Fastest search which meets all conditions gets the top 10 result set. Top ten is defined by earliest available time. 9 – 11AM is best result set. 9 – 5pm is not that good.
Exhaustive search (Find all combinations) - all sets – Every possible combination
First available met (Only return the first match) – stop after one of the conditions have been met
I would appreciate your help.
Thanks
Nick
First, it seems the number of employees, rooms, and equipment are irrelevant. It seems like you only care about which of those is the lowest number. That is your inventory. So in your case, inventory = 30.
Next, it sounds like you can service all 10 people at the same time within the first hour of business. In fact, you can service 30 people at the same time.
So, no algorithm is necessary to figure that out, it's a static solution. If you take #Mario The Spoon's advice and weight the different duration massages with their corresponding profits, then you can start optimizing when you have more than 30 customers at a time.
Looks like you are trying to solve a problem for which there are quite specialized software applications. If your problem is small enough, you could try to do a brute force approach using some looping and backtracking, but as soon as the problem becomes too big, it will take too much time to iterate through all possibilities.
If the problem starts to get big, look for more specialized software. Things to look for are "constraint based optimization" and "constraint programming".
E.g. the ECLIPSe tool is an open-source constraint programming environment. You can find some examples on http://eclipseclp.org/examples/index.html. One nice example you can find there is the SEND+MORE=MONEY problem. In this problem you have the following equation:
S E N D
+ M O R E
-----------
= M O N E Y
Replace every letter by a digit so that the sum is correct.
This also illustrates that although you can solve this brute-force, there are more intelligent ways to solve this (see http://eclipseclp.org/examples/sendmore.pl.txt).
Just an idea to find a solution:
You might want to try to solve it with a constraint satisfaction problem (CSP) algorithm. That's what some people do if they have to solve timetable problems in general (e.g. room reservation at the University).
There are several tricks to improve CSP performance like forward checking, building a DAG and then do a topological sort and so on...
Just let me know, if you need more information about CSP :)

Resources