1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic...

57
ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    0

Transcript of 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic...

Page 1: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 1

Constraint Satisfaction Problems

Basic Algorithms

My Thanks to Roman Bartak

(for “stealing” some of his slides)

Page 2: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 2

Search Algorithms for CSPs

Simple or Chronological Backtracking (BT) Backjumping (BJ) and Conflict-Based Backjumping Forward Checking (FC) Maintaining Arc Consistency (MAC)

We will study variations of DFS especially for CSPs. These algorithms are based on backtracking search

Also two variations of hill climbing Min-conflicts Min-conflicts with Random Walk

Page 3: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 3

Intelligent Backtracking

ΒΤ suffers from thrashing it visits again and again the same regions of the search tree because it

has a very local view of the problem

One way to get rid of the problem is using intelligent backtracking algorithms

BJ, CBJ, DB, Graph-based BJ, Learning Backjumping (BJ) is different from ΒΤ in the following:

When BJ reaches a dead-end it does not backtrack to the immediately preceding variables. It backtracks to the deepest variable in the search tree which is in conflict with the current variable

Page 4: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 4

BJ vs. BT

We want to color each area in the map with a different color

We have three colorsred, green, blue

Page 5: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 5

BJ vs. BT

Let’s consider what ΒΤ does in the map coloring problem Assume that variables are assigned in the order Q, NSW, V, T, SA, WA,

NT Assume that we have reached the partial assignment

Q = red, NSW = green, V = blue, T = red

When we try to give a value to the next variable SA, we find out that all possible values violate constraints

Dead end! BT will backtrack to try a new value for variable Τ!

Not a good idea!

Page 6: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 6

BJ vs. BT

BJ has a smarter approach to backtracking It tells us to go back to one of the variables which are responsible for the

dead-end The set of these variables is called a conflict set

The conflict set for SA is {Q, NSW, V} BJ backjumps to the deepest variable in the conflict set of the variable

where the dead-end occurred deepest = the one we visited most recently

CBJ, DB, Graph-based BJ, Learning, Backmarking

Page 7: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 7

Conflict-based Backjumping (CBJ)

Conflict-based Backjumping is a look-back algorithm that performs intelligent backtracking at dead-ends

In contrast to BJ which backjumps only from leaf dead-ends, CBJ can also backjump from dead-ends at inner nodes

for each variable x we have a conflict set when an assignment (x,a) fails because of a constraint violation with a

previous variable y, y is added to the conflict set of x if there are no values left in the domain of the current variable x, CBJ

backjumps to the deepest variable w in the conflict set of x (as BJ) and the conflict set of x is added to the conflict set of w then a further backjump can occur from w

Page 8: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 8

Forward Checking

Forward Checking (FC) belongs to the family of backtracking algorithms called lookahead algorithms

The basic idea of lookahead is that when you assign a value to a variable the problem is reduced through constraint propagation

constraint propagation is defined in a different way for each look-ahead algorithm

FC does the following: When a variable x takes a value v, for each future variabe y which

appears in a constraint with x we remove from Dx all the values that are not consistent with v

Page 9: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 9

Forward Checking

If the domain of some variable becomes empty then value v is rejected and we try the next value of x

The operation of FC means that the following holds for each step of the search:

All values of eachAll values of each future variablefuture variable are compatible with all the values that are compatible with all the values that have been assigned to past variableshave been assigned to past variables

FC maintains a restricted form of arc consistency

Page 10: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 10

Forward Checking

procedure FORWARD_CHECKING (vars,doms,cons)solution FC (vars,Ø,doms,cons)

function FC (unlabelled,compound_label,doms,cons) returns a solution or NIL

if unlabelled = Ø then return compound_label else pick a variable x from unlabelled repeat pick a value v from Dx; delete v from Dx

doms’ UPDATE(unlabelled-{x},doms,cons,compound_label + {(x,v)}) if no domain in doms’ is empty then result FC(unlabelled - {x}, compound_label +

{(x,v)}, doms’,cons) if result NIL then return result

end until Dx = Ø return NILend

Page 11: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 11

Forward Checking

function UPDATE (unlab_vars,doms,cons,compound_label) returns an updated set of domains

for each variable y in unlab_vars do for each value v in Dy’ do if (y,v) is incompatible with compound_label with respect

to the constraints between y and the variables of compound_label

then Dy’ Dy’ – {v} endend return doms’

Page 12: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 12

