views:

2445

answers:

12

This is a problem I've had on my mind for a long time. Being the son of a teacher and a programmer, it occurred to me early on... but I still haven't found a solution for it.

So this is the problem. One needs to create a time schedule for a school, using some constraints. These are generally divided in two categories:

Sanity Checks

  • A teacher cannot teach two classes at the same time
  • A student cannot follow two lessons at the same time
  • Some teachers must have at least one day off during the week
  • All the days of the week should be covered by the time table
  • Subject X must have exactly so-and-so hours each week
  • ...

Preferences

  • Each teacher's schedule should be as compact as possible (i.e. the teacher should work all hours for the day in a row with no pauses if possible)
  • Teachers that have days off should be able to express a preference on which day
  • Teachers that work part-time should be able to express a preference whether to work in the beginning or the end of the school day.
  • ...

Now, after a few years of not finding a solution (and learning a thing or two in the meanwhile...), I realized that this smells like a NP-hard problem.

Is it proven as NP-hard?

Does anyone have an idea on how to crack this thing?

Looking at this question made me think about this problem, and whether genetic algorithms would be usable in this case. However it would be pretty hard to mutate possibilities while maintaining the sanity check rules. Also it's not clear to me how to distinguish incompatible requirements.

Thanks in advance for any contribution on this. I will vote up any sane response :)

EDIT To better specify the problem. This is applied to Italian style classrooms where all students are associated in different classes (for example: year 1 section A) and the teachers move between classes. All students of the same class have the same schedule, and have no choice over which lessons to attend.

+1  A: 

I think you might be missing some constraints.

One would prefer where possible to have teachers scheduled to classes for which they are certified.

One would suspect that the classes that are requested, and the expected headcount in each would be significant.

I think the place to start would be to list all of your constraints, figure out a data structure to represent them.

Then create some sort of engine to that builds a trial solution, then evaluates it for fitness according to the constraints.

You could then throw the fun genetic algorithms part at it, and see if you can get the fitness to increase over time as the genes mix.

Start with a small set of constraints, and increase them as you see success (if you see success)

There might be a way to take the constraints and shoehorn them together with something like a linear programming algorithm.

I agree. It sounds like a fun challenge

EvilTeach
The headcount would be fixed (say x classes of y students). All teachers are equally certified. At least in Italian schools that is... ;)
Sklivvz
The problem is, I suspect that even generating a bad, but sane solution is almost np-hard... Should I generate a random one to start with? That really doesn't sound feasible
Sklivvz
Ya, omitting those constraints simplifies things. I think sitting down and drawing a picture of possible data structures for the constraints would be a good step. Do the part you know, and the rest may become visible.
EvilTeach
Ya, I probably would generate a large initial set, run them through fitness, then start breeding them, injecting new random ones intermittently, if they start to die off too fast. You are really gonna have to do it, to see what happens.
EvilTeach
you need a domain expert
EvilTeach
did she do the scheduling at the school?
EvilTeach
+1  A: 

This is a mapping problem: you need to map to every hour in a week and every teacher an activity (teach to a certain class or free hour ).

Split the problem:

  1. Create the list of teachers, classes and preferences then let the user populate some of the preferences on a map to have as a starting point.
  2. Randomly take one element from the list and put it at a random free position on the map if it doesn't cross any sanity checks until the list is empty. If at any certain iteration you can't place an element on the map without crossing a sanity check shift two positions on the map and try again.
  3. When the map is filled, try shifting positions on the map to optimize the result.

In steps 2 and 3 show each iteration to the user: items left in the list, positions on the map and the next computed move and let the user intervene.

I did not try this, but this would be my initial approach.

Ovidiu Pacurar
Interesting approach, I am not so sure how viable step 2 is in terms of performance, but it could work. Thanks,
Sklivvz
+15  A: 

I am one of the developer that works on the scheduler part of a student information system. During our original approach of the scheduling problem, we researched genetic algorithms to solve constraint satisfaction problems, and even though we were successful initially, we realized that there was a less complicated solution to the problem (after attending a school scheduling workshop)

Our current implementation works great, and uses brute force with smart heuristics to get a valid schedule in a short amount of time. The master schedule (assignment of the classes to the teachers) is first built, taking in consideration all the constraints that each teacher has while minimizing the possibility of conflicts for the students (based of their course requests). The students are then scheduled in the classes using the same method.

Doing this allows you to have the machine build a master schedule first, and then have a human tweak it if needed.

The scheduler current implementation is written in perl, but other options we visited early on were Prolog and CLIPS (expert system)

+1 because it's a sane answer. I would give another because you did it in Perl, but unfortunately I can't :) Thanks!
Sklivvz
+1 because Sklivvz didn't have a second vote to give :-)
stevemegson
Another vote for brute force. The problem is small enough that this applies and works quite well.
Nick Johnson
+1  A: 

One of the worst open source webpages ever, but the project looks promising: http://www.lalescu.ro/liviu/fet/

A web-based aproach:
phpScheduleIt (not school-specific)

gnud
+2  A: 

Answering my own question:

The FET project mentioned by gnud uses this algorithm:

