嗨,我有这些数据
Quote:
flight(Place1,Place2,departure time,Arrival time,flight code,Day)
flight(paris,milano,6,8,ba4733,mo).
flight(milano,london,9,10,ba4733,mo).
flight(london,athens,10,15,ba4733,mo).
flight(milano,paris,10,15,ba4733,mo).
我想找到2个地方之间的所有路径,例如
?- route(paris,athens,mo,A).
A = [flight(paris,milano,ba4733,8), flight(milano,london,ba4733,10), flight(london,athens,ba4822,15)] ;
从place1到place2的飞行也需要Day1 = Day2和到达时间< =出发时间
我已经完成了这些但我的程序堆栈非常糟糕:
apeuthias_ptisi(P1,P2):-ptisi(P1,P2,_,_,_,_).
flight(P1,P2,COD,AF):-ptisi(P1,P2,_,AF,COD,_).
antapokrisi(P1,Y,P1):-!.
antapokrisi(P1,Y,P2):-ptisi(P1,Y,AN1,AF1,COD1,DAY1),
ptisi(Y,P2,AN2,AF2,COD2,DAY2), AF1=<AN2, DAY1==DAY2.
path(X,Y,D,L2):-c2(X,Y,[],D,L),reverse(L,L2).
c2(X,X,L,D,L):-!.
c2(X,Y,L,D,L2):-antapokrisi(X,Z,P2),ptisi(X,Z,AN,AF,COD,D),not(member(Z,L)),c2(Z,Y,[flight(X,Z,COD,AF)|L],D,L2).
route(P1,P1,D,R):-!.
route(P1,P2,D,R2):-setof(R,path(P1,P2,D,R),R2).
任何人都可以帮助我吗?
答案 0 :(得分:1)
答案 1 :(得分:0)
请不要将以下内容用于作业解决方案;这可能是非常错误的!它也是用我的(可能很糟糕的)Prolog编程风格编写的。我解决这个问题的方法是家族树的着名祖先谓词:
ancestor( X, Y ) :-
parent( X, Y ).
ancestor( X, Y ) :-
parent( X, Z ),
ancestor( Z, Y ).
其中parent以正常方式定义,因此它使用的是其他谓词。
您是否还有航班/ 6条规则?
% list of flights
% need more to properly test really
flight( paris, milano, 6, 8, ba4733, mo).
flight( milano, london, 9, 10, ba4733, mo ).
flight( london, athens, 10, 15, ba4733, mo ).
flight( milano, paris, 10, 15, ba4733, mo ).
flight( milano, paris, 17, 15, ba4733, mo ).
% clause to add an intermediate variable
route( Start, End, Day, F ) :-
route( Start, End, Day, [], F ), !.
% base case
% we use reverse/2 as [Head|List] notation means
% that the list is backwards
route( Start, End, Day, Inter, F ) :-
flight( Start, End, ST1, ET1, Code1, Day ),
ET1 >= ST1,
F1 = [flight( Start, End, ST1, ET1, Code1, Day )|Inter],
reverse( F1, F ).
% recursive case
% start and end times checked on each recursion so the
% overall start and end time must satisfy our constraint
% of end time >= start time
route( Start, End, Day, Inter, F ) :-
flight( Start, Somewhere, ST1, ET1, Code1, Day ),
ET1 >= ST1,
route( Somewhere, End, Day, [flight(Start, Somewhere, ST1, ET1, Code1, Day)|Inter], F ).