Docsity

Constraint Satisfaction Problems (CSPs), Exercises of Computer science

Partial preview of the text

Download Constraint Satisfaction Problems (CSPs) and more Exercises Computer science in PDF only on Docsity!

  • Tutorial

EXAMPLE

VARIABLES: A,B,C,D DOMAIN: {1,2,3}

CONSTRAINTS: A ≠ B, A=D, C < B, C < D

A B D C C<B C<D A ≠ B A=D

A= B=1 (^) B= C= D=1 D=2 D= C=2 C= B= C= D=1 D=2 D= C= C= A= B=1 B= C=1 C=2 C= B= C= D=1 D=

EXAMPLE

VARIABLES: A,B,C,D DOMAIN: {1,2,3}

CONSTRAINTS: A ≠ B, A=D, C < B, C < D

VARIABLE ORDER: ALPHABETICAL VALUE ORDER: ASCENDING A B D C C<B C<D A ≠ B A=D D=1 (^) D=2 (^) D=

§ (A=1, B=3, C=2) § (A=1, B=3, C=2, D=1) inconsistemt with C<D § (A=1, B=3, C=2, D=2) inconsistemt with C<D and A = D § (A=1, B=3, C=2, D=3) inconsistemt with A = D § No remaining assignments for D, return fail § Backtract to (A=1, B=3, C=)

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

AC- 3 EXAMPLE A B D^ C C<B C<D {1,2,3} (^) A ≠ B {1,2,3} {1,2,3} {1,2,3} Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3}

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

Constraints both ways: A≠ B, B≠ A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC Pop AB: for each x in Domain(A) if no value y in Domain(B) that allows (x,y) to satisfy constraint between (A,B) delete x from Domain(A) No change to domain of A AC- 3 EXAMPLE Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3}

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

Constraints both ways: A≠ B, B≠ A, C < B, B > C, C < D, D > C Queue: BC, CB, CD, DC Pop BC for each x in Domain(B) if no value y in Domain(C) that allows (x,y) to satisfy constraint between (B,C) delete x from Domain(B) If B is 1, constraint B >C cannot be satisfied. So delete 1 from B’s domain, B={2,3} Also have to add neighbors of B (except C) back to queue: AB (Queue: AB, CB, CD, DC) AC- 3 EXAMPLE Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3}

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

Constraints both ways: A≠ B, B≠ A, C < B, B > C, C < D, D > C Queue: AB, CB, CD, DC Pop AB For every value of A is there a value of B such that A ≠ B? Yes, so no change AC- 3 EXAMPLE Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC. B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3}

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

Constraints both ways: A≠ B, B≠ A, C < B, B > C, C < D, D > C Queue: CD, DC Pop CD For every value of C, is there a value of D such that C < D? Yes, so no change AC- 3 EXAMPLE Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC. B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CD, DC B={2,3}, C = {1,2} A,D = {1,2,3} Queue: DC B={2,3}, C = {1,2} A,D = {1,2,3}

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D (subset of constraints frombefore)

Constraints both ways: A≠ B, B≠ A, C < B, B > C, C < D, D > C Queue: DC Pop DC For every value of D is there a value of C such that D > C? Not if D = 1, So D = {2,3} AC- 3 EXAMPLE Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC. B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CD, DC B={2,3}, C = {1,2} A,D = {1,2,3} Queue: D = {2,3} B={2,3}, C = {1,2} A = {1,2,3}

Problem 1 Consider the problem of assigning colors to the five squares on board below such that horizontally adjacent and vertically adjacent squares do not have the same color. Assume there are possible two colors, red (R) and black (B). Formulated as a constraint satisfaction problem, there are five variables (the squares) and two possible values (R, B) for each variable. X={X 1 , X 2 ,X 3 ,X 4 , X 5 } D 1 =D 2 =D 3 =D 4 = D 5 ={R, B} C={C 12 , C 14 , C 23 , C 25 , C34, , C 45 } R 12 ={(R,B),(B,R)} or X 1 ≠ X 2 R 14 ={(R,B),(B,R)} or X 1 ≠ X 4 R 23 ={(R,B),(B,R)} or X 2 ≠ X 3 R 25 ={(R,B),(B,R)} or X 2 ≠ X 5 R 45 ={(R,B),(B,R)} or X 4 ≠ X 5 X 1 X 2 X 4 X 5 X 3 X 1 ≠ X 2 X 1 ≠ X 4 X 2 ≠ X 3 X 2 ≠ X 5 X 4 ≠ X 5

Problem 1 a) If initially every variable has both possible values and we then assign variable 1 to have value R, what is the result of the Forward Checking algorithm? Forward checking propagates constraints from the current assigned variable to all adjacent unassigned variables, which in this case are variables 2 and 4. This results in the following domains for the variables: D 1 = {R}, D 2 = {B}, D 3 = {R, B}, D 4 = {B}, D 5 = {R, B} X 1 X 2 X 4 X 5 X 3 X 1 ≠ X 2 X 1 ≠ X 4 X 2 ≠ X 3 X 2 ≠ X 5 X 4 ≠ X 5

Problem 2 Solve the following cryptarithmetic problem by hand, using backtracking, Forward checking and MRV and least constraining value heuristics (LCV).

  • MRV : choose the variable with the fewest legal values
  • Least constraining value heuristics (LCV): It prefers the value that rules out the fewest choices for the neighboring variables.
  • Forward checking: Looks at each unassigned variable, Y say, connected to X and delete from Y’s domain any value inconsistent with X’s assignment.

Problem 2 Solve the following cryptarithmetic problem by hand, using backtracking, Forward checking and MRV and least constraining value heuristics (LCV). See the solution: http://www.cs.cmu.edu/afs/cs/academic/class/15780-s15/www/hw/hw2key.pdf