You are on page 1of 7

CIS 391/521: HW 2 - Search Solutions (Preliminary)

Written Portion [35 pts, 5 points each]


1. Consider the following problem: You have two jugs of water with capacities 4 and 13 liters. You also have an innite supply of water. Can you use the two jugs to get exactly 2 liters of water? Cast this as a search problem: what is your state space, initial state, action space, goal condition, and costs of actions? SOLUTION: Initial state: two jugs have values [0, 0] State space: [x, y] for all non-negative integer x and y. Successor function: Given values [x, y], the successor function can generate [4, y, z], [x, 13] (by action lling), [0, y], [x, 0] (by action emptying) from water supply. Also for two jugs A, B with current values x and y respectively, pouring A into B can be an action. The result of this action will make B to z = min(x + y, capacity(B)) and make amount in A to x (z y). Cost function: Number of actions. Goal function: any of [x, 2] or [2, x] for all x. 2. Describe a search space in which iterative deepening search performs much worse than depthrst search. In this situation, give big-O descriptions of the running time for iterative deepening and for depth-rst search. SOLUTION: Consider a domain in which every state has a single successor, and single goal exists at depth n. Then depth-rst search will nd the goal in n steps whereas iterative deepening search will take 1 + 2 + 3 + . . . + n = n(n+1) = O(n2 ) steps. 2 3. Sometimes there is no good evaluation function for a problem, but there is a good comparison method:a way to tell if one node is better than another, without assigning numerical values to either. Show that this is enough to do a best-rst search. (a) Describe in words and pseudo code how you would implement this method. (b) Show what the dierences in time and memory would be (if any) compared to a standard greedy search where you know the particular value associated with each node.

SOLUTION: (a) If we assume the comparison function is transitive, then we can always sort the nodes using it, and choose the node that is at the top of the sort. (b) Ecient priority queue data structures rely only on comparison operations, so we lose nothing in eciency except for the fact that the comparison operation on states may be much more expensive than comparing two values in standard greedy search, where each values can be computed just once. 4. There are n people 0,1,...,n-1. They all need to cross the bridge but they have just one ashlight. A maximum of two people may cross at a time. It is nighttime, so someone must carry the single ashlight during each crossing. They all have dierent speeds such that the time taken to cross the bridge for person i is given by T (i) minutes. You may assume without loss of generality that for i < j, T (i) T (j). The speed of two people crossing the bridge is determined by the slower of the two. You need to nd the shortest amount of time in which all the people can cross the bridge. As an example suppose there are 4 person and time taken (T (i)) are 1,2,5 and 10 for i = 0, 1, 2, 3 respectively. A solution to the problem is: (a) (b) (c) (d) (e) (f) 0 and 3 go across 10 minutes 0 goes back with light 1 minute 0 and 2 go across 5 minutes 0 goes back with light 1 minute 0 and 1 go across 2 minutes Total 19 minutes

