g53clp constraint logic programming - nottinghampszrq/files/8nqueenmodel.pdf · constraint logic...

Post on 07-Aug-2020

20 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

G53CLPConstraint Logic Programming

Modeling CSPs – Case Study I

Dr Rong Qu

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

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

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

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

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

7 of 26

Solving CSP

problem model testinganalyse coding

softwaretools

specificcoding

integration

G53CLP – Constraint Logic Programming Dr R. Qu

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

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

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

11 of 26

The n-Queen Problem

Any solution?

How many solutions?

G53CLP – Constraint Logic Programming Dr R. Qu

12 of 26

The n-Queen Problem

G53CLP – Constraint Logic Programming Dr R. Qu

13 of 26

The n-Queen Problem

Any solution?

How many solutions?

Some demos

G53CLP – Constraint Logic Programming Dr R. Qu

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

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

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

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

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

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

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

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

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

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

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

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

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

top related