I'm working on an interactive job scheduling application. Given a set of resources with corresponding capacity/availabilty profiles, a set of jobs to be executed on these resources and a set of constraints that determine job sequence and earliest/latest start/end times for jobs I want to enable the user to manually move jobs around. Essentially I want the user to be able to "grab" a node of the job network and drag that forwards/backwards in time without violating any of the constraints.
The image shows a simple example configuration. The triangular job at the end denotes the latest finish time for all jobs, the connecting lines between jobs impose an order on the jobs and the gray/green bars denote resource availabilty and load.
You can drag any of the jobs to compress the schedule. Note that jobs will change in length due to different capacity profiles.
I have implemented an ad-hock algorithm that kinda works. However there are still cases where it'll fail and violate some constraints. However, since job-shop-scheduling is a well researched field with lots of algorithms and heuristics for finding an optimal (or rather good) solution to the general NP-hard problem - I'm thinking solutions ought to exist for my easier subset. I have looked into constraint programming topics and even physics based solutions (rigid bodies connected via static joints) but so far couldn't find anything suitable. Any pointers/hints/tips/search key words for me?
I highly recommend you take a look at Mozart Oz, if your problem
deals only with integers. Oz has excellent support for finite domain
constraint specification, inference, and optimization. In your case
typically you would do the following:
Specify your constraints in a declarative manner. In this, you would
specify all the variables and their domains (say V1: 1#100, means
V1 variable can take values in the range of 1--100). Some variables
might have values directly, say V1: 99. In addition you would specify
all the constraints on the variables.
Ask the system for a solution: either any solution which satisfies
the constraints or an optimal solution. Then you would display this
solution on the UI.
Lets say the user changes the value of a variable, may be the start
time of a task. Now you can go to step 1 to post the problem to the
Oz solver. This time, solving the problem most probably will not take
as much time as earlier, as all the variables are already instantiated.
It may be the case that the user chose a inconsistent value. In that
case, the solver returns null. Then, you can take the UI to the earlier
solution.
If Oz suits your need and you like the language, then you may want to
write a constraint solver as a server which listens on a socket. This way,
you can keep the constraint solver separate from the rest of your code,
including the UI.
Hope this helps.
I would vote in favor of constraint programming for several reasons:
1) CP will quickly tell you if there is no schedule that satifies your constraints
2) It would appear that you want to give you users a feasible solution to start with but
allow them to manipulate jobs in order to improve the solution. CP is good at this too.
3) An MILP approach is usually complex and hard to formulate and you have to artificially create an objective function.
4) CP is not that difficult to learn especially for experienced programmers - it really comes more from the computer science community than the operations researchers (like me).
Good luck.
You could probably alter the Waltz constraint propagation algorithm to deal with changing constraints to quickly find out if a given solution is valid. I don't have a reference to hand, but this might point you in the right direction:
http://www.sciencedirect.com/science?_ob=ArticleURL&_udi=B6TYF-41C30BN-5&_user=809099&_rdoc=1&_fmt=&_orig=search&_sort=d&_docanchor=&view=c&_searchStrId=1102030809&_rerunOrigin=google&_acct=C000043939&_version=1&_urlVersion=0&_userid=809099&md5=696143716f0d363581a1805b34ae32d9
Have you considered using an Integer Linear Programming engine (like lp_solve)? It's quite a good fit for scheduling applications.
Related
Job Shop Scheduling Problem (JSSP): I have jobs that consist of tasks and I have machines that can perform these tasks.
I should be able to add new jobs dynamically. E.g. I have a schedule for the first 5 jobs, and when the 6th arrive - I need to be able to fit it into the schedule in the best way. It is possible to adjust existing schedule within the given flexibility constrains.
Look at the picture below.
Jobs have tasks, each task is the same type of action. Think about painting of some objects with paint spray. All the machines are the same (paint sprays), and all of the tasks are the same.
Constraint 1. Jobs have a preferred deadline for completion, but the deadline is flexible to some extent.
Edit after #tucuxi answer: Flexible deadline mean that the time of completion can be extended by some delta if necessary.
Constraint 2. Between the jobs there is resting phase. Think about drying the paint. Resting phase has minimal required duration. Resting phase can be longer or shorter if necessary.
Edit after #tucuxi answer: So there is planned time of rest Tp which is desired, but flexible value that can be increased or decreased if this allows for better scheduling. And there is minimal time of rest Tm. So Tp-Tadjustmenet>=Tm.
The machine is occupied by the job from the start to the completion.
Here goes parts that make this problem very distinct from what I have read about.
Jobs arrive in batches of several jobs. For example a batch can contain 10 jobs of the type Job_1 and 5 of Job_2. Different batches can contain different types of jobs. All the jobs from the batches should be finished as close to each other as possible. Not necessary at the same time, but we need to minimize the delay between the completion of first and last jobs from the batch.
Constraint 3. Machines are grouped. In each group only M machines can work simultaneously. Think about paint sprays that are connected to the common pressurizer that has limited performance.
The goal.
Having given description of the problem, it should be possible to solve JSSP. It should be also possible to add new jobs to the existing schedule.
Edit after #tucuxi answer: This is not a task that should be solved immediately: it is not a time-critical system. But it shouldn't be too long to irritate a human who put new tasks into the algorithm.
Question
What kind of many JSSP algorithms can help me solve this? I can implement an algorithm by myself, if there is one. The closest I found is This - Resource Constrained Project Scheduling Problem. But I was not able to comprehend how can I glue it to the JSSP solving algorithm.
Edit after #tucuxianswer: No, I haven't tried it yet.
Is there any libraries that can be used to solve this problem? Python or C# are the preferred languages, but in the end it doesn't really matter.
I appreciate any help: keyword to search for, link, reference to a book, reference to a library.
Thank you.
I doubt that there is a pre-made algorithm that solves your exact problem.
If I had to solve it, I would first:
compile datasets of inputs that I can feed into candidate solvers.
think of a metric to rank outputs, so that I can compare the candidates to see which is better.
A baseline solver could be a brute-force search: test and rate all possible job schedulings for small sample problems. This is of course infeasible for large inputs, but for small inputs it allows you to compare the outputs of more efficient solvers to a known-best answer.
Your link is to localsolver.com, which appears to provide a library for specifying problem constraints to then solve them. It is not freely available, requiring a license to use; but it would seem that your problem can be readily modeled in it. Have you tried to do so? They appear to support both C++ and Python. Other free options exist, including optaplanner (2.8k stars in github) or python-constraint (I have not looked into other languages).
Note that a good metric is crucial to choosing a good algorithm: unless you have a clear cost function to minimize, choosing "a good algorithm" is impossible. In your description of the problem, I see several places where cost is unclear (marked in italics):
job deadlines are flexible
minimal required rest times... which may be shortened
jobs from a batch should be finished as close together as possible
(not from specification): how long can you wait for an optimal vs a less-optimal-but-faster solution?
(First of all, sorry for my english, it's not my first language)
I have a list of tasks/jobs, each task must start after a specific start time, needs to run for a certain time and has to be finished after a certain end time.
I can dynamically add and remove workers, so it is possible to execute 2 or more tasks at the same time if I have to. My Goal is to find a scheduling plan that executes each job successfully and uses the minimal amount of workers possible.
I'm currently using an EDF (http://en.wikipedia.org/wiki/Earliest_deadline_first_scheduling) Algorithm and recursively call the function with a higher Worker Limit if it can't schedule all jobs correctly, but I think this doesn't work right because I don't have a real way to measure when I can lower the ressource limit again.
Are there any Algorithms that work for my problem, or any other clever ideas?
Thanks for your help.
A scheduling problem can often be solved very effectively by formulating it either as mixed-integer program (MIP)
http://en.wikipedia.org/wiki/Mixed_integer_programming#Integer_unknowns
or expressing it using constraint programming (CP)
http://en.wikipedia.org/wiki/Constraint_programming
For either MIP or CP, you will find both free and commercial solvers that can address your problem.
In both of these approaches, you put your effort into stating the properties that the solution must have, and the hard work of applying an appropriate algorithm is left to a specialized solver.
I'm writing a small software application that needs to serve as a simple planning tool for a local school. The 'problem' it needs to solve is fairly basic. Namely, the teachers need to talk with the parents of all children. However, some children have, of course, brothers and sisters in different groups, so these talks need to be scheduled next to eachother, to avoid the situations were parents have a talk at 6 pm and another one at 10 pm. Thus in short, given a collection of n children, where some children have 1 or more brothers or sisters, generate a schedule where all the talks of these children are planned next to each other.
Now, maybe the problem can be solved extremely easy, but on the other I have a feeling this can be a pretty complicated problem, that needs and can be solved with some sort of algorithm. Elegantly. But am I right? Is there? I've looked at the Hungarian alorithm but it doesn't quite apply to this particular problem.
Edit: I forgot to mention, that all talks take the same amount of time.
Thanks!
I think it is quite easy.
First group the kids which belong together because they share parents. Schedule the children inside a group consecutively, schedule the rest as random.
Another way to abstract it and make the problem easier is to look from the parent perspective, see brothers and sister as "child" and give them more time. Then you can just schedule the parents at random, but some need more time (because they have multiple childeren).
One approach woul dbe to define the problem in a declarative constraint language and then let it solve the problem for you. The last time I did this, I used ECLiPSe, which is a nifty little language where you define your problem space by constraints, and then let it find allowable values that satisfy those constraints.
For example, I believe you have two classes of constraints:
A teacher may only have one
conference at a time
All students in the same family must
have consecutive slots
Once you define these in ECLiPSe, it will calculate values for each student that satisfy the requirements. If you go this way, you can also easily add constraints as you need to. For example, it's easy to say that a teach is unavailable for slot Y, or teachers must take turns doing administrative work, etc.
This sorts feels like a "backpack algorithm" type of problem. You need to group the family members together then fill slots appropriately.
If you google "backpack algorithm", you'll see enough write-ups to make your head spin and also some good coded solutions.
I think if each talk could be reduced to "activities" where each activity has a start time and an end time you could use the activity-selection algorithm studied in computer science. It is based on a greedy approach and could be solved in O(n) (where n is the number of activities). You could find more information here. I am sure you will need to have to do a pre-processing here to be able to reduce the brother/sister issue as activities of the same type.
I need to solve a job affectation problem and I would like to find preferably efficient algorithms to solve this problem.
Let's say there are some workers that can do several kind of tasks. We also have a pool of tasks which must be done every week. Each task takes some time. Each task must be taken by someone. Each worker must work between N an P hours a week.
This first part of the problem seems to be a good candidate for a constraint programming algorithm.
But here is the complication: because a worker can do different tasks they may also have preferences (or wishes). If one want to satisfy all wishes for everyone there is no solution to the problem (too many constraints).
So I need an algorithm to solve this problem. I don't want to reinvent the wheel if the perfect wheel already exists.
The algorithm must be fair (if one can define this word) so for example I should be able to add a constraint like "try to satisfy at least one wish per people". I'm not sure that this problem can be solved by Constraint Hierarchies methods described here: Constraint Herarchies. In fact I'm not sure that "fairness" and wishes can be expressed by valid constraints for this category of algorithms.
Is there a constraint programming expert to give me some advices ? Do I need to develop a new wheel with some heuristics instead of using efficient CP algorithms ?
Thanks !
Your problem is complex enough that a general solution will probably require formulating as a linear-integer problem. If on the other hand you are able to relax certain requirements, you may be able to use a simpler approach. For example, bipartite matching would allow you to schedule multiple workers to multiple jobs, and can even handle preferences, but would not be able to enforce general 'fairness' constraints. See e.g. this related SO question. Vertex colouring has efficient algorithms for enforcing job separation constraints.
Other posters have mentioned simplex and job shop scheduling. Simplex is an optimisation algorithm - it traverses a solution space seeking to maximise some objective function. Formulating the objective function can certainly be done, but is non-trivial. Classical job shop scheduling, like bipartite matching, can model some aspects of your problem, but not all. There are no precedence constraints, for example. There are extended versions that can handle some constraints, for example placing time bounds on tasks.
If you're interested in existing implementations, the Python networkx library has an implementation of this matching algorithm. An example of an open source timetabling program that might be of interest is Tablix.
I've done timetabling, which can be considered a form of constraint programming. You have hard (inviolable) constraints and soft constraints (such as interval preferences).
Linear integer programming usually becomes useless after more than 30 variables, and this can also be said about simplex.
It was trough domain-specific optimizations of heuristic algorithms that a solution was found.
The heuristic algorithms used were simmulated annealing, genetic algorithms, metaheuristic algorithms and similar, but in the end the best result were provided by an "intelligent" domain customized greedy search algorithm.
Basically, you might get some decent results with one of the heuristics here, but the main problem is being able to discern when a problem is overconstrained.
A great open-source tool for research is the HeuristicLab.
I agree with what have been proposed here. However, MIP (Mixed Integer Programming problems) of very large size (far beyond 30 variables !) are practically solved nowadays thanks to commercial codes (Xpress, Cplex, Gurobi) or open-source (Coin-Or/Cbc). Furthermore, fancy modeling languages such as OPL Studio, GAMS, AMPL, Flop ... allow to write easily mathematic models instead of using APIs.
You can take advantage of NEOS server (http://neos.mcs.anl.gov/neos/solvers/index.html) to try very esaily different MIPs available. You send your model in AMPL format . Although AMPL comes as a free limited version, NEOS can handle unlimited instances.
Modeling languages exist also for CP (COMET / OPL Studio) and Local Search (COMET).
Feel free to get in touch with me through my web site www.rostudel.com ('contact' page)
David
This sounds like job shop scheduling.
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.
A small addendum to better specify the problem. This is applied to Italian school 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.
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)
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
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:
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.
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.
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.
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.
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.
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.
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.
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.
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
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"