 Hello everyone, this is Alice Gao. In this video, I will show you how to formulate a constraint satisfaction problem. We have spent quite a bit of time discussing search algorithms. Given this, why are we devoting a separate lecture for constraint satisfaction problems? How are CSPs different from other search problems? Let's take a look at how we can solve 4queens problem using a form of depth first search. Start with an empty board and add queens to the board one by one. Once we have added four queens, the algorithm tests whether the state is a goal state or not. If it is not a goal state, we backtrack, changing the position of the last queen place on the board. Whenever we have a full board, we'll run the goal test again. This form of depth first search is also called backtracking search. Notice that backtracking search treats each state as a black box. It does not know what is inside each state. Backtracking search can perform two tasks. One, test whether a state is a goal, and two, generate successors of a state. Let me show you one reason why we may not be happy with backtracking search. Consider a partial state in which we have put two queens in the same row. To us, it is clear that this state does not lead to a solution, since the two queens already violate the row constraint. We should backtrack immediately and put the second queen in a different row. Unfortunately, backtracking search does not know this. It only knows that this state is not a goal. So it needs to explore the entire subtree starting from this state to realize that this partial state does not lead to a solution. For this example, it is very useful for the search algorithm to understand the internal structure of the state. In particular, if it knows it understands the positions of the queens, it may be able to prune more states and complete the search much more quickly. Given this, when we are solving a CSP as a search problem, we will explicitly model the internal structure of each state. And we will discuss specialized algorithms that can solve CSPs efficiently. Let's take a look at the components of a CSP. A CSP is one type of a search problem. Like any other search problem, we need to define the states, the initial state, the goal states, the successor function, and the cost function. In addition, we will model the internal structure of each state. A state includes three parts, the variables, a domain for each variable, and a set of constraints. Each constraint involves one or two variables, one or more variables, and specifies the combinations of allowable values for these variables. A solution to a CSP will include an assignment of values to all the variables, such that this assignment satisfies all the constraints. Let's formulate the four queens problem as a CSP. I will focus on defining the components of a state. How should we define the variables, the domains, and the constraints? Here's the first idea that I came up with. We can define one variable for each queen. The variable stores the row and the column positions of the queen. The domain for each variable contains 16 possible positions for this queen. Our constraints are, no two queens can be in the same row, the same column, or the same diagonal. This formulation works, but I will show you a better one. The improved formulation keeps track of less information and has fewer constraints. We will make an assumption that there is exactly one queen in each column. Having assumed this, we no longer need to keep track of the column positions of the queens. We also don't have to worry about the column constraints. Here are the details. I will define four variables, x0 to x3. The subscript i refers to the column for each queen. For example, x2 is the queen placing the third column from the left. The value of each variable is the row position of the queen. For example, if x0 has the value 0, the leftmost queen is in the top row. The domain of each variable contains the four possible row positions. The constraints are, no two queens can be in the same row, or in the same diagonal. The description of the constraints in words is quite clear. However, if you prefer, you can express the constraints as a logical formula. Here's an example. The first part encodes the row constraint, and the second part encodes the diagonal constraint. If the queens are in the same diagonal, then the difference in their row positions must be equal to the difference in their column positions. We must require these two differences to be not equal. For example, to encode that, the leftmost two queens cannot be in the same row and also cannot be in the same diagonal. We have x0 is not equal to x1, and the absolute difference between x0 and x1 is not 1. That's everything on formulating a constraint satisfaction problem. Let me summarize. After watching this video, you should be able to do the following. Explain why we should model the internal structure of a state in a CSP. Describe the components of a CSP, especially the components of a state. Formulate a real-world problem as a CSP. Thank you very much for watching. I will see you in the next video. Bye for now.