Domanda

I have this Prolog problem, and I want to check 3 things.

  1. is it possible to travel from a to b and also from b to a
  2. is it possible to travel from a to b and also from b to a AND it tells me the route travelling from a to b.
  3. same as number 2 and additional information on what mode of transport to use for each travel in the route.

Currently I managed to get this program below for part (1) but as I run, the program comes out 'yes' even if it is a wrong route. Have no idea why.

byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).

byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).

byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(losAngeles,auckland).

(1)

connect(X,Y):-byCar(X,Y);byCar(Y,X).
connect(X,Y):-byTrain(X,Y);byTrain(Y,X).
connect(X,Y):-byPlane(X,Y);byPlane(Y,X).

travel(X,Y):-travelLoop(X,Y,[]).
travelLoop(X,Y,_):-connect(X,Y).
travelLoop(X,Y,Passed):-connect(X,Thru),
    \+memberchk(Thru,Passed),
    travelLoop(Thru,Y,[Thru|Passed]),X\=Y.
travel(_,_):-write('Wrong travel input, please try again.'),nl.

(2)

travel(X,Y,go(X,Y)):-byCar(X,Y).
travel(X,Y,go(X,Y)):-byTrain(X,Y).
travel(X,Y,go(X,Y)):-byPlane(X,Y).
travel(X,Y,go(X,Z,W)):-travel(X,Z,go(X,Z)),travel(Z,Y,W).

(3)

travel1(X,Y,go(X,Y,car)):-byCar(X,Y).
travel1(X,Y,go(X,Y,train)):-byTrain(X,Y).
travel1(X,Y,go(X,Y,plane)):-byPlane(X,Y).
travel1(X,Y,go(X,Z,V,W)):-travel1(X,Z,go(X,Z,V)),travel1(Z,Y,W).

P.S. (2) and (3) couldn't think of the to and fro predicates, only managed to get one way. Please help. Thanks a lot.

È stato utile?

Soluzione

First, your connect predicate forgets the mode of transport. Change it to:

connect(X,Y,byCar):-   byCar(X,Y)   ; byCar(Y,X).
connect(X,Y,byTrain):- byTrain(X,Y) ; byTrain(Y,X).
connect(X,Y,byPlane):- byPlane(X,Y) ; byPlane(Y,X).

Second, there's inconsistency in your use of travelLoop's path argument:

travel(X,Y):-travelLoop(X,Y,[]).            %// -- start empty. Why not [X]?
travelLoop(X,Y,_):-connect(X,Y).            %// (1)
travelLoop(X,Y,Passed):-connect(X,Thru),
    \+memberchk(Thru,Passed),
    travelLoop(Thru,Y,[Thru|Passed]),       %// -- start from [Thru|...].
    X\=Y.                                   %// why here? should be at (1)

The second clause of travel needs to fire only if the first was unsuccessful. And why is it detached? Both clauses should be one near the other in the file.

travel(_,_):-write('Wrong travel input, please try again.'),nl.
%// This says: succeed always, and tell the user it didn't. 
%// Need to join both clauses into one IF: (success -> true; wrong).

The success clause for travelLoop is missing:

travelLoop(X,Y, Path):- X=Y, %// report the Path to user: ... 
                        writeln( ... ).

As for the return voyage, isn't it as simple as switching the arguments? So, rename travelLoop to travel_path, and rewrite your travel as

travel(X,Y):-   %// the to and fro loop
  travel_path(X,Y,[X]), travel_path(Y,X,[Y]) -> true ; wrong.

wrong:- writeln("Wrong input. Try again."), nl.

Altri suggerimenti

Actually below is part of my program, we are required to write in a menu form, so this is one part of the menu. I inserted what u have corrected for me, i tried n i think it works, as i figure that any 2 destinations chosen will be have "yes" answer since there is a to and fro loop. But, I think it affects the option (c) and (d). So i am not sure where the contradiction is.

