1 / 43

G53CLP Constraint Logic Programming

G53CLP Constraint Logic Programming. Modeling CSPs – Case Study I. Dr Rong Qu. “... 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. Constraint Programming.

knop
Download Presentation

G53CLP Constraint Logic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. G53CLPConstraint Logic Programming Modeling CSPs – Case Study I Dr Rong Qu

  2. “... 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 Constraint Programming G53CLP – Constraint Logic Programming Dr R. Qu

  3. 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 Constraint Programming G53CLP – Constraint Logic Programming Dr R. Qu

  4. 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 Constraint Programming G53CLP – Constraint Logic Programming Dr R. Qu

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

  6. 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 Solving CSP G53CLP – Constraint Logic Programming Dr R. Qu

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

  8. “... 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 Solving CSP G53CLP – Constraint Logic Programming Dr R. Qu

  9. Primary role of constraint programmers Model the problem Translatinghigh level constraints in the problemlow level constraints supported by CP solvers Solving CSP G53CLP – Constraint Logic Programming Dr R. Qu

  10. 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 These Two Lectures G53CLP – Constraint Logic Programming Dr R. Qu

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

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

  13. The n-Queen Problem Any solution? How many solutions? Some demos G53CLP – Constraint Logic Programming Dr 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 G53CLP – Constraint Logic Programming Dr 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 Programming Dr R. Qu

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

  17. Constraints One queen each row ∑nj=1xi,j = 1, i = 1, …, n One queen each column ∑ni=1xi,j = 1, j = 1, …, n The n-Queen Problem – model 1 G53CLP – Constraint Logic Programming Dr R. Qu

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

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

  20. Constraint R = xi / n + 1 C = xi mod n + 1 Given R1, R2and C1, C2 of two queens’ positions One queen each row R1 ≠ R2 One queen each column C1 ≠ C2 The n-Queen Problem – model 2 G53CLP – Constraint Logic Programming Dr R. Qu

  21. Constraint R = xi / n + 1 C = xi mod n + 1 Given R1, R2and C1, C2 of two queens’ positions One queen diagonally R1– R2 ≠ C1 – C2 R1– R2 ≠ C2 – C1 The n-Queen Problem – model 2 G53CLP – Constraint Logic Programming Dr R. Qu

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

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

  24. The n-Queen Problem – models • The search space • contains all possible assignments • For the above three models • model 3: {x1, …, xn}, {1, …, n} • model 2: {x1, …, xn}, (0, …, (n2 – 1)} • model 1: {xi,j}, {0, 1} G53CLP – Constraint Logic Programming Dr 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 G53CLP – Constraint Logic Programming Dr R. Qu

  26. Solving 8-Queen Puzzle – Demo G53CLP – Constraint Logic Programming Dr R. Qu

  27. 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 ILOG OPL Studio http://www.ilog.com/products/cplex/ G53CLP – Constraint Logic Programming Dr R. Qu

  28. 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 ILOG OPL Studio G53CLP – Constraint Logic Programming Dr R. Qu

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

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

  31. G52AIP – AI Programming

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

  33. Solving The 8-Queen Problem – model 2 • Variables • x1, x2, …, xn: position of queens on the chessboard • Domain • {0 … n2-1}: tile index of each queen placed • Constraint R = xi / n + 1 C = xi mod n + 1 Given R1, R2and C1, C2 of two queens’ positions • One queen each row/column • R1 ≠ R2; C1 ≠ C2 • One queen each diagnal • R1– R2 ≠ C1 – C2 • R1– R2 ≠ C2 – C1 G53CLP – Constraint Logic Programming Dr R. Qu

  34. ILOG OPL Studio G53CLP – Constraint Logic Programming Dr R. Qu

  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 { … }; • x1, x2, …, xn: position of queens on the chessboard • {0 … n2-1}: tile index of each queen placed • R = xi / n + 1 • C = xi mod n + 1 • R1 ≠ R2; C1 ≠ C2 • R1 – R2 ≠ C1 – C2 • R1 – R2 ≠ C2 – C1 G53CLP – Constraint Logic Programming Dr R. Qu

  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 Programming Dr R. Qu

  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 Programming Dr R. Qu

  38. G52AIP – AI Programming

  39. G52AIP – AI Programming

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

  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 Programming Dr R. Qu

  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 Programming Dr R. Qu

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

More Related