CSC 261 Grinnell College Fall, 2007 Artificial Intelligence

Programming and Projects

The accompanying table indicates programming assignments, projects, and due dates for Computer Science 261 and are subject to the following notes.

• Unless otherwise indicated, textbook references are to Stuart J. Russell and Peter Norvig, Artificial Intelligence: A Modern Approach (Second Edition), Prentice-Hall, 2003.

• Details for programming exercises and projects are given later on this page.

• Unless otherwise stated,

• collaboration IS NOT allowed on assignments either from the text or from the supplemental problems, but
• collaboration IS allowed on programming projects.

• You always may consult the instructor regarding questions on labs or assignments (at least if the instructor is in his office with the door open).

Due Date Collaboration
Allowed
Problems

Fri., Sept. 7 Yes Programming Problem 1
Mon., Sept. 10 No Programming Problem 2
Fri., Sept. 21 Yes Project 1
Mon., Oct. 8 Yes Programming Problem 3
Wed., Oct. 10 No Programming Problem 4
Mon., Oct. 15 Yes Project 2
Wed., Nov. 7 Yes Expert Systems Lab
Mon., Nov. 19 Yes Project 3
Wed., Dec. 5 Yes Neural Network Lab
Mon., Dec. 10 Yes Project 4

• Since every programming task should yield readable and carefully tested code, the grading of all programs for this course will begin with the following algorithm (expressed in C/Java format):

```
|| (no_evidence_of_compilation)
|| (no_test_runs)
|| (no_commentary_on_correctness))
```
• When a program is submitted, it should be understood that the program listing produces any output submitted. Editing of the output or turning in output that does match a program may raise questions of academic dishonesty; and, by College policy, any evidence of academic dishonesty must be turned over to the Academic Standing Committee for action.

Programming Exercises

1. Given a multiplicative expression in list form, write a LISP procedure simplify-1 to reformat and simplify the expression, as follows:

• all numbers in the expression should be multiplied,
• if the product of the numbers is 0, the entire product returned should be 0,
• if the product of the numbers is 1, the expression returned should be the product symbol *, followed by all symbols and list expressions with all numbers removed,
• if the product of the numbers is neither 0 nor 1, then the expression returned should be the product symbol *, followed by the numeric product and then all symbols and list expressions.

Note: You need not evaluate nested lists in this procedure.

Examples:

```
(simplify-1 '(* 3 a b (- x y) 0 5 r 8 z))         ; product 0
==> 0

(simplify-1 '(* 3 a b (- x y) 1/120 5 r 8 z))     ; product 1
==> (* a b (- x y) r z)

(simplify-1 '(* 3 a b (- x y)  5 r 8 z))          ; product of 3 5 8 is 120
==> (* 120 a b (- x y) r z)

(simplify-1 '(* 3 a b (- x y)  -5 r 8 z))         ; product is -120
==> (* -120 a b (- x y) r z)

(simplify-1 '(* 3 a b (- x y) (- 3 3) 5 r 8 z))   ; (- 3 3) not evaluated
==>  (* 120 a b (- x y) (- 3 3) r z)

```
2. Write a LISP procedure same-term that takes two multiplicative LISP expressions as parameters, and returns true (T) if they are the same (except for leading numeric coefficient), and returns false (NIL) otherwise.

Examples:

```
(same-term '(* 3 x y) (* -2 x y)) ==> T
(same-term '(* 3 x y) (* -2 y x)) ==> T
(same-term '(* 3 x y) (* -2 x z)) ==> NIL
(same-term '(* 3 x y) (* -2 x)) ==> NIL
```
3. Write Prolog predicates to solve the following three problems

1. double has two list parameters and returns true if the second parameter is obtained from the first by repeating each element in succession, as each appears. Examples:

double([a, b, c], [a, a, b, b, c, c]). yields yes
double([a, b, c], [a, b, c, a, b, c]). yields no
double([1, 2, 3, 4]), Y). yields Y = [1, 1, 2, 2, 3, 3, 4, 4]
double([], Y). yields Y = []

2. insert has three parameters, an item and two lists, and returns true in the second list may be obtained by inserting the item at some position of the first. Examples:

• insert(1, [a, b], [1, a, b]). insert(1, [a, b], [a, 1, b]). and insert(1, [a, b], [a, b, 1]). return yes.

• insert(1, [a, b], [b, 1, a]). returns no, because a comes before b in the first list but after in the second.

3. ordered has a single list (of numbers) as parameter and returns true if the numbers are in ascending order. Examples:

ordered([]). yields yes (no numbers out of order)
ordered([1]). yields yes
ordered([1, 2, 3]). yields yes
ordered([1, 3, 2, 4]). yields no

Note: You may not use the built-in sort predicate for this problem.

4. More Practice with Prolog:

1. Write a Prolog predicate nextItem that has three parameters: a list, and two items. The predicate is true if the second item follows immediately after the first on the given list. Examples:

nextItem([1,2,3,4], 1, 2), nextItem([1,2,3,4], 2, 3), and nextItem([1,2,3,4], 3, 4) all yield yes.
nextItem([1,2,3,4], 1, 3) yields no because 3 does not follow immediately after 1 on the list — there is a 2 between them. nextItem([1,2,3,4], 1, 5) yields no because 5 is not on the list at all.

2. Consider these rules that determine whether one ordered list (L3) of integers is obtained as the merge of two other ordered lists (L1 and L2).
```
merge([],Y,Y).
merge(X,[],X).
merge([H|T1], [H|T2], [H,H|T3]) :- merge(T1, T2, T3).
merge([H1|T1], [H2|T2], [H1|T3]) :- merge(T1, [H2|T2], T3), H1<H2 .
merge([H1|T1], [H2|T2], [H2|T3]) :- merge([H1|T1], T2, T3), H1>H2 .
```

Draw a full search tree to show the processing involved for the query merge(X,Y,[1,2,3]).

Projects

Initial planning for the course anticipates four programming-based projects.

1. Symbolic differentiation and expression simplification (using LISP)
2. A simplified version of the Eliza program, initially developed by Joseph Weizenbaum (using Prolog)
3. An expert system to aid in the placement of incoming students in statistics (using the TMYCIN, a LISP-based inference engine by Gordon Novak at the University of Texas at Austin)
4. A neural network to address the same statistics-placement problem (using the NevProp system by Phil Goodman at the University of Nevada at Reno)

Students may collaborate in pairs on each project. (Groups of more than 2 may be considered only with prior approval of the instructor.)

Project 1: Symbolic Differential and Expression Simplification>

Consider valid algebraic LISP expressions defined recursively as follows:

• Any number is a valid algebraic LISP expression.
• Any symbol is a valid algebraic LISP expression.
• If X is any valid algebraic LISP expression, then (sin X), (cos X), and (exp X) are also valid algebraic LISP expressions, where sin and cos are the standard trigonometric functions and (exp X) represents the exponential function eX.
• If X1, X2, X3, ... , Xn are valid algebraic LISP expressions (for n a positive integer), then so are
(+ X1 X2 X3 ... Xn)
(- X1 X2 X3 ... Xn)
(* X1 X2 X3 ... Xn)
(/ X1 X2)

Note that the operations + (addition), - (subtraction), and + (multiplication) are defined in this problem for any (positive) number of arguments. For the purposes of this problem, however, / (division) is defined only for exactly two arguments.

Basic Problem: Write a LISP function diff-basic that takes two parameters, a valid algebraic LISP expression VALP and a symbol X, and that returns the (unsimplified) derivative of VALP with respect to X. The result of VALP must again be a valid algebraic LISP expression. To emphasize the note in the first sentence, diff-basic need not perform any simplifications of any kind. Thus, (diff-basic '(* 2 X) 'X) might return (+ (* 2 1) (* X 0)) and (diff-basic '(* 2 X Y) 'X) might return (+ (* 2 (+ (* X 0) (* Y 1))) (* (* X Y) 0))).

Refined Problem: Write a LISP function diff-better that takes the same two parameters as diff-basic, differentiates the given expression, and performs at least three of the following simplifications:

• Reduction of multiplicative terms to 0 if any term is 0
• Combination of numbers in a multiplicative expression to yield a single number (similar to simplify-1 in the first programming assignment.
• Removal of terms of 0 from addition or subtraction expressions.
• Combination of numbers in an additive expression to yield a single number.
• Combination of like symbolic terms by combining coefficients (e.g., (+ (* 2 x y) (* 3 y x)) combines to yield (* 5 x y).
• Combination of additive terms nested within an additive expression. Thus, (+ 2 (+ x 3 y) z) simplifies to (+ 2 x 3 y z) , but of course (+ 2 (* x (+ 3 y)) z) is not easily simplified.
• Combination of multiplicative terms nested within a multiplicative expression. Thus, (* 2 (* x 3 y) z) simplifies to (* 2 x 3 y z).

Additional simplifications may be considered, but check with the instructor first (e-mail: walker@cs.grinnell.edu).

Extra Credit is possible on this project if more than 3 types of simplifications are performed within diff-better.

Project 2: A Simplified Eliza Program

One of the early, well-known programs within the field of artificial intelligence was called Eliza, a program by Joseph Weizenbaum. The idea was to simulate conversation between a patient and psycho-therapist, based on a series of patterns and corresponding responses. Although the original program had an extensive sequence of patterns, this assignment focuses on the following five rules:

Pattern     Response Template
---1 i am ---2 i am sorry to hear you are ---2
---1 am i ---2 do you believe you are ---2
---1 you ---2 me --- 3 why do you think i ---2 you
---1 in what way

In this table, the blank spaces in a pattern, denoted by subscripted dashes, can be filled in with any sequence of words. Similarly, singleWord represents exactly one word in a sentence. For example, these patterns might generate the following "conversation," where the responses are indented and in italics:

Sample Dialogue:

well my friend made me come here
he says i am depressed
i am sorry to hear you are depressed
i think i need help
in what way
i wonder if you would help me learning to program Scheme
when i registered for the course i wondered am i crazy
do you believe you are crazy

Each of these statements and responses follows the template/response patterns given. For example, in the first statement:

well my friend made me come here

the presence of the word `my` near the start of the sentence with the word `me` later on corresponds to the first pattern, with the following matches:

Pattern Element     Words from this Sentence
---1 well
my my
singleWord friend
me me
---3 come here

After identifying these pattern elements for the first pattern, the Eliza program uses the corresponding response template. In this case, the program prints "Tell me about your friend". The first four of these words come directly from the template. For the final word, singleWord in the template was matched with the word "friend" in the above analysis.

Historical Note

Although this approach may seem simple today, Joseph Weizenbaum used this approach as the basis for his widely heralded Eliza in 1966. In 1976, Weizenbaum noted he "was startled to see how quickly and how deeply people conversing with [ Eliza] became emotionally involved" in the conversation. People shared their hopes and secrets, and they became annoyed if other people looked over their shoulders or otherwise interrupted. Even when they knew Eliza was a program, they often talked as if it were a close personal friend.

Project 2 Assignment — Due: Monday, October 15

Write a PROLOG program to solve this Eliza-based pattern-matching problem. In the interests of time, we will largely ignore niceties of I/O for this project. Thus, you are not allowed to use such I/O predicates as get, put, read, or write. Rather, you should enter a sentence directly as a list, within the context of Prolog query. For example,

```eliza ([well, my, friend, made, me, come, here], Response).
```

Project 3: A Expert System for the Statistics Placement Problem

The current rule-base to place incoming students in computer science and mathematics courses may be found at ~walker/261/tmycin/newstudents.lsp. After copying this program to your account, expand it to include statistics placements, following the Placement Cases for Statistics as outlined in supplemental problem 2 fof this course.

Notes:

• Refinements of this assignment are given in green
• Possible statistics placements are:
• unknown
• 115
• 115/133
• 115/131
• 209
• 209/133
• 309/310
In this context,
• 115/131 will be interpreted as indicating that the student might take MAT 115. However, the student is encouraged to take MAT 131 first and then jump directly to 209 rather than 115.
• 115/133 will be interpreted as indicating that the student might take MAT 115. However, the student is encouraged to take MAT 131 and MAT 133 and then jump directly to 209 rather than 115.
• 209/133 will be interpreted as indicating that the student might take MAT 209. However, this placement also may encourage the student to take through MAT 133 now and then jump directly to 309/310 (omitting MAT 209 completely).
• The above placements are listed in ascending order of preparation. If a student has adequate background for several placements, the student should be placed in the highest of the relevant courses.

• You should assume that newstudents.lsp already gives appropriate computer science and mathematics placements, and you are free to use those placements in your rules for statistics.

• newstudents.lsp classifies ACT/SAT scores into ranges, giving attribute stdscores values superior, high, good, fair, poor, low, and unknown. For the purposes of this problem, you should consider an ACT of 29 or higher as being superior or high for attribute stdscores. (This is off by 1 on the ACT scores from the Placements Cases for Statistics, but this categorization will be considered appropriate for this exercise.)

• As with computer science and mathematics placements, your expert system should give a placement for every student; use unknown if all else fails!

Project 4: A Neural Network for the Statistics Placement Problem

Develop a neural network to solve the statistics placement problem.

Several scripts may help with data sets for setting up this problem:

• Input:Form placement-data-collection.shtml provides a mechanism for members of the class to enter sample records for training and testing a neural network.
• Output: Output scripts present data in a form that can be used by the NevProp simulator. Differences in the output scripts relate to columns displayed and sample means.
• Script student-data-1.php retrieves all sample records, using a mean of 500 for an unavailable SAT score, 25 for an unavailable ACT score, 3 for any unavailable AP score, 4 for any unavailable IB score, 6 as semesters of English or Math, 2.5 for all grade averages, and 1 for semesters of each course.
• Script student-data-2.php retrieves all sample records, using a mean of 500 for an unavailable SAT score, 25 for an unavailable ACT score, 1 for any unavailable AP score, 2 for any unavailable IB score, 6 as semesters of English or Math, 2.5 for all grade averages, and 1 for semesters of each course.
• Script student-data-3.php excludes all IB scores, using a mean of 500 for an unavailable SAT score, 25 for an unavailable ACT score, 3 for any unavailable AP score, 6 as semesters of English or Math, 2.5 for all grade averages, and 1 for semesters of each course.
• Script student-data-4.php excludes all IB scores, using a mean of 500 for an unavailable SAT score, 25 for an unavailable ACT score, 1 for any unavailable AP score, 6 as semesters of English or Math, 2.5 for all grade averages, and 1 for semesters of each course.

This document is available on the World Wide Web as

```http://www.walker.cs.grinnell.edu/courses/261.fa07/projects.shtml
```