FC in operation

R G B R G B R G B R G B R G B R G B

R G B

R G B R G B R G B R G B G B R G B

R B G R B R G B B R G B

R B G R B R G B

WA NT Q NSW V TSA

initial domains

after WA=R

after Q=G

after V=B

Page 13: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 13

Consistency Techniques

removing inconsistent values from variables’ domains graph representation of the CSP

binary and unary constraints only (relatively easy) nodes = variables edges = constraints

node consistency (NC) arc consistency (AC) path consistency (PC) (strong) k-consistency

A

B

C

A>5

AB

A<C

B=C

Page 14: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 14

Node Consistency

A variable X is node consistent iff each value a of X satisfies all the unary constraints on X

Node consistency can be applied as a preprocessing step before starting search to remove all the node inconsistent values

A

B

C

A>5

AB

A<C

B=C

If D(A)={0,…,9} node consistency

will remove values 0,…,5

Page 15: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 15

Arc Consistency

Definition: A variable X is arc consistent iff for each other variable Y the

following holds: For each value a of Χ there is at least one value b of Υ such that a and b are compatible

Then we say that a supports b

An algorithm that applies arc consistency deletes values from the domain of a variable when they are not supported by any value in the domain of another variable

Page 16: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 16

Arc Consistency (AC)

the most widely used consistency technique (good simplification/performance ratio)

deals with individual binary constraints

repeated revisions of arcs Directional (one pass) AC

a

b

c

a

b

c

X Y

a

b

c

Z

Page 17: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 17

AC - Example

Problem:X::{1,2}, Y::{1,2}, Z::{1,2} X = Y, X Z, Y > Z

1 2

1 2

1 2

1 2

1 2

1 2

X

Y

Z

X

Y

Z

Page 18: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 18

Arc Consistency propagation: Crossword Puzzle example

1 2 3

4

5

X1 X2 X4

astar

happy

hello

hoses

liveloadpealpeelsavetalk

liveloadpealpeelsavetalk

….No more changes!

Page 19: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 19

Arc Consistency

We apply arc consistency: As a (preprocessing) step before we start search

in that way we can reduce the size of the search tree and in some cases discover inconsistent problems

While searching after an assignment of a value to a variable constraint propagation fast discovery of dead ends

The search algorithm which applies arc consistency is called MAC (maintaining arc consistency)

Page 20: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 20

MAC

procedure Maintaining Arc Consistency (vars,doms,cons)solution MAC (vars,Ø,doms,cons)

function MAC (unlabelled,compound_label,doms,cons) returns a solution or NIL

if unlabelled = Ø then return compound_label else pick a variable x from unlabelled repeat pick a value v from Dx; delete v from Dx

doms’ AC(unlabelled-{x},doms,cons,compound_label + {(x,v)}) if no domain in doms’ is empty then result MAC(unlabelled - {x}, compound_label +

{(x,v)}, doms’,cons) if result NIL then return result

end until Dx = Ø return NILend

Page 21: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 21

Algorithms for Arc Consistency

Arc consistency can be enforced with Ο(ed2) optimal worst case time complexity

AC-4, AC-6, AC-7, AC-2001 AC-3: non-optimal, but simple AC algorithm

AC-3 and AC-2001 use: a queue (or stack) where the variables that are checked for arc

consistency are inserted a routine Revise which deletes values that are not supported

AC-4, AC-6, AC-7 use more complex data structures support lists

Page 22: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 22

Achieving Arc Consistency

From Mackworth (1977a):

procedure AC-3(G)

Let Q be the set of (directed) arcs of G (not self-cyclic)

while Q not empty do

select and remove any arc (x,y) from Q;

REVISE(x,y)

if REVISE(x,y) changed the domain of x then

add to Q the set of all arcs of G (z,x) that go into x;

procedure REVISE (x,y)

for each value a in domain of x do

if there is no value b in the domain of y such that (a,b) is consistent

then delete a from the domain of x

Page 23: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 23

Achieving Arc Consistency

AC-2001/3.1 achieves the optimal Ο(ed2) complexity by using a set of pointers Lastx,a,y For each value a of a variable, Lastx,a,y points to the most recently

discovered value in the domain of y that supports a

Runtime of AC-3: O(ed3) for graph e binary constraints, and maximum domain size of d

For one constraint, function Revise costs O(d2) and it can be called d times

there are e constraints, so the complexity is O(ed3)

