Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

31
Artificial Intelligence Constraint satisfaction Chapter 5, AIMA
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

Transcript of Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Page 1: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Artificial Intelligence

Constraint satisfactionChapter 5, AIMA

Page 2: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Constraint Satisfaction Problem (CSP)

• A set X = {X1,X2,...,Xn} of variables

• Each variable Xi has a nonempty domain Di = {v1,v2,...,vk(i)} of possible values

• A set C = {C1,C2,...,Cm} of constraints

• A solution is a complete assignment of values for the variables, which satisfies all the constraints.

• Examples: Map coloring, scheduling, logistics (transportation), N-queens, crossword puzzles,...

Page 3: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Map coloring

(Image borrowed from V. Pavlovic)

• X = {WA, NT, Q, NSW, V, SA, T}• D = {R,G,B} (for all variables)• C = {WA NT, WA SA, NT SA, NT Q, SA Q,

SA NSW, SA V, Q NSW, NSW V} (Neighboring regions must have different colors)

• Goal: All regions must have a color

Page 4: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Map coloring

• CSP can be visualized with constraint graph– Nodes = variables– Arcs = constraints

Page 5: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Types of constraints

• Unary: Xi vj

• Binary: Xi Xj

• Higher order

• Preferences

Page 6: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

CSP cast as a search problem

• Depth-first search (depth limit = n)

Backtracking search

Very inefficient

• Initial state• Successor

function• Goal test• Path cost

(not relevant)

Page 7: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

”Vanilla” backtracking

WA

NT

SA

Q

NSW

V

T

WA

NTQ

SA

Animation adapted from V. Pavlovic

V

NSW

Q

WA WA WA

NTNT NT

Q QQ

SA SASA

VV V

NSW NSWNSW

V VV

Page 8: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Improve ”vanilla” Backtracking

1. Smart choice of next variable- Min. remaining values (+ degree heuristic)- Least constraining value

2. Check consequences of choice- Forward checking- Arc consistency

3. Intelligent backtracking- Backjumping

Page 9: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Minimum remaining valuesChoose next the variable that is most constrained based on current

assignment (& choose the one with the highest degree first in ties)

WA

NT

SA

Q

NSW

V

T

WA

NT

SA

Animation adapted from V. Pavlovic

Q

NSW

V

T

Page 10: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Least constraining value

Image borrowed from V. Pavlovic

Select the value that least constrains the other variables (rules our fewest other variables)

Page 11: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Forward checkingDelete values that are inconsistent with the last selection

Animation adapted from V. Pavlovic

Stop search early, if necessary

Page 12: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency

• The arc Xi Xj is consistent if for every value of Xj there

exists some valid value of Xi.

• Detects failures sooner than forward checking.• Use as preprocessor or as check after each move (MAC)

Animation adapted from V. Pavlovic

Page 13: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

4-queens

• X = {q1, q2, q3, q4} (qi = queen in column i)

• D = {1,2,3,4} (row for queen)

• C: No queen may attack another queen• Goal: Place all queens on the board

7888894 0q064% 0 64 0 %640q0 6122223

q1

q4

q3q2

Page 14: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

F

{1,2,3,4}

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

Page 15: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

Page 16: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

Page 17: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q2,q1)

Page 18: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q3,q1)

Page 19: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q4,q1)

Page 20: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q3,q2)

Page 21: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

q4

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

(q3,q2)

Page 22: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q2,q3)

Page 23: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q2,q3)

Page 24: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q3,q4)

Page 25: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 1

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894q0 0640 0 64 0 0640 0 6122223

The arc (i,j) is consistent if each value at the tail jof the arc has at least one value at the head i thatdoes not violate the constraints.

1

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

(q3,q4)

No solution with q1 = 1

Page 26: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Arc consistency: 4-queens

• qi = queen placed in column i

• {1,2,3,4} = The allowed values (row number) for the queen.

• First move: q1 = 2

2

{1,2,3,4}

{1,2,3,4}{1,2,3,4}

q1

q3q2

q4

Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998

7888894 0q064% 0 64 0 %640q0 6122223Arc consistency (constraint propagation) finds the

solution

Page 27: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Local search: Simple and efficient

• Start with initial (invalid) state• Modify this state, using the min-conflicts

heuristic: Select the value that minimizes the number of conflicts

• Continue until a solution is found

Page 28: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

7888894 0 064% 0 64 0q0640q0q6122223

Local search: 4-queens

7888894 0 %64% 0 64 0q0640q0 6122223

Stuck!

Choose random movethat does not increaseconflicts.

7888894 0q%64% 0 64 0 0640q0 6122223

7888894 0q064% 0 64 0 %640q0 6122223

Solution in three moves!

Page 29: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Map coloring: Australia

Using Java code @ AIMA site, calling from within MATLAB.

• Backtracking ~ 1.5 sec

7 variables (countries)9 constraints3 values (R,G,B)

Page 30: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Map coloring: Africa

49 variables (countries)107 constraints4 values (R,G,B,Y)

Page 31: Artificial Intelligence Constraint satisfaction Chapter 5, AIMA.

Map coloring: Africa

Modified the AIMA Java code, calling from within MATLAB.

• Backtracking ~ 5.5 min