Note: This is not the optimal solution for this problem. Describe three possible heuristic functions for this search problem, and state for each whether it is admissible and whether it is consistent. You should include at least one consistent heuristic function. SOLUTION: First let us cast this as a search problem. Here is one possible representation: A state can consists of a variable boat indicating where the boat is(0 for left side and 1 for the other side) and a list of tuples of pairs of (n, side) where n is ID of each person and side is 0 if the person is on the left side and 1 otherwise. The goal is a state with tuples have 1 as right hand value. An action can be dened as picking one or two person i, j with side(i) = side(j) = boat and switching the value of all three. The successors function maps a all the states that move 1 or 2 people and 1 boat from one side to another. The cost function here is max(T (i), T (j)). Goal function is the state with all people i having side(i) = 1. We can think of three possible estimated cost function. First it can be the number of person on the left side. It can be half of sum of T (i)s for all people i on the left side. Lastly it can be i,j T (i) T (j) for pairs i and j that makes this sum the smallest. Certainly the second one is admissible heuristic because in a relaxed problem that we can move the boat from left to right without the need to return the boat back to left, T (i)/2 max(T (i), T (j) for all possible (i, j) pair. (The best case is when we pair the closest two values starting from the smallest one.) Therefore, in the original problem, n, h(n) C(n) and h(n) is admissible. 5. Which of the following are admissible, given admissible heuristics h1 and h2? (a) h(n) = min(h1 (n), h2 (n)) 2

SOLUTION: From the denition of admissible heuristic, we have n, h1 (n) C(n), h2 (n) C(n) where C(n) is the actual cost to reach a goal from n. Since we know h(n) h1 (n) and h(n) h2 (n), n, h(n) C(n), and thus h(n) is admissible. (b) h(n) = wh1 (n) + (1 w)h2 (n), where 0 w 1. SOLUTION: Since n, wh1 (n) wC(n), (1 w)h2 (n) (1 w)C(n), h(n) C(n), and thus h(n) is admissible. (c) h(n) = max(h1 (n), h2 (n)) SOLUTION: h(n) is admissible because it is the greater of h1 (n) and h2 (n), which are given as admissible heuristic. 6. Which of the following are consistent, given consistent heuristics h1 and h2 ? (a) h(n) = min(h1 (n), h2 (n)) SOLUTION: From the denition of a consistent heuristic, h(G) = 0 since h1 (G) = 0 and h2 (G) = 0. Also we have h1 (N ) c(N, P ) + h1 (P ), h2 (N ) c(N, P ) + h2 (P ) where P is a successor of N . If both h1 (N ) and h1 (G) is smaller than h2 (N ) and h2 (G) respectively, h(n) is also consistent. Same argument can be applied for the opposite case. Let min(h1 (N ), h2 (N )) = h1 (N ) and min(h1 (P ), h2 (P )) = h2 (P ) without loss of generality. then c(N, P ) + h(P ) = c(N, P ) + h2 (P ) h2 (N ) h1 (N ) = h(N ). Therefore h(n) is consistent. (b) h(n) = wh1 (n) + (1 w)h2 (n), where 0 w 1 SOLUTION: Clearly h(G) = 0. Also h(N ) = wh1 (N )+(1w)h2 (N ) w(c(N, P )+ h1 (P ))+(1w)(c(N, P )+h2 (P )) = c(N, P )+wh1 (P )+(1w)h2 (P ) = c(N, P )+H(P ). Therefore h(n) is consistent. (c) h(n) = max(h1 (n), h2 (n)) SOLUTION: False. If max(h1 (N ), h2 (N )) = h1 (N ) and max(h1 (P ), h2 (P )) = h2 (P ), c(N, P ) + h(P ) = c(N, P ) + h2 (P ) h2 (N ) but h(N ) = h1 (N ) and h2 (N ) = h1 (N ). So h(N ) c(N, P ) + h(P ) is not always true. 7. Assume we have a very large search space with a very large branching factor at most nodes, and we do not have any heuristic function. What search method would be good to use in this situation? Why? SOLUTION: In large or innite state spaces, local hill climbing search or simulated annealing can be useful if we can nd best state according to some objective function because it uses very little memory as it doesnt remember which states its been.

2
2.1

Searching for Sudoku (35 points)


Written Questions: Searching for Sudoku [10 pts]

In the next section, you will be asked to implement two dierent types of solvers for the Sudoku problem introduced in the previous homework: uninformed graph based search and simulated annealing. For graph based search, you will represent the problem much like humans do: a given state is just a partially lled in solution to the initial Sudoku board. Please note: your answers to these questions can be very brief. One sentence should suce for each. 1. Suppose that your successor function is to choose the next available square (reading the board left-to-right, up-to-down, like English) and write down a number in that square. What is the branching factor of this successor function? SOLUTION: For the next cell, there are 9 possibilities; therefore b = 9. 2. Will Depth First Search (DFS) be nite on the Sudoku problem? Let r be the number of empty positions on the initial Sudoku board. What is the maximum search depth of DFS in terms of r? SOLUTION: The Sudoku problem is nite because there is a nite number of unknowns on the board. The maximum depth is r. 3. What is the best-case run time complexity of DFS, in terms of r? What is the worst-case? SOLUTION: The best case runtime is O(r), where DFS magically guesses correctly for each known. In the worst case, it must check all O(9r ) possible solutions, so the worst case is O(9r ). 4. What are the best-case and worst-case run times for Breadth First Search (BFS)? SOLUTION: BFS will expand all possible solutions before checking any of them; therefore the best and worst runtime is O(9r ). 5. We saw in class that the (worst-case) space complexity of BFS is often quite large. In the case of Sudoku, suppose you try to run BFS for a board with r = 40 on your laptop. Assume (optimistically) that storing a state in memory requires only 1 byte. How much memory might your laptop need by the time it reached the solution?

SOLUTION: We would need at least 9r = 940 1.5 1038 bytes, which is larger than the number of stars in the observable universe (approximately 1022 ). 6. In simulated annealing, we use a local representation of the problem. For Sudoku, the local representation is a complete assignment to the r originally empty positions on the board. What is the size of this entire state space, in terms of r? SOLUTION: 9 possibilities per unknown cell and r unknown cells = 9r possible states. 7. What is a successor function you might use for simulated annealing? SOLUTION: There are many possible successors, perhaps the simplest is to choose a non-xed cell and enter in a random numbebr from the range [1, 9].

2.2

Programming Portion [25 pts]

For this part of the assignment, you will write two Sudoku solvers in Python, and test them on real Sudoku problems. It is mostly up to you how to code them, as we will not be running any automated tests.

2.3

Uninformed Graph Search [30 pts]

1. See posted reference implementation. COMMON MISTAKE: Many did not follow the instructions on how to compute time complexity: to get the correct #s, you needed to compute the number of times the successor function was called, not the runtime of the algorithm, the # of loop iterations, or the # of nodes added to the queue. 2. Run the uniformed BFS and DFS algorithm on tiny.txt and report time and space complexity. SOLUTION: tiny.txt, uninformed BFS: time 3863, space 6561 tiny.txt, uninformed DFS: time 3958, space 33 3. See posted reference implementation.

COMMON MISTAKE: Many did not follow the instructions on how to implement the informed algorithm. In order to get the correct time and space complexity, one needed to check for validity only before expansion, not when states were being added to the queue. Even though there are many alternatives to this approach, this was the one we asked you to implement, and this was the one we expected you to report the performance for. 4. Re-run the DFS experiments from part 2 and report the new time and space complexities. Did you nd any improvement? (Hint: it should be dramatic.) Run your informed algorithms on easy.txt and medium.txt and report time and space complexities. SOLUTION: tiny.txt, informed DFS: time 25, space 19 easy.txt, informed DFS: time 94, space 103 medium.txt, informed DFS: time 321, space 133 5. Implement BFS by applying the same modication from part 3. The algorithm should not generate successors that violates any constraints. Run the algorithm on tiny.txt, easy.txt, and medium.txt and report time and space complexities. Did you nd any improvement from normal BFS? SOLUTION: tiny.txt, informed BFS: time 29 space 16 easy.txt, informed BFS: time 182 space 18 medium.txt, informed BFS: time 897 space 75

2.4

Simulated Annealing [40 pts]

Recall that in simulated annealing, we use a local representation of the problem. For Sudoku, the local representation is a complete assignment to the r originally empty positions on the board. 1. See reference implementation. COMMON MISTAKE: The instructions specied a way of setting random probability that was dierent, and much simpler, than that used in lecture. In class, there was an equation of the form p = exp E/T . In the homework, you were supposed to use a much simpler equation: p(t) = 0.99 p(t 1), where t is the current loop iteration. COMMON MISTAKE: In many cases, subtle bugs in the code led to reported statistics dierent from the reference code, even if the programs returned the correct answer for the smaller problems. For instance, due to typos or other errors, many programs would only eectively take only downhill steps, leading to 10-15 downhill steps to solve a problem. In the reference solution, the annealer takes both uphill and downhill steps correctly, and thus requires more downhill steps to oset the uphill steps that were taken. Another example of a subtle bug is that tiny.txt could be solved simply by random guessing from the initialization; solvers that solved tiny.txt, but not easy.txt or medium.txt, often did not actually move from the initial state. 6

2. Run your simulated annealing code on tiny.txt, easy.txt, and medium.txt, with T = 104 . Please run your algorithm at least three times on each of these T values, with dierent random seeds each time. Report whether the total number of steps for each run of the algorithm and whether or not the algorithm returned a solution, along with breakdowns into uphill and downhill steps. SOLUTION: To check this, we checked the numbers you reported against those reported by the reference solution. They had to be in the same ballpark as the reference solution, but not exact. Running the reference solution 10 times on each problem, we get the following statistics.
tiny.txt: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: easy.txt: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: SOLVED: 13 uphill, 8 downhill, 8 rejected 0 uphill, 0 downhill, 0 rejected 0 uphill, 0 downhill, 0 rejected 8 uphill, 4 downhill, 4 rejected 3 uphill, 2 downhill, 1 rejected 16 uphill, 7 downhill, 10 rejected 1 uphill, 1 downhill, 0 rejected 11 uphill, 4 downhill, 5 rejected 9 uphill, 5 downhill, 2 rejected 4 uphill, 2 downhill, 1 rejected

65 61 55 61 75 64 64 62 63 70

uphill, uphill, uphill, uphill, uphill, uphill, uphill, uphill, uphill, uphill,

66 55 60 67 58 56 73 60 65 62

downhill, downhill, downhill, downhill, downhill, downhill, downhill, downhill, downhill, downhill,

897 rejected 524 rejected 1735 rejected 943 rejected 1077 rejected 508 rejected 1337 rejected 969 rejected 1040 rejected 629 rejected

medium.txt: FAILED: 67 uphill, 80 downhill, 9852 rejected SOLVED: 54 uphill, 72 downhill, 5212 rejected FAILED: 72 uphill, 73 downhill, 9854 rejected FAILED: 59 uphill, 68 downhill, 9872 rejected FAILED: 60 uphill, 71 downhill, 9868 rejected FAILED: 62 uphill, 75 downhill, 9862 rejected FAILED: 71 uphill, 74 downhill, 9854 rejected SOLVED: 65 uphill, 72 downhill, 2066 rejected FAILED: 68 uphill, 88 downhill, 9843 rejected FAILED: 56 uphill, 66 downhill, 9877 rejected

You might also like