Chapter 9: Choosing the Model (Sports Scheduling)
Helmut Simonis
Cork Constraint Computation Centre Computer Science Department
University College Cork Ireland
ECLiPSe ELearning
OverviewLicence
This work is licensed under the Creative Commons
Attribution-Noncommercial-Share Alike 3.0 Unported License.
To view a copy of this license, visit http:
//creativecommons.org/licenses/by-nc-sa/3.0/ or
send a letter to Creative Commons, 171 Second Street, Suite
300, San Francisco, California, 94105, USA.
Outline
1 Problem
2 Model
3 Program
4 Search
5 Redundant Modelling
What we want to introduce
How to come up with a model for a problem Why choosing a good model is an art Channeling
Projection
Redundant Constraints
Outline
1 Problem
2 Model
3 Program
4 Search
5 Redundant Modelling
Sports Scheduling
Tournament Planning
We plan a tournament with 8 teams, where every team plays every other team exactly once. The tournament is played on 7 days, each team playing on each day. The games are
scheduled in 7 venues, and each team should play in each venue exactly once.
As part of the TV arrangements, some preassignments are
done: We may either fix the game between two particular
teams to a fixed day and venue, or only state that some team
must play on a particular day at a given venue. The objective is
to complete the schedule, so that all constraints are satisfied.
Example
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
Solution
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 6, 8 1, 2 5, 7 3, 4
Day 2 2, 3 1, 5 4, 8 6, 7
Day 3 1, 7 2, 4 3, 8 5, 6
Day 4 4, 7 2, 6 3, 5 1, 8
Day 5 5, 8 3, 6 1, 4 2, 7
Day 6 3, 7 1, 6 4, 5 2, 8
Day 7 4, 6 2, 5 7, 8 1, 3
Problem Model Program Search Redundant Modelling
A More Abstract Formulation
Rooms Puzzle, (Thomas G. Room, 1955)
Place numbers 1 to 8 in cells so that each row and each
column has each number exactly once, each cell contains
either no numbers or two numbers (which must be different
from each other), and each combination of two different
numbers appears in exactly one cell.
A More Abstract Formulation
Rooms Puzzle, (Thomas G. Room, 1955)
Place numbers 1 to 8 in cells so that each row and each column has each number exactly once, each cell contains either no numbers or two numbers (which must be different from each other), and each combination of two different numbers appears in exactly one cell.
Puzzle presented by R. Finkel
Outline
1 Problem
2 Model
Exploring Ideas Expanding Idea 7 Comparing Ideas Channeling Selected Model
3 Program
4 Search
How to come up with a model
What are the variables/what are their values?
How can we express the constraints?
Do we have these constraints in our system?
Does this do good propagation?
Backtrack to earlier step as required
Requirements
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Idea 1
Matrix Day × Game (7 × 4)
Each cell contains two variables, denoting teams Easy to say that team plays once on each day, alldifferent
Columns don’t have significance
Model does not mention location, how to add this?
How to express that each team plays each other once?
Idea 2, Change problem structure
Matrix of Day × Location (7 × 7)
Each cell contains two variables, each denoting a team How do we avoid symmetry inside cell?
Need special value (0) to denote that there is no game In one cell, either both or none of the variables are 0 Easy to say that each row and column contains each team exactly once
Except for value 0, can not use alldifferent
Link between two variables in cell to state that game needs two different teams
How to express that each (ordered) pair occurs exactly
Idea 3, Add location variables
Model as in Idea 1, matrix Day × Game
Each cell contains two variables for teams and one for location
Easy to state that games on one day are in different locations
How to express condition that each team plays in each location once?
Also, how to express that each team plays each other
exactly once?
Idea 4, Use variables for pairs
Matrix Day × Location
Each cell contains one variable ranging over (sorted) pairs of teams, and special value 0 (no game)
Each pair value occurs once, except for 0 Special constraint alldifferent0 Or use gcc
How to state that each team plays once per day?
How to state that each team plays in each location?
Idea 5: If all else fails, use binary variables
Binary variable stating that team i plays in location j at day k
Three dimensional matrix
Each team plays once on each day Each team plays once in each location
Each game has two (different) teams, needs auxiliary variable
Each pair of team meets once, needs auxiliary variables
Idea 6: An even bigger binary model
Use four dimensions
Team i meets team j in location k on day l 3136 = 8*8*7*7 variables
Constraints all linear
Why use finite domain constraints?
Idea 7: A different mapping
Each team plays each other exactly once, one variable for each combination (8*7/2=28 variables)
Decide when and where this game is played, values range over combinations of days and locations (7*7=49 values) All variables must be different (no two games at same time and location)
Each team plays 7 games, by construction
How to express that each team plays once per day?
How to express that each team plays in each location
once?
Expand Idea 7 into Full Model
Numbering Values
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
Four games on each day
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
Day 1 corresponds to values 1..7 Four variables can take these values Day 2 corresponds to values 8..14, etc One constraint per day
Exactly four of all variables take their value in the set ...
Seven such constraints
Four games at each location
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
City 1 corresponds to values 1, 8, 15, 22, 29, 36, 43
Four variables can take these values City 2 corresponds to values
2, 9, 16, 23, 30, 37, 44 One constraint per location
Exactly four of all variables take their value in the set ...
Seven such constraints over 28 variables each
Teams plays once on a day (at a location)
Select those variables which correspond to Team i
Exactly one of those variables takes its value in the set 1..7 Same for all other days
Same for all other teams
56 Constraints over 7 variables each
Similar for teams and locations, another 56 constraints
Are we there yet?
28 variables with 49 possible values 1 alldifferent
7 exactly constraints over all variables (Days) 7 exactly constraints over all variables (Locations) 56 exactly constraints over 7 variables each (Days) 56 exactly constraints over 7 variables each (Locations) Forgotten anything?
Check the requirements
Are we there yet?
28 variables with 49 possible values 1 alldifferent
7 exactly constraints over all variables (Days) 7 exactly constraints over all variables (Locations) 56 exactly constraints over 7 variables each (Days) 56 exactly constraints over 7 variables each (Locations) Forgotten anything?
Check the requirements
Are we there yet?
28 variables with 49 possible values 1 alldifferent
7 exactly constraints over all variables (Days) 7 exactly constraints over all variables (Locations) 56 exactly constraints over 7 variables each (Days) 56 exactly constraints over 7 variables each (Locations) Forgotten anything?
Check the requirements
Do we satisfy the requirements?
By construction
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
By construction
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
By construction
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
56 exactly constraints on 7 variables
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
56 exactly constraints on 7 variables
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
By construction
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
7 exactly constraints on 28 variables
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
7 exactly constraints on 28 variables
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
Do we satisfy the requirements?
alldifferent
1
There are 8 teams, seven days and seven locations
2
Each team plays each other team exactly once
3
Each team plays 7 games (redundant)
4
Each team plays in each location exactly once
5
Each team plays on each day exactly once
6
A game consists of two (different) teams
7
There are four games on each day (redundant)
8
There are four games at each location (redundant)
9
In any location there is atmost one game at a time
What about the exactly constraint?
ECLiPSe doesn’t provide this constraint Other system might do, could switch system Implement it
Extend gcc to allow multiple values Should be last resort
Emulate constraint with others
Idea 8: Mapping games to days and locations
For each game to be played, we have two variables One ranges over the days
The other over the locations
Easy to state that there are four games per day an location Easy to state that each team plays once per day and location
How do we express that no two games are played at the same location and the same time?
If we had an alldifferent over pairs of variables...
Not in ECLiPSe
We have four games on each day
Each row value is taken four times amongst the variables gcc([gcc(4,4,1),...,gcc(4,4,7)],Rows) Similar for columns:
gcc([gcc(4,4,1),...,gcc(4,4,7)],Cols)
Each team plays once per day
For the seven variables which describe games of a team Each row value is taken exactly once amongst the variables
Could use
gcc([gcc(1,1,1),...,gcc(1,1,7)],Vars)
But alldifferent(Vars) is more compact
Similar for columns
How do the models differ?
D Days
T Teams
L Locations
G Games
Idea Mapping
1 D × G × {f , s} → T 2 D × L × {f , s} → T ∪ {0}
3 D × G × {f , s} → T D × G → L 4 D × L → T M T ∪ {0}
5 T × D × L → {0, 1}
6 T × T × D × L → {0, 1}
7 T M T → D × L
8 T M T → D
T M T → L
Requirements Capture
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 1: There are 8 teams, seven days and seven locations
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 2: Each team plays each other team exactly once
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 3: Each team plays 7 games
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 4: Each team plays in each location exactly once
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 5: Each team plays on each day exactly once
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 6: A game consists of two (different) teams
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 7: There are four games on each day
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 8: There are four games at each location
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Requirements Capture
Req 9: In any location there is atmost one game at a time
Y yes
N no
? unknown
C by construction
L linear
NL non-linear
E exactly
G gcc
A alldifferent
Idea Requirement
1 2 3 4 5 6 7 8 9
1 N ? Y ? Y Y Y ? ?
2 C ? Y Y Y Y Y Y Y
3 C ? Y ? Y Y Y Y Y
4 C Y Y Y Y Y Y Y Y
5 C NL L L L NL L L NL
6 C L L L L L L L L
7 C C C E E C E E A
8 C C C A A C G G ?
Comments on models
Idea Main point
1 missing locations, first second symmetry 2 spare value, first second symmetry 3 first second symmetry
4 spare value
5 0/1, non-linear constraints
6 0/1, large matrix
7 needs exactly constraint
8 needs alldifferent on tuples
Channeling
Instead of expressing all constraints over one set of variables
Use multiple sets of variables
Decide which constraint to express over which variables Allows more freedom on how to express problem
Link the different variables with channeling constraints
In Our Case
Combine ideas 7 and 8
One set of variables ranging over pairs
Another using two variables per game for day and location How to combine variables?
Minimize loss of information
Projection
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
Link pair variables to row and column variables Pair variable uses cell numbers 1-49 as values
Row and column variables indicate on which day (row) and in which location (column) the game is played
Pair value 23 = row 4, column 2
element constraint to link the variables
Two projections from D × L space onto D and L
Mapping cells to rows and columns
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
element(Cell,[1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6, 7,7,7,7,7,7,7],Row),
element(Cell,[1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7,
1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7,
1,2,3,4,5,6,7],Col),
Mapping cells to rows and columns
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 1 2 3 4 5 6 7
Day 2 8 9 10 11 12 13 14
Day 3 15 16 17 18 19 20 21
Day 4 22 23 24 25 26 27 28
Day 5 29 30 31 32 33 34 35
Day 6 36 37 38 39 40 41 42
Day 7 43 44 45 46 47 48 49
element(23 ,[1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6, 7,7,7,7,7,7,7],4 ),
element(23 ,[1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7,
1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7,
1,2,3,4,5,6,7],2 ),
Channeling Constraints
This is one common type, a projection Another common type is the inverse
Link a variable A → B to another B → A Typically used for bijective mappings Built-in inverse/2
Also used: Boolean channeling
Link variables A → B and A × B → {0, 1}
Built-in bool_channeling/3
Selected Model
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 1: There are 8 teams, seven days and seven locations Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 2: Each team plays each other team exactly once Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 3: Each team plays 7 games
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 4: Each team plays in each location exactly once Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 5: Each team plays on each day exactly once
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 6: A game consists of two (different) teams
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 7: There are four games on each day
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 8: There are four games at each location
Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Selected Model
Req 9: In any location there is atmost one game at a time Two sets of variables (Req 1, 2, 3, 6, by construction) Pair variables (T M T → D × L)
alldifferent (Req 9)
Day and Location variables (T M T → D), (T M T → L) gcc (Req 4, 5)
alldifferent (Req 7, 8) Channeling Constraints
element projection from pairs onto rows and columns
Search only on pair variables
Handling of hints (I)
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
This value (17) can not be used by pairs not involving team 8
One of the pairs involving team 8 must use this value (17)
Handling of hints (I)
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
This value (17) can not be used by pairs not involving team 8
One of the pairs involving team 8 must use this value (17)
Handling of hints (I)
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
This value (17) can not be used by pairs not involving team 8
One of the pairs involving team 8 must use this value (17)
Handling of hints (II)
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
The pair involving teams 5 and 7 must take value 5, fixes
variable
Handling of hints (II)
City 1 City 2 City 3 City 4 City 5 City 6 City 7
Day 1 8 7, 5
Day 2 2 1, 5
Day 3 7 8
Day 4 2 5 1
Day 5 8 1
Day 6 5, 4
Day 7 4 1, 3
The pair involving teams 5 and 7 must take value 5, fixes
variable
Outline
1 Problem
2 Model
3 Program
4 Search
5 Redundant Modelling
Problem Data
hint(1,8,[2-[8],5-[5,7],8-[2],9-[1,5],15-[7],
17-[8],26-[2],27-[5],28-[1],29-[8],
34-[1],39-[4,5],43-[4],47-[1,3]]).
Main Program
top(Problem,L):-
hint(Problem,N,Hints), N1 is N-1,
N2 is N//2,
NrVars is N*N1//2, SizeDomain is N1*N1, length(L,NrVars), L :: 1..SizeDomain,
create_pairs(N,Contains,Names),
ic_global_gac:alldifferent(L),
process_hints(L,Contains,Hints),
...
Main Program (continued)
project_row_cols(L,N1,Rows,Cols), limit(Rows,N2,N1),
limit(Cols,N2,N1),
separate(Contains,Rows,N,SplitRows), separate(Contains,Cols,N,SplitCols), (foreach(K,SplitRows) do
ic_global_gac:alldifferent(K) ),
(foreach(K,SplitCols) do
ic_global_gac:alldifferent(K) ),
search(L,0,input_order,indomain,
Create Pairs and Names
create_pairs(N,Contains,Names):- (for(I,1,N-1),
fromto(Names,A1,A,[]), fromto(Contains,B1,B,[]), param(N) do
(for(J,I+1,N),
fromto(A1,[Name|AA],AA,A), fromto(B1,[I-J|BB],BB,B), param(I) do
concat_string([I,J],Name)
)
Projecting Rows and Columns
project_row_cols(L,N,Rows,Cols):-
generate_tables(N,RowTable,ColTable), (foreach(X,L),
foreach(R,Rows), foreach(C,Cols),
param(RowTable,ColTable) do
element(X,RowTable,R),
element(X,ColTable,C)
).
Generating Projection Tables
generate_tables(N,RowTable,ColTable):- (for(I,1,N),
fromto(RowTable,A1,A,[]), fromto(ColTable,B1,B,[]), param(N) do
(for(J,1,N),
fromto(A1,[I|AA],AA,A), fromto(B1,[J|BB],BB,B), param(I) do
true
)
Extract row variables
separate(Contains,Rows,Values,SplitRows):- (for(Value,1,Values),
foreach(SplitRow,SplitRows), param(Contains,Rows) do
(foreach(A-B,Contains),foreach(V,Rows), fromto([],R,R1,SplitRow),param(Value) do
(memberchk(Value,[A,B]) ->
R1 = [V|R]
;
R1 = R )
)
Set up gcc constraint
limit(L,Bound,Values):- (for(I,1,Values),
foreach(gcc(Bound,Bound,I),Pattern), param(Bound) do
true ),
gcc(Pattern,L).
Setting up hints
process_hints(L,Contains,Hints):- (foreach(Pos-Values,Hints),
param(L,Contains) do
process_hint(Pos,Values,L,Contains) ).
process_hint(Pos,[A,B],L,Contains):- % clause 1
!,
match_hint(A-B,Contains,L,X),
X #= Pos.
Setting up hints
process_hint(Pos,[Value],L,Contains):- % clause 2 (foreach(X,L),
foreach(A-B,Contains), fromto([],R,R1,Required), param(Pos,Value) do
(not_mentioned(A,B,Value) ->
X #\= Pos, R1 = R
;
R1 = [X|R]
)
),
Setting up hints
not_mentioned(A,B,V):- A \= V,
B \= V.
match_hint(H,[H|_],[X|_],X):-
!.
match_hint(H,[_|T],[_|R],X):-
match_hint(H,T,R,X).
Outline
1 Problem
2 Model
3 Program
4 Search
Using input order
First Fail Strategy
Before Search
Input Order
Input Order
12
1
Input Order
12
1 3
Input Order
12
1
14
20
3
Input Order
12
1
14
20 21
3
Input Order
12
1
14
20 21 22
3
Input Order
12
1
14
20 21 22 28
3
Input Order
12
1
14
20 21 22
16
18
28
3
Input Order
12
1
14
20 21 22
16 17
34
18
28
3
Input Order
12
1
14
20 21 22
16 17
34 36
18
28
3
Input Order
12
1
14
20 21 22
16 17
34 36 41
18
28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20
28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32 34 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
17
15 34 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
17
15 18 34 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
17
15 18 36 34 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34 36 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34
17
18 36 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34
17
18 20 36 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34
17
18 20 32 36 28
3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34
17
18 20 32 34 36
28 3
Input Order
12
1
14
20 21 22
16
1 3
18 20 32
1 3
34
1 4
36 41 28
3
Input Order
12
1
14
20 21 22
4 13
28 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
18
34
3
Input Order
12
1
14
20 21 22
4 13
28
16 17
22
18
34
3
Input Order
12
1
14
20 21 22
4 13
28
16 17
22 28
18
34
3
Input Order
12
1
14
20 21 22
4 13
28
16 17
22 28 36 18 34 3
Input Order
12
1
14
20 21 22
4 13
28
16 17
22 28 36 42 18 34 3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21
34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25 28 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
17
15 28 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
17
15 18 28 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
17
15 18 36 28 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
1 3
28 36 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
1 3
28
17
18 36 34
3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
1 3
28
17
18 21 36 34
3
Input Order
12 1
14 20 21 22
4 13
28 16
1 4
18 21 22 25
1 3
28 17 18 21 25 36 34
3
Input Order
12
1
14
20 21 22
4 13 28
16
1 4
18 21 22 25
1 3 28
17
18 21 25 28 36
34 3
Input Order
12
1
14
20 21 22
4 13
28
16
1 4
18 21 22 25
1 3
28
1 4
36 42 34
3
Input Order
12
1
14
20 21 22
4 13
28
4 15
34 35 3
Input Order
12
1
9 32
3 4
Input Order
12
1
9 32
3
14
20
4
Input Order
12
1
9 32
3
14
20 21
4
Input Order
12
1
9 32
3
14
20 21 22
4
Input Order
12
1
9 32
3
14
20 21 22 24
4
Input Order
12
1
9 32
3
14
20 21 22 24 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20
28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31 34 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
17
15 34 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
17
15 36 34 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
17
15 36 38 34 28 4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34 36 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 36 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31 36 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31 34 36 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31
23
8 34 36 28
4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31
23
8 10 34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 34 36 28 4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31
23
8 10 13 14 34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16
34 36 28 4
Input Order
12
1
9 32
3
14
20 21 22 24
16
20 31
1 3
34
17
20 31
23
8 10 13 14 16 20 34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23
8 10 13 14 16 20 21 22 23 24 30 34
36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 30 34
36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 10 30 34
36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31 1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 10 12 30
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31 1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 10 12 13 30
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31 1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 10 12 13 19 30
34 36 28 4
Input Order
12 1
9 32
3 14 20 21 22 24
16 20 31
1 3
34 17 20 31
23 8 10 13 14 16 20 21 22 23 24
24 8 10 12 13 19 20 30
34 36 28 4