Some words about the algorithm: FET uses a heuristical algorithm, placing the activities in turn, starting with the most difficult ones. If it cannot find a solution it points you to the potential impossible activities, so you can correct errors. The algorithm swaps activities recursively if that is possible in order to make space for a new activity, or, in extreme cases, backtracks and switches order of evaluation. The important code is in src/engine/generate.cpp. Please e-mail me for details or join the mailing list. The algorithm mimics the operation of a human timetabler, I think.

Link


Following up the "Constraint Based Reasoning" lead by Stringent Software on Wikipedia lead me to these pages which have an interesting paragraph:

Solving a constraint satisfaction problem on a finite domain is an NP-complete problem in general. Research has shown a number of polynomial-time subcases, mostly obtained by restricting either the allowed domains or constraints or the way constraints can be placed over the variables. Research has also established relationship of the constraint satisfaction problem with problems in other areas such as finite model theory and databases.

Sklivvz
+2  A: 

I've tackled similar planning/scheduling problems in the past and the AI technique that is often best suited for this class of problem is "Constraint Based Reasoning".

It's basically a brute force method like Laurenty suggested, but the approach involves applying constraints in an efficient order to cause the infeasible solutions to fail fast - to minimise the computation required.

Googling "Constraint Based Reasoning" brings up a lot of resources on the technique and its application to scheduling problems.

Stringent Software
I don't agree. I think that a hybrid between CP-OR and then maybe AI. I have seen good academic generic solutions with CP-OR mix but this research field never gets any money so it seems that we are doomed.
Jonke
+1  A: 

Looking at this question made me think about this problem, and whether genetic algorithms would be usable in this case. However it would be pretty hard to mutate possibilities while maintaining the sanity check rules. Also it's not clear to me how to distinguish incompatible requirements.

Genetic Algorithms are very well suited to problems such as this. Once you come up with a decent representation of the chromosome (in this case, probably a vector representing all of the available class slots) you're most of the way there.

Don't worry about maintaining sanity checks during the mutation phase. Mutation is random. Sanity and preference checks both belong in the selection phase. A failed sanity check would drastically lower the fitness of an individual, while a failed preference would only mildly lower the fitness.

Incompatible requirements are a different problem altogether. If they're completely incompatible, you'll get a population that doesn't converge on anything useful.

Trevor Oke
+1  A: 

This reminds me of this blog post about scheduling a conference, with a video explanation here.

How I would do it:

Have the population include two things:

  • Who teaches what class (I expect the teachers to teach one subject).
  • What a class takes on a specific time slot.

This way we can't have conflicts (a teacher in 2 places, or a class having two subjects at the same time).

The fitness function would include:

  • How many time slots each teacher gives per week.
  • How many time slots a teacher has on the same day (They can't have a full day of teaching, this too must be balanced).
  • How many time slots of the same subject a class has on the same day (They can't have a full day of math!).

Maybe take the standard deviation for all of them since they should be balanced.

Osama ALASSIRY
A: 

Good luck. Being the son of a father with this sort of problem is what took me to the research group that I ended up in ...


When I was a kid my father scheduled match officials in a local sports league, this had a similarly long list of constraints and I tried to write something to help. When I got to University I even used it as my final year project eventually settling on a Monte Carlo implementation (using a Simulated Annealing model).

The basic idea is that if it's not NP, it's pretty close, so rather than assuming there is a solution, I would set out to find the best within a given timeframe. I would weight all the constraints with costs for breaking them: sanity checks would have huge costs, the preferences would have lower costs (but increasing for more breaks, so breaking it once would be less than half the cost of breaking it twice).

The basic idea is that I started with a 'random' solution and costed it; then made changes by swapping a small number of assignments, re-valued it and then, probalistically accepted or declined the change.

After thousands of iterations you inch closer to an acceptable solution.

Believe me, though, that this class of problems has research groups churning out PhDs so you're in very good company.

You might also find some interest in the Linear Programming arena, e.g. simplex and so on.

Unsliced
+2  A: 

I'm not sure if this covers the same ground as @Stringent Software's answer (as the name is slightly different), but I have a couple of very good friends who are researching the area of Constraint Programming. Creating timetables is one application of their research.

Dr Chris Jefferson created a program called Minion that you can download from SourceForge, and is a very fast brute force constraint problem solver written in C++

Andrew
A: 

Yes, I think this is NP complete - or at least to find the optimal solution is NP complete.

I worked on a similar problem in college when i told a friend's father (who was a teacher) that I could solve his scheduling problems for him if he did not find a suitable program for it (this was back in 1990 or so)

I had no idea what I got myself into. Luckily for us all I had to do was find one solution that fit the constraints. But in my testing I was always worried about determining IF there was a solution at all. He never had too many constraints and the program used different heuristics and back tracking. It was a lot of fun.

I think Bill Gates also worked on a system like this in high school or college for his high school. Not sure though.

Good luck. All my notes are gone and I never got around to implementing a solution that I could market. It was a specialty project that I re-coded as I learned new languages (Basic, Scheme, C, VB, C++)

Have fun with it

Tim
A: 

i see that this problem can be solved by Prolog program by connecting it to a database and the program can make the schedule given a set of constraints read abt "Constraint satisfaction Problem prolog"

Muslims