choice(3):- write('Select the way: '),nl,nl,
  write('a. Travel by using the same transportation.'),nl,
  write('b. Travel by chaining together car, train, and plane journeys.'),nl,
  write('c. How to travel from one place to another: cities.'),nl,
  write('d. How to travel from one place to another: cities and transportation.'),nl,
  read(Option),nl,nl,
  write('Enter the place start from: '),read(Start),
  write('Enter the place end at: '),read(End),
  option(Option,Start,End),nl,nl.

option(a,A,B):- travelSimple(A,B),nl,write('yes'),nl,nl,menu. 
option(b,A,B):- travel(A,B),nl,write('yes'),nl,nl,menu.
option(c,A,B):- findall(X,travel(A,B,X),List),nl,writelist(List),nl,nl,menu.    
option(d,A,B):- findall(Y,travel1(A,B,Y),List1),nl,writelist(List1),nl,nl,menu.
option(_,_,_):- write('Wrong option input, please try again.'),nl,nl,menu.

travel1(X,Y):-byCar(X,Y).
travel1(Y,X):-byCar(X,Y).
travel2(P,Q):-byTrain(P,Q).
travel2(Q,P):-byTrain(P,Q).
travel3(M,N):-byPlane(M,N).
travel3(N,M):-byPlane(M,N).

travelSimple(X,Y):-byCar(X,Z),travel1(Z,Y);byCar(X,Y).
travelSimple(Y,X):-byCar(X,Z),travel1(Z,Y);byCar(X,Y).
travelSimple(P,Q):-byTrain(P,R),travel2(R,Q);byTrain(P,Q).
travelSimple(Q,P):-byTrain(P,R),travel2(R,Q);byTrain(P,Q).
travelSimple(M,N):-byPlane(M,L),travel3(L,N);byPlane(M,N).
travelSimple(N,M):-byPlane(M,L),travel3(L,N);byPlane(M,N).
travelSimple(_,_):-write('Wrong travel simple input, please try again.'),nl.

connect(X,Y,byCar):-   byCar(X,Y)   ; byCar(Y,X).
connect(X,Y,byTrain):- byTrain(X,Y) ; byTrain(Y,X).
connect(X,Y,byPlane):- byPlane(X,Y) ; byPlane(Y,X).

travel(X,Y):-travelLoop(X,Y,[X]).          
travelLoop(X,Y,_):- connect(X,Y,byCar);
                    connect(X,Y,byTrain);
                    connect(X,Y,byPlane), X\=Y.           
travelLoop(X,Y,Passed):-connect(X,Thru,byCar);
                    connect(X,Thru,byTrain);
                    connect(X,Thru,byPlane);
                    \+memberchk(Thru,Passed),travelLoop([Thru|Passed],Y,Thru). 
travelLoop(X,Y,Path):- X=Y, writeln('Yes')-> true ; wrong.      
travel(X,Y):-travel_Path(X,Y,[X]), 
     travel_Path(Y,X,[Y]) -> true ; wrong. 
wrong:- writeln('Wrong travel input, please try again.'), nl.

travel(X,Y,go(X,Y)):-byCar(X,Y).
travel(X,Y,go(X,Y)):-byTrain(X,Y).
travel(X,Y,go(X,Y)):-byPlane(X,Y).
travel(X,Y,go(X,Z,W)):-travel(X,Z,go(X,Z)),travel(Z,Y,W).
travel1(X,Y,go(X,Y,car)):-byCar(X,Y).
travel1(X,Y,go(X,Y,train)):-byTrain(X,Y).
travel1(X,Y,go(X,Y,plane)):-byPlane(X,Y).
travel1(X,Y,go(X,Z,V,W)):-travel1(X,Z,go(X,Z,V)),travel1(Z,Y,W).

writelist([]).
writelist([L|Lt]):-write(L),nl,writelist(Lt).
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top