30 lines
948 B
OpenEdge ABL
30 lines
948 B
OpenEdge ABL
/*Jacob Besse
|
|
Prolog*/
|
|
|
|
/*This is the data set.*/
|
|
|
|
:- include('data10.P').
|
|
|
|
len([], 0).
|
|
len([_|T], N):- len(T, X), N is X+1 .
|
|
|
|
best_path(Visited, Total):- path(a, a, Visited, Total).
|
|
|
|
path(Start, Fin, Visited, Total) :- path(Start, Fin, [Start], Visited, 0, Total).
|
|
|
|
path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-
|
|
edge(Start, StopLoc, Distance), NewCostn is Costn + Distance, \+ member(StopLoc, CurrentLoc),
|
|
path(StopLoc, Fin, [StopLoc|CurrentLoc], Visited, NewCostn, Total).
|
|
|
|
path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-
|
|
edge(Start, Fin, Distance), reverse([Fin|CurrentLoc], Visited), len(Visited, Q),
|
|
(Q\=11 -> Total is 100000; Total is Costn + Distance).
|
|
|
|
shortest_path(Path):-setof(Cost-Path, best_path(Path,Cost), Holder),pick(Holder,Path).
|
|
|
|
best(Cost-Holder,Bcost-_,Cost-Holder):- Cost<Bcost,!.
|
|
best(_,X,X).
|
|
|
|
pick([Cost-Holder|R],X):- pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-Bholder,X),!.
|
|
pick([X],X).
|