CIS603 S03: Solutions for Homework 2

1. Search Methods

(30 points total)

(5 points) Suppose that we run a greedy search algorithm with h(n)=-g(n). What sort of search will the greedy search emulate?

(5 points) Come up with heuristics for the following problems. Explain whether they are admissible, and whether the state spaces contain local maxima with your heuristic: (a) path planning in the plane with rectangular obstacles, (b) maze problems, as defined in Chapter 3.

(10 points) 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 best-first search. What properties of best-first search do we give up if we only have a comparison method?

(10 points) Prove that if the heuristic function h obeys the triangle inequality, then the f-cost along any path in the search tree is nondecreasing. (The triangle inequality says that the sum of the costs from A to B and B to C must not be less than the cost from A to C directly).

  1. Setting h(n) to -g(n) means that the best nodes (the nodes with the lowest score) will be those with the longest path (the higher g(n), the lower -g(n)). Preferring long paths means depth-first-search. If we set h(n) = g(n) we get breadth first search.
  2. For path planning a reasonable heuristic is the straight line distance to the goal. This is admissible becasue the straight line distance is always less than or equal to the best path There are local maxima in this state space. For example, the graph we saw in class when we talked about A* search. Another example is a space where the start point is at the middle of a concave crescent moon shape, and the goalis just on the other side of the crescent. The heuristic will lead to a local maxima in the middle of the crescent when actually the best solution is to go around to the other side. Maze problems are very similar.
  3. If we assume the comparison function to be transitive, then we can always sort the nodes using it and choose the node that is at the top of the sorted list. The property we give up is run-time efficiency, as a priority queue is not as efficient when we only have a comparison operator not a numeric ranking.
  4. The triangle inequality applied to a heuristic funciton h says that h(n) <= k(n,n') + h(n'), for any node n and n', where k(n, n') is the cost of the shortest path from n to n'. Nondecreasing f-cost along a path means that the f-cost of a successor is always at least as large as that of the node itself, so f(n) <= f(n') if n' is in S(n). Rewriting this in terms of h and g gives g(n) + h(n) <= g(n') + h(n') if n' is in S(n). Our aim is to show that this is implied by the triangle inequality. To do this, add g(n) to both sides of the triangle inequality h(n) + g(n) <= k(n,n') + h(n') + g(n). If n' is a successor of n, then g(n') = g(n) + k(n,n'). Then g(n) + h(n) <= g(n') + h(n') which is what we need to prove.

2. Game Playing

(10 points)

There's a simple game you can play with silver dollars. (A silver dollar is a coin worth $1.) There are five (5) silver dollars on the table and you and your opponent take turns picking up 1, 2, or 3 coins until none is left. You get to keep each silver dollar you pick up. But, if you pick up the last coin, you have to pay $2 to your opponent. The objective of the game is to finish with the most money.

  1. Consider building a game tree to solve the above problem. What would each state in the game tree represent? What is/are the operator(s)?
  2. What cost function, i.e., static evaluation function, do you use to evaluate terminal nodes?
  3. Show the (complete) resulting minimax tree for this problem (on the back of this page or on the preceding page).
  4. Your opponent courteously offers to let you go first. If you acce pt, what is your first move? How much will you earn?
  5. Clearly indicate one example of an alpha-beta pruning in this tree. If there isn't one, say how you would rearrange the tree to facilitate pruning.
Solution (ps,pdf)

Note that the solution to the second to last problem is wrong in the above solution writeup. You should take 3 dollars finally earning 2 dollars.

3. Scheme

(60 points)
(define (RotateLeft L)
  (cond ((null? L) #f)
        (else (append (cdr L) (list (car L))))))

(define (RotateRight L)
  (cond ((null? L) #f)
        (else (append (list-tail L (- (length L) 1)) (list-head L (-
(length L) 1))))))

(define (RotateRight L)
        (cond ((null? L) L)
               (else
                     (append (list (car (reverse L)))

                     (reverse (cdr (reverse L)))))))

(define (Rotate L N)
  (cond ((null? L) #f)
        ((= N 0) L)
        ((> N 0) (Rotate (RotateRight1 L) (- N 1)))
        ((< N 0) (Rotate (RotateLeft1 L) (+ N 1)))
        )
  )
---------------------------------------------------------------------------
(define leaf?
  (lambda (obj)
    (and (not (null? obj))
         (not (pair? obj)))))

(define (countCellsRecursive L)
  (cond ((null? L) 1)                   ;if at a null, it means we're hit a ")",
 so + 1
        ((leaf? L) 1)                   ;+1 for every atom
        (else (+ (countCellsRecursive (car L)) (countCellsRecursive (cdr L))))))

(define (countCells L)            ;a dummy procedure used to subtract 1 from the
  (- (countCellsRecursive L) 1))  ;result of countCellsRecursive. We subtract 1
                                  ;to account for the ")" at the end of the
                                  ;outermost "()"

---------------------------------------------------------------------------
(define (BinarySearch Tree Goal)
  (cond ((null? Tree) #f)              ;empty tree (have reached a null leaf)
        ((null? Goal) #f)              ;null goal
        ((= Goal (car Tree)) Goal)     ;found node
        ((< Goal (car Tree)) (BinarySearch (car (car (cdr Tree))) Goal))
        ((> Goal (car Tree)) (BinarySearch (car (cdr (car (cdr Tree)))) Goal))))

;The tree representation I'm using is as follows:
;-All subtrees are a list unto themselves
;-The value stored at a particular node is the first value in the list of that t
ree
;-If a node does not have a left and/or right child, a null entry is stored ther
e.

;An Example
;(6 ((3 (() (5 (() ())))) (8 ((7 (() ())) (10 (() ()))))))
;
;                    _____6_____
;                 __3__       __8__
;                ()   _5_   _7_   _10_
;                    () () () () ()  ()



Switch to:


vasilis@cis.temple.edu