I have a few prolog predicates which calculate the cost of given cities. The process begins with a command like: best_route([std, lhr, bud, dse], 2013-5-5, X).
best_route(Cities, StartDate, Cost):-
begin_routing(Cities, StartDate, Cost, []).
begin_routing(Cities, StartDate, Cost, CostList):-
route(Cities, StartDate, CostList),
min_list(CostList, Cost).
route(Cities, StartDate, Costing):-
% stop if all cities have been covered once.
length(Cities, Stop),
length(Costing, Stop);
[Origin, Dest|_] = Cities,
flights(Origin, Dest, StartDate, Costing, Cities, [Cities, Origin, StartDate]).
Using the trace function in SWI-Prolog, I found that once the route predicate - length(Costing, Stop)
is satisfied i.e., length of Costing List is equal to Stop. Prolog instead of stopping there, and proceeding with min_list(CostList, Cost)
, instead backtracks until CostLost loses all its values again. Once it finishes that, it goes to min_list
when the list is []
.
I am not sure why this might be happening. Any help is appreciated.
EDIT:
flights(..):-
% Code omitted.
get_next_date(OriginalDate, NextDate),
route(Cities, NextDate, [DayCost|Costing]).
% where DayCost is a simple integer calculated before this is added to the current Costing list
Towards the end, the last correct call is route([std, lhr, bud, dse], 2013-5-6, [329, 499, 323, 311]).
best_route
andbegin_routing
, it appears that whenroute
is first called,Cities
is instantiated to[std, lhr, bud, dse]
andCosting
is instantiated to[]
. The call tolength(Costing, Stop)
becomeslength([], 4)
and fails. It is terminated in a disjunction (;
) and, thereforeroute
doesn't fail at that point but continues with[Origin, Dest|_] = Cities
instantiatingOrigin
andDest
and then callingflights
. Since you don't show whatflights
does, it's unclear what it does from there. - lurkerCostList
is not being carried over the recursion period. Addingtrue
does not make much difference either. - Namitmin_list
- lurker