Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 3. Use Cases and Examples
3.1. Examples Overview Copier lienLien copié sur presse-papiers!
Planner has several examples. In this manual we explain mainly using the n queens example and cloud balancing example. So it’s advisable to read at least those sections.
The source code of all these examples is available in the distribution ZIP under examples/sources
and also in git under optaplanner/optaplanner-examples
.
Example | Domain | Size | Competition? | Special features used |
---|---|---|---|---|
|
|
| None | |
|
|
| ||
|
|
| ||
|
|
|
| |
|
|
| ||
|
|
| ||
|
|
| ||
|
|
| ||
|
|
|
| |
Vehicle routing with time windows | Extra on Vehicle routing:
|
|
| Extra on Vehicle routing:
|
|
|
| ||
|
|
| ||
|
|
|
| |
|
|
| ||
|
|
|
| |
|
|
| ||
|
|
|
A realistic competition is an official, independent competition:
- that clearly defines a real-word use case
- with real-world constraints
- with multiple, real-world datasets
- that expects reproducible results within a specific time limit on specific hardware
- that has had serious participation from the academic and/or enterprise Operations Research community
These realistic competitions provide an objective comparison of Planner with competitive software and academic research.
3.2. Basic Examples Copier lienLien copié sur presse-papiers!
3.2.1. N Queens Copier lienLien copié sur presse-papiers!
3.2.1.1. Problem Description Copier lienLien copié sur presse-papiers!
Place n queens on a n sized chessboard so no 2 queens can attack each other. The most common n queens puzzle is the 8 queens puzzle, with n = 8:
Constraints:
- Use a chessboard of n columns and n rows.
- Place n queens on the chessboard.
- No 2 queens can attack each other. A queen can attack any other queen on the same horizontal, vertical or diagonal line.
This documentation heavily uses the 4 queens puzzle as the primary example.
A proposed solution could be:
Figure 3.1. A Wrong Solution for the 4 Queens Puzzle
The above solution is wrong because queens A1
and B0
can attack each other (so can queens B0
and D0
). Removing queen B0
would respect the “no 2 queens can attack each other” constraint, but would break the “place n queens” constraint.
Below is a correct solution:
Figure 3.2. A Correct Solution for the 4 Queens Puzzle
All the constraints have been met, so the solution is correct. Note that most n queens puzzles have multiple correct solutions. We’ll focus on finding a single correct solution for a given n, not on finding the number of possible correct solutions for a given n.
3.2.1.2. Problem Size Copier lienLien copié sur presse-papiers!
The implementation of the N queens example has not been optimized because it functions as a beginner example. Nevertheless, it can easily handle 64 queens. With a few changes it has been shown to easily handle 5000 queens and more.
3.2.1.3. Domain Model Copier lienLien copié sur presse-papiers!
Use a good domain model: it will be easier to understand and solve your planning problem. This is the domain model for the n queens example:
A Queen
instance has a Column
(for example: 0 is column A, 1 is column B, …) and a Row
(its row, for example: 0 is row 0, 1 is row 1, …). Based on the column and the row, the ascending diagonal line as well as the descending diagonal line can be calculated. The column and row indexes start from the upper left corner of the chessboard.
A single NQueens
instance contains a list of all Queen
instances. It is the Solution
implementation which will be supplied to, solved by and retrieved from the Solver. Notice that in the 4 queens example, NQueens’s getN()
method will always return 4.
A solution | Queen | columnIndex | rowIndex | ascendingDiagonalIndex (columnIndex + rowIndex) | descendingDiagonalIndex (columnIndex - rowIndex) |
---|---|---|---|---|---|
A1 | 0 | 1 | 1 (**) | -1 | |
B0 | 1 | 0 (*) | 1 (**) | 1 | |
C2 | 2 | 2 | 4 | 0 | |
D0 | 3 | 0 (*) | 3 | 3 |
When 2 queens share the same column, row or diagonal line, such as (*) and (**), they can attack each other.
3.2.2. Cloud Balancing Copier lienLien copié sur presse-papiers!
This example is explained in a tutorial.
3.2.3. Traveling Salesman (TSP - Traveling Salesman Problem) Copier lienLien copié sur presse-papiers!
3.2.3.1. Problem Description Copier lienLien copié sur presse-papiers!
Given a list of cities, find the shortest tour for a salesman that visits each city exactly once.
The problem is defined by Wikipedia. It is one of the most intensively studied problems in computational mathematics. Yet, in the real world, it’s often only part of a planning problem, along with other constraints, such as employee shift rostering constraints.
3.2.3.2. Problem Size Copier lienLien copié sur presse-papiers!
dj38 has 38 cities with a search space of 10^58. europe40 has 40 cities with a search space of 10^62. st70 has 70 cities with a search space of 10^126. pcb442 has 442 cities with a search space of 10^1166. lu980 has 980 cities with a search space of 10^2927.
dj38 has 38 cities with a search space of 10^58.
europe40 has 40 cities with a search space of 10^62.
st70 has 70 cities with a search space of 10^126.
pcb442 has 442 cities with a search space of 10^1166.
lu980 has 980 cities with a search space of 10^2927.
3.2.3.3. Problem Difficulty Copier lienLien copié sur presse-papiers!
Despite TSP’s simple definition, the problem is surprisingly hard to solve. Because it’s an NP-hard problem (like most planning problems), the optimal solution for a specific problem dataset can change a lot when that problem dataset is slightly altered:
3.2.4. Dinner Party Copier lienLien copié sur presse-papiers!
3.2.4.1. Problem Description Copier lienLien copié sur presse-papiers!
Miss Manners is throwing another dinner party.
- This time she invited 144 guests and prepared 12 round tables with 12 seats each.
- Every guest should sit next to someone (left and right) of the opposite gender.
- And that neighbour should have at least one hobby in common with the guest.
- At every table, there should be 2 politicians, 2 doctors, 2 socialites, 2 coaches, 2 teachers and 2 programmers.
- And the 2 politicians, 2 doctors, 2 coaches and 2 programmers shouldn’t be the same kind at a table.
Drools Expert also has the normal Miss Manners example (which is much smaller) and employs an exhaustive heuristic to solve it. Planner’s implementation is far more scalable because it uses heuristics to find the best solution and Drools Expert to calculate the score of each solution.
3.2.4.2. Problem Size Copier lienLien copié sur presse-papiers!
wedding01 has 18 jobs, 144 guests, 288 hobby practicians, 12 tables and 144 seats with a search space of 10^310.
wedding01 has 18 jobs, 144 guests, 288 hobby practicians, 12 tables and 144 seats with a search space of 10^310.
3.2.5. Tennis Club Scheduling Copier lienLien copié sur presse-papiers!
3.2.5.1. Problem Description Copier lienLien copié sur presse-papiers!
Every week the tennis club has 4 teams playing round robin against each other. Assign those 4 spots to the teams fairly.
Hard constraints:
- Conflict: A team can only play once per day.
- Unavailability: Some teams are unavailable on some dates.
Medium constraints:
- Fair assignment: All teams should play an (almost) equal number of times.
Soft constraints:
- Evenly confrontation: Each team should play against every other team an equal number of times.
3.2.5.2. Problem Size Copier lienLien copié sur presse-papiers!
munich-7teams has 7 teams, 18 days, 12 unavailabilityPenalties and 72 teamAssignments with a search space of 10^60.
munich-7teams has 7 teams, 18 days, 12 unavailabilityPenalties and 72 teamAssignments with a search space of 10^60.
3.2.5.3. Domain Model Copier lienLien copié sur presse-papiers!
3.2.6. Meeting Scheduling Copier lienLien copié sur presse-papiers!
3.2.6.1. Problem Description Copier lienLien copié sur presse-papiers!
Assign each meeting to a starting time and a room. Meetings have different durations.
Hard constraints:
- Room conflict: 2 meetings must not use the same room at the same time.
- Required attendance: A person cannot have 2 required meetings at the same time.
Medium constraints:
- Preferred attendance: A person cannot have 2 preferred meetings at the same time, nor a preferred and a required meeting at the same time.
Soft constraints:
- Sooner rather than later: Schedule all meetings as soon as possible.
3.2.6.2. Problem Size Copier lienLien copié sur presse-papiers!
50meetings-160timegrains-5rooms has 50 meetings, 160 timeGrains and 5 rooms with a search space of 10^145. 100meetings-320timegrains-5rooms has 100 meetings, 320 timeGrains and 5 rooms with a search space of 10^320.
50meetings-160timegrains-5rooms has 50 meetings, 160 timeGrains and 5 rooms with a search space of 10^145.
100meetings-320timegrains-5rooms has 100 meetings, 320 timeGrains and 5 rooms with a search space of 10^320.
3.3. Real Examples Copier lienLien copié sur presse-papiers!
3.3.1. Course Timetabling (ITC 2007 Track 3 - Curriculum Course Scheduling) Copier lienLien copié sur presse-papiers!
3.3.1.1. Problem Description Copier lienLien copié sur presse-papiers!
Schedule each lecture into a timeslot and into a room.
Hard constraints:
- Teacher conflict: A teacher must not have 2 lectures in the same period.
- Curriculum conflict: A curriculum must not have 2 lectures in the same period.
- Room occupancy: 2 lectures must not be in the same room in the same period.
- Unavailable period (specified per dataset): A specific lecture must not be assigned to a specific period.
Soft constraints:
- Room capacity: A room’s capacity should not be less than the number of students in its lecture.
- Minimum working days: Lectures of the same course should be spread out into a minimum number of days.
- Curriculum compactness: Lectures belonging to the same curriculum should be adjacent to each other (so in consecutive periods).
- Room stability: Lectures of the same course should be assigned the same room.
The problem is defined by the International Timetabling Competition 2007 track 3.
3.3.1.2. Problem Size Copier lienLien copié sur presse-papiers!
3.3.1.3. Domain Model Copier lienLien copié sur presse-papiers!
3.3.2. Machine Reassignment (Google ROADEF 2012) Copier lienLien copié sur presse-papiers!
3.3.2.1. Problem Description Copier lienLien copié sur presse-papiers!
Assign each process to a machine. All processes already have an original (unoptimized) assignment. Each process requires an amount of each resource (such as CPU, RAM, …). This is a more complex version of the Cloud Balancing example.
Hard constraints:
- Maximum capacity: The maximum capacity for each resource for each machine must not be exceeded.
- Conflict: Processes of the same service must run on distinct machines.
- Spread: Processes of the same service must be spread out across locations.
- Dependency: The processes of a service depending on another service must run in the neighborhood of a process of the other service.
- Transient usage: Some resources are transient and count towards the maximum capacity of both the original machine as the newly assigned machine.
Soft constraints:
- Load: The safety capacity for each resource for each machine should not be exceeded.
- Balance: Leave room for future assignments by balancing the available resources on each machine.
- Process move cost: A process has a move cost.
- Service move cost: A service has a move cost.
- Machine move cost: Moving a process from machine A to machine B has another A-B specific move cost.
The problem is defined by the Google ROADEF/EURO Challenge 2012.
3.3.2.2. Problem Size Copier lienLien copié sur presse-papiers!
3.3.2.3. Domain Model Copier lienLien copié sur presse-papiers!
3.3.3. Vehicle Routing Copier lienLien copié sur presse-papiers!
3.3.3.1. Problem Description Copier lienLien copié sur presse-papiers!
Using a fleet of vehicles, pick up the objects of each customer and bring them to the depot. Each vehicle can service multiple customers, but it has a limited capacity.
Besides the basic case (CVRP), there is also a variant with time windows (CVRPTW).
Hard constraints:
- Vehicle capacity: a vehicle cannot carry more items than its capacity.
Time windows (only in CVRPTW):
- Travel time: Traveling from one location to another takes time.
- Customer service duration: a vehicle must stay at the customer for the length of the service duration.
- Customer ready time: a vehicle may arrive before the customer’s ready time, but it must wait until the ready time before servicing.
- Customer due time: a vehicle must arrive on time, before the customer’s due time.
Soft constraints:
- Total distance: minimize the total distance driven (fuel consumption) of all vehicles.
The capacitated vehicle routing problem (CVRP) and its timewindowed variant (CVRPTW) are defined by the VRP web.
3.3.3.2. Problem Size Copier lienLien copié sur presse-papiers!
CVRP instances (without time windows):
CVRPTW instances (with time windows):
3.3.3.3. Domain Model Copier lienLien copié sur presse-papiers!
The vehicle routing with timewindows domain model makes heavy use of shadow variables. This allows it to express its constraints more naturally, because properties such as arrivalTime
and departureTime
, are directly available on the domain model.
3.3.3.4. Road Distances Instead of Air Distances Copier lienLien copié sur presse-papiers!
In the real world, vehicles can’t follow a straight line from location to location: they have to use roads and highways. From a business point of view, this matters a lot:
For the optimization algorithm, this doesn’t matter much, as long as the distance between 2 points can be looked up (and are preferably precalculated). The road cost doesn’t even need to be a distance, it can also be travel time, fuel cost, or a weighted function of those. There are several technologies available to precalculate road costs, such as GraphHopper (embeddable, offline Java engine), Open MapQuest (web service) and Google Maps Client API (web service).
There are also several technologies to render it, such as Leaflet and Google Maps for developers: the optaplanner-webexamples-*.war
has an example which demonstrates such rendering:
It’s even possible to render the actual road routes with GraphHopper or Google Map Directions, but because of route overlaps on highways, it can become harder to see the standstill order:
Take special care that the road costs between 2 points use the same optimization criteria as the one used in Planner. For example, GraphHopper etc will by default return the fastest route, not the shortest route. Don’t use the km (or miles) distances of the fastest GPS routes to optimize the shortest trip in Planner: this leads to a suboptimal solution as shown below:
Contrary to popular belief, most users don’t want the shortest route: they want the fastest route instead. They prefer highways over normal roads. They prefer normal roads over dirt roads. In the real world, the fastest and shortest route are rarely the same.
3.3.4. Project Job Scheduling Copier lienLien copié sur presse-papiers!
3.3.4.1. Problem Description Copier lienLien copié sur presse-papiers!
Schedule all jobs in time and execution mode to minimize project delays. Each job is part of a project. A job can be executed in different ways: each way is an execution mode that implies a different duration but also different resource usages. This is a form of flexible job shop scheduling.
Hard constraints:
- Job precedence: a job can only start when all its predecessor jobs are finished.
Resource capacity: do not use more resources than available.
- Resources are local (shared between jobs of the same project) or global (shared between all jobs)
- Resource are renewable (capacity available per day) or nonrenewable (capacity available for all days)
Medium constraints:
- Total project delay: minimize the duration (makespan) of each project.
Soft constraints:
- Total makespan: minimize the duration of the whole multi-project schedule.
The problem is defined by the MISTA 2013 challenge.
3.3.4.2. Problem Size Copier lienLien copié sur presse-papiers!
3.3.5. Hospital Bed Planning (PAS - Patient Admission Scheduling) Copier lienLien copié sur presse-papiers!
3.3.5.1. Problem Description Copier lienLien copié sur presse-papiers!
Assign each patient (that will come to the hospital) into a bed for each night that the patient will stay in the hospital. Each bed belongs to a room and each room belongs to a department. The arrival and departure dates of the patients are fixed: only a bed needs to be assigned for each night.
This problem features overconstrained datasets.
Hard constraints:
-
2 patients must not be assigned to the same bed in the same night. Weight:
-1000hard * conflictNightCount
. -
A room can have a gender limitation: only females, only males, the same gender in the same night or no gender limitation at all. Weight:
-50hard * nightCount
. -
A department can have a minimum or maximum age. Weight:
-100hard * nightCount
. -
A patient can require a room with specific equipment(s). Weight:
-50hard * nightCount
.
Medium constraints:
-
Assign every patient to a bed, unless the dataset is overconstrained. Weight:
-1medium * nightCount
.
Soft constraints:
-
A patient can prefer a maximum room size, for example if he/she wants a single room. Weight:
-8soft * nightCount
. -
A patient is best assigned to a department that specializes in his/her problem. Weight:
-10soft * nightCount
. A patient is best assigned to a room that specializes in his/her problem. Weight:
-20soft * nightCount
.-
That room speciality should be priority 1. Weight:
-10soft * (priority - 1) * nightCount
.
-
That room speciality should be priority 1. Weight:
-
A patient can prefer a room with specific equipment(s). Weight:
-20soft * nightCount
.
The problem is a variant on Kaho’s Patient Scheduling and the datasets come from real world hospitals.
3.3.5.2. Problem Size Copier lienLien copié sur presse-papiers!
3.3.5.3. Domain Model Copier lienLien copié sur presse-papiers!
3.4. Difficult Examples Copier lienLien copié sur presse-papiers!
3.4.1. Exam Timetabling (ITC 2007 track 1 - Examination) Copier lienLien copié sur presse-papiers!
3.4.1.1. Problem Description Copier lienLien copié sur presse-papiers!
Schedule each exam into a period and into a room. Multiple exams can share the same room during the same period.
Hard constraints:
- Exam conflict: 2 exams that share students must not occur in the same period.
- Room capacity: A room’s seating capacity must suffice at all times.
- Period duration: A period’s duration must suffice for all of its exams.
Period related hard constraints (specified per dataset):
- Coincidence: 2 specified exams must use the same period (but possibly another room).
- Exclusion: 2 specified exams must not use the same period.
- After: A specified exam must occur in a period after another specified exam’s period.
Room related hard constraints (specified per dataset):
- Exclusive: 1 specified exam should not have to share its room with any other exam.
Soft constraints (each of which has a parametrized penalty):
- The same student should not have 2 exams in a row.
- The same student should not have 2 exams on the same day.
- Period spread: 2 exams that share students should be a number of periods apart.
- Mixed durations: 2 exams that share a room should not have different durations.
- Front load: Large exams should be scheduled earlier in the schedule.
- Period penalty (specified per dataset): Some periods have a penalty when used.
- Room penalty (specified per dataset): Some rooms have a penalty when used.
It uses large test data sets of real-life universities.
The problem is defined by the International Timetabling Competition 2007 track 1. Geoffrey De Smet finished 4th in that competition with a very early version of Planner. Many improvements have been made since then.
3.4.1.2. Problem Size Copier lienLien copié sur presse-papiers!
3.4.1.3. Domain Model Copier lienLien copié sur presse-papiers!
Below you can see the main examination domain classes:
Figure 3.3. Examination Domain Class Diagram
Notice that we’ve split up the exam concept into an Exam
class and a Topic
class. The Exam
instances change during solving (this is the planning entity class), when their period or room property changes. The Topic
, Period
and Room
instances never change during solving (these are problem facts, just like some other classes).
3.4.2. Employee Rostering (INRC 2010 - Nurse Rostering) Copier lienLien copié sur presse-papiers!
3.4.2.1. Problem Description Copier lienLien copié sur presse-papiers!
For each shift, assign a nurse to work that shift.
Hard constraints:
- No unassigned shifts (built-in): Every shift need to be assigned to an employee.
- Shift conflict: An employee can have only 1 shift per day.
Soft constraints:
Contract obligations. The business frequently violates these, so they decided to define these as soft constraints instead of hard constraints.
- Minimum and maximum assignments: Each employee needs to work more than x shifts and less than y shifts (depending on their contract).
- Minimum and maximum consecutive working days: Each employee needs to work between x and y days in a row (depending on their contract).
- Minimum and maximum consecutive free days: Each employee needs to be free between x and y days in a row (depending on their contract).
- Minimum and maximum consecutive working weekends: Each employee needs to work between x and y weekends in a row (depending on their contract).
- Complete weekends: Each employee needs to work every day in a weekend or not at all.
- Identical shift types during weekend: Each weekend shift for the same weekend of the same employee must be the same shift type.
- Unwanted patterns: A combination of unwanted shift types in a row. For example: a late shift followed by an early shift followed by a late shift.
Employee wishes:
- Day on request: An employee wants to work on a specific day.
- Day off request: An employee does not want to work on a specific day.
- Shift on request: An employee wants to be assigned to a specific shift.
- Shift off request: An employee does not want to be assigned to a specific shift.
- Alternative skill: An employee assigned to a shift should have a proficiency in every skill required by that shift.
The problem is defined by the International Nurse Rostering Competition 2010.
3.4.2.2. Problem Size Copier lienLien copié sur presse-papiers!
There are 3 dataset types:
- sprint: must be solved in seconds.
- medium: must be solved in minutes.
- long: must be solved in hours.
3.4.2.3. Domain Model Copier lienLien copié sur presse-papiers!
3.4.3. Traveling Tournament Problem (TTP) Copier lienLien copié sur presse-papiers!
3.4.3.1. Problem Description Copier lienLien copié sur presse-papiers!
Schedule matches between n teams.
Hard constraints:
- Each team plays twice against every other team: once home and once away.
- Each team has exactly 1 match on each timeslot.
- No team must have more than 3 consecutive home or 3 consecutive away matches.
- No repeaters: no 2 consecutive matches of the same 2 opposing teams.
Soft constraints:
- Minimize the total distance traveled by all teams.
The problem is defined on Michael Trick’s website (which contains the world records too).
3.4.3.2. Problem Size Copier lienLien copié sur presse-papiers!
3.4.4. Cheap Time Scheduling Copier lienLien copié sur presse-papiers!
3.4.4.1. Problem Description Copier lienLien copié sur presse-papiers!
Schedule all tasks in time and on a machine to minimize power cost. Power prices differs in time. This is a form of job shop scheduling.
Hard constraints:
- Start time limits: each task must start between its earliest start and latest start limit.
- Maximum capacity: the maximum capacity for each resource for each machine must not be exceeded.
- Startup and shutdown: each machine must be active in the periods during which it has assigned tasks. Between tasks it is allowed to be idle to avoid startup and shutdown costs.
Medium constraints:
Power cost: minimize the total power cost of the whole schedule.
- Machine power cost: Each active or idle machine consumes power, which infers a power cost (depending on the power price during that time).
- Task power cost: Each task consumes power too, which infers a power cost (depending on the power price during its time).
- Machine startup and shutdown cost: Every time a machine starts up or shuts down, an extra cost is inflicted.
Soft constraints (addendum to the original problem definition):
- Start early: prefer starting a task sooner rather than later.
The problem is defined by the ICON challenge.
3.4.4.2. Problem Size Copier lienLien copié sur presse-papiers!
3.4.5. Investment asset class allocation (portfolio optimization) Copier lienLien copié sur presse-papiers!
3.4.5.1. Problem Description Copier lienLien copié sur presse-papiers!
Decide the relative quantity to invest in each asset class.
Hard constraints:
Risk maximum: the total standard deviation must not be higher than the standard deviation maximum.
- Total standard deviation calculation takes asset class correlations into account by applying Markowitz Portfolio Theory.
- Region maximum: Each region has a quantity maximum.
- Sector maximum: Each sector has a quantity maximum.
Soft constraints:
- Maximize expected return.
3.4.5.2. Problem Size Copier lienLien copié sur presse-papiers!
de_smet_1 has 1 regions, 3 sectors and 11 asset classes with a search space of 10^4. irrinki_1 has 2 regions, 3 sectors and 6 asset classes with a search space of 10^3.
de_smet_1 has 1 regions, 3 sectors and 11 asset classes with a search space of 10^4.
irrinki_1 has 2 regions, 3 sectors and 6 asset classes with a search space of 10^3.
Larger datasets have not been created or tested yet, but should not pose a problem.