CMPT 301 Artificial Intelligence Fall 2014 Homework #4 75 Total Points Due Date: No Later than 5:30 PM on Monday, October 6, 2014. You may either hand-in this homework written or submit it to the drop box on Canvas. It is expected that you write neatly and legibly and that you provide support for all answers. 1. [10 points] Given a set of locations and distances between them, the Traveling Salesperson Problem (TSP) is to find a shortest tour that visits each location exactly once, and returns to the starting location. We would like to solve the TSP [this] using a greedy hill-climbing algorithm. Each state corresponds to a permutation of locations (a tour). The successor operator Successor(s) generates all neighboring states of s by swapping two locations. For example, if s = (A B C) is a tour, then (B A C), (C B A) and (A C B) are the three neighbors generated by Successor(s). We can set the evaluation function for a state to be the total distance of the tour where each pairwise distance is looked up from a distance matrix. Assume that ties in the evaluation function are broken randomly. (a) [2 points] If there are n locations, how many neighboring states does the Successor() function produce? Solution Because we are just swapping two locations out of a possible n locations, the number of neighboring states is ( ) n 2 (b) [3 points] If there are n locations, how many total states are possible? Solution This is a classic factorial example problem O(n!) (c) [5 points] Imagine you wish to hang posters that mention the incredible career opportunities for computer science majors and minors (especially those that have taken a course in Artificial Intelligence!) The buildings that must be visited are Malouf, Converse, Wellness, Eccles, and Shaw. The goal is to find a tour as short as possible. The distance matrix is as follows: M C W E S M 0.9.6.8.7 C.9 0 1.3 1.5 1.3 W.6 1.3 0.2.3 E.8 1.5.2 0.2 S.7 1.3.3.2 0 1
The student starts applying the hill-climbing algorithm from the initial state of (M E C S W ) (which is cost 3.9). (a) What is the next state reached by hill-climbing? (b) Will a global optimal solution be found by hill-climbing from this initial state, or will it get stuck at some local maxima? Solution From the starting state of (M E C S W ) there are ten possible tours where we exchange any 2 buildings: i. (E M C S W ) = 3.3 ii. (C E M S W ) = 3.3 iii. (S E C M W ) = 3.2 iv. (W E C S M) = 3.7 v. (M C E S W ) = 2.9 vi. (M S C E W ) = 3.7 vii. (M W C S E) = 3.4 viii. (M E S C W ) = 3.6 ix. (M E W S C) = 2.6 x. (M E C W S) = 3.9 The hill climbing algorithm will select (M E W S C) with a cost of 2.6 as the next state. The hill climbing algorithm will repeat, beginning from (M E W S C), and it will find the global optimal solution in one more step with the tour of (S E W M C) with a cost of 1.9. We don t actually know if this is a local or global maximum. In general, the algorithm repeats until the successors return higher costs (it has hit a maximum), or the same costs (it has hit a plateau.) If we knew ahead of time what the actual global maximum was, we could determine if we were at a local or global maximum. (But obviously in most searching situations we do not know what the maximum is!) The tsp.py Python script demonstrates how the solution is found using hill climbing. It is worthwhile looking through this code to determine how hill climbing operates. This program also generates a brute force listing of all tours, and shows what the optimal tour is. As the optimal tour from the brute force is also 1.9, we know the tour (S E W M C) is a global maximum. 2
2. [10 points] The Class Scheduling Problem is where there are a fixed number of professors and classrooms, a list of classes to be offered, and a list of possible time slots that classes may be offered. Each professor has a set of classes that he or she can teach. Consider the Class Scheduling Problem as a constraint satisfaction problem. In particular: (a) What are the variables? Solution Class, Classroom and Instructor. (b) What is the domain of each variable? Solution Class = { list of possible time slots } Instructor = { list of classes he or she is teaching } Classroom = { list of classes being taught } (c) What are the constraints? Solution They include (a) only one class can be in the same classroom at the same time, (b) an instructor can only teach one class at a time. 3. [10 points] Consider the problem of constructing (not solving) crossword puzzles where you fit words into a rectangular grid. The grid given as part of the problem specifies which squares are blank and which are shaded. Assume that a list of words (i.e. a dictionary) is provided and that the task is to fill in the blank squares by using subset of the list of words. Formulate this problem in two ways: (a) As a general search problem (using algorithms we went over in Chapters 3 and 4). Choose an appropriate search algorithm and specify its heuristic. Is it better to fill in blanks one letter at a time or one word at a time? Solution A simple idea is a depth-first search where each successor fills in a word in the puzzle with one of the words from the dictionary. A possible heuristic is assigning each word in the dictionary some level of difficulty (i.e. the word stochastic would be assigned a higher difficulty level than the word cat) and try to achieve a certain difficulty level for a particular puzzle. Given this approach, it is probably a better idea to fill blanks in one word at a time rather than one letter at a time. (b) As a constraint satisfaction problem. Should the variables be letters or words? Solution If we choose letters, a variable is each box in the puzzle and the constraints are the variables must combine to make a word in the dictionary. If strings of boxes are variables, the domain is the set of words from the dictionary with the constraint that the intersection of two words must have the same letter in the intersecting box. 3
4. [10 points] The diagram shown in Figure 1 represents a map of a country with 6 states. Each state must be colored in Red, Green, or Blue, such that no two adjacent states get the same color. We represent this as a constraint satisfaction problem with 6 variables (A, B, C, D, E, and F) each having the same domain of {Red, Blue, Green} of values. Use CSP-Backtracking with forward checking algorithm to assign each state a color according to the constraint that no two adjacent states are assigned the same color. Use the minimum-remaining-value MRV as a heuristic. Solution Since you were never asked which state to begin with, we ll choose the initial state alphabetically. The solution is trivial. A B C D E F Domain RGB RGB RGB RGB RGB RGB R GB GB RGB RGB GB G B RB RGB B B R RGB B R G B G B B Figure 1: Map. 5. Consider the 5-Queens problem as such: Variables: Q 1, Q 2, Q 3, Q 4, Q 5 (One variable per row of the chessboard.) Domains: {1, 2, 3, 4, 5} (The column in which a Queen is placed.) Constraints: i, j {1..5}, NON-THREATENING (Q i, Q j ) Solution Note Because chessboards are symmetric (i.e. N N) this works if we consider the variables as representing each row or each column on a chessboard. 4
(a) [5 points] Suppose we try the assignment Q 1 = 3. Run the forward checking algorithm and draw a map of the chessboard indicating the remaining domains for Q 2,...Q 5. Solution If we assign Q 1 = 3, the remaining domains are: Q 2 = {1, 5} Q 3 = {2, 4} Q 4 = {1, 2, 4, 5} Q 5 = {1, 2, 4, 5} (b) [10 points] Now run the AC-3 algorithm and draw a new map of the chessboard indicating the remaining domains for Q 2,...Q 5. Did AC-3 help? Solution When we run AC-3 we do not reduce the size of the domains for Q 2, Q 3, or Q 4. However, when we consider assigning Q 5 = 2, we see that the current domain of {2,4} for Q 3 = {2, 4} does not allow it, so we remove 2 from the domain of Q 5. The same thing occurs when we consider assigning Q 5 = 4. The resulting domains after running AC-3 are Q 2 = {1, 5} Q 3 = {2, 4} Q 4 = {1, 2, 4, 5} Q 5 = {1, 5} So yes, AC-3 did help because it reduced the domain of Q 5. 6. [10 points] The tree shown in Figure 2 begins with Max at the root. You may find it is easiest to answer these questions by redrawing the tree, illustrating the values being returned at each node, and in the case of α β pruning, showing which subtrees were pruned. (a) What is the value of Minimax at the root? Solution 2 is returned at the root. (b) Repeat the problem, but this time using α β pruning. Solution: Figure 3 7. [10 points] The tree shown in Figure 4 begins with Max at the root. You may find it is easiest to answer these questions by redrawing the tree, illustrating the values being returned at each node, and in the case of α β pruning, showing which subtrees were pruned. 5
Figure 2: Example of 2-ply Minimax tree. (a) What is the value of Minimax at the root? Solution: Figure 5 (b) Repeat the problem, but this time using α β pruning. Solution: Figure 6 6
Figure 3: Solution of 2-ply Minimax tree with α β pruning. Figure 4: Example of 3-ply Minimax tree. 7
Figure 5: Solution of 3-ply Minimax tree. Figure 6: Solution of 3-ply Minimax tree with α β pruning. 8