Last active
January 2, 2016 23:28
-
-
Save lawrencejones/8376244 to your computer and use it in GitHub Desktop.
Imperial Prolog assessed Cwk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
% % | |
% 276 Introduction to Prolog % | |
% % | |
% Coursework 2013-14 (crossings) % | |
% % | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
%% ------------ (utilities) DO NOT EDIT | |
fees(2,3). | |
forall(P,Q) :- \+ (P, \+ Q). | |
app_remove(X,List,Remainder) :- | |
append(Front, [X|Back], List), | |
append(Front, Back, Remainder). | |
% solutions for testing | |
solution([f(g),f,f(w),f(g),f(c),f,f(b),f,f(g)]). | |
solution([f(g),f,f(c),f(g),f(w),f,f(b),f,f(g)]). | |
solution([f(g),f,f(b),f,f(w),f(g),f(c),f,f(g)]). | |
solution([f(g),f,f(b),f,f(c),f(g),f(w),f,f(g)]). | |
%% --------- END (utilities) | |
%% ------ Add your code to this file here. | |
safe(Bank) :- | |
([H | _ ] = Bank, H = f); | |
\+ ((member(w, Bank), member(g, Bank)); | |
(member(g, Bank), member(c, Bank))). | |
safe_state(N-S) :- | |
safe(N), safe(S). | |
sorted_state(N-S, SN-SS) :- | |
sort(N, SN), sort(S, SS). | |
equiv(S1, S2) :- | |
sorted_state(S1, SS1), | |
sorted_state(S2, SS2), | |
SS1 = SS2. | |
goal(S) :- | |
equiv(S, []-[f,b,c,g,w]). | |
visited(State, History) :- | |
setof(S, (member(S,History), equiv(State,S)), EquivStates), | |
\+ (EquivStates = []). | |
remove(X, [H | T], Remainder) :- | |
X = H, | |
Remainder = T; | |
\+ (T = []) | |
-> remove(X, T, SubRemainder), | |
Remainder = [H|SubRemainder]. | |
% move(Start, Move, Result) | |
move(B1-[ f | Rem ], Move, F1-F2) :- | |
move([f|Rem]-B1, Move, F2-F1). | |
move([ f | B1 ]-B2, f, F1-F2) :- | |
F1 = B1, F2 = [ f | B2 ]. | |
move([ f | B1 ]-B2, f(X), F1-F2) :- | |
remove(X, B1, F1), | |
F2 = [ f | [X | B2 ] ]. | |
crossing(Start, Move, Finish) :- | |
safe_state(Start), | |
move(Start, Move, Finish), | |
safe_state(Finish). | |
journey(Crrt, History, Sequence) :- | |
goal(Crrt) | |
->Sequence = []; | |
crossing(Crrt, Move, Next), | |
\+ visited(Next, History), | |
journey(Next, [Next | History], SubSequence), | |
Sequence = [Move | SubSequence]. | |
succeeds(Sequence) :- | |
journey([f,w,g,c,b]-[], [], Sequence). | |
journey_cost_helper([], 0). | |
journey_cost_helper([Move | Rem], Cost) :- | |
fees(Farmer,Accomp), | |
journey_cost_helper(Rem, SubCost), | |
(Move = f -> Cost is Farmer + SubCost; | |
Cost is Accomp + SubCost). | |
journey_cost(Sequence, Cost) :- | |
succeeds(Sequence), | |
journey_cost_helper(Sequence, Cost). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment