Download presentation
Presentation is loading. Please wait.
Published byMaurice McCormick Modified over 8 years ago
1
Håkan Kjellerstrand (hakank@gmail.com) http://hakank.org/ My Constraint Programming Blog: http://hakank.org/constraint_programming_blog/ My GitHub page: https://github.com/hakank/hakank/ Constraint Programming: An introduction
2
- What is Constraint Programming (CP) - why is it so fascinating? - Key concepts in CP - Examples of modelling, code in MiniZinc - Questions, please ask during the talk Overview
3
Håkan Kjellerstrand - Systems developer at Bokus (http://www.bokus.com/) - Constraint Programming is a private interest - Built CP models, tested CP systems, and blogged/etc about CP since 2008 - Use CP for puzzles, explorations in mathematics/computer science, etc.
4
What is CP used for? In industry CP is used for, among many other things: - scheduling and resource allocation - staff rostering - complex configuration problems - packing problems - DNA sequencing - vehicle / transport routing - analysis of analog circuits - financial planning, investment management -... In general: combinatorial search and optimization
5
What is CP? Some key concepts * Techniques * Modeling In this talk I will focus on the modeling part.
6
Main principle of CP – much simplified Key components: - DECISION VARIABLES - DOMAINS of the decision variables - CONSTRAINTS between the decision variables - All decision variables are declared with a domain (often finite domain), including arrays, matrices, etc. The CP solver loop: - The solver PROPAGATES the constraints and domains of the decision variables in the model until: * a FIXPOINT is reached (no changes in the domains) * or assign the variables in the search tree, perhaps via BACKTRACKING. - If a domain of a variable is empty then the assignment is illegal and the solver backtracks to test new assignments.
7
Time for some code! - MiniZinc Sites: - http://minizinc.org/ - http://hakank.org/minizinc/ (> 1100 public models) * One of the most high-level CP systems * Try a model and test with many solvers * About 30 different FlatZinc solvers with very different strengths * Drawback: not a general programming language
8
Sudoku 4x4 4 _ _ _ 3 1 _ _ _ _ 4 1 _ _ _ 2 Constraints: - all rows must have different digits - all columns must have different digits - all boxes (2x2) must have different digits Let's see how this can be stated in a CP system. 4 _ _ _ 3 1 _ _ _ _ 4 1 _ _ _ 2 Constraints: - all rows must have different digits - all columns must have different digits - all boxes (2x2) must have different digits Let's see how this can be stated in a CP system. 4 _ _ _ 3 1 _ _ _ _ 4 1 _ _ _ 2 Constraints: - all rows must have different digits - all columns must have different digits - all boxes (2x2) must have different digits Let's see how this can be stated in a CP system. 4 _ _ _ 3 1 _ _ _ _ 4 1 _ _ _ 2 Constraints: - all rows must have different digits - all columns must have different digits - all boxes (2x2) must have different digits Let's see how this can be stated in a CP system.
9
Sudoku 4x4 – MiniZinc code % Note: init of hints, search, and output missing % parameters (constants) int: r = 2; int: n = r*r; % i.e. 4 % decision variables array[1..n, 1..n] of var 1..n: x; % matrix constraint % i'th row forall(i in 1..n) (all_different([x[i,j] | j in 1..n])) /\ % j'th column forall(j in 1..n) (all_different([x[i,j] | i in 1..n])) /\ % the blocks forall(i in 0..r-1,j in 0..r-1) ( all_different([x[r,c] | r in i*r+1..i*r+r, c in j*r+1..j*r+r]) ) ;
10
Sudoku 4x4 – propagation 4 _ _ _ 3 1 _ _ _ _ 4 1 _ _ _ 2 Solution (unique) 4 2 1 3 3 1 2 4 2 3 4 1 1 4 3 2 How does a CP solver reach this solution?
11
Sudoku 4x4 – propagation example (simplified ) 4 1234 1234 1234 3 1 1234 1234 1234 1234 4 1 1234 1234 1234 2 Add DOMAINS (1..4) to all unknown variables. Hints are FIXED already. Now we will propagate the three alldifferent constraints: - all_different(ROW) - all_different(COLUMN) - all_different(BLOCK) This is a very simplified example. Real CP systems use more intelligent propagation.
12
Sudoku 4x4 – simple propagation example 2 4 2 1234 1234 3 1 1234 1234 1234 1234 4 1 1234 1234 1234 2 Cell (1,1): Fixed value (4). Cell (1,2): Reduce: - remove 4 (row, block) - remove 1 (column, block) - remove 3 (block) → Single value: 2
13
Sudoku 4x4 – simple propagation example 2 4 2 1 3 1234 3 1 1234 1234 1234 1234 4 1 1234 1234 1234 2 Cell (1,3): Reduce: - remove 4 (row, column) - remove 2 (row) → {1 3}
14
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 1234 1234 1234 1234 4 1 1234 1234 1234 2 Cell (1,4): Reduce: - remove 4 (row) - remove 1 (column) - remove 2 (column) → 3 Note: Here we don't go back to fix cell (1,3). Cell (2,1): fixed (3) Cell (2,2): fixed (1)
15
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 1234 1234 1234 4 1 1234 1234 1234 2 Cell (2,3): Reduce: - remove 3 (row) - remove 1 (row) - remove 4 (column) → 2
16
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 1234 1234 4 1 1234 1234 1234 2 Cell (2,4): Reduce: - remove 3 (row) - remove 1 (row, column) - remove 2 (row, column) → 4
17
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 1234 4 1 1234 1234 1234 2 Cell (3,1): Reduce: - remove 4 (row, column) - remove 1 (row) - remove 3 (column) → 2
18
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 3 4 1 1234 1234 1234 2 Cell (3,2): Reduce: - remove 1 (row, column, block) - remove 2 (row) - remove 4 (row) → 3
19
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 3 4 1 1234 1234 1234 2 Cell (3,3): Fixed. Cell (3,4): Fixed.
20
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 3 4 1 1 1234 1234 2 Cell (4,1): Reduce - remove 2 (row) - remove 4 (column) - remove 3 (column) → 1
21
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 3 4 1 1 4 1234 2 Cell (4,2): Reduce - remove 1 (row) - remove 2 (row) - remove 3 (column) → 4
22
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 3 1 2 4 2 3 4 1 1 4 3 2 Cell (4,3): Reduce - remove 2 (row, block) - remove 4 (column, block) – - remove 1 (block) → 3 Cell (4,4): Fixed 2 Are we finished? No! There is still a variable/cell with no single assignment, i.e. Cell (1,3).
23
Sudoku 4x4 – simple propagation example 2 4 2 1 3 3 1 2 4 2 3 4 1 1 4 3 2 Cell (1,3): Reduce - remove 3 (row, block) → 1 And now all variables has been assigned to a single value.
24
Sudoku 4x4 – simple propagation example 4 2 1 3 3 1 2 4 2 3 4 1 1 4 3 2 … and we got a solution! It is unique – as a Sudoku should be.
25
Sudoku 4x4: Points to take home * Propagation and domain reduction is one of the key principles to CP. * CP solvers are more intelligent than this.
26
Another model: Minesweeper – Where are the bombs? Minesweeper – in this version – is a simple grid problem:..2.3. 2.......24.3 1.34.......3.3.3.. - Each number represents how many bombs there are in the nearby cells (including the diagonals) - The “.” (dot) represents an unknown cell: either a bomb or empty cell - If a cell contains a number (hint) then there cannot be a bomb
27
Minesweeper – the setup int: X = -1; % representing the unknowns in hints matrix % >= 0 for number of mines in the neighbourhood array[1..r, 1..c] of -1..8: game; % problem instance, the hints int: r = 6; % rows int: c = 6; % column game = array2d(1..r, 1..c, [ X,X,2,X,3,X, 2,X,X,X,X,X, X,X,2,4,X,3, 1,X,3,4,X,X, X,X,X,X,X,3, X,3,X,3,X,X, ]); % decision variables: 0/1 for no bomb/bomb array[1..r, 1..c] of var 0..1: mines;
28
Minesweeper – constraints % game[1..n, 1..n]: the given hints % mines[1..n, 1..n]: 0/1 where 1 represent a bomb % X: -1 represents the unknown constraint forall(i in 1..r, j in 1..c) ( ( (game[i,j] > X ) -> % the hint number must be the number % of all the surrounded bombs (1s) game[i,j] = sum(a,b in {-1,0,1} where i+a > 0 /\ j+b > 0 /\ i+a <= r /\ j+b <= c ) (mines[i+a,j+b]) ) /\ % if a hint, then it can't be a bomb (game[i,j] > X -> mines[i,j] = 0) ) ;
29
Minesweeper Declarative aspect of Constraint Programming The ideal: - Just state the requirements (the constraints) - It's now up to the CP solver to find the solution. %... % the hint number must be the number % of all the surrounded bombs game[i,j] = sum(a,b in {-1,0,1} where i+a > 0 /\ j+b > 0 /\ i+a <= r /\ j+b <= c ) (mines[i+a,j+b]) %...
30
Minesweeper - Solution..2.3. 2.......24.3 1.34.......3.3.3.. 100001 % 1: Bomb, 0: no bomb 010110 000010 011100 100011
31
Key concepts in CP modelling Some of the most important - and IMHO the most fascinating - concepts in CP regarding the modelling: - Global constraints - Symmetry breaking - Element - Reification (logical relations between constraints) - Bi-directedness (reversibility, multiple flow pattern) - Channelling (dual views) - Symmetry breaking - 1/N/All solutions
32
Global constraints - “Patterns” in modelling problems - Constraint for arrays and variable arguments - Much research are focused on effective global constraints - Often much faster than basic constraints (all_different vs. collection of '!=') - Global Constraint Catalog, describes >400 global constraints http://sofdem.github.io/gccat/ Some of the most common global constraints: - All different: all elements must be distinct - All different except 0: all elements != 0 must be distinct - Element: indexing with decision variables - Global Cardinality Count: count occurrences of values - Decrease/Increase: ensure sortedness - Cumulative: for scheduling - Circuit: Hamiltonian circuit - Table: Allowed assignments Decompositions: http://hakank.org/minizinc/#global (~170 decompositions)
33
Element constraint (z = x[y]) One of the most common – and important - constraint: - z = x[y], where the index (y) is a decision variable % MiniZinc array[1..4] of var 1..4: x; var 1..4: y; var 1..4: z; constraint z = x[y] ; % example A, z unknown: z = x[y] % x = [4,3,1,2] % y = 2 % -> z = 3 % example B, y unknown: z = x[y] % x = [4,3,1,2] % z = 1 % -> y = 3
34
Reification Reification: truth values of constraints - implication: C => b: If constraint C holds then BoolVar b = 1 else b = 0. - equivalence: C1 C2: If constraint C1 holds then C2 also holds (and vice versa) - C1 and C2, C1 or C2, not C1, (C1 xor C2) Some examples (in MiniZinc syntax): - x and y are var int (domain 1..10) - b1 and b2 are var boolean (0..1) (x = y) -> (b1 = 1) % implication (x > 4) -> (y <= 4) % implication b1 not(b2) % equivalence (x = 1 /\ y = 1) (b1 = b2) % AND, equivalence
35
Reification – alldifferent_except_0 The global constraint alldifferent_except_0 is quite useful: All variables that are != 0 must be distinct. Applications: - 0 as a “dummy” value in an array (of decision variables) Decomposition using reifications: predicate alldifferent_except_0(array[int] of var int: x)= forall(i,j in index_set(x) where i < j) ( (x[i] != 0 /\ x[j] != 0) -> x[i] != x[j] ) ;
36
Reversibility - “bi-direction” Cf Prolog where a predicate can have variables that may be either input, output or both (“multiple flow pattern”). Simple example: - converting a number (variable) TO/FROM its digits (array) given a base
37
Reversibility - converting a number (variable) TO/FROM its digits (array) given a base function var int: to_num_base(array[int] of var int: a, int: base) = let { int: len = length(a); var int: n = sum(i in index_set(a)) ( pow(base, len-i) * a[i] ); } in n % n is the “return” value ; % n = 532 a = [5,3,2], i.e. [5*100,3*10,2*1]
38
Reversibility – “dual view” - converting a number (variable) TO/FROM its digits (array) given a base We can now add constraints on either n or x: - a “dual view” on two different representations Example: int: m = 5; array[1..m] of var 0..9: x; var 0..pow(10,m)-1: n; constraint n = to_num(x) /\ all_different(x) /\ x[3] = 4 /\ n >= 50000 ; The ability to add constraints quite easily to a model is one of the strengths of CP.
39
Symmetry breaking A CP model is - under the hood - a search tree. A common technique to reduce a large search tree is to use SYMMETRY BREAKING. Some standard global constraints for symmetry breaking (sortedness): - increasing/decreasing: the array must be sorted - lex_less(x, y): array x is lexicographic less then array y - lex2(matrix): all rows and columns must be in lexicographic order - increasing_except_0: all values except 0 must be sorted Special forms: - x[1] < x[n] - Magic square - Frénicle standard form: constraint minimum(x[1,1], [x[1,1], x[1,n], x[n,1], x[n,n]]) /\ x[1,2] < x[2,1] ;
40
1/N/All solution(s) and optimal solution Most CP solvers can show any number of solutions. - First solution: just any valid solution (schedule/seating/etc). - N solutions, e.g. N=2 to check if/ensure that there are > 1 solutions. * Sudoku: must be unique solution The model is wrong if 2 solutions are printed. - All solutions * Debugging aid: N-queens for N=8 should yield 92 solutions * In mathematical problems, the number of solutions give clues to the structure. (Online Encyclopedia of Integer Sequences: http://oeis.org/) - Optimal solution Important in industrial applications (as well as some puzzles)
41
Constraint Programming – not a silver bullet Pros: - Excellent tool for modeling certain type of combinatorial problems - Modelling language syntax is often high or very high - Easy to add new constraints. Cons: - It takes time to model a problem, often with much experiments, especially to get search strategies right: art not science - Requires a different mind set - Debugging can be harder than in non-CP programming - Can be hard to translate traditional algorithms/data structures to a CP model.
42
Thank you! - Questions? Håkan Kjellerstrand (hakank@gmail.com) http://hakank.org/ My Constraint Programming Blog: http://hakank.org/constraint_programming_blog/ Constraint Programming: http://hakank.org/constraint_programming/ My GitHub page: https://github.com/hakank/hakank/
43
Minesweeper – search strategies To ensure the best/good performance, search strategies (heuristics) are used: - variable selection: which variable to test (in the search tree traversal) - value selection: which value to try on the selected variable Selecting the best strategy is an art - not an science - and often requires much experimentation. Example: solve :: int_search( [mines[i,j]|i in 1..r,j in 1..c], first_fail, indomain_min, complete) satisfy;
44
What have I NOT talked about? Advanced features in CP: - how search heuristics and propagation really works - set variables (can be very handy) - float variables - advanced examples - working of a CP solver: * parsing * propagators * search tree * etc
45
CP - Swedish terms This talk will be in Swedish. Here is a translation of “CP English” to Swedish. Most English terms are actually used in the Swedish discourse. - Constraint Programming: villkorsprogrammering - Constraints: villkor - Decision variables: beslutsvariabler - (Finite) domains: (finit) domän - Search tree: sökträd - Propagator/propagate: propagerare/propagera - (CP) Solver: (CP) lösare Modelling: - Global constraints: global constraints (perhaps “globala villkor”) - Search strategies (heuristics): sökstrategier (heuristiker) - Reification: reifikation - Reversibility: reversibilitet - Symmetry breaking: symmetribrytning - Dual view: “variabelkoppling”(?)
46
Element constraint (z = x[y]) Most CP solvers don't have support for the direct syntax as MiniZinc. Instead: - z = element(x, y) - z = x.element(y) - element(x,y,z) Element in other CP systems: # Google or-tools/Python solver.Add(z == solver.Element(x, y)) // Google or-tools/C# solver.Add(z == x.Element(y)); // Google or-tools/Java solver.addConstraint( solver.makeEquality(z, solver.makeElement(x, y).var()));
47
Langford problem (element, symmetry breaking, dual view) Langford problem: Arrange 2 sets of positive integers 1..k to a sequence, such that, following the first occurrence of an integer i, each subsequent occurrence of i, appears i+1 indices later than the last. For example, for k=4, a solution would be 41312432 CSPLIB Problem 24: http://www.csplib.org/prob/prob024/
48
Langford problem (element, symmetry breaking, dual view) int: k = 4; set of int: pos_domain = 1..2*k; % position domain array[pos_domain] of var pos_domain: pos; % dual array[pos_domain] of var 1..k: sol; % for presentation constraint forall(i in 1..k) ( pos[i+k] = pos[i] + i+1 /\ sol[pos[i]] = i /\ % element sol[pos[k+i]] = i % element ) /\ all_different(pos) /\ sol[1] < sol[2*k] % symmetry breaking ; % unique solution: % position: [2, 5, 3, 1, 4, 8, 7, 6] % solution: [4, 1, 3, 1, 2, 4, 3, 2]
49
Reification – alldifferent_except_0 (or-tools/C#) In Google or-tools one have to use Boolean algebra. public static void AllDifferentExcept0(Sol s, IntVar[] a) { int n = a.Length; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { // Boolean algebra: (b1 <= b2) s.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j])); } Note: Most (other) CP solvers have some direct syntax for reifications.
50
Minesweeper – other implementations Implementations of Minesweeper using the same (as possible) approach: http://hakank.org/common_cp_models/#minesweeper Choco: MineSweeper.java Comet: minesweeper.co ECLiPSE: minesweeper.ecl Gecode/R: minesweeper.rb Google CP Solver/C#: minesweeper.cs Google CP Solver/Java: Minesweeper.java Google CP Solver/Python: minesweeper.py JaCoP: MineSweeper.java JaCoP/Scala: Minesweeper.scala MiniZinc: minesweeper.mzn SICStus: minesweeper.pl Essence': minesweeper.eprime Picat: minesweeper.pi Etc...
51
What do I do with CP? (NB: Private interests...) - CP/OR concepts: bin packing, seating problems, TSP,... - Math/CS concepts: clique, hitting set, graph related,... - Combinatorial structures: 17x17 problem, Graeco-Latin (magic) squares,... - Grid/pencil games: Sudoku, KenKen, Strimko, Rogo,... - Recreational math/CS puzzles: 8809 = 6 problem, alphametic problems,... - Word games: Crosswords, Word chains,... - Planning problems (optimal steps for a plan): 15-puzzle, M12,... - “Combinatorial Magic“ - Etc.
52
My main interests and goals with CP Main interest in CP: - learning CP - modelling - testing CP systems - global constraints (decompositions) Method: - Explore (testing a lot of CP systems) - Exploit (modelling) - Explain (blogging, or at least on Twitter/Google+) In general: - trying to make CP more known - solve specific problems
53
Constraint Programming - Pure private interest, “private researcher” - Checked out CLP somewhat in 1998 and later 2001 - 2007 autumn: Mathematical Programming/Operations Research/Excel - 2008 February: Found MiniZinc - Late 2008: Started “My Constraint Programming Blog” - Have now tested about 25 different CP systems (bug reports, suggestions, examples) - Two academic papers, three conference papers Other interests: - Programming languages - Machine learning/data mining, symbolic regression - (Recreational) Mathematics - AI - Magic (not performing) Have written some - unpublished - magic related CP models
54
Tested Constraint Programming systems - Choco (25 models) - Comet (169 models) - ECLiPSe CLP (181 models) - Gecode (165 models) - Gecode/R (29 models) - JaCoP (18 models) - JaCoP/Scala (39 models) - MiniZinc (1031 models) - SICStus Prolog (152 models) - Essence'/Tailor (26 models) - Essence'/Savile Row (70 models) - Zinc (39 models) - Google or-tools/Python (195 models) - Google or-tools/Java (36 models) - Google or-tools/C# (133 models) - OscaR (Scala in OR) (147 models) - Java JSR-331 (42 models) - Numberjack (52 models) - AIMMS+CP (39 models) - B-Prolog (207 models) - Choco3 (90 models) - AMPL (100 "pure" CP models) - Picat (390 CP models, in all: 760 programs) - ILOG CP Optimizer OPL (unpublished, > 100 models) - Answer Set Programming ("related paradigm" 87 encodings) - and about 25 MiniZinc (FlatZinc) solvers Common constraint models: http://hakank.org/common_cp_models/
55
CP in software development CP can be used in software development, e.g. for generating test cases. Example: generating valid (Swedish) personal identification numbers. MiniZinc % init and decision variables int: n = 10; array[1..n] of var 0..9: pno; var 1..99: year = 10*pno[1] + pno[2]; var 1..12: month = 10*pno[3] + pno[4]; var 1..31: day = 10*pno[5] + pno[6]; var 0..999: birthno = 100*pno[7] + 10*pno[8] + pno[9]; array[1..9*2] of var 0..9: ca; % checksum array var 0..9: checksum = pno[10]; % checksum solve satisfy;
56
CP in software development – continued MiniZinc. % Handle the date % define leap year predicate leap_year(var int: y) = (y mod 4 == 0 /\ y mod 100 != 0) \/ y mod 400 == 0 ; constraint if month == 2 then if leap_year(year) then day <= 29 else day <= 28 endif elseif month in {4,6,9,11} then day <= 30 else day <= 31 endif ;
57
CP in software development – continued MiniZinc % check sum constraint forall(i in 1..n-1) ( if i mod 2 = 1 then pno[i]*2 = 10*ca[2*i-1] + ca[2*i] else pno[i]*1 = 10*ca[2*i-1] + ca[2*i] endif ) /\ checksum = 10 - (sum(ca) mod 10) ;
58
Some wishes: CP systems in general - Built-in support for testing/benchmarking, e.g. * different search heuristics * different problem instances - Built-in support for generating all optimal solutions - Support for planning problems (thought I'm not sure how...) - Uniform statistics report - FlatZinc support - Perl interface to an existing CP system - More syntactic sugar...
59
My Learning Problems - Same ~18 (or 30) problems when learning a new CP system - Kind of Project Euler/Rosetta Code - Increasing complexity of both CP and host language - I know these problems well: which solutions and how many - Then blogging “A first look at System X” Collection of models implemented in at least 2 CP systems: http://hakank.org/common_cp_models/
60
My “about 18” Learning Problems - SEND+MORE=MONEY / N-Queens: running a model, what to expect in output - Least Diff: minimize the difference ABCDE - FGHIJ (digits all distinct) - Diet: how to interact with integer arrays and variable arrays - Seseman: generate one or all solutions, handling of matrices - Coins grid: Tony Hubermann's grid puzzle, minimize distances, MIP - Simple map colouring: using graph/matrix, optimization - de Bruijn sequence: a personal favourite, command line options, all solutions - alldifferent_except_0: (decomposition of a) global constraint, reification - Furniture Moving: scheduling, cumulative - Minesweeper: more advanced example, problems from a file. - Quasigroup Completion: alldifferent on rows/columns, matrices - Survo puzzle: alldifferent, reading instances from file - Young Tableaux and partitions: combinatorial problem - Send Most Money in any base: first optimize, then generating all solutions - xkcd: simple problem, knapsack / set covering - Crosswords: simple (from Apt etc), strings/chars, non-trivial Element - Word square: another non-trivial Element, how to read a file (word list) - Who killed Agatha: logical problem, Element, reification - Nowadays: about 30 models before blogging...
61
Literature: blogs - My Constraint Programming Blog http://hakank.org/constraint_programming_blog/ - Helmut Simonis "Constraint Application Blog" http://hsimonis.wordpress.com/ - Pierre Schaus (et.al) "CP is fun" http://cp-is-fun.blogspot.com/ - Jacob Feldman "Constraint Standardization Blog" http://cpstandard.wordpress.com/ - Nathan Brixius http://blogs.msdn.com/b/natbr/ - Mike Trick's OR blog (links to a lot of other OR blogs) http://mat.gsia.cmu.edu/blog/
62
Literature: books - Pascal Van Hentenryck: "Constraint Satisfaction in Logic Programming" (1989) - Kim Marriott, Peter J. Stuckey "Programming with Constraints" (1998) - Pascal Van Hentenryck "The OPL Optimization Programming Language" (1999) - Krzysztof Apt "Principles of Constraint Programming" (2003) - Rina Dechter: "Constraint Processing" (2003) - "Handbook of Constraint Programming" (2006) - Krzysztof Apt, Mark Wallace "CLP using Eclipse" (2007) - Guido Tack's “Constraint Propagation” (thesis, 2009) - Antoni Niederlinski: "A Quick and Gentle Guide to Constraint Logic Programming via ECLiPSe" (2011) - Christian Schulte, Gert Smolka "FD Constraint Programming in Oz" - Helmut Simonis: http://4c.ucc.ie/~hsimonis/ - Barbara Smith: http://www.comp.leeds.ac.uk/bms/papers.html Related paradigms: Mathematical Modeling and Local Search - Tony Hürlimann "Mathematical Modelling of Puzzles and Games" - H. Paul Williams: “Model Building in Mathematical Programming” - Pascal van Hentenryck, Laurent Michel "Constraint-Based Local Search"
63
Literature: video lectures - From CPAIOR 2009 http://wpweb2.tepper.cmu.edu/rlang/CPAIOR09/constraintProgTutorial.html - CP Summer School 2007 Ian Gent, John Hooker, Christian Schulte, etc http://www.iiia.csic.es/summerschools/sscp2007/ - Helmut Simonis ECLiPSe ELearning website http://4c.ucc.ie/~hsimonis/ELearning/ - Alan Frisch: "Decade of Progress in Constraint Modelling & Reformulation: The Quest for Abstraction and Automation" http://www.cs.st-andrews.ac.uk/files/alan-frisch-seminar.m4v
64
Debugging in CP Can be hard to debug CP programs: - everything happens at once (in the constraint store) - assignments shown only after success - few CP systems has step-debugging - tricky: when indices (decision variables) are wrong When there is no solution: - comment out all constraints and add one after one - add boolean values to check which constraints are active - print indices in loops (if possible) - print the domains of variables (if possible) Too slow: - systematic testing of search strategies (for small problem instances) - study the search tree (CPViz, Gist, etc) - try other representations (point of views) of the problem. Perhaps combine them (dual view)
65
Minesweeper – or-tools/Java for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { if (game[i][j] >= 0) { solver.addConstraint( solver.makeEquality(mines[i][j], 0)); ArrayList neighbours = new ArrayList (); for(int a: S) { for(int b: S) { if (i + a >= 0 && j + b >= 0 && i + a < r && j + b < c) { neighbours.add(mines[i + a][j + b]); } } } solver.addConstraint( solver.makeSumEquality( neighbours.toArray(new IntVar[1]), game[i][j])); } if (game[i][j] > X) { solver.addConstraint( solver.makeEquality(mines[i][j], 0)); } } }
66
Reification – alldifferent_except_0 (or-tools/Python) def alldifferent_except_0(solver, a): n = len(a) for i in range(n): for j in range(i): s.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j])) def main(): #... n = 6 x = [s.IntVar(0,n-1) for i in range(n)] alldifferent_except_0(solver, x)
67
Reification – alldifferent_except_0 (or-tools/Java) public static void alldifferent_except_0(Solver s, IntVar[] a) { int n = a.length; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { IntVar bi = s.makeIsDifferentCstVar(a[i], 0); IntVar bj = s.makeIsDifferentCstVar(a[j], 0); IntVar bij = s.makeIsDifferentCstVar(a[i], a[j]); solver.addConstraint( s.makeLessOrEqual( s.makeProd(bi, bj).var(), bij)); } // … int n = 5; IntVar[] x = solver.makeIntVarArray(n, 0, n-1); alldifferent_except_0(solver, x);
68
Element constraint: Langford problem (or-tools/Python) k = 4 p = range(2*k) pos= [solver.IntVar(0, 2*k-1) for i in p] sol= [solver.IntVar(1, k) for i in p] solver.Add(solver.AllDifferent(pos)) for i in range(1,k+1): solver.Add(pos[i+k-1] == pos[i-1] + i+1) # (sol[pos[i-1]] == i) solver.Add(solver.Element(sol, pos[i-1]) == i) solver.Add(solver.Element(sol, pos[k+i-1]) == i) # symmetry breaking solver.Add(sol[0] < sol[2*k-1])
69
Element constraint: Langford problem (or-tools/C#) Int k = 4; int p = 2*k; IntVar[] pos= solver.MakeIntVarArray(p, 0, p-1); IntVar[] sol= solver.MakeIntVarArray(p, 1, k); solver.Add(pos.AllDifferent()); for(int i = 1; i <= k; i++) { solver.Add(pos[i+k-1] - (pos[i-1] + solver.MakeIntVar(i+1,i+1)) == 0); // (sol[pos[i-1]] == i) solver.Add(sol.Element(pos[i-1]) == i); solver.Add(sol.Element(pos[k+i-1]) == i); } // Symmetry breaking solver.Add(sol[0] < sol[2*k-1]);
70
Symmetry breaking: Magic squares For an NxN matrix, all - rows - columns - diagonals must sum to same number Declarative: total may be either - a decision variable or - fixed as (N*(N*N+1))/2 % MiniZinc is 1-based (in case you wonder) all_different([x[i,j] | i in 1..N, j in 1..N]) /\ forall(k in 1..N) ( sum(i in 1..N) (x[k,i]) = total /\ sum(i in 1..N) (x[i,k]) = total ) /\ % diagonals sum(i in 1..N) (x[i,i]) = total /\ sum(i in 1..N) (x[i,n-i+1]) = total
71
Symmetry breaking Langford problem Without symmetry breaking (k=4), two solutions: solution: 4 1 3 1 2 4 3 2 solution: 2 3 4 2 1 3 1 4 With symmetry breaking, one solution solution[0] < solution[2*k-1] solution: 2 3 4 2 1 3 1 4 Different k, with/without symmetry breaking: K without with --------------------- 3 2 1 4 2 1 7 52 26 8 300 15011 11 35584 17792
72
Symmetry breaking: Magic squares – Frénicle standard form Frénicle standard form: - the element at position [1,1] (top left corner) is the smallest of the four corner elements; and - the element at position [1,2] (top edge, second from left) is smaller than the element in [2,1]. % MiniZinc Constraint minimum(x[1,1], [x[1,1], x[1,n], x[n,1], x[n,n]]) /\ x[1,2] < x[2,1]; N Without symb. With symb. --------------------------------------- 1 1 1 2 0 0 3 8 1 4 7040 880 5 ? 275305224 6 ? ~1.7745×10^19 (estimate)
73
Scheduling: Furniture moving (cumulative) Moving 4 furnitures - Duration of each task: 30, 10, 15, 15 minutes - Required resources : 3, 1, 3, 2 (people required) int: ul = 120; % upper limit of time array[1..n] of var 0..ul: Start; % Start times array[1..n] of var 0..ul: Dur; % Durations array[1..n] of var 0..ul: Res; % Resources array[1..n] of var 0..ul*2: End; % End times var 0..10: numP; solve minimize numP; % objective constraint cumulative(Start, Dur, Res, numP) /\ forall(i in 1..n) (End[i] = Start[i] + Dur[i]); % numP: 3 % Resources: [ 3, 1, 3, 2] % Start : [30, 0, 15, 0] % Durations: [30, 10, 15, 15] % End : [60, 10, 30, 15]
74
Comparison of the systems, for learning a CP system Focus on: - Ease of modelling, syntax (example: Element for integer variable matrices) - Documentation, site, introduction material - Number of examples - Number of built in constraints (can be hard to count) - Active community: mailing list, forum, Wiki - Input/output, command line options - Build in search strategies - Other features Note: These are - of course - not all the criteria for using or buying a system. I have focused on the criteria how easy it was – for me – to learn a system.
75
G12 MiniZinc - http://www.g12.cs.mu.oz.au/minizinc/ - http://www.hakank.org/minizinc/ - Version 1.4.3 - Part of the G12 project - FlatZinc, many solvers: distribution, Gecode + Gist, ECLiPSe, SICStus Prolog, JaCoP. Fzn2smt, Chuffed (not yet public), fzntini, (Choco), etc. - ease of modelling: high level, Element: m[i,j], reifications, logical operators - documentation: specification + tutorial - examples: many examples + benchmarks (some are mine), many test cases - constraints: many (decompositions and solver dependent) - community: not active. Has a Wiki, and SVN for public examples - input/output: no support except data files - search strategies: fixed (~ subset of ECLiPSe's), cannot write own - other features: + set vars, float vars (solver dependent) - typed, e.g. bool2int, int2float (Mercury) - cannot write own propagators in MiniZinc (experimental: Search Combinators) - no recursion, no “external loops”
76
JaCoP - http://jacop.osolpro.com/ - http://www.hakank.org/JaCoP/ - version 3.1 - ease of modelling: Java, Element can be tricky, "minimalistic" approach - documentation: Nice tutorial, API from source code - examples: many (6 are mine) - constraints: many - community: Wiki, not very active - input/output: Java - search strategies: yes - other features: + great class structure (find easy) + XCSP (reading and writing) + regular expressions + FlatZinc + set vars + Scala interface (scalaInJaCoP) - no float vars - minimalistic approach to constraints
77
Choco - http://www.emn.fr/x-info/choco-solver/doku.php - http://www.hakank.org/choco/ - version 2.1 - ease of modelling: Java, Element can be tricky - documentation: great site, introduction material - examples: many (but undocumented) - constraints: many, nicely documented with example code - community: active forum - input/output: Java - search strategies: yes - other features: + XCSP + FlatZinc (beta version) + SVN + set vars, (float vars) - class structure “cluttered” (hard to find constraints and methods)
78
Comet - http://dynadec.com/ - http://www.hakank.org/comet/ - version: Update: 2.1.1 - many solvers: FD, LP, MIP, Local search (slightly different syntax) - ease of modelling: High level, very OPL like, Element: m[i,j], reification, boolean operators, C++ like objects etc - documentation: great tutorial, API in distribution (also: OPL book & Comet book), - examples: Many documented examples - constraints: many - community: active forum (though declined last year) - input/output: C++ like - search strategies: easy to writing own - other features: + GUI (for local search) + debugger: text and GUI + scheduling (OPL like) + C++ and Java (not very much tested though) + set vars for FD (LS has had support for this for a long time) - license: commercial, academic or evaluation
79
Gecode - http://www.gecode.org/ - http://www.hakank.org/gecode/ - version: 3.7.2 - ease of modelling: C++, logical operators overloaded, Element sometimes tricky - documentation: great site (searchable), great introduction - examples: many (with bells & whistles, can be hard to read) - constraints: many (overridden with same name) - community: active - input/output: C++, great command line options - search strategies: many different built in - other features: + one of the fastest CP solvers + Gist (interactive search tree) + great FlatZinc support + regular expressions + Element constraints for matrices - gotcha: Matrix view of integers m(cols, rows)
80
Gecode/R - http://gecoder.rubyforge.org/ - http://www.hakank.org/gecode_r/ - version: For Gecode 2.2 - ease of modelling: Ruby, nice short cuts - documentation: well structured site, API, introduction - examples: not many (12, 3 are mine) - constraints: not many, some missing from Gecode (e.g. gcc) - community: not active - input/output: Ruby - search strategies: some of Gecode's - other features: + Ruby's ease of handling arrays + regular expressions - matrices can be tricky - reifications can be tricky - error messages can be confusing - not updated with current Gecode versions
81
ECLiPSe CLP - http://www.eclipse-clp.org/ - http://www.hakank.org/eclipse/ - version: 6.0 - ease of modelling: Prolog with extensions: loops and arrays - documentation: great, with tutorials, reference, guides etc - examples: many - constraints: quite many - community: active - input/output: Prolog - search strategies: many, extensible - other features: + Interface with C++ and Java + FlatZinc, two FD-solvers with set/float: ic and fd, one MIP solver: eplex + GUI/shell for interactive testing and debugging + SVN version (I tend to use latest dev version) - Prolog can be tricky - element constraint can be tricky sometimes
82
Tailor/Essence' - http://www.cs.st-andrews.ac.uk/~andrea/tailor/ - http://www.hakank.org/tailor/ - version: 0.3.2 (discontinued) - translates Essence' models to other solvers: Minion, Gecode, FlatZinc - ease of modelling: very high level (about as MiniZinc, Comet) - documentation: basic tutorial - examples: about 23 examples (some are mine) - constraints: not many - community: not active - input/output: just datafiles - search strategies: N/A (see below) - other features: + Tailor also supports XSCP format + ”array slice”, e.g. x[i,...] - cannot write predicates - cannot state search strategies for solvers - no float vars - no full support of FlatZinc - discontinued
83
SICStus Prolog - http://www.sics.se/isl/sicstuswww/site/index.html - http://www.hakank.org/sicstus/ - version: 4.2.1 - ease of modelling: Prolog (with do-loops) - documentation: extensive - examples: many examples - constraints: many - community: active - input/output: Prolog - search strategies: many (can declare own) - other features: + Fast CLP solver + FlatZinc + SPIDER (GUI) - Prolog can be tricky - license to use (evaluation versions exists)
84
G12 Zinc - http://g12.research.nicta.com.au/zinc_current/index_home_zinc.php - http://www.hakank.org/minizinc/zinc.html - version: 2.0 - “big brother” of MiniZinc - ease of modelling: Very high level - documentation: not very much - examples: not many examples - constraints: may use MiniZinc's constraints - community: not public active - input/output: Zinc (MiniZinc) data files - search strategies: many, cannot declare own direct - other features: + more data structures than MiniZinc: tuples, records, enum, “hash table” + more built-in functions than MiniZinc, can define functions (return value) + handle MiniZinc files directly - no “external” loop constructs - slow - no external solver
85
JaCoP/Scala - http://sourceforge.net/projects/jacop-solver/files/JaCoP-3.1.1/ - http://hakank.org/jacop/jacop_scala.html - version: 1.0(?) - Scala interface to JaCoP - ease of modelling: High Level, Scala - documentation: not much - examples: not many examples (21) - constraints: All JaCoP's constraints - community: not public active - input/output: Scala I/O - search strategies: using JaCoP's - other features: + nice use of Scala features (overloading etc) - variable overflow is a fail (from JaCoP) - reification needs extra BoolVar - element must be stated explicitly
86
Answer Set Programming - http://potassco.sourceforge.net/ - http://hakank.org/answer_set_programming/ - version: - not Constraint Programming, but interesting to compare to CP - I have tested the Potassco system most - ease of modelling: Very High Level (Prolog like), different modelling approach - documentation: much (though scattered), no modelling book - examples: many examples (though scattered) - constraints: no global constraints as we mean, “decompositions” - community: active - input/output: data files - search strategies: N/A - other features: + supports optimization + some modelling constructs can be stated easy, e.g. transitive closures + experimental merge with CP + there are many different ASP grounders/solvers - grounding can take very long - has to create decompositions for all (global) constraints
87
Google or-tools/Python - https://code.google.com/p/or-tools/ - http://www.hakank.org/or-tools/#python - version: (SVN) - this is the Python interface to the C++ engine - ease of modelling: Python with some extra sugar - documentation: not much (started in 2012) - examples: many examples - constraints: many - community: active - input/output: Python - search strategies: C++, cannot create own direct via Python - other features + easy to port from/to or-tools/Java and or-tools/C# + fast + profiling, CPViz + supports CP and LP/MIP - element can be tricky - reifications must be handled explicit (have to use “MIP tricks”) - missing some standard search strategies, e.g. max_regret
88
Google or-tools/Java - https://code.google.com/p/or-tools/ - http://www.hakank.org/or-tools/#java - version: (SVN) - this is the Java interface to C++ engine - ease of modelling: Java with some extra sugar - documentation: not much (started) - examples: many examples - constraints: many (from the underlying C++ code) - community: active - input/output: Java - search strategies: C++, cannot create own direct via Java - other features: + support CP and LP/MIP + Profiling, CPViz + impact search can be very powerful + easy to port from/to or-tools/Python and or-tools/C# - Element and reifications can be tricky - not much syntactic sugar (being Java), more verbose code
89
Google or-tools/C# - https://code.google.com/p/or-tools/ - http://www.hakank.org/or-tools/#csharp - version: (SVN) - this is the C# interface to C++ engine - ease of modelling: high level, C# with quite much extra sugar - documentation: not much (started) - examples: many examples - constraints: many (from the underlying C++ code) - community: active - input/output: C# - search strategies: C++, cannot create own direct via C# - other features: + great interface to C# + much syntactic sugar + supports CP and MIP + easy to port from/to or-tools/Python and or-tools/Java + Profiling, CPViz - element and reifications can sometimes be tricky
90
Range: 1..5 where 5 is very good, 1 is not good (or N/A). MiniZinc Comet Choco JaCoP Gecode Gecode/R - # my models 861 166 20 18 162 27 - ease of modelling 5 5 3 3 4 4 - documentation, site 3 4 4 4 4 3 - num. examples 4 4 3 4 4 3 - num. constraints 3 4 4 4 4 3 - active community 2 5 4 2 4 1 - input/output 2 3 4 4 5 4 - command line option 2 4 4 4 5 3 - reification 5 5 3 3 4 3 - propagators etc 1 4 4 4 4 2 - regular expressions 1 1 4 4 5 4 - Element 5 5 4 3 4 3 - set var 5 5 5 5 5 4 - float var 4 4 4 1 1 1 - debugging 2 5 3 3 4 3 - CVS/SVN 3 1 5 1 5 5 - FlatZinc 5 1 3 5 5 1 - XCSP 1 1 4 5 1 1 - open source 5 1 5 5 5 5 - host language - - Java Java C++ Ruby Note: These are features (and my subjected grades) for ease of learning/modeling. Comparison of the systems, subjective feature matrix
91
Range: 1..5 where 5 is very good, 1 is not good (or N/A). ECLiPSe Tailor/ JaCoP/ SICStus Zinc AnsSet Essence' Scala Prog - # my models 176 29 38 151 39 84 - ease of modelling 4 4 4 4 5 4 - documentation, site 5 3 2 5 4 4 - num. examples 4 3 3 5 3 3 - num. constraints 4 3 4 4 4 2 - active community 4 1 2 4 2 4 - input/output 4 2 4 4 2 2 - command line option 3 2 4 4 2 2 - reification 4 5 4 4 5 3 - propagators etc 3 2 3 4 1 1 - regular expressions 1 1 4 5 5 1 - Element 4 4 4 4 5 2 - set var 5 1 1 4 5 4 - float var 5 1 1 1 3 1 - debugging 4 2 3 4 2 2 - CVS/SVN 5 1 1 1 1 4 - FlatZinc 5 1 1 4 - 1 - XCSP 1 1 1 1 1 1 - open source 5 3 4 1 4 5 - host language Prolog - Scala Prolog - ASP Note: These are features (and my subjected grades) for ease of learning/modeling. Comparison of the systems, subjective feature matrix
92
Range: 1..5 where 5 is very good, 1 is not good (or N/A). or-tools Python Java C# - # my models 202 36 107 - ease of modelling 4 3 4 - documentation, site 3(forthcom.) 3(forthcom.) 3(forthcoming) - num. examples 4 3 4 - num. constraints 4 4 4 - active community 4 4 4 - input/output 4 4 4 - command line option 5 5 5 - reification 4 3 4 - propagators etc 4 4 4 - regular expressions 5 5 5 - Element 4 3 4 - set var 1 1 1 - float var 1 1 1 - debugging 4 4 4 - CVS/SVN 5 5 5 - FlatZinc 1 1 1 - XCSP 1 1 1 - open source 5 5 5 - host language Python Java C# Note: These are features (and my subjected grades) for ease of learning/modeling. Comparison of the systems, subjective feature matrix
93
Approach Source 1/N/all Lang. Set Float Strength Heur. Avail. Sols vars vars ---------------------------------------------------------- - G12/FD CP N N/All Mercury Y N 3-4 Y - G12/LazyFD Lazy N N/All Mercury Y N 4-5 Y/N - G12/CPX Lazy N 1/All C++ Y N 3-4 Y - G12/CBC MIP N 1 Mercury? N Y 2 ? - Gecode CP Y N/All C++ Y N 5 Y - JaCoP CP Y N/All Java Y N 4-5 Y - SICStus CP Y N/All Prolog N N 4 Y - ECLiPSe/ic CP Y N/All Prolog Y Y 3-4 Y - ECLiPSe/fd CP Y N/All Prolog Y N 3-4 Y - ECLiPSe/eplex MIP Y 1 Prolog Y? Y 2 ? - fzn2smt SMT N 1 C++ Y N 4 N - fzntini SAT N 1 C++(?) Y? N 3-4 N - Chuffed Lazy N N/All C++ N N 5 Y - BProlog CP Y N?/All Prolog Y? N 2-3 Y? - (Choco) CP Y N?/All Java Y Y? 2-3 Y - SCIP MIP Y 1 C++ N Y 1-2 ? MIP solvers downgraded in strength since they are restricted in what they can solve. SCIP is even more restricted. Choco is a beta version. MiniZinc solvers (that I use)
94
Comparing/benchmarks Hard to benchmark different solvers since they have different statistics measurements, though the idea is the same: complexity of the search tree * failures * choice points * wrong search decisions * backtracks Just comparing times for different CP-solvers is not reliable, unless – perhaps – over a large number of models/problem instances. Report: - time + failures (etc) - average value of a couple of runs
95
- http://www.hakank.org/minizinc/nquees4.mzn - mostly alldifferent, first_fail/indomain_median - time/statistics for the first solution - Timeout: 120 seconds (2 minutes) The times includes the overhead of creating FlatZinc file etc. time "/" failures/etc N: 8 100 500 1000 ----------------------------------------------- G12/FD 0.08s/11 0.43s/9236 TO TO G12/LazyFD 0.1s/NA 40.7s/NA TO TO G12/CBC NA NA NA NA Gecode 0.00s/3 0.02s/16 3.9s/15 51.3s/0 JaCoP 0.02s/3 0.6s/14 6.9s/17 68.47s/0 SICStus 0.003s/3 0.21s/14 11.8s/15 115.1s/0 ECLiPSe/ic 1.0s/NA 3.4s/NA TO TO ECLiPSe/fd 0.7s/NA 2.8s/NA TO TO ECLiPSe/eplex NA NA NA NA fzn2smt 0.6s 77.0s/NA TO TO fzntini 0.46s TO TO TO Chuffed 0.08s/16 1.0s/613 43.5s/3436 TO --free BProlog 0.3s/NA 0.7s/NA TO TO Choco 1.48s/1 2.8s/2 TO TO SCIP NA NA NA NA Benchmark N-queens: MiniZinc solvers
96
- code from distribution if possible (perhaps not the best) - time/statistics for the first solution - Timeout: 120 seconds (2 minutes) time "/" failures/etc N 8 100 500 1000 10000 ---------------------------------------------------- - Comet 0.2s/26 0.24s/29 NA NA NA - Choco 0.36s/ 0.59s NA NA NA - JaCoP 0.01s/3 0.46s/5 8.2s/0 80s/2 NA - Gecode 0.02s/20 0.04s/8 TO TO TO - Gecode/R 0.01s/23 0.19/22 TO TO TO - ECLiPSe CLP 0.29s/0 1.56s/307 25.6s/6039 32.6s/0 TO - Tailor/Essence' 0.39s/26 TO TO TO TO - JaCoP/Scala 0.49s/1 TO TO TO TO - SICStus 0.07s/18 TO TO TO TO - Zinc 0.038 TO TO TO TO - ASP 0.009s 0.63s 31.5s TO TO - or-tools/Python 0.05/8 0.01/12 0.2s/2 0.56s/0 78.9s/0 - or-tools/Java same as or-tools/Python - or-tools/C# same as or-tools/Python Benchmark N-queens: CP solvers
97
Benchmarking different CP solvers (esp. on a single problem instance) may be misleading: - different models - different search heuristics - different statistics - different strengths Benchmark N-queens
98
constraint all_different(queens) /\ all_different([queens[i]+i | i in 1..n]) /\ all_different([queens[i]-i | i in 1..n]) ; N-queens code (MiniZinc)
99
solver.Add(q.AllDifferent()); IntVar[] q1 = new IntVar[n]; IntVar[] q2 = new IntVar[n]; for(int i = 0; i < n; i++) { q1[i] = (q[i] + i).Var(); q2[i] = (q[i] - i).Var(); } solver.Add(q1.AllDifferent()); solver.Add(q2.AllDifferent()); N-queens code (or-tools/C#)
100
all_different_except_0: decomposition MiniZinc forall(i,j in 1..length(x) where i != j) ( (x[i] > 0 /\ x[j] > 0) -> x[i] != x[j] ) Comet int n = x.getSize(); forall(i in 1..n, j in i+1..n) { m.post(x[i] > 0 && x[j] > 0 => x[i] != x[j]); }
101
all_different_except_0: decomposition Choco for(int i = 0; i < v.length; i++) { for(int j = i+1; j < v.length; j++) { m.addConstraint(ifThenElse( and( gt(v[i], 0), gt(v[j], 0) ), neq(v[i], v[j]), TRUE ) ); }
102
all_different_except_0: decomposition JaCoP for(int i = 0; i < v.length; i++) { for(int j = i+1; j < v.length; j++) { m.impose(new IfThen( new And( new XneqC(v[i], 0), new XneqC(v[j], 0) ), new XneqY(v[i], v[j]) ) ); }
103
all_different_except_0: decomposition Gecode/R # count (1..self[0].domain.max).each{|i| self.count(i).must <= 1 } # global cardinality. xgcc is an array containing the # counts to be satisfied (self[0].domain.max+1).times{|i| xgcc[i].must == self.count(i) }
104
all_different_except_0: decomposition Gecode/R (continued) # reification n = x.length b1_is_an bool_var_matrix(n,n) b2_is_an bool_var_matrix(n,n) b3_is_an bool_var_matrix(n,n) n.times{|i| n.times{|j| if i != j then x[i].must_not.equal(0, :reify => b1[i,j]) x[i].must_not.equal(0, :reify => b2[i,j]) x[i].must_not.equal(x[j], :reify => b3[i,j]) (b1[i,j] & b2[i,j]).must.imply(b3[i,j]) else b1[i,j].must.true b2[i,j].must.true b3[i,j].must.true end }
105
all_different_except_0: decomposition Gecode for(int i = 0; i < x.size(); i++) { for(int j = i+1; j < x.size(); j++) { post(space, tt( imp( x[i] != 0 && x[j] != 0, x[i] != x[j]) ) ); }
106
all_different_except_0: decomposition ECLiPSe alldifferent_except_0(Xs) :- dim(Xs, [Len]), labeling(Xs), ( for(I, 1, Len) * for(J, 1, Len), param(Xs) do ( I \= J, Xs[I] #\= 0, Xs[J] #\= 0 ) -> Xs[I] #\= Xs[J] ; true ).
107
all_different_except_0: decomposition Tailor/Essence' forall i,j : int(1..n). ( (i != j) => (((x[i] != 0) /\ (x[j] != 0)) => (x[i] != x[j])) ),
108
Case study: Non trivial Element Word square problem: aalborg abaiser lantaca bittman osamine recanes granese Same word for i'th row and i'th column ("aalborg").
109
Word square problem, problem E = [{0..8}, {0..8}, {0..8}, {0..8}, {0..8}, {0..8}, {0..8}] words = [[aalborg] [abaiser] [bittman] [gardner] [granese] [lantaca] [osamine] [recanes] [yoghurt]] meaning: E0E1E2E3E4E5E6 E0 E1 E2 E3 E4 E5 E6 Size of length 7 words: 40230 words
110
Word square problem, solution E = [0, 1, 5, 2, 6, 7, 4] words = [[aalborg] [abaiser] [bittman] [gardner] [granese] [lantaca] [osamine] [recanes] [yoghurt]] meaning: E0E1E2E3E4E5E6 E0 a a l b o r g E1 a b a i s e r E2 l a n t a c a E3 b i t t m a n E4 o s a m i n e E5 r e c a n e s E6 g r a n e s e
111
Case study: Non trivial Element Principle: Find the crossings of the words. MiniZinc forall(I, J in 0..word_len-1) ( words[E[I], J] = words[E[J],I] ) Comet forall(i in 0..word_len-1) { forall(j in 0..word_len-1) { cp.post(words[E[i], j] == words[E[j],i]); }
112
Case study: Non trivial Element JaCoP // Note: words is a transposed word matrix for(int i = 0; i < word_length ; i++) { for(int j = 0; j < word_length ; j++) { FDV tmp = new FDV(store, "tmp" + i + " " + j, 0, dict_size); store.impose(new Element(E[i], words[j], tmp)); store.impose(new Element(E[j], words[i], tmp)); } Choco for(int I = 0; I < word_length ; I++) { for(int J = 0; J < word_length ; J++) { IntegerVariable tmp = makeIntVar("tmp" + I + " " + J, 0, dict_size); M.addConstraint(nth(E[I], C[J], words, tmp)); M.addConstraint(nth(E[J], C[I], words, tmp)); }
113
Case study: Non-trivial Element Gecode // Gecode version 3.2 now has support for Element on matrices // so it's much simpler than earlier versions. IntArgs words(num_words*word_len); // define the matrix Matrix words_m(words, word_len, num_words); //... for(int i = 0; i < word_len; i++) { for(int j = 0; j < word_len; j++) { IntVar tmp(*this, 0, num_words); element(*this, words_m, C[j], E[i], tmp, opt.icl()); element(*this, words_m, C[i], E[j], tmp, opt.icl()); }
114
Case study: Non trivial Element Gecode/R TBW.
115
Case study: Non-trivial Element ECLiPSe %... ( for(I,1,Len), param(Words,E,Len) do ( for(J,1,Len), param(Words,E,I) do % fetch the two E variables to use nth1(I,E,E1), nth1(J,E,E2), % Get the words nth1(E1,Words,Word1), nth1(E2,Words,Word2), % The same char in the overlapping. nth1(J,Word1,C), nth1(I,Word2,C) ) ).
116
Case study: Non trivial Element Tailor/Essence' TBW.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.