G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic...

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

Transcript of G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic...

Page 1: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

G53CLPConstraint Logic Programming

Modeling CSPs – Case Study I

Dr Rong Qu

Page 2: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

2 of 26

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

Page 3: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

3 of 26

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

Page 4: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

4 of 26

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

Page 5: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

5 of 26

Solving CSP

How can we state the problem?

Languages of different tools are different

Model is the same

G53CLP – Constraint Logic Programming Dr R. Qu

Page 6: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

6 of 26

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

Add redundant constraints

More fundamental way

Number of variables

Number of constraints

G53CLP – Constraint Logic Programming Dr R. Qu

Page 7: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

7 of 26

Solving CSP

problem model testinganalyse coding

softwaretools

specificcoding

integration

G53CLP – Constraint Logic Programming Dr R. Qu

Page 8: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

8 of 26

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

Page 9: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

9 of 26

Solving CSP

Primary role of constraint programmers

Model the problem

Translatinghigh level constraints in the problem

low level constraints supported by CP solvers

G53CLP – Constraint Logic Programming Dr R. Qu

Page 10: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

10 of 26

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

G53CLP – Constraint Logic Programming Dr R. Qu

Page 11: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

11 of 26

The n-Queen Problem

Any solution?

How many solutions?

G53CLP – Constraint Logic Programming Dr R. Qu

Page 12: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

12 of 26

The n-Queen Problem

G53CLP – Constraint Logic Programming Dr R. Qu

Page 13: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

13 of 26

The n-Queen Problem

Any solution?

How many solutions?

Some demos

G53CLP – Constraint Logic Programming Dr R. Qu

Page 14: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

14 of 26

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: 1 2 3 4 5 6 7 8 9 10

1 0 0 2 10 4 40 92 352 724

n: 11 12 13 14 15 …

2,680 14,200 73,712 365,596 2,279,184 …

G53CLP – Constraint Logic Programming Dr R. Qu

Page 15: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

15 of 26

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

Page 16: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

16 of 26

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

Page 17: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

17 of 26

The n-Queen Problem – model 1

Constraints

1. One queen each row

∑nj=1 xi,j = 1, i = 1, …, n

2. One queen each column

∑ni=1 xi,j = 1, j = 1, …, n

G53CLP – Constraint Logic Programming Dr R. Qu

Page 18: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

18 of 26

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

Page 19: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

19 of 26

The n-Queen Problem – model 2

Variables

x1, x2, …, xn: position of queens on the chessboard

Domain

{0 … n2-1}: tile index of each queen placed

G53CLP – Constraint Logic Programming Dr R. Qu

Page 20: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

20 of 26

The n-Queen Problem – model 2

Constraint

R = xi / n + 1

C = xi mod n + 1

Given R1, R2 and C1, C2 of two queens’ positions

1. One queen each row

R1 ≠ R2

2. One queen each column

C1 ≠ C2

G53CLP – Constraint Logic Programming Dr R. Qu

Page 21: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

21 of 26

The n-Queen Problem – model 2

Constraint

R = xi / n + 1

C = xi mod n + 1

Given R1, R2 and C1, C2 of two queens’ positions

3. One queen diagonally

R1 – R2 ≠ C1 – C2

R1 – R2 ≠ C2 – C1

G53CLP – Constraint Logic Programming Dr R. Qu

Page 22: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

22 of 26

The n-Queen Problem – model 3

Variables

x1, x2, …, xn: rows of the chessboard

Domain

{1 … n}: column index of each queen placed

G53CLP – Constraint Logic Programming Dr R. Qu

Page 23: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

23 of 26

The n-Queen Problem – model 3

Constraint

1. One queen each column

for all i, j = {1, …, n}, xi ≠ xj

2. One queen diagonally

xi - xj ≠ i - j & xi - xj ≠ j - i

3. One queen each row

?

Row constraint is in the formulation

G53CLP – Constraint Logic Programming Dr R. Qu

Page 24: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

24 of 26

The n-Queen Problem – models

The search space

contains all possible assignments

For the above three models

module 3: {x1, …, xn}, {1, …, n}

module 2: {x1, …, xn}, (0, …, (n2 – 1)}

module 1: {xi,j}, {0, 1}

model variables domain search space n=4 n = 8 n = 10 n = 20

3 n n n! (or nn) 24 4 E4 3.6 E6 2.4 E18

2 n n2 (n2)n

6.6 E4 2.8 E14 1 E20 1.1 E52

1 n2 2 2(n2) 6.6 E4 1.84 E19 1.27 E30 2.6 E120

G53CLP – Constraint Logic Programming Dr R. Qu

Page 25: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

25 of 26

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

model variables domain search space n=4 n = 8 n = 10 n = 20

3 n n n! 24 4 E4 3.6 E6 2.4 E18

2 n n2 (n2)n

6.6 E4 2.8 E14 1 E20 1.1 E52

1 n2 2 2(n2) 6.6 E4 1.84 E19 1.27 E30 2.6 E120

G53CLP – Constraint Logic Programming Dr R. Qu

Page 26: G53CLP Constraint Logic Programming - Nottinghampszrq/files/8nQueenModel.pdf · Constraint Logic Programming Modeling CSPs ... The n-Queen Problem Why modeling? Large computational

26 of 26

Summary

Modeling in constraint programming

Case study on 8-queen problem

Comparison on 3 models

Model in constraint programming

Few number of variables

Small domain

G53CLP – Constraint Logic Programming Dr R. Qu