Sample solutions made from Bryan Anderson`s solutions 22C:145 AI

advertisement
Sample solutions made from Bryan Anderson’s solutions
22C:145
AI HW3
1-7.17-A. If we negate G, this causes the last two literals to be not-C and not-D, which can then be used
to solve the second and third literals, leading to not-A and not-B. We can then resolve these against the
first clause to produce the empty clause.
1-7.17-B. Each 2-CNF clause has two places to put literals. There are 2n distinct literals, so there are (2n)2
syntactically distinct clauses. This includes semantically identical clauses. Since (A or B) is equivalent to
(B or A), (2n)2 is reduced to 2n2 +n. There are n clauses, i.e, (A or not A), that are equivalent to true. (And
2n clauses with repeated literals that are all distinct.) So there are 2n2+1 distinct clauses in all.
1-7.17-C. Resolving two 2-CNF clauses cannot increase the clause size; therefore, resolution can
generate only O(n2) distinct clauses before it must terminate.
1-7.17-D. When resolving two 3-CNF clauses, it can increase the clause size to 4, and so forth, so clause
size can grow at O(n) giving O(2n) possible clauses.
2-8.23-A. there is a => used with ∃. And it doesn’t restrict itself when x and y are not equal
¬∃ x, y, n Person(x) ∧ Person(y) ∧ ¬(x = y) ∧ [HasSS#(x, n) ∧ HasSS#(y, n)]
2-8.23-B. This is correct
2-8.23-C. This says everyone has every number.
∀ x, n Person(x) ∧ HasSS#(x, n) ⇒ Digits(n, 9)
2-8.23-D.
¬∃ x, y Person(x) ∧ Person(y) ⇒ [SS#(x) = SS#(y)]
SS#(John) = SS#(Mary)
∀ x Person(x) ⇒ Digits(SS#(x), 9)
3-8.28-A. Wrote( Gershwin, The Man I Love)
3-8.28-B. ¬Wrote( Gershwin, Eleanor Rigby)
3-8.28-C. Wrote(Gershwin, The Man I Love) v Wrote(McCartney, The Man I Love)
3-8.28-D. ∃s Wrote(Joe, s)
3-8.28-E. ∃x CopyOf( x, Revolver) ^ Owns( Joe, x)
3-8.28-F. ∀ s Sings(McCartney, s, Revolver) ⇒ W(McCartney, s)
3-8.28-G. ¬[∃ s Wrote(Gershwin, s) ∧ ∃ p Sings(p, s, Revolver)]
3-8.28-H. ∀ s Wrote(Gershwin, s) ⇒ ∃ p, a Sings(p, s, a)
3-8.28-I. ∃ a ∀ s Wrote(Joe, s) ⇒ ∃ p S(p, s, a)
3-8.28-J. ∃ d, a, s CopyOf(d, a) ∧ Owns(Joe, d) ∧ Sings(BHoliday, The Man I Love, a)
3-8.28-K. ∀ a [∃ s Sings(McCartney, s, a)] ⇒ ∃ d CopyOf(d, a) ∧ Owns(Joe, d).
3-8.28-L. ∀ a [∀ s, p Sings(p, s, a) ⇒ S(BHoliday, s, a)] ⇒ ∃ d CopyOf(d, a) ∧ Owns(Joe, d)
4-9.16-A.
sorted([]).
sorted([X]).
sorted([X,Y|L]) :- X<=Y, sorted([Y|L]).
4-9.16-B.
perm([],[]).
perm([X|L],M) :delete(X,M,M1),
perm(L,M1).
delete(X,[X|L],L). %% deleting an X from [X|L] yields L
delete(X,[Y|L],[Y|M]) :- delete(X,L,M).
member(X,[X|L]).
member(X,[_|L]) :- member(X,L).
sort(L,M) :- perm(L,M), sorted(M).
4-9.16-C.
4-9.16-D. it doesn’t work well as a program as it does as a specification, the worst case can end up being
O(n!n2).
4-9.16-E.
isort([],[]).
isort([X|L],M) :- isort(L,M1), insert(X,M1,M).
insert(X,[],[X]).
insert(X,[Y|L],[X,Y|L]) :- X=<Y.
insert(X,[Y|L],[Y|M]) :- Y<X, insert(X,L,M).
quicksort([X|Xs],Ys) :partition(Xs,X,Left,Right),
quicksort(Left,Ls),
quicksort(Right,Rs),
append(Ls,[X|Rs],Ys).
quicksort([],[]).
partition([X|Xs],Y,[X|Ls],Rs) :X <= Y, partition(Xs,Y,Ls,Rs).
partition([X|Xs],Y,Ls,[X|Rs]) :X > Y, partition(Xs,Y,Ls,Rs).
partition([],Y,[],[]).
append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).
5-6.7.


live(N, X), means X lives in house number N, where N is in { 1, 2, 3, 4, 5} and X in { englishman,
spinard, norweigian, ukrainian, japanese }
color(N, C), means the color of house number N is C, where N in { 1, 2, 3, 4, 5} and C in { red,
ivory, yellow, blue, green }



pet(N, P), means pet P lives in house number N, where N in { 1, 2, 3, 4, 5} and P in { dog, fox,
snail, horse, zebra }
snack(N, S), means snack S is consumed in house number N, where N in { 1, 2, 3, 4, 5} and S in {
hershey, kitkats, smarties, snickers, mickyways }
drink(N, D), means drink D is consumed in house number N, where N in { 1, 2, 3, 4, 5} and S in {
orangejuice, tea, coffee, milk, water }
S = [[C1,N1,P1,D1,S1],
[C2,N2,P2,D2,S2],
[C3,N3,P3,D3,S3],
[C4,N4,P4,D4,S4],
[C5,N5,P5,D5,S5]],
// Englishman lives in the red house
member([red, 'englishman', _, _, _], S),
// Spaniard owns the dog
member([_, 'spaniard', dog, _, _], S),
// Norwegian lives in the first house on the left
N1 = ‘norweigian’,
// The green house is immediately to the right of the ivory house
is_right_of( [green, _, _, _, _], [ivory, _, _, _, _], S),
// The man who eats Hershey bars lives in the house next to the man with the fox
next_to( [_, _, _, _, Hershey], [_, _, fox, _, _], S),
// Kit kats are eaten in the yellow house
member([yellow, _, _, _, kit kats], S),
// The Norwegian lives next to the blue house
next_to( [_, norweigan, _, _, _], [blue, _, _, _, _], S),
// The smarties eater owns snails
member([_, _, snail, _, smarties], S),
// The snickers eater drinks orange juice
member([_, _, _, orange juice, snickers], S),
// The Ukrainian drinks tea
member([_, ukrainian, _, tea, _], S),
// The Japanese eats milky ways
member([_, japanese, _, _, milky ways], S),
// Kit kats are eaten in a house next to the house where the horse is kept
next_to( [_, _, _, _, kit kats], [_, _, horse, _, _], S),
// Milk is drunk in the middle house
D3 = milk,
is_right_of(L, R, [L | [R | _]]).
is_right_of(L, R, [_ | Rest]) :- is_right_of(L, R, Rest).
next_to(X, Y, List) :- is_right_of(X, Y, List).
next_to(X, Y, List) :- is_right_of(Y, X, List).
live(N, X) :- member([_, X, _, _, _], S).
color(N, C) :- member([C, _, _, _, _], S),
pet(N, P) :- member([_, _, P, _, _], S),
snack(N, Sn) :- member([_, _, _, _, Sn], S),
drink(N, D) :- member([_, _, _, D, _], S),
% Who owns the zebra?
member([_, Who, zebra, _, _], S),
format("The ~w owns the zebra.", Who).
% Who drinks water?
member([_, Who, _, water, _], S),
format("The ~w drinks water.", Who
Download