qwertyagnesi
Desconectado
Mensajes: 13
|
Esta es la solucion para programada en prolog solo basta cambiar un par de variables pasra que quede mas bonito el codigo pero funca igual bien no? From: Rina Dechter, "Enhancement schemes for constraint processing: Backjumping, learning, and cutset decomposition", Artificial Intelligence, Volume 41, Number 3, Pages 273-312, 1990
There are five houses, each of a different colour and inhabited by men of different nationalities, with different pets, drinks and cigarettes. 1. The Englishman lives in the red house. 2. The Spaniard owns a dog. 3. Coffee is drunk in the green house. 4. The Ukrainian drinks tea. 5. The green house is directly to the right of the ivory house. 6. The Old-Gold smoker owns snails. 7. Kools are being smoked in the yellow house. 8. Milk is drunk in the middle house. 9. The Norwegian lives in the first house on the left. 10. The Chesterfield smoker lives next to the fox owner. 11. Kools are smoked in the house next to the house where the horse is kept. 12. The Lucky-Strike smoker drinks orange juice. 13. The Japanese smokes Parliament. 14. The Norwegian lives next to the blue house. Who drinks water and who owns the zebra? */
% Data
% house(Number, Color, Nationality, Drink, Smoke, Pet)
street([ house(1, _, _, _, _, _), house(2, _, _, _, _, _), house(3, _, _, _, _, _), house(4, _, _, _, _, _), house(5, _, _, _, _, _) ]).
% colors([red, green, ivory, yellow, blue]). % nationalities([english, spanish, ukrainian, norwegian, japanese]). % drinks([coffee, tea, milk, orange_juice, water]). % smokes([old_gold, kools, chesterfield, lucky_strike, parliament]). % pets([dog, snails, fox, horse, zebra]).
left(1, 2). left(2, 3). left(3, 4). left(4, 5).
right(X, Y) :- left(Y, X).
next(X, Y) :- left(X, Y); right(X, Y).
% Solution
solve(St) :- street(St),
% 1. The Englishman lives in the red house.
member(house(_, red, english, _, _, _), St),
% 2. The Spaniard owns a dog.
member(house(_, _, spanish, _, _, dog), St),
% 3. Coffee is drunk in the green house.
member(house(_, green, _, coffee, _, _), St),
% 4. The Ukrainian drinks tea.
member(house(_, _, ukrainian, tea, _, _), St),
% 5. The green house is directly to the right of the ivory house.
member(house(N5a, green, _, _, _, _), St), member(house(N5b, ivory, _, _, _, _), St), right(N5a, N5b),
% 6. The Old-Gold smoker owns snails.
member(house(_, _, _, _, old_gold, snails), St),
% 7. Kools are being smoked in the yellow house.
member(house(_, yellow, _, _, kools, _), St),
% 8. Milk is drunk in the middle house.
member(house(3, _, _, milk, _, _), St),
% 9. The Norwegian lives in the first house on the left.
member(house(1, _, norwegian, _, _, _), St),
% 10. The Chesterfield smoker lives next to the fox owner.
member(house(N10a, _, _, _, chestefield, _), St), member(house(N10b, _, _, _, _, fox), St), next(N10a, N10b),
% 11. Kools are smoked in the house next to the house where the horse is kept.
member(house(N11a, _, _, _, kools, _), St), member(house(N11b, _, _, _, _, horse), St), next(N11a, N11b),
% 12. The Lucky-Strike smoker drinks orange juice.
member(house(_, _, _, orange_juice, lucky_strike, _), St),
% 13. The Japanese smokes Parliament.
member(house(_, _, japanese, _, parliament, _), St),
% 14. The Norwegian lives next to the blue house.
member(house(N14a, _, norwegian, _, _, _), St), member(house(N14b, blue, _, _, _, _), St), next(N14a, N14b),
% Answer: member(house(_, _, _, _, _, zebra), St), member(house(_, _, _, water, _, _), St).
go :- solve(St), member(house(N1, C1, Na1, Dr1, Sm1, zebra), St), print('The owner of the zebra is the '), print(Na1), print(', who lives in house number '), print(N1), nl, print('(the '), print(C1), print(' house)'), print(', smokes '), print(Sm1), print(', and drinks '), print(Dr1), print('.'), nl,
member(house(N2, C2, Na2, water, Sm2, P2), St), print('The water drinker is the '), print(Na2), print(', who lives in house number '), print(N2), nl, print('(the '), print(C2), print(' house)'), print(', smokes '), print(Sm2), print(', and whose pet is a '), print(P2), print('.'), nl.
show :- solve(St), show_street(St).
show_street([]). show_street([H|T]) :- show(H), show_street(T).
show(house(Number, Color, Nationality, Drink, Smoke, Pet)) :- print('House '), print(Number), print(' is '), print(Color), print(', there lives the '), print(Nationality), print(', who drinks '), print(Drink), print(', smokes '), print(Smoke), (Pet = snails, print(', and has pet '); print(', and has a pet ')), print(Pet), print('.'), nl.
show_simple :- solve(St), show_simple(St).
show_simple([]). show_simple([H|T]) :- print(H), nl, show_simple(T). granjero.pl 0000644 0005253 0005253 00000005617 10252331767 013511 0 ustar noell noell 0000000 0000000 /* Un Granjero tiene un Lobo, una Cabra y un Repollo. Quiere cruzar el rio, pero solo hay un bote capaz de transportar al granjero y a una de sus pertenencias a la vez.
Idea: izquierda, derecha son las riberas del rio.
estado(Bote, Lobo, Cabra, Repollo) describe un estado del mundo. Estados legales son aquellos en que el Lobo no esta con la Cabra ni la Cabra con el Repollo, salvo que esten junto al Granjero (o sea, con el Bote). En resumen, son seguros todos aquellos estados en los cuales el Bote esta donde esta la Cabra, o Cabra y Lobo y Cabra y Repollo estan en orillas distintas. El estado inicial es estado(izquierda, izquierda, izquierda, izquierda), el final es que los ultimos 3 esten al lado derecho (la ubicacion del bote no importa).
Movidas posibles son todas aquellas que mueven al Bote y a lo mas a uno de los demas de la misma orilla del rio a la otra. */
/* Datos del problema */
% Auxiliar
opuesta(izquierda, derecha). opuesta(derecha, izquierda).
% Estados y sus caracteristicas
inicial(estado(izquierda, izquierda, izquierda, izquierda)). final(estado(_, derecha, derecha, derecha)).
seguro(estado(B, _, B, _)). seguro(estado(_, L, C, R)) :- opuesta(L, C), opuesta(C, R).
% Movidas legales
movida(estado(B1, L, C, R), estado(B2, L, C, R)) :- opuesta(B1, B2). movida(estado(B1, B1, C, R), estado(B2, B2, C, R)) :- opuesta(B1, B2). movida(estado(B1, L, B1, R), estado(B2, L, B2, R)) :- opuesta(B1, B2). movida(estado(B1, L, C, B1), estado(B2, L, C, B2)) :- opuesta(B1, B2).
% Usado para mostrar la solucion
muestre_movida(estado(B1, L, C, R), estado(B2, L, C, R)) :- write('El granjero cruza de '), write(B1), write(' a '), write(B2), write_ln('.'). muestre_movida(estado(B1, B1, C, R), estado(B2, B2, C, R)) :- write('El granjero cruza de '), write(B1), write(' a '), write(B2), write_ln(' con el lobo.'). muestre_movida(estado(B1, L, B1, R), estado(B2, L, B2, R)) :- write('El granjero cruza de '), write(B1), write(' a '), write(B2), write_ln(' con la cabra.'). muestre_movida(estado(B1, L, C, B1), estado(B2, L, C, B2)) :- write('El granjero cruza de '), write(B1), write(' a '), write(B2), write_ln(' con el repollo.').
/* El programa que resuelve el problema general de ir del estado inicial a alguno final via movidas legales pasando solo por estados seguros */
go :- inicial(E), resuelva(E, [E], H), muestre_solucion(H).
% resuelva(E, C, R) % E: Estado de partida % C: Camino recorrido hasta aca (en reversa) % R: Camino total hasta la solucion (en reversa)
resuelva(E, P, P) :- final(E). resuelva(E, P, H) :- movida(E, E1), not(member(E1, P)), seguro(E1), resuelva(E1, [E1|P], H). muestre_solucion([_]). muestre_solucion([E1, E2 | T]) :- muestre_solucion([E2|T]), muestre_movida(E2, E1).
|