procedure REVISE-2001/3.1 (x,y)

for each value a in domain of x do

if there is no value b in the domain of y such that b> Lastx,a,y and (a,b) is consistent

then delete a from the domain of x

else Lastx,a,y = first such value

Page 24: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 24

Algorithms for Arc Consistency

In some cases we can exploit the semantics of certain binary constraints to achieve an even better complexity

functional, anti-functional, monotonic, piecewise functional, etc. algorithm AC-5

What the complexity of AC processing for a constraint of the following types?

x = y x ≠ y x < y x > y

Page 25: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 25

Directional Arc Consistency (DAC)

Observation: AC has to repeat arc revisions; the total number of revisions depends on the number of arcs but also on the size of domains (while cycle)

Is it possible to weaken AC in such a way that every arc is revised just once?

Definition: A CSP is directional arc consistent using a given order of variables iff every arc (i,j) such that i<j is arc consistent

Again, every arc has to be revised, but revision in one direction is enough now

Page 26: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 26

Arc Consistency as a Solution Method

Question: Are there cases where we can guarantee that solubility (or insolubility)

will be determined by applying arc consistency?

Answer (Freuder 1982): When the constraint graph of the problem is a tree In this case, a solution can be found (if one exists) in a backtrack-free

manner by first applying directional arc consistency A case of polynomially solvable CSPs

Many other such cases exist depending on the structure of the constraint graph and the nature of the constraints

Page 27: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 27

Is AC enough?

empty domain => no solution cardinality of all domains is 1 => solution Problem:

X::{1,2}, Y::{1,2}, Z::{1,2} X Y, X Z, Y Z 1 2

1 2

1 2

X

YZ

Page 28: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 28

Stronger Levels of Consistency

Beyond arc consistency there are numerous other levels of consistency path consistency singleton arc consistency neighborhood inverse consistency …

These are stronger than arc consistency (i.e. they delete more inconsistent values when they are applied)

But they are more expensive (higher time complexity)

We will review some of them in the next lecture

Page 29: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 29

Constraint Propagation

systematic search only => no efficient consistency only => no complete combination of search (backtracking) with consistency techniques methods:

look back (restoring from conflicts) look ahead (preventing conflicts)

look back

Labelling order

look ahead

Page 30: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 30

Look Back Methods

intelligent backtracking consistency checks among instantiated variables

backjumping backtracks to the conflicting variable

backchecking and backmarking avoids redundant constraint checking

by remembering conflicting levelfor each value

jump here

a

b b b

conflict

still conflict

Page 31: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 31

Look Ahead Methods

preventing future conflicts via consistency checks among not yet instantiated variables

forward checking (FC) AC to direct neighbourhood

partial look ahead (PLA) DAC

(full) look ahead (LA) Arc Consistency Path Consistency

labellin

g o

rder

instantiated variable

Page 32: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 32

Look Ahead - Example

Problem:X::{1,2}, Y::{1,2}, Z::{1,2}

X = Y, X Z, Y > Z

generate & test - 7 stepsbacktracking - 5 stepspropagation - 2 steps

X Y Z action result1 labelling

{1} {} propagation fail2 labelling

{2} {1} propagation solution

Page 33: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 33

4-queen problem

Place 4 queens so that no two queens are in attack.

1234

Q1 Q2 Q3 Q4

Qi: line number of queen in column i, for 1i4

Q1, Q2, Q3, Q4 Q1Q2, Q1Q3, Q1Q4,Q2Q3, Q2Q4,Q3Q4,Q1Q2-1, Q1Q2+1, Q1Q3-2, Q1Q3+2,Q1Q4-3, Q1Q4+3,Q2Q3-1, Q2Q3+1, Q2Q4-2, Q2Q4+2,Q3Q4-1, Q3Q4+1

Page 34: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 34

4-queen problem first solution

1234

Q1 Q2 Q3 Q4

There is a total of 256 valuations

GT algorithm will generate

64 valuations with Q1=1;

+ 48 valuations with Q1=2, 1Q23;

+ 3 valuations with Q1=2, Q2=4, Q3=1;

= 115 valuations to find first solution

Page 35: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 35

4-queen problem, BT algorithm

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

Page 36: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 36

4-queen problem, FC algorithm

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4

Page 37: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 37

4-queen problem, MAC algorithm

Value 3 of Q2 is unsupported in Q3, Value 4 of Q3 is unsupported in Q2,Value 2 of Q3 is unsupported in Q4,

