Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information Systems.

Similar presentations


Presentation on theme: "ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information Systems."— Presentation transcript:

1 ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information Systems

2 (CHAPTER-6) CONSTRAINT SATISFACTION PROBLEMS

3 Outline What is a CSP CSP applications Backtracking search Problem structure and decomposition 3

4 Mean of Constraint Satisfaction Problems 4

5 Constraint Satisfaction Problems A constraint satisfaction problem consists of three components, X, D, and C X is a set of variables, {Xi,., XT,}. D is a set of domains, {Di,…, Dn.}, one for each variable. C is a set of constraints that specify allowable values. Each constraint C i limits the values that variables can take, e.g., V 1 ≠ V 2 A state is defined as an assignment of values to some or all variables. 5

6 CSPs (continued) An assignment is complete when every variable is mentioned. A solution to a CSP is a complete assignment that satisfies all constraints. Some CSPs require a solution that maximizes an objective function. 6

7 Varieties of constraints Unary constraints involve a single variable. e.g. SA  green Binary constraints involve pairs of variables. e.g. SA  WA Higher-order constraints involve 3 or more variables. Professors A, B,and C cannot be on a committee together Can always be represented by multiple binary constraints Preference (soft constraints) e.g. red is better than green often can be represented by a cost for each variable assignment combination of optimization with CSPs 7

8 Constraint Satisfaction Applications 8

9 CSPs (continued) Examples of Applications Scheduling : Scheduling the timetable of lectures Airline schedules Scheduling your MS or PhD thesis exam Configuration To arrange objects of different sizes in a container, Map Coloring. --------------------------------------------------------------------------------- CSP solvers can be faster than state-space searchers because the CSP solver can quickly eliminate large swatches of the search space. 9

10 10 Example problem – timetable of university (1) state - collection of schedule objects Schedule (Professor, course, time slot, location ) Objective functionConstraints 1.minimize total course enrollment in early morning time slots 2.distribute program courses across days 3.accommodate special requests (off course for professors )  professors assigned to courses  professors assigned only once per time slot  one course per location per time  courses in program in different time slots  slot  location capacity >= course (projected) enrollment  rooms assigned only once per time slot

11 11 Solving CSPs analysis of problem state structure is critical, two basic approaches: 1. start state is null: assign state variables one at a time and backtrack if constraint is violated 2. assign variables ‘randomly/intelligently’ to make a start state and use greedy search by changing variable assignments

12 CSP example: map coloring Variables: WA, NT, Q, NSW, V, SA, T Domains: D i ={red,green,blue} Constraints: adjacent regions must have different colors. E.g. WA  NT

13 CSP example: map coloring Solutions are assignments satisfying all constraints, e.g. {WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=green}

14 Solving CSPs As general searching problem. Back-Tracking. Arc-consistency Checking. 14

15 As General Searching Problem 15

16 CSP as a standard search problem A CSP can easily be expressed as a standard search problem. Incremental formulation Initial State: the empty assignment {} Successor function: Assign a value to any unassigned variable provided that it does not violate a constraint Goal test: the current assignment is complete (by construction its consistent) Path cost: constant cost for every step (not really relevant) 16

17 Backtracking Search 17

18 Variable assignments are commutative if the order of any given set of actions has no effect on the outcome. i.e., [WA = red then NT = green] same as [NT = green then WA = red]. Only need to consider assignments to a single variable at each node. and backtracks when a variable has no legal values left to assign. Depth-first search for CSPs with single-variable assignments is called Backtracking Search: Chooses values for one variable at a time. Checks for constraint violations at each assignment. Backtracks when a variable has no legal values left to ass Backtracking Search 18

19 Backtracking search function BACKTRACKING-SEARCH(csp) return a solution or failure return RECURSIVE-BACKTRACKING({}, csp) function RECURSIVE-BACKTRACKING(assignment, csp) return a solution or failure if assignment is complete then return assignment var  SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp) for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do if value is consistent with assignment according to CONSTRAINTS [csp] then add {var=value} to assignment result  RRECURSIVE-BACTRACKING(assignment, csp) if result  failure then return result remove {var=value} from assignment return failure Backtacking 19

20 Backtracking example 20

21 Backtracking example 21

22 Backtracking example 22

23 Backtracking example 23 Constraints on SA will eventually cause failure when WA != Q. When not the same color (bottom right), SA cannot be assigned. The algorithm will backtrack to a node with unexplored states. For example, such as WA=red, NT=blue.

24 Improving Backtracking Efficiency Consider Which variable should be assigned next? In what order should the variables be tried? Can we detect inevitable failure early? Can we take advantage of problem structure? 24

25 By default, SELECT-UNASSIGNED-VARIABLE simply selects the next unassigned variable in the order given by the list VARIABLES[csp]. This static variable ordering seldom results in the most efficient search. there are three ways to improve search 25 3-Forward checking2-Least Constraining Value 1- “Most constrained variable” or Minimum remaining values (MRV) heuristic keep track of remaining legal values for unassigned variables. Terminate search when any variable has no legal values Idea is to explore paths, those with most options, most likely to lead to a solution. idea—choosing the variable with the fewest “legal” values

26 1-Most Constrained Variable or minimum remaining values (MRV) Heuristic Rule: Choose the variable with the fewest legal values. Idea is that variable with fewest choices will likely fail sooner allowing branch to be pruned early. Legal values are those that do not conflict with any already assigned to a neighbor (i.e. do not violate constrains). After WA=red, NT and SA have only 2 remaining legal values, the other unassigned variables all have 3. Choose between NT and SA. 26

