CS440 Fall 2001
Assignment 2

Due 5:00 PM, Thursday, Sept. 27th
Department of Computer Science
Link
 to Colorado State University Home 
 Page


Uninformed Tree Search

Provided Lisp Code

For this assignment, you will implement and apply breadth-first and depth-first search algorithms. Implement both algorithms using a single function named tree-search as discussed in class. Here is assign2-start.lisp, a lisp file containing the tree-search code and an example application on the shortest path problem from Graham's book. When you load assign2-start.lisp you will see
CL-USER(163): :ld assign2-start
; Loading /s/parsons/e/fac/anderson/cs440/development/assign2-start.lisp


Example map search.
Map is ((A B C D Z) (B E F) (C G H) (D I J) (E K Z))
Path from A to Z is (A B E Z).

Example robot searches.  World is 5x5.
 Path from (1 1) to (4 4) is ((1 1) (1 2) (1 3) (1 4) (2 4) (3 4) (4 4)).
 Path from (3 2) to (1 4) is ((3 2) (3 3) (3 4) (2 4) (1 4)).

What You Must Do

Requirements for this assignment are the following. First, here are the easy steps.
  1. The A to Z problem doesn't find the shortest path. Write a new function named pathproblem4 by modify pathproblem3 so that the shortest path is found. Explain how you did this.
  2. The robot currently can only step north (N), south (S), east (E), or west (W). To make this more realistic, we have decided to allow it to also move diagonally, in NE, SE, SW, and NW directions. Create a new function named robot-xy-diagonal that does this. The only change needed will be in the successors code. You must generate successors in the order N, NE, E, SE, S, SW, W, then NW, so that our automatic grading function will detect the correct answer.
  3. We really want movement commands to send to the robot, not just new positions. Write a new function named robot-xy-commands by first copying robot-xy-diagonal then modifying it so what it returns is not a list of locations, but a list of directions: N, NE, E, SE, S, SW, W, or NW.
You are free to define as many additional functions as you like in order to solve these steps. We will test your code using calls exactly like the ones at the end of the assign2-start.lisp file using the function names specified above in each item, so make sure your lisp files runs the tests correctly when we load your lisp file. We will also test your functions with several other calls to your functions.

Now for the harder steps. You are going to solve a search problem very much like Exercise 3.16 in our Russell & Norvig book for which the next number in a sequence is predicted by searching for and finding a mathematical expression that correctly generates the given sequence. For example, the next number in the sequence 2, 4, 6 is 8. An expression of the form that meets the specification in Exercise 3.16 is (+ (+ (+ 1 N) N) 1) . Your predict function must return two values, using the values function. Your result should look like this:

