我必须做一个Prolog应用程序,它必须找到最便宜的路径,最快的路径,最长时间到达的最便宜,最高价格的最快。我写了一点,但我不知道如何找到最便宜的,这是行不通的。
%train/plane(name,from,to,departure,arrival,price).
train(p1,gdansk, sopot, odjazd(10:15), przyjazd(10:30), cena(5)).
train(p2,sopot, gdynia, odjazd(11:00), przyjazd(11:30), cena(5)).
train(p3,sopot, gdynia, odjazd(11:15), przyjazd(11:45), cena(5)).
plane(s1,gdansk, warszawa, odlot(16:00), przylot(17:15), cena(300)).
plane(s2,gdansk, wroclaw, odlot(14:00), przylot(15:30), cena(300)).
plane(s3,gdansk, poznan, odlot(18:00), przylot(19:30), cena(200)).
path(From, To, Path) :-
path(From, To, [], Path).
path(From, From, _, [From]).
path(From, To, Visited, [From|Nodes]) :-
\+ member(From, Visited),
dif(From, To),
(train(_, From,Node,_,_,_);
plane(_, From,Node,_,_,_)
),
path(Node, To, [From|Visited], Nodes).
path_cost(P, C) :- aggregate_all(sum(Cost), member(train(_,_,_,_,_,Cost), P), C).
path_min_cost(From, To, Min, Path) :- aggregate(min(C,P),
(path(From,To,P), path_cost(P,C)),
min(Min,Path)).