/* Adjacent rooms in Picat. https://stackoverflow.com/questions/58468084/prolog-logic-with-adjacent-rooms """ Prolog Logic with adjacent rooms Here is my question: Hunter, Laura, Addiley (jack), Ramey(Sally), and Arnie(Jim) all live in the same dorm with five adjacent bedrooms. Hunter doesn’t sleep in the 5th bedroom and Laura doesn’t sleep in the first bedroom. Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent to Addiley or Laura. Ramey sleeps in some bedroom higher than Laura’s. Who sleeps in which bedrooms? Write a Prolog program to solve this problem. Define what adjacency is, then what the bedrooms are, and then create a layout(X) that allows you to put in all the rules. This is the code I have so far, I have tried a few variations of this as well: adjcnt(X,Y) :- X = (Y+1;Y-1). rooms([bedroom(, 1), bedroom(, 2), bedroom(, 3), bedroom(, 4), bedroom(_, 5)]). layout(X) :- rooms(X), member(bedroom(hunter, V), X), member(bedroom(laura, W), X), member(bedroom(arnie, X), X), member(bedroom(ramey, Y), X), member(bedroom(addiley, Z), X), V\= 5, W\= 1, X\= 1, X\= 5, X\= adjcnt(X,Z), X\= adjcnt(X,W), Y@>W. The main issue being, am I accounting for adjacent rooms correctly? and how do I implement that correctly. I continually am getting "NO" when I try to run the code. Thank you to anyone who can assist me!! """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. %% %% Using cp. %% go ?=> %% Hunter, Laura, Addiley (jack), Ramey(Sally), and Arnie(Jim) all live in the same dorm with five %% adjacent bedrooms. %% Hunter doesn’t sleep in the 5th bedroom and %% Laura doesn’t sleep in the first bedroom. %% Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent %% to Addiley or Laura. %% Ramey sleeps in some bedroom higher than Laura’s. %% Who sleeps in which bedrooms? %% Write a Prolog program to solve this problem. N = 5, L = [Hunter, Laura, Addiley, Ramey, Arnie ], %% In what bedroom sleeps each person? S = [hunter, laura, addiley, ramey, arnie ], L :: 1..N, all_distinct(L), %% Hunter doesn’t sleep in the 5th bedroom and Hunter #\= 5, %% Laura doesn’t sleep in the first bedroom. Laura #\= 1, %% Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent %% to Addiley or Laura. Arnie #\= 1, Arnie #\= 5, abs(Arnie-Addiley) #> 1, abs(Arnie-Laura) #> 1, %% Ramey sleeps in some bedroom higher than Laura’s Ramey #> Laura, %% writeln(before_label=L), solve(L), writeln(l=L), assignment(L,Rooms), writeln(rooms=Rooms), Is = 1..N, Sol = findall([Room,Person], (member(Room,Is), nth(Room,L,PersonN), nth(PersonN,S,Person) )), writeln(sol=Sol), fail, nl. go => true. %% %% Using select/3 to generate the distinct person <-> room %% go2 ?=> People = [1,2,3,4,5], select(Hunter,People,People1), select(Laura,People1,People2), select(Addiley,People2,People3), select(Ramey,People3,People4), select(Arnie,People4,_People5), L = [Hunter, Laura, Addiley, Ramey, Arnie ], S = [hunter, laura, addiley, ramey, arnie ], %% Hunter doesn’t sleep in the 5th bedroom and Hunter \= 5, %% Laura doesn’t sleep in the first bedroom. Laura \= 1, %% Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent %% to Addiley or Laura. Arnie \= 1, Arnie \= 5, abs(Arnie-Addiley) > 1, abs(Arnie-Laura) > 1, %% Ramey sleeps in some bedroom higher than Laura’s Ramey > Laura, writeln(L), Sol = findall([Room,Person], (member(Room,1..5), nth(Room,L,PersonN), nth(PersonN,S,Person) )), writeln(sol=Sol), fail, nl. go2 => true. %% %% Using permutation/2 %% go3 ?=> People = [1,2,3,4,5], permutation(People,[Hunter, Laura, Addiley, Ramey, Arnie ]), L = [Hunter, Laura, Addiley, Ramey, Arnie ], S = [hunter, laura, addiley, ramey, arnie ], %% Hunter doesn’t sleep in the 5th bedroom and Hunter \= 5, %% Laura doesn’t sleep in the first bedroom. Laura \= 1, %% Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent %% to Addiley or Laura. Arnie \= 1, Arnie \= 5, abs(Arnie-Addiley) > 1, abs(Arnie-Laura) > 1, %% Ramey sleeps in some bedroom higher than Laura’s Ramey > Laura, writeln(L), Sol = findall([Room,Person], (member(Room,1..5), nth(Room,L,PersonN), nth(PersonN,S,Person) )), writeln(sol=Sol), fail, nl. go3 => true. %% adjcnt(X,Y) => X = (Y+1;Y-1). % original adjcnt(X,Y) => X is Y+1; X is Y-1. %% hakank: I added "_" that was missing in the question (probably a rendering thing) rooms(Rooms) => Rooms = $[bedroom(_, 1), bedroom(_, 2), bedroom(_, 3), bedroom(_, 4), bedroom(_, 5)]. %% %% The code in the question %% go4 ?=> %% rooms(X), % orig rooms(Rooms), member($bedroom(hunter, V), Rooms), member($bedroom(laura, W), Rooms), member($bedroom(arnie, X), Rooms), member($bedroom(ramey, Y), Rooms), member($bedroom(addiley, Z), Rooms), V\= 5, W\= 1, X\= 1, X\= 5, %% X\= adjcnt(X,Z), % orig not adjcnt(X,Z), %% X\= adjcnt(X,W), % orig not adjcnt(X,W), Y@>W, writeln(Rooms), writeln([[V,W,X,Y,Z]]), fail. go4 => true.