Code Comments
Programming Forum and web based access to our favorite programming groups.Anyone have the prolog program solving this problem? Use that link to read the problem http://www.dcs.warwick.ac.uk/~mju/C...ment-bridge.pdf
Post Follow-up to this messageHi! vag wrote: > Anyone have the prolog program solving this problem? Here is one: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% touristList([t1,t2,t3,t4]). time(t1, 6). time(t2, 7). time(t3, 10). time(t4, 15). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% member_rest(E, [E|Es], Es). member_rest(M, [E|Es], [E|Rest]) :- member_rest(M, Es, Rest). % move two people from left to right solve_left(s(Lefts0, Rights0), Time0, Plan0, Plan) :- member_rest(T1, Lefts0, Lefts1), member_rest(T2, Lefts1, Lefts2), time(T1, TT1), time(T2, TT2), Time1 is Time0 - max(TT1, TT2), Time1 >= 0, Plan0 = [[T1,T2]|Rest], solve_right(s(Lefts2, [T1,T2|Rights0]), Time1, Rest, Plan). % move one tourist from right to left if necessary solve_right(s(Lefts0, Rights0), Time0, Plan0, Plan) :- ( Lefts0 == [] -> Plan0 = Plan ; member_rest(T, Rights0, Rights1), time(T, TT), Time1 is Time0 - TT, Time1 >= 0, Plan0 = [[T]|Rest], solve_left(s([T|Lefts0], Rights1), Time1, Rest, Plan) ). cross(Max, Plan) :- touristList(Ts), ( Ts = [Single] -> time(Single, Time), Time =< Max, Plan = [[Single]] ; solve_left(s(Ts, []), Max, Plan, []) ). Example: ?- cross(42, Plan). Plan = [[t1, t2], [t1], [t3, t4], [t2], [t2, t1]] Yes All the best, Markus.
Post Follow-up to this message"Markus Triska" <triska@gmx.at> wrote in message news:43dd22cc$0$12384$3b214f66@tunews.univie.ac.at... > > Hi! > > vag wrote: > > > Here is one: > > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% > touristList([t1,t2,t3,t4]). > > time(t1, 6). > time(t2, 7). > time(t3, 10). > time(t4, 15). > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% > > member_rest(E, [E|Es], Es). > member_rest(M, [E|Es], [E|Rest]) :- > member_rest(M, Es, Rest). > > % move two people from left to right > > solve_left(s(Lefts0, Rights0), Time0, Plan0, Plan) :- > member_rest(T1, Lefts0, Lefts1), > member_rest(T2, Lefts1, Lefts2), > time(T1, TT1), > time(T2, TT2), > Time1 is Time0 - max(TT1, TT2), > Time1 >= 0, > Plan0 = [[T1,T2]|Rest], > solve_right(s(Lefts2, [T1,T2|Rights0]), Time1, Rest, Plan). > > > % move one tourist from right to left if necessary > > solve_right(s(Lefts0, Rights0), Time0, Plan0, Plan) :- > ( Lefts0 == [] -> > Plan0 = Plan > ; > member_rest(T, Rights0, Rights1), > time(T, TT), > Time1 is Time0 - TT, > Time1 >= 0, > Plan0 = [[T]|Rest], > solve_left(s([T|Lefts0], Rights1), Time1, Rest, Plan) > ). > > > cross(Max, Plan) :- > touristList(Ts), > ( Ts = [Single] -> > time(Single, Time), > Time =< Max, > Plan = [[Single]] > ; > solve_left(s(Ts, []), Max, Plan, []) > ). > > Example: > > ?- cross(42, Plan). > > Plan = [[t1, t2], [t1], [t3, t4], [t2], [t2, t1]] > > Yes > > All the best, > Markus. Thanks man but when i compile it and make the question cross(42, Plan). it dosnt work. It answers only NO. I use SWI-Prolog (Multi-threaded, Version 5.4.7) Any suggestions?
Post Follow-up to this messageHi! vag wrote: > > Thanks man but when i compile it and make the question > cross(42, Plan). it dosnt work. It answers only NO. > I use SWI-Prolog (Multi-threaded, Version 5.4.7) > Any suggestions? I tested it with 5.5.40. You can do ?- guitracer. ?- trace. ?- cross(42, Plan). to graphically step through the computation and see where it fails. All the best, Markus.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.