1234

Q1 Q2 Q3 Q4

1234

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

1234

x

1234

Q1 Q2 Q3 Q4

x

x

Page 38: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 38

Hybrid Algorithms

We can combine the operations of various backtracking algorithms to design hybrid algorithms

For example we can combine the lookahead function of forward checking and the lookback function of BJ

FC-BJ FC-CBJ MAC-BJ MAC-CBJ …

Page 39: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 39

FC-CBJ

Forward Checking with Conflict-based Backjumping FC-CBJ combines the look-ahead of FC and the intelligent backjumping

of CBJ each variable is associated with a conflict set when the forward checking of an assignment (x,a) results in a value

deletion from the domain of a variable y, x is added to the conflict set of y if after the forward checking of an assignment (x,a) the domain of a

variable y is wiped out, the variables in the conflict set of y are added to the conflict set of x

why is this done? if there are no more values left in the domain of the current variable x, FC-

CBJ backjumps to the deepest variable w in the conflict set of x the conflict set of x is added to the conflict set of w

Page 40: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 40

Evaluation of Backtracking Algorithms

How can we compare backtracking algorithms for CSPs ? Time / Space Complexity

not very useful. They all have exponential time complexity!

cpu times

number of nodes they visit in the search tree

amount of consistency checks they perform

amount of backtracks they perform

Page 41: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 41

Evaluation of Backtracking Algorithms

Some theoretical results: Search tree nodes visited

FC-CBJ FC-BJ FC BJ BT

CBJ BJ

Number of consistency checks

CBJ BJ ΒΤ

FC-CBJ FC-BJ FC

CPU times ?

We always need

experiments!!!

Page 42: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 42

Heuristic Methods for CSPs

Search algorithms must take decisions:1) Which will be the next variable to assign ?

2) Which value should I give it ?

3) Which constraint should I check ?

The decisions that the algorithm takes at each step have a drastic effect on the search space (and the efficiency of the algorithm) Especially decision (1)

Heuristics help the algorithms take correct decisions fail first principle

Page 43: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 43

Heuristic Methods for CSPs

Variable ordering heuristics static heuristics

MaxDegree, Bandwidth, … dynamic heuristics

MRV, Brelaz, dom/deg, dom/wdeg…

Value ordering heuristics Geelen’s promise, least-constraining…

Heuristics for constraint ordering based on the cost of propagation

Page 44: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 44

Variable Ordering Heuristics

Minimum Width The width of a variable x is the number of variables that are before x,

according to a given ordering, and are constrained with x The width of an ordering is the maximum width of all the variables

under that ordering The width of a constraint graph is the minimum width of all possible

orderings Variables are ordered in descending width

useful when the degree of the nodes varies significantly

Problem: How many are the possible orderings?

Page 45: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 45

Variable Ordering Heuristics

Maximum Degree Variables are ordered in decreasing order of their degree in the constraint

graph degree is the number of adjacent variables in the graph

Heuristic to find a minimum width ordering

Maximum Cardinality Selects the first variable arbitrarily Then, at each stage, selects the variable that is adjacent to the largest set

of already selected variables.

Page 46: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 46

Variable Ordering Heuristics

Minimum Bandwidth The bandwidth of a variable x, according to a given ordering, is the

maximum distance between x and any other variable which is adjacent to x

The bandwidth of an ordering is the maximum bandwidth of all the variables under that ordering

The bandwidth of a constraint graph is the minimum bandwidth of all possible orderings

Idea: The closer the variables involved in a constraint are placed to each other the less backtracking will be required

Problem: Computing the minimum bandwidth is NP-complete

Page 47: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 47

Dynamic Variable Ordering Heuristics

Minimum Remaining Values (MRV) or Smallest Domain (SD) At each stage of search select the variable with the smallest domain size

How do we break ties?

Select a variable randomly Select the variable with the highest degree in the original graph Select the variable with the highest future degree (i.e. the one involved

in the maximum number of constraints with future variables). This is called the Brelaz heuristic

Many variations have been proposed dom/deg, dom/fdeg

Page 48: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 48

State-of-the-art Dynamic Variable Ordering Heuristics

Weighted degree heuristics each constraint is associated with a weight initially set to 1 each time a constraint c removes the last value from a domain (i.e.

causes a domain wipeout - DWO) its weight is incremented by 1 the weighted degree of a variable x is the sum of the weights of the

