I am trying to find the minimum cost of a path while retaining the variable Min
over backtracking. The code below does not work, however it gives a slight idea over what I want.
The Min is variable which holds the current minimum value, when compared with the new Total
if the Total
is smaller then the NewMin
should be Total. This is possible I send the NewMin
as Min
foreward. However, because I am relying on backtracking, the clause before forcefully fails, and hence all the values stored are lost.
calculate_cost(Start, [], CostList, Min, NewMin):-
sum_list(CostList, Total),
Total < Min,
NewMin is Total.
calculate_cost(Start, Rest, CostList, Min, NewMin):-
flatten(Rest, Places),
[Next|Left] = Places,
route(Start, Next, Cost),
calculate_cost(Next, Left, [Cost|CostList], Min, NewMin),
fail.
Now I want to essentially retain the Min
variable till the programs ends, while making several comparisons.
Note: The predicate calculate_cost is called several times (a lot more than 1 million), so lists is not feasible as i've tried and I leads to Out of global stack
exception.
Assert option has also been tried, but it leads to the same problem.
The only option is to search through and keep the minimum.