Constraint Processing and Programming Introductory Exemple Javier Larrosa.

40
Constraint Processing and Programming Introductory Exemple Javier Larrosa

Transcript of Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Page 1: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint Processing and ProgrammingIntroductory Exemple

Javier Larrosa

Page 2: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Combinatorial Problem Solving

• Science and Engineering are full of Combinatorial Problems

Solving algorithms are exponentially expensive A lot of tricks can be applied to improve

performance

• Naive Approach

For each problem, design and implement an efficient algorithm

Page 3: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Place numbers 1 through 8 on nodes

• each number appears exactly once

• no connected nodes have consecutive numbers

?

?

?

?

?

?

??

Note the graph symmetry

Acknowledgement: Patrick Prosser

Page 4: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Heuristic Search

Which nodes are hardest to number?

?

?

?

?

?

?

??

Guess a value, but be prepared to backtrack

Page 5: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Heuristic Search

?

?

?

?

?

?

??

Which nodes are hardest to number?

Page 6: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Heuristic Search

Which are the least constraining values to use?

?

?

?

?

?

?

??

Values 1 & 8

Page 7: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Heuristic Search

Symmetry means we don’t need to consider: 8 1

?

1

?

?

8

?

??

Page 8: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

We can now eliminate many values for other nodes

?

1

?

?

8

?

??

{1,2,3,4,5,6,7,8}

Page 9: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

By symmetry

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

Page 10: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{1,2,3,4,5,6,7,8}

Page 11: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

By symmetry

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Page 12: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

?

1

?

?

8

?

??

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6,7} {2,3,4,5,6}

Page 13: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

?

1

?

?

8

?

27

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

Page 14: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

?

1

?

?

8

?

27

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

{3,4,5,6}

And propagate

Page 15: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

?

1

?

?

8

?

27

{3,4,5}

{3,4,5}

{4,5,6}

{4,5,6}

Page 16: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

3

1

?

?

8

?

27

{3,4,5}

{4,5,6}

{4,5,6}

By symmetry

Page 17: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

3

1

?

?

8

?

27

{3,4,5}

{4,5,6}

{4,5,6}

And propagate

Page 18: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

3

1

?

?

8

?

27

{4,5}

{5,6}

{4,5,6}

More propagation?

Page 19: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Inference/propagation

3

1

4

5

8

6

27

A solution

Page 20: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

The Declarative Approach to Combinatorial Problem Solving

• Declarative Approach

1. Model the problem using a well-defined language 2. Solve the problem using general-purpose

techniques

Constraint Programming and SAT solving follow this approach.

Page 21: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint programming methodology

• Model problem

• Solve model

• Verify and analyze solution

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose algorithm• define/choose heuristics

Page 22: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint programming methodology

• Model problem

• Solve model

• Verify and analyze solution

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose algorithm• define/choose heuristics

ConstraintSatisfaction

Problem

Page 23: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint satisfaction problem

• A CSP is defined by

• a set of variables

• a domain of values for each variable

• a set of constraints between variables

• A solution is

• an assignment of a value to each variable that satisfies the constraints

Page 24: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Given a CSP

• Determine whether it has a solution or not

• Find any solution

• Find all solutions

• Find an optimal solution, given some cost function

Page 25: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint model for puzzle

variables

v1, …, v8

domains

{1, …, 8}

constraints

| v1 – v2 | 1

| v1 – v3 | 1

| v7 – v8 | 1

alldifferent(v1, …, v8)

?

?

?

?

?

?

??

Page 26: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: instruction scheduling

(a + b) + cGiven a basic-block of code and a single-issue pipelined processor, find the minimum schedule

Page 27: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: evaluate (a + b) + c

instructions

A r1 a

B r2 b

C r3 c

D r1 r1 + r2

E r1 r1 + r3

3 3

31

A B

D C

E

dependency DAG

Page 28: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: evaluate (a + b) + c

non-optimal schedule

A r1 a

B r2 b

nop

nop

D r1 r1 + r2

C r3 c

nop

nop

E r1 r1 + r3

3 3

31

A B

D C

E

dependency DAG

Page 29: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: evaluate (a + b) + c

optimal schedule

A r1 a

B r2 b

C r3 c

nop

D r1 r1 + r2

E r1 r1 + r3

3 3

31

A B

D C

E

dependency DAG

Page 30: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint model

variables

A, B, C, D, E

domains

{1, …, m}

constraints

D A + 3

D B + 3

E C + 3

E D + 1

alldifferent(A, B, C, D, E)

3 3

31

A B

D C

E

dependency DAG

Page 31: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: Graph coloring

Given k colors, does there exist a coloring of the nodes such that adjacent nodes are assigned different colors

Page 32: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: 3-coloring

variables: v1, v2 , v3 , v4 , v5

domains: {1, 2, 3}

constraints: vi vj if vi and vj

are adjacent

v2

v3

v1

v5

v4

Page 33: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: 3-coloring

A solution

v1 1

v2 2

v3 2

v4 1

v5 3

v2

v3

v1

v5

v4

Page 34: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: n-queens

Place n-queens on an n n board so that no pair of queens attacks each other

Page 35: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint model

4

3

2

1

x1 x2 x3 x4variables: x1, x2 , x3 , x4

domains: {1, 2, 3, 4}

constraints: xi xj and

| xi - xj | | i - j |

Page 36: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Example: 4-queens

A solution

x1 2

x2 4

x3 1

x4 3

Q

Q

Q

Q

x1 x2 x3 x4

4

3

2

1

Page 37: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint programming methodology

• Model problem

• Solve model

• Verify and analyze solution

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose algorithm• define/choose heuristics

Page 38: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Constraint programming methodology

• Model problem

• Solve model

• Verify and analyze solution

• specify in terms of constraints on acceptable solutions• define/choose constraint model:

variables, domains, constraints

• define/choose algorithm• define/choose heuristics

Page 39: Constraint Processing and Programming Introductory Exemple Javier Larrosa.

Application areas

• Paradigm of choice for many hard combinatorial problems

• scheduling

• planning

• vehicle routing

• configuration

• bioinformatics

• …