COMP3411/9814 Artificial Intelligence
Artificial Intelligence
COMP3411/9814 Artificial Intelligence
Assignment 1 – Prolog and Search
Due: Friday 19 March, 10:00 pm
Marks: 20% of final assessment for COMP3411/9814 Artificial Intelligence
Part 1 - PrologIn this part, you are to write some Prolog programs. At the top of your 8ile, place a comment containing your full name, student
number and assignment name. You may add additional information like the date the program was completed, etc. if you wish. At the start of each Prolog predicate, write a comment describing the operation of the predicate.
Testing Your Code A signi8icant part of completing this assignment will be testing the code you write to make sure that it works correctly. To do this, you will need to design test cases that exercise every part of the code. You should pay particular attention to "boundary cases", that is, what happens when the data you are testing with is very small, or in some way special. For example:
• What happens when the list you input has no members, or only one member?
• Does you code work for lists with both even and odd numbers of members?
• Does your code work for negative numbers? Note: not all of these matter in all cases, so for example with sqrt_table, negative numbers don't have square roots, so it doesn't make sense to ask whether your code works with negative numbers. With each question, some example test data are provided to clarify what the code is intended to do. You need to design further test data. Testing, and designing test cases, is part of the total programming task.
It is important to use exactly the names given below for your predicates,
otherwise the automated testing procedure will not be able to 9ind your
predicates and you will lose marks. Even the capitalisation of your predicate
names must be as given below.
Question 1.1: List Processing
Write a predicate sumsq_even(Numbers, Sum) that sums the squares of only the
even numbers in a list of integers.
Example:
?- sumsq_even([1,3,5,2,-4,6,8,-7], Sum).
Sum = 120
Note that it is the element of the list, not its position, that should be tested for oddness.
(The example computes 2*2 + (-4)*(-4) + 6*6 + 8*8). Think carefully about how the
predicate should behave on the empty list — should it fail or is there a reasonable value
that Sum can be bound to?
To decide whether a number is even or odd, you can use the built-in Prolog operator N
mod M, which computes the remainder after dividing the whole number N by the whole
number M. Thus a number N is even if the goal 0 is N mod 2 succeeds. Remember that
arithmetic expressions like X + 1 and N mod M are only evaluated, in Prolog, if they
appear after the is operator. So 0 is N mod 2 works, but N mod 2 is 0 doesn't work.
Question 1.2: List Processing
Eliza was the name of the first “chatbot” written by Joseph Weizenbaum at MIT in the
mid-1960s. It pretended to be a psychiatrist, so that it only had to do simple
transformations on the input and turn a statement into a sentence. If a sentence is
represented by a list of words, an example of a simple transformation is:
?- eliza1([you,do,not,like,me], X).
X = [what,makes,you,say,i,do,not,like,you]
Here, the simple transformation is to put “What makes you say” in the front of the
sentence and replace “you” with “i” and “me” with “you”.
Write a Prolog program that takes a sentence in the form of a list and replaces any
occurrence:
you → i
me → you
my → your
and prepends the list [what, makes, you, say] to the transformed list.
Note 1: your predicate MUST be called “eliza1”. Don’t forget the “1”.
Note 2: To prevent trying to print lists that are accidentally too long, SWI Prolog
limits the number of elements in a list that it prints. You might see the answer to your
query ending with [a, b c | …]. You can force SWI Prolog to print longer lists with the
directive
:- set_prolog_flag(answer_write_options,[max_depth(0)]).
which you can put at the top of your file. max_depth(0) means no limit.
Question 1.3: List Processing
The rules in Question 1.2 work if “you” starts a sentence but won’t make much sense
for an example like this:
?- eliza1([i,wonder,if,you,would,help,me,learn,prolog], X).
X = [what,makes,you,say,i,if,wonder,i,would,help,you,learn,prolog]
What would be better is:
?- eliza2([i,wonder,if,you,would,help,me,learn,prolog], X).
X = [what,makes,you,think,i,would,help,you]
Write a predicate eliza2 (don’t forget the “2”) that takes a list of words:
[ …, you, , me, …]