constraints that include x wdeg heuristic

selects the variable with maximum weighted degree dom/wdeg heuristic

selects the variable with minimum ration of domain size to weighted degree What is the rationale behind these heuristics?

they use information gathered throughout search – not just from the current search state like dom/fdeg

Page 49: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 49

Value Ordering Heuristics

Min-Conflicts Associate with each value a the total number of values in future

variables that are incompatible with a Select the value with lowest sum Alternative: Divide the number of incompatible values of future variable

x with the domain size of x

Geelen’s Promise For each value a count the total number of values in each future variable

that are compatible with a Take the product of the counts. This is called the promise of value a Select the value with the maximum promise

Page 50: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 50

Constraint Ordering Heuristics

Is this issue important? not very much when maintaining arc consistency

but there exist heuristics for ordering the constraints in the propagation queue. Can you think of such a heuristic?

but very important in modern advanced solvers that use propagators for the various (global) constraints

the idea here is to propagate the less expensive constraints first

Page 51: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 51

Stochastic and Local Search Methods

local search - chooses best neighbouring configuration hill climbing

neighbourhood = value of one variable changed min-conflicts

neighbourhood = value of selected conflicting variable changed

can we avoid local optima? restarts

if at a local optimum, start procedure from scratch random-walk

sometimes picks neighbouring configuration randomly tabu search

few last configurations are forbidden for next step local search does not guarantee completeness

Page 52: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 52

The Min-Conflicts Algorithm

Start with a random assignment of values to variables or a seemingly good one according to some heuristic some constraints will be violated

Try to repair it change the value assignment that resolves the greatest numbers of

constraints local optima can be escaped using random restarts Otherwise:

Simulated annealing Tabu search Random walk

Page 53: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 53

Min-Conflicts (version 1)

procedure Min_Conflicts(P, maxTries, maxChanges)for i :=1 to maxTries do

A := initial complete assignment of the variables in Pfor j:=1 to maxChanges do

if A satisfies P then return (A)else x := randomly chosen variable whose assignment is in conflict (x,a) := alternative assignment of x which satisfies

the maximum number of constraints under the current assignment A

if by making assignment (x,a) you get a cost ≤ current cost then

make the assignmentendif

endforendforreturn (“No solution found”)

Page 54: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 54

Min-Conflicts (version 2)

procedure Min_Conflicts(P, maxTries, maxChanges)for i :=1 to maxTries do

A := initial complete assignment of the variables in Pfor j:=1 to maxChanges do

if A satisfies P then return (A)else (x,a) := the alternative assignment of a variable x which

minimizes the number of constraint violations under the current assignment A

if by making assignment (x,a) you get a cost ≤ current cost then

make the assignment else breakendif

endforendforreturn (“No solution found”)

Page 55: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 55

Min-Conflicts with Random Walk

How can we leave the local optimum without a restart (i.e. via a local step)?

By adding some “noise” to the algorithm! Random walk

a state from the neighbourhood is selected randomly (e.g., the value is chosen randomly)

such technique can hardly find a solution so it needs some guide

Random walk can be combined with the heuristic guiding the search via probability distribution:

p - probability of using the random walk (1-p) - probability of using the heuristic guide

Page 56: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 56

Min-Conflicts with Random Walk (version 1)

procedure Min_Conflicts(P, maxChanges,p)A := initial complete assignment of the variables in Pfor j:=1 to maxChanges do

if A satisfies P then return (A)else if probability p verified x := randomly chosen variable whose assignment is in conflict

(x,a) := randomly chosen alternative assignment of x else (x,a) := the alternative assignment of a variable x which minimizes

the number of constraint violations under the current assignment A

make the assignment (x,a) endif

endforreturn (“No solution found”)

Page 57: 1 ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 Constraint Satisfaction Problems Basic Algorithms My Thanks to Roman Bartak (for “stealing” some of his slides)

ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ - Lecture 1 57

Min-Conflicts with Random Walk (version 2)

procedure Min_Conflicts(P, maxChanges,p)A := initial complete assignment of the variables in Pfor j:=1 to maxChanges do

if A satisfies P then return (A)else

x := randomly chosen variable whose assignment is in conflict if probability p verified (x,a) := randomly chosen alternative assignment of x else (x,a) := the alternative assignment of x which satisfies the maximum number of constraints under the current assignment A make the assignment (x,a) endif

endforreturn (“No solution found”)