For Programmers: Free Programming Magazines  


Home > Archive > Prolog > January 2006 > The Bridge crossing prolog program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author The Bridge crossing prolog program
vag

2006-01-29, 6:59 pm

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


Markus Triska

2006-01-29, 6:59 pm

Hi!

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.
vag

2006-01-29, 6:59 pm


"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?


Markus Triska

2006-01-30, 7:01 pm

Hi!

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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com