Download presentation

Presentation is loading. Please wait.

Published byTamia Bestwick Modified about 1 year ago

1
G53CLP Constraint Logic Programming Modeling CSPs – Case Study I Dr Rong Qu

2
Constraint Programming “... represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.” Eugene C. Freuder 1999 G53CLP – Constraint Logic ProgrammingDr R. Qu

3
Constraint Programming The computer solves it A number of CP software tools available One can use the tools effectively after some training Necessary settings We’ve seen foundations of constraint satisfaction Many software tools are based on this theory and algorithms G53CLP – Constraint Logic ProgrammingDr R. Qu

4
Constraint Programming The user states the problem Not the way we write/code the problem using languages of tools How we model the problem effectively in formulations In practice Model the problem understood by computers Code in specific languages Own codes for specific problems G53CLP – Constraint Logic ProgrammingDr R. Qu

5
Solving CSP How can we state the problem? Languages of different tools are different Model is the same G53CLP – Constraint Logic ProgrammingDr R. Qu

6
Solving CSP To improve the efficiency of CP solving Improve the algorithms Specialised domain information Higher level (complex) constraints (alldifferent) Different models for the problem More fundamental way Number of variables Number of constraints G53CLP – Constraint Logic ProgrammingDr R. Qu

7
Solving CSP problem model testing analyse coding software tools specific coding integration … G53CLP – Constraint Logic ProgrammingDr R. Qu

8
Solving CSP “... makes stating combinatorial problems easy in terms of a constraint satisfaction problem, however, it is more complicated to design solvable models.” Roman Barták 2002 G53CLP – Constraint Logic ProgrammingDr R. Qu

9
Solving CSP Primary role of constraint programmers Model the problem Translating high level constraints in the problem low level constraints supported by CP solvers G53CLP – Constraint Logic ProgrammingDr R. Qu

10
These Two Lectures Case study on AI puzzle n-Queen Some in-class exercises Interactive Questions Discussions You can see the implementation/coding of these models in ILOG Solver G53CLP – Constraint Logic ProgrammingDr R. Qu

11
The n-Queen Problem Any solution? How many solutions? G53CLP – Constraint Logic ProgrammingDr R. Qu

12
The n-Queen Problem G53CLP – Constraint Logic ProgrammingDr R. Qu

13
The n-Queen Problem Any solution? How many solutions? Some demos G53CLP – Constraint Logic ProgrammingDr R. Qu

14
The n-Queen Problem Some questions How about n is (much) larger? Is the solution you have the only one? … Number of solutions for n-queen* from wikipedia n:n:12345678910 1002 44092352724 n:n:1112131415… 2,68014,20073,712365,5962,279,184… G53CLP – Constraint Logic ProgrammingDr R. Qu

15
The n-Queen Problem Why modeling? Large computational time We have large problems We have problems of different sizes We have different problem (but can be modeled the same, see later) G53CLP – Constraint Logic ProgrammingDr R. Qu

16
The n-Queen Problem – model 1 Variables x i,j 1, if there is a queen in row i, column j; 0, otherwise Domain x i,j = {0, 1} G53CLP – Constraint Logic ProgrammingDr R. Qu

17
The n-Queen Problem – model 1 Constraints 1. One queen each row ∑ n j=1 x i,j = 1, i = 1, …, n 2. One queen each column ∑ n i=1 x i,j = 1, j = 1, …, n G53CLP – Constraint Logic ProgrammingDr R. Qu

18
The n-Queen Problem – model 1 Constraints 3. One queen diagonally Explicitly record all compatible values between each pair of variables Or Use function to represent the relationship between variables G53CLP – Constraint Logic ProgrammingDr R. Qu

19
The n-Queen Problem – model 2 Variables x 1, x 2, …, x n : position of queens on the chessboard Domain {0 … n 2 -1}: tile index of each queen placed G53CLP – Constraint Logic ProgrammingDr R. Qu

20
The n-Queen Problem – model 2 Constraint R = x i / n + 1 C = x i mod n + 1 Given R 1, R 2 and C 1, C 2 of two queens’ positions 1. One queen each row R 1 ≠ R 2 2. One queen each column C 1 ≠ C 2 G53CLP – Constraint Logic ProgrammingDr R. Qu

21
The n-Queen Problem – model 2 Constraint R = x i / n + 1 C = x i mod n + 1 Given R 1, R 2 and C 1, C 2 of two queens’ positions 3. One queen diagonally R 1 – R 2 ≠ C 1 – C 2 R 1 – R 2 ≠ C 2 – C 1 G53CLP – Constraint Logic ProgrammingDr R. Qu

22
The n-Queen Problem – model 3 Variables x 1, x 2, …, x n : rows of the chessboard Domain {1 … n}: column index of each queen placed G53CLP – Constraint Logic ProgrammingDr R. Qu

23
The n-Queen Problem – model 3 Constraint 1. One queen diagonally x i - x j ≠ i - j & x i - x j ≠ j - i 2. One queen each column for all i, j = {1, …, n}, x i ≠ x j 3. One queen each row ? Row constraint is in the formulation G53CLP – Constraint Logic ProgrammingDr R. Qu

