I’ll preface this with the prompt I have to follow:
Bobby went to Truman Theaters. Taking a quick glance around, he can see that a different movie is playing on each screen (1 through 10) and that each film is a different genre (one is a spy thriller). From the information provided, can you determine the genre of the movie playing on each screen?
- The screen on which Brisbane is playing is numbered exactly 1 higher than the screen on which the historical epic is showing but exactly 1 lower than the screen on which Long Shot is playing.
- Jet Set is playing on a higher-numbered screen than Pigeon but a lower-numbered screen than the Western.
- The musical is playing on screen 4.
- The screen on which the comedy is playing is numbered exactly 1 higher than the screen on which The Gentleman is showing but exactly 1 lower than the screen on which Frostfire is playing.
- The Gentleman is not a science-fiction movie.
- The horror movie is playing on the screen numbered exactly 1 higher than the screen on which Newton’s Laws is showing.
- Vulture Gulch is playing on the screen numbered exactly 1 higher than the screen on which the documentary is showing.
- The documentary is playing on a higher-numbered screen than the comedy.
- The screen on which Ellipsis is playing is numbered exactly 1 higher than the screen on which the action flick is showing but exactly 1 lower than the screen on which the science-fiction movie is playing.
- The action film, the historical epic, and the romance movie are playing on screens 1, 2, and 3, in some order; one of these films is Tower of London.
- The difference between the numbers of the screen on which Newton’s Laws and Vulture Gulch are playing is the same as the difference between the numbers of screen on which the Gentleman and Tower of London are showing.
- We use the variables Brisbane, Ellipsis, Frostfire, Jet_Set, Long_Shot, Pigeon, Newton_s_Laws,Gentleman, Tower_of_London, and Vulture_Gulch to represent the screen number on which the movie Brisbane, Ellipsis, Frostfire, Jet Set, Long Shot, Pigeon, Newton’s Laws, The Gentleman, Tower of London, and Vulture Gulch are playing, respectively.
- We use the variables Action, Comedy, Documentary, Historical, Horror, Musical, Romance, Spy, SciFi,and Western to represent the screen on which action flick, comedy, documentary, historical epic, horror, musical, romance, spy thriller, science fiction, and western movie is playing, respectively.
Posting my prolog code first and then detailing the issue I have.
% Arrangement structure for 10 screens
arrangement([
screen(1, _, _),
screen(2, _, _),
screen(3, _, _),
screen(4, _, _),
screen(5, _, _),
screen(6, _, _),
screen(7, _, _),
screen(8, _, _),
screen(9, _, _),
screen(10, _, _)
]).
% Solution predicate with rules in specified order and singleton variable fixes
solution(S) :-
arrangement(S),
% Rule 1: Brisbane is 1 higher than the historical epic and 1 lower than Long Shot
member(screen(Brisbane, 'Brisbane', _), S),
member(screen(Historical, _, 'historical epic'), S),
member(screen(Long_Shot, 'Long Shot', _), S),
Brisbane =:= Historical + 1,
Brisbane =:= Long_Shot - 1,
% Rule 2: Jet Set is higher than Pigeon but lower than the Western
member(screen(Jet_Set, 'Jet Set', _), S),
member(screen(Pigeon, 'Pigeon', _), S),
member(screen(Western, _, 'western'), S),
Jet_Set > Pigeon,
Jet_Set < Western,
% Rule 3: Musical is on screen 4
member(screen(Musical, _, 'musical'), S),
Musical =:= 4,
% Rule 4: Comedy is 1 higher than The Gentleman and 1 lower than Frostfire
member(screen(Comedy, _, 'comedy'), S),
member(screen(The_Gentleman, 'The Gentleman', _), S),
member(screen(Frostfire, 'Frostfire', _), S),
Comedy =:= The_Gentleman + 1,
Comedy =:= Frostfire - 1,
% Rule 5: The Gentleman is not a science fiction movie
member(screen(SciFi, _, 'science fiction'), S),
The_Gentleman == SciFi,
% Rule 6: Horror movie is 1 higher than Newton's Laws
member(screen(Newton_s_Laws, 'Newtons Laws', _), S),
member(screen(Horror, _, 'horror'), S),
Horror =:= Newton_s_Laws + 1,
% Rule 7: Vulture Gulch is 1 higher than the documentary
member(screen(Vulture_Gulch, 'Vulture Gulch', _), S),
member(screen(Documentary, _, 'documentary'), S),
Vulture_Gulch =:= Documentary + 1,
% Rule 8: Documentary is on a higher-numbered screen than Comedy
Documentary > Comedy,
% Rule 9: Ellipsis is 1 higher than Action and 1 lower than SciFi
member(screen(Ellipsis, 'Ellipsis', _), S),
member(screen(Action, _, 'action flick'), S),
Ellipsis =:= Action + 1,
Ellipsis =:= SciFi - 1,
% Rule 10: Action, Historical Epic, Romance on screens 1, 2, 3
member(screen(Romance, _, 'romance'), S),
member(screen(Tower_of_London, 'Tower of London', _), S),
permutation([1, 2, 3], [Action, Historical, Romance]),
Action = Historical, % Each screen has a unique movie
Historical = Romance,
Romance = Action,
Tower_of_London =< 3, % One of these is Tower_of_London
% Including Spy thriller genre
member(screen(Spy, _, 'spy thriller'), S),
% Rule 11: Absolute differences between Newton's Laws and Vulture Gulch, and The Gentleman and Tower of London
Diff1 is abs(The_Gentleman - Tower_of_London),
Diff2 is abs(Newton_s_Laws - Vulture_Gulch),
Diff1 =:= Diff2.
MY ISSUE: Whenever I run my program through prolog –> solution(Ans). My program has a hiccup at the 8th screen and never completes. I’ve tried tracing but I’m still unable to pinpoint where the problem lies. I figure it might have something to do with the spy thriller genre but I can’t figure why.
I have tried tracing the program and debugging but it runs through a lot of computations and must be done a certain way. The prompt states “To overcome this problem, we will put the membership predicates as late as possible and put them just before the first time we use a variable. This allows Prolog to eliminate a lot (13,168,189,440,000 permutations.) of incorrect answers and avoid generating those permutations that it knows will not lead to a solution. Prolog should be able to produce the answer almost instantly (less than 1 second) in
all cases.”
I can get up to the 8th screen but then it just stalls out and never finishes. I already know the 9th and 10th movie as well as their genres as the tracing shows. I believe it may have something to do with the spy thriller genre but I’m not sure why.
When I trace my program I get this result:
?- solution(Ans).
Ans = [screen(1, ‘Tower of London’, ‘historical epic’), screen(2, ‘Brisbane’, romance), screen(3, ‘Long Shot’, ‘action flick’), screen(4, ‘Ellipsis’, musical), screen(5, ‘Newtons Laws’, ‘science fiction’), screen(6, ‘The Gentleman’, horror), screen(7, ‘Pigeon’, comedy), screen(8, ‘Frostfire’, ‘spy thriller’), screen(…, …, …)|…] ; [trace]
Redo: (19) lists:member_([screen(9, ‘Jet Set’, documentary), screen(10, ‘Vulture Gulch’, western)], screen(_24118, _24120, ‘spy thriller’), screen(8, ‘Frostfire’, 23160)) ? creep
Fail: (19) lists:member([screen(9, ‘Jet Set’, documentary), screen(10, ‘Vulture Gulch’, western)], screen(_24118, _24120, ‘spy thriller’), screen(8, ‘Frostfire’, _23160)) ?