Suggested answers

advertisement
Suggested answers
This document contains suggested answers to selected questions from the exam in TDDC65
Artificial Intelligence and Lisp from 2008-03-28.
Question 1: Lisp programming (9p)
1a (3p) We have created a variable bowl with information about some fruits. What will the
Lisp interpreter return if we enter the following three expressions (exactly as stated) with
labels 2-4 in the example?
CL-USER(1): (setq bowl '((banana . 5) (apple . 8)))
((BANANA . 5) (APPLE . 8))
CL-USER(2): (cadr bowl)
(APPLE . 8)
CL-USER(3): (list (first (second bowl)) 'macintosh)
(APPLE MACINTOSH)
CL-USER(4): (* (cdar bowl) 3)
15
1b (3p) Write a Lisp function sum that computes the sum of all the numbers in an ordinary list.
Elements that are not numbers should be ignored. Assume that the list does not contain
sublists.
(defun sum (sequence)
(cond ((endp sequence) 0)
((numberp (first sequence))
(+ (first sequence) (sum (rest sequence))))
(t (sum (rest sequence)))))
1c (2p) Write a Lisp function sum-generic that is a more generic version of the previous
function. The second argument should be a predicate function to be applied to all numbers.
The function sum-generic should sum the numbers for which the predicate function returns
true.
(defun sum-generic (sequence pred)
(cond ((endp sequence) 0)
((and (numberp (first sequence))
(funcall pred (first sequence)))
(+ (first sequence) (sum-generic (rest sequence) pred)))
(t (sum-generic (rest sequence) pred))))
1d (1p) What should we give as second argument to sum-generic in order to sum all
numbers in the interval -5 to 5?
CL-USER(10): (sum-generic '(-10 -4 -1 0 2 4 20)
#'(lambda (x) (<= -5 x 5)))
1
Question 2: Logic (10p)
2a (2p) Construct a truth table for the following formula:
(P ∧ Q) ⇒ P
P
0
0
1
1
Q
0
1
0
1
P∧Q
0
0
0
1
(P ∧ Q) ⇒ P
1
1
1
1
2b (2p) Is the formula in 2a satisfiable? Is it valid? Motivate your answer using the truth table.
Yes, it is satisfiable, since there are at least one row in the truth table that is true for the
whole formula. Yes, it is valid, since in fact all rows in the truth table are true for the
whole formula.
2c (2p) Translate the following three sentences into suitable predicate formulas:
A: It never rains in southern California.
B: Oh, sure it does! Some winter days it rains, and now it’s late January.
C: You see, in the winter people always surf if it’s not raining, and the beach is
completely empty today.
One suitable translation (where B and C are two formulas each) would be:
A:
¬∃t[Rain(t)]
B:
∃t[Winter(t) ⇒ Rain(t)]
Winter(tnow)
C:
Winter(t) ∧ ¬Rain(t) ⇒ Surf(t)
¬Surf(tnow)
2d (1p) Does it rain today? Assume that the three people talking are actually in southern
California. Use informal reasoning to find out if it rains or not. (Ignore what A is saying, since
he is only quoting song lyrics.)
We know that it’s January (i.e. winter) and the beach is empty (i.e. nobody is surfing).
Assume that it is not raining. Then people would be surfing, according to C, but they
are not. Hence it must be raining.
2e (3p) Prove your claim from 2d using resolution. (Again, ignore what A is saying.)
Ignoring A, we have the following set of formulas
(1)
(2)
(3)
(4)
∃t[Winter(t) ⇒ Rain(t)]
Winter(tnow)
Winter(t) ∧ ¬Rain(t) ⇒ Surf(t)
¬Surf(tnow)
Remember that t is a variable and tnow is a constant. Our conclusion from the previous
question is
(5)
Rain(tnow)
In order to prove this using resolution, we must negate the conclusion and convert all
formulas to clause form.
∃t[Winter(t) ⇒ Rain(t)]
Winter(k) ⇒ Rain(k)
¬Winter(k) ∨ Rain(k)
where k is a Skolem constant
rewrite ⇒
(2)
Winter(tnow)
already clause form
(3)
Winter(t) ∧ ¬Rain(t) ⇒ Surf(t)
¬(Winter(t) ∧ ¬Rain(t)) ∨ Surf(t)
¬Winter(t) ∨ Rain(t) ∨ Surf(t)
rewrite ⇒
De Morgan
(4)
¬Surf(tnow)
already clause form
(5)
¬Rain(tnow)
already clause form
(1)
Now we can do resolution. This can be expressed graphically or in list form.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
¬Winter(k) ∨ Rain(k)
Winter(tnow)
¬Winter(t) ∨ Rain(t) ∨ Surf(t)
¬Surf(tnow)
¬Rain(tnow)
Rain(tnow) ∨ Surf(tnow)
Surf(tnow)
⊥
Now, we have proven that it is indeed raining.
(2) + (3), t = tnow
(5) + (6)
(4) + (7)
Question 3: Search (9p)
3a (2p) We are going to use the straight-line distance as a heuristic for this search problem.
What is a heuristic? Why is straight-line distance an admissible heuristic?
See section 4.1 in the course book.
3b (5p) Illustrate the different stages in A* search for the shortest route from Lidköping to
Karlsborg. In each stage, label the leaf nodes with g + h, i.e. the distance so far plus the
estimated distance to the goal.
See diagram on next page. See also section 4.1 in the course book, especially figure 4.3
on page 98 for an example of how to illustrate different stages of the search.
3c (2p) Assume that we have two search algorithms B and C. The description of the
algorithms say that B is O(kn) and C is O(n2). What could this mean (i.e. what does the O
thing mean)? Which algorithm is best?
See relevant parts of chapter 3 and appendix A.1 in the course book.
The following diagram illustrates the first five stages:
Lid
0+92 = 92
Lid
Ska
Göt
22+73 = 95
20+69 = 89
Lid
Ska
Göt
22+73 = 95
Ska
Mar
Lid
38+73 = 111
52+51 = 103
40+92 = 132
Lid
Ska
Göt
Lid
Göt
Skö
Ska
Mar
Lid
44+92 = 136
40+69 = 109
48+50 = 98
38+73 = 111
52+51 = 103
40+92 = 132
Lid
Lid
Göt
44+92 = 136
40+69 = 109
Ska
Göt
Skö
Ska
Mar
Lid
38+73 = 111
52+51 = 103
40+92 = 132
Mar
Ska
Tib
Hjo
86+51 = 137
74+73 = 147
69+27 = 96
79+27 = 106
In the last stage, Tibro is expanded, which gives us three sub-nodes (Skövde, Hjo and
Karlsborg). The Karlsborg node has the shortest distance, so there is no need to expand other
nodes to look for alternative routes to Karlsborg.
Download