24
The n-Queen Problem – models The search space contains all possible assignments For the above three models model 3: {x 1, …, x n }, {1, …, n} model 2: {x 1, …, x n }, (0, …, (n 2 – 1)} model 1: {x i,j }, {0, 1} modelvariablesdomainsearch spacen=4n = 8n = 10n = 20 3nnn! (or n n )244 E43.6 E62.4 E18 2nn2n2 (n 2 ) n 6.6 E42.8 E141 E201.1 E52 1n2n2 22 (n 2 ) 6.6 E41.84 E191.27 E302.6 E120 G53CLP – Constraint Logic ProgrammingDr R. Qu

25
The n-Queen Problem – models Fewer number of variables with small domains of variables are preferable Smaller search space Problems solved more quickly Fewer number of variables with large domains of variables are preferable modelvariablesdomainsearch spacen=4n = 8n = 10n = 20 3nnn!244 E43.6 E62.4 E18 2nn2n2 (n 2 ) n 6.6 E42.8 E141 E201.1 E52 1n2n2 22 (n 2 ) 6.6 E41.84 E191.27 E302.6 E120 G53CLP – Constraint Logic ProgrammingDr R. Qu

26
Solving 8-Queen Puzzle – Demo

27
27 ILOG OPL Studio CPLEX Optimization software package Sold via CPLEX Optimization Inc. Acquired by ILOG Inc in 1997 Acquired by IBM in 2009 Also solves integer programming and large linear programming problems http://www.ilog.com/products/cplex/ G53CLP – Constraint Logic ProgrammingDr R. Qu

28
28 ILOG OPL Studio OPL Studio One of the modeling systems in ILOG For both mathematic programming and constraint programming OPL (Optimization Programming Language) was originally developed by Pascal van Hentenryck Provide an interpreter OPL models; A script language An IDE; An API G53CLP – Constraint Logic ProgrammingDr R. Qu

29
29 Other CP Solvers? Gecode 1.0.1 Java interface for constraint programming Free for download Released in Nov 2006 http://www.gecode.org/gecodej/ G53CLP – Constraint Logic ProgrammingDr R. Qu

30
30 Solving the Map Colouring Problem ILOG Solver G53CLP – Constraint Logic ProgrammingDr R. Qu

31
G52AIP – AI Programming 31

32
32 Solving the 8-Queen Problem G53CLP – Constraint Logic ProgrammingDr R. Qu

33
33 Solving The 8-Queen Problem – model 2 Variables x 1, x 2, …, x n : position of queens on the chessboard Domain {0 … n 2 -1}: tile index of each queen placed Constraint R = x i / n + 1 C = x i mod n + 1 Given R 1, R 2 and C 1, C 2 of two queens’ positions One queen each row/column R 1 ≠ R 2; C 1 ≠ C 2 One queen each diagnal R 1 – R 2 ≠ C 1 – C 2 R 1 – R 2 ≠ C 2 – C 1 G53CLP – Constraint Logic ProgrammingDr R. Qu

34
34 ILOG OPL Studio G53CLP – Constraint Logic ProgrammingDr R. Qu

35
35 Solving The 8-Queen Problem – model 2 //.mod file //declaration part var int queens[1..8] in 0..63; var int r[1..8] in 1..8; var int c[1..8] in 1..8; // problem model solve { … }; R = xi / n + 1 C = xi mod n + 1 R1 ≠ R2; C1 ≠ C2 R1 – R2 ≠ C1 – C2 R1 – R2 ≠ C2 – C1 x1, x2, …, xn: position of queens on the chessboard {0 … n 2 -1}: tile index of each queen placed G53CLP – Constraint Logic ProgrammingDr R. Qu

36
36 Solving The 8-Queen Problem – model 2 //.mod file //declaration part … //problem model solve { forall(ordered i,j in 1..8) { r[i] = queens[i] / 8 + 1; c[i] = queens[i] mod 8 + 1; r[j] = queens[j] / 8 + 1; c[j] = queens[j] mod 8 + 1; … }; R = xi / n + 1 C = xi mod n + 1 R1 ≠ R2; C1 ≠ C2 R1 – R2 ≠ C1 – C2 R1 – R2 ≠ C2 – C1 G53CLP – Constraint Logic ProgrammingDr R. Qu

37
37 Solving The 8-Queen Problem – model 2 //.mod file //declaration part … //problem model solve { forall(ordered i,j in 1..8) { … r[i] <> r[j] ; c[i] <> c[j] ; r[i] - r[j] <> c[i] - c[j]; r[i] - r[j] <> c[j] - c[i]; }; R = xi / n + 1 C = xi mod n + 1 R1 ≠ R2; C1 ≠ C2 R1 – R2 ≠ C1 – C2 R1 – R2 ≠ C2 – C1 G53CLP – Constraint Logic ProgrammingDr R. Qu

38
G52AIP – AI Programming 38

39
G52AIP – AI Programming 39

40
40 Solving The 8-Queen Problem – model 2 G53CLP – Constraint Logic ProgrammingDr R. Qu

41
41 Solving The 8-Queen Problem To display decision tree Debug/Display Decision Tree To run Execution/Run, or To stop at a decision point Debug/Stop at Decision Point Next solution All solutions G53CLP – Constraint Logic ProgrammingDr R. Qu

42
42 Solving The 8-Queen Problem – 3 models Lab sessions start Tuesday next week In IBM ILOG OPL IDE I: getting familiar with IBM ILOG by studying the graph coloring example II: comparing the 3 models for solving the n- Queen problem G53CLP – Constraint Logic ProgrammingDr R. Qu

43
43 Brief Intro to IBM ILOG OPL IDE G53CLP – Constraint Logic ProgrammingDr R. Qu

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google