I got this code to return a shortest path between 2 cities:
goal(Z,Path,Cost) :-
A = mycity,
caminhAux2(A,[Z],0,Path, Cost).
caminhAux2(A,[A|Path1],Cost1,[A|Path1],Cost1).
caminhAux2(A,[Y|Path1],Cost1,Path,Cost) :-
conn(_,X,Y,Dist,N),
road(N,Vmed,_),
CostXY is Dist/Vmed,
Cost2 is Cost1 + CostXY,
caminhAux2(A,[X,Y|Path1],Cost2,Path,Cost).
best(Z,P,C) :-
goal(Z,P,C),
\+ (goal(Z,P1,C1),C1<C).
How I can make this to avoid already visited locations? This is in infinity cycling.
Easy: In the recursive clause of the strangely named “caminhAux2” predicate, it seems that X must not be a member of Path1, so at a place that I leave as an exercise, the goal
\+ member(X, Path1)might do it?