Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

The Bridge crossing prolog program
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



Report this thread to moderator Post Follow-up to this message
Old Post
vag
01-29-06 11:59 PM


Re: The Bridge crossing prolog program
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
01-29-06 11:59 PM


Re: The Bridge crossing prolog program
"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?



Report this thread to moderator Post Follow-up to this message
Old Post
vag
01-29-06 11:59 PM


Re: The Bridge crossing prolog program
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
01-31-06 12:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:12 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.