Retain variable while backtracking prolog -
i trying find minimum cost of path while retaining variable min
on backtracking. code below not work, gives slight idea on want.
the min variable holds current minimum value, when compared new total
if total
smaller newmin
should total. possible send newmin
min
foreward. however, because relying on backtracking, clause before forcefully fails, , hence values stored lost.
calculate_cost(start, [], costlist, min, newmin):- sum_list(costlist, total), total < min, newmin 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 want retain min
variable till programs ends, while making several comparisons.
note: predicate calculate_cost called several times (a lot more 1 million), lists not feasible i've tried , leads out of global stack
exception.
assert option has been tried, leads same problem.
the option search through , keep minimum.
keep updated path & mincost when calculate_cost complete:
:- dynamic current_min/2. % path, cost calculate_cost(start, [], costlist, min, newmin):- sum_list(costlist, total), total < min, newmin total, ( current_min(cpath, cmin), cmin =< newmin -> true ; retract(current_min(_,_)), assert(current_min(newpath, newmin))).
i know newpath isn't available now: see if can change program flow make newpath available calculate_cost
btw there dijkstra algorithm available: why don't use ?
Comments
Post a Comment