CS480/580: Introduction to Artificial Intelligence Fall 2009 (Lecture: R 1620-1900) Homework #1 Points: 20 Solution Question 1 [Points 3] Exercise 3 (Chapter 2 - Cawsey) (a) Semantic Network likes swimming eats grass Hippopotamus instance size lives in small herbert Edinburgh_zoo (b) Predicate logic: ∀ X hippo(X) →likes(X, swimming) ∀ X hippo(X) →eats(X, grass) hippo(herbert) size(herbert,small) lives(herbert, edinburgh_zoo) Question 2 [Points 3] Exercise 5 (Chapter 2 - Cawsey) (a) See below invest_stocks R2 savings_adequate AND R3 NOT has_children income_adequate R4 has_partner AND partner_has_job (b) (R5) IF income_huge THEN ADD income_adequate (R6) IF no_income AND buy_house THEN DELETE savings_adequate (R7) IF more_kids and partner_low_income THEN DELETE income_adequate Question 3 [Points 14] Download the movies.pl file and open it in SWI-Prolog using the consult/1 predicate. This file defines a database of facts of the following format (NB. Anything following '%' is treated as a comment in Prolog): % movie(M,Y) <- movie M came out in year Y movie(american_beauty, 1999). % director(M,D) <- movie M was directed by director D director(american_beauty, sam_mendes). % actor(M,A,R) <- actor A played role R in movie M actor(american_beauty, kevin_spacey, lester_burnham). % actress(M,A,R) <- actress A played role R in movie M actress(american_beauty, annette_bening, carolyn_burnham). Write queries to answer the following questions (NB. Press ; after each answer to see if there are any more answers in the database): 1. % In which year was the movie American Beauty released? movie(american_beauty, X). 2. % Find a movie that was released before 2000? movie(X, Y), Y < 2000, write(X), nl, fail. 3. % Find an actor who has appeared in more than one movie? actor(M1,A,_), actor(M2,A,_), \+ M1=M2, write(A), nl, fail. 4. % Find an actor or actress who has also directed a movie? % (Hint: to do this in a single query you will need to use disjunction (semicolon) as well as conjunction (comma). (actor(M1,A1,_),director(M2,A1)); (actress(M3,A2,_), director(M4,A2)). Write a definition in Prolog for each of the following predicates: 5. % released_since(M,Y) <- movie M was released after year X released_since(M,Y) :- movie(M,X), X>Y. 6. % released_between(M,Y1,Y2) <- movie M was released between year X and year Y inclusive released_since(M,Y1,Y2) :- movie(M,X), X>=Y1, X =< Y2. 7. % cast_member(A,M) <- person A was an actor or actress in movie M (Give as a two predicates) cast_member(A,M) :- actor(M, A, _). cast_member(A,M) :- actress(M,A,_). 8. % cast_member2(A,M) <- person A was an actor or actress in movie M(Give as a single predicate using the ; disjunction predicate) cast_member2(A,M) :- actor(M, A, _); actress(M,A,_). 9. % directed_by(X,Y) <- person X has been in a movie directed by Y % (Hint: re-use your cast_member/2 predicate) directed_by(X,Y) :- cast_member2(X,M), director(M,Y). Test your definitions by checking all answers to the appropriate query relation(X,Y). In some cases you will find unwanted answers of the form relation(X,X), but don't worry about these for now. What's the difference between these queries? 10. 11. 12. 13. ?- actor(M1,D,_),actor(M2,D,_). ?- actor(M1,D,_),actor(M2,D,_),M1\=M2. ?- actor(M1,D,_),actor(M2,D,_),M1@<M2. 10---D acted at least in one movie 11---D acted in at least two movies; both (Mi, Mj) as well as (Mj, Mi) pairs are selected---duplicate pairs selected. 12--- Same as 11 except that it avoids duplicate pairs being selected. Why do these queries return answers multiple times? 14. ?- director(_,D),actor(_,D,_). Because same male may have directed one or more movies and acted in one or more movies. 15. ?- director(_,D),actress(_,D,_). Because same female may have directed one or more movies and acted in one or more movies.
© Copyright 2026 Paperzz