27 Most constraining variable Choose the variable that constrains the most remaining variables SA places constrains on 5 variables; WA, NT, Q, V, NSW only on 3, T on 0. Purpose is to prune early by reducing number of choices. Tie breaker among minimum remaining variables (MRV) 27

28 2-Least Constraining Value Heuristic Rule: Given a variable, choose the least constraining value 28 Idea is to explore paths, those with most options, most likely to lead to a solution.

29 3-Forward checking Can we detect inevitable failure early? And avoid it later? Forward checking idea: keep track of remaining legal values for unassigned variables. Terminate search when any variable has no legal values.

30 Forward checking Assign {WA=red} Effects on other variables connected by constraints to WA NT can no longer be red SA can no longer be red

31 Forward checking Assign {Q=green} Effects on other variables connected by constraints with WA NT can no longer be green NSW can no longer be green SA can no longer be green

32 Forward checking If V is assigned blue Effects on other variables connected by constraints with WA NSW can no longer be blue SA is empty FC has detected that partial assignment is inconsistent with the constraints and backtracking can occur.

33 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {1,2,3,4} X4 {1,2,3,4} X2 {1,2,3,4} Forward checking 33

34 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {1,2,3,4} X4 {1,2,3,4} X2 {1,2,3,4} Forward checking 34

35 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,,4} X4 {,2,3, } X2 {,,3,4} 35

36 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,,4} X4 {,2,3, } X2 {,,3,4} 36

37 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,,, } X4 {,,3, } X2 {,,3,4} 37

38 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,,4} X4 {,2,3, } X2 {,,,4} 38

39 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,,4} X4 {,2,3, } X2 {,,,4} 39

40 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,, } X4 {,,3, } X2 {,,,4} 40

41 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,, } X4 {,,3, } X2 {,,,4} 41

42 Example: 4-Queens Problem 1 3 2 4 3241 X1 {1,2,3,4} X3 {,2,, } X4 {,,, } X2 {,,3,4} Dead End → Backtrack 42

43 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,2, 3,4 } X4 { 1, 2, 3, 4 } X2 { 1, 2,3,4} 43

44 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,, 3, } X4 { 1,, 3, 4 } X2 {,,,4} 44

45 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,, 3, } X4 { 1,, 3, 4 } X2 {,,,4} 45

46 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,, 3, } X4 { 1,, 3, } X2 {,,,4} 46

47 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,,, } X4 { 1,, 3, } X2 {,,,4} 47

48 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,,, } X4 {,, 3, } X2 {,,,4} 48

49 Example: 4-Queens Problem 1 3 2 4 3241 X1 {,2,3,4} X3 { 1,,, } X4 {,, 3, } X2 {,,,4} Solution !!!! 49

50 Consistency Techniques removing inconsistent values from variables’ domains graph representation of the CSP arc consistency (AC)

51 Arc consistency Simplest form of propagation makes each arc consistent X  Y is consistent iff for every value x of X there is some allowed y 51 Arc from SA to NSW is consistent because SA (blue) allows NSW (red)

52 Arc consistency 52 Arc from NSW to SA is not consistent because NSW (blue) does not allow SA (blue) Can be made consistent by removing blue from NSW domain

53 Arc consistency 53 If X loses a value, neighbors of X need to be rechecked. After removing blue from NSW, must check neighbors for consistency leading to removal of red from V.

54 If X loses a value, neighbors of X need to be rechecked Arc consistency detects failure earlier than forward checking Can be run as a preprocessor or after each assignment After removing blue, SA domain is {}, pruning SA completely for the two assignments {NSW=red, Q=green} and forcing backtracking. 54 Arc consistency

55 55 Is the arc (WA,NSW) consistent? Is the arc (NSW,WA) consistent? If not, what should be the domain of NSW and WA? Is the arc (SA,T) consistent? is the arc (T, SA) consistent?

56 [R][R] [R,B,G][R,B,G][R,B,G][R,B,G] [R,B,G][R,B,G] [R,B,G][R,B,G] Arc consistency AC3

57 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] [R,B,G][R,B,G] Arc consistency AC3

58 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

59 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

60 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

61 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

62 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

63 [R][R] [R,B,G][R,B,G][,B,G] [R,B,G][R,B,G] Arc consistency AC3

64 [R][R] [R,B,G][R,B,G][,B, ] [,B,G] [R,B,G][R,B,G] Arc consistency AC3

65 [R][R] [R,,G][,B, ] [,B,G] [R,B,G][R,B,G] Arc consistency AC3

66 [R][R] [R,,G][,B, ] [,B,G] [R,,G] Arc consistency AC3

67 [R][R] [R,,G][,B, ] [,,G] [R,,G] Arc consistency AC3

68 [R][R] [R,,G][,B, ] [,,G] [R,, ] Arc consistency AC3

69 [R][R] [,,G][,B, ] [,,G] [R,, ] Arc consistency AC3

70 [R][R] [,,G][,B, ] [,,G] [R,, ] Arc consistency AC3

71 [R][R] [,,G][,B, ] [,,G] [R,, ] Arc consistency AC3

72 [R][R] [,,G][,B, ] [,,G] [R,, ] Solution !!! Arc consistency AC3

73 Thank you End of Chapter 6 73


Download ppt "ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information Systems."

Similar presentations


Ads by Google