i wanting use strategy pattern modularize heuristic in a* implementation having trouble separating it. tried initialize priority queue comparator
uses heuristic in following way:
public astarsearch(graph g, astarheuristic heuristic) { this.heuristic = heuristic; this.open = new priorityqueue<node>(10, new comparator<node>(){ @override public int compare(node n1, node n2) { int fscoreone = n1.gettotalpathweight() + heuristic.calculate(n1, open); int fscoretwo = n1.gettotalpathweight() + heuristic.calculate(n1, open); if (fscoreone < fscoretwo) return 1; else if (fscoreone > fscoretwo) return -1; return 0; } }); }
but get: "cannot refer non-final variable heuristic inside , inner class defined in different method."
i running on weighted complete graph plan of using basic heuristic of moving toward closest node in open set (i don't have destination node, set of nodes need visited). of course, find least weight edge node in open set, need list/queue of open nodes , current node (which has list of edges), made heuristic interface follows:
public interface astarheuristic { public int calculate(node curr, queue<node> open); }
how can separate out heurisitc such can used sort queue @ run-time?
the issue here creating anonymous inner class , trying refer local variable in calling function (here, heuristic
). in order this, java requires variable marked final
. if try changing method to
public astarsearch(graph g, final astarheuristic heuristic) { // ... same before ... }
the issue should go away.
hope helps!
Comments
Post a Comment