CL-USER(6): (predict '(2 4 6))
(LAMBDA (N) (IGNORE-ERRORS (+ (+ (+ 1 N) N) 1)))
8
Details of this problem and clues to its solution will be discussed in class. Use the tree-search function from above.

Warning: Do not try to predict anything to complex. Your code will never stop. Don't forget to use control-C control-C in emacs to terminate execution of your lisp function.

Do and answer the following items.

  1. Write a recursive function called all-ways-to-replace-atoms that returns a list of all expressions possible from all allowed ways of replacing terms in a given expression. For example,
    CL-USER(66): (print (all-ways-to-replace-atoms '(- n 1)))
    ((- (+ N 1) 1) (- (+ N N) 1) (- (- N 1) 1) (- (- 1 N) 1) (- (- N N) 1)
     (- (- N N) 1) (- (* N N) 1) (- (/ 1 N) 1) (- (/ N N) 1) (- (/ N N) 1)
     (- (EXPT N N) 1) (- (EXPT N N) 1) (- N (+ 1 1)) (- N (+ 1 N)) (- N (- 1 1))
     (- N (- 1 1)) (- N (- 1 N)) (- N (- N 1)) (- N (* 1 N)) (- N (/ 1 1))
     (- N (/ 1 N)) (- N (/ N 1)) (- N (EXPT 1 N)) (- N (EXPT N 1)))
    
    print was called to force lisp to print the entire list. Ways to generate this will be discussed in class. You will be calling this function from your successors code for this problem. You must generate the list of successors in the same order as the example above shows so our automatic answer checking function will detect the correct answer.
  2. Write the function predict that solves the prediction problem using a breadth-first search. predict must return two values using the values function. The first value must be the lambda function that is evaluated for a given value of N to produce the corresponding value in the sequence. The second value returned is the predicted next value in the sequence.
  3. Write a function named do-predict-problem that calls predict and produces output exactly like this (and returns nil):
    CL-USER(69): (do-predict-problem '(2 4 6))
    
    The next value in sequence (2 4 6) is 8 
     using function (LAMBDA (N) (IGNORE-ERRORS (+ (+ (+ 1 N) N) 1)))
    NIL
    
Now here are a few questions to answer. Answer these by typing comments at the end of your lisp file. Number the answers using the numbers listed here. You may type a long multi-line comment by preceding the text with #| and following it with |#
  1. After you have written robot-xy-diagonal, rerun the same search problems you had run before and compare your new answers to your old ones. Are the resulting paths any different? Why?
  2. What is the danger of using a depth-first search for the prediction problem?
  3. Think of and describe at least one somewhat goal-directed way to guide the choice of the next expression to expand. You do not need to write any lisp code for this question.

Grading

Put all of your lisp code and commented answers to questions in a file named assignment2.lisp and electronically check it in when you are ready. We will be automatically checking the values returned by your functions. The lisp code we will use to do this is available here and will produce the following output, assuming you have already loaded your assignment2.lisp file. The actual tests we perform will be different than the ones shown above.
CL-USER(101): :ld assign2-answer
:ld assign2-answer
; Loading
;    /s/bach/a/class/cs440/public_html/assignments-answers/assign2-answer.lisp
CL-USER(102): :ld assign2-answer-checker
; Loading
;    /s/bach/a/class/cs440/public_html/assignments-answers/assign2-answer-checker.lisp


  (PATHPROBLEM4 'A 'Z BIGMAP) evals to
  (A Z)
----------Problem 1 is Correct

  (ROBOT-XY-DIAGONAL '(3 2) '(1 4) 5) evals to
  ((3 2) (2 3) (1 4))
----------Problem 2 is Correct

  (ROBOT-XY-COMMANDS '(3 1) '(1 4) 5) evals to
  (N NW NW)
----------Problem 3 is Correct

  (ALL-WAYS-TO-REPLACE-ATOMS 'A) evals to
  ((+ A 1) (+ A N) (- A 1) (- 1 A) (- A N) (- N A) (* A N) (/ 1 A) (/ A N)
   (/ N A) (EXPT A N) (EXPT N A))
----------Problem 4A is Correct

  (ALL-WAYS-TO-REPLACE-ATOMS '(+ A B)) evals to
  ((+ (+ A 1) B) (+ (+ A N) B) (+ (- A 1) B) (+ (- 1 A) B) (+ (- A N) B)
   (+ (- N A) B) (+ (* A N) B) (+ (/ 1 A) B) (+ (/ A N) B) (+ (/ N A) B)
   (+ (EXPT A N) B) (+ (EXPT N A) B) (+ A (+ B 1)) (+ A (+ B N))
   (+ A (- B 1)) (+ A (- 1 B)) (+ A (- B N)) (+ A (- N B)) (+ A (* B N))
   (+ A (/ 1 B)) (+ A (/ B N)) (+ A (/ N B)) (+ A (EXPT B N))
   (+ A (EXPT N B)))
----------Problem 4B is Correct

  (ALL-WAYS-TO-REPLACE-ATOMS '(- N 1)) evals to
  ((- (+ N 1) 1) (- (+ N N) 1) (- (- N 1) 1) (- (- 1 N) 1) (- (- N N) 1)
   (- (- N N) 1) (- (* N N) 1) (- (/ 1 N) 1) (- (/ N N) 1) (- (/ N N) 1)
   (- (EXPT N N) 1) (- (EXPT N N) 1) (- N (+ 1 1)) (- N (+ 1 N))
   (- N (- 1 1)) (- N (- 1 1)) (- N (- 1 N)) (- N (- N 1)) (- N (* 1 N))
   (- N (/ 1 1)) (- N (/ 1 N)) (- N (/ N 1)) (- N (EXPT 1 N))
   (- N (EXPT N 1)))
----------Problem 4C is Correct

  (SECOND (LIST (LAMBDA (N) (IGNORE-ERRORS (+ 1 N))) 4)) evals to
  4
----------Problem 5 is Correct

Problem 6.:

The next value in sequence (2 3 4) is 5 
 using function (LAMBDA (N) (IGNORE-ERRORS (+ (+ 1 N) 1)))
CL-USER(103): 
The all-ways-to-replace-atoms in Problems 4a and 4b is called with arguments that will not appear in your predictions problems. They are included here just to clearly demonstrate the order in which expressions should be generated by your code.