【发布时间】:2013-05-06 03:39:28
【问题描述】:
我想使用strategy pattern 来模块化我的 A* 实现中的启发式,但在分离它时遇到了一些麻烦。我尝试使用Comparator 初始化我的优先级队列,该Comparator 通过以下方式使用我的启发式算法:
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;
}
});
}
但我得到:“不能引用非最终变量启发式内部和以不同方法定义的内部类。”
我在加权完整图上运行它,并计划使用基本启发式向开放集中最近的节点移动(我没有目标节点,只有一组需要访问的节点)。当然,要找到开放集中某个节点的权重最小的边,我需要开放节点的列表/队列和当前节点(其中有边列表),所以我做了如下启发式接口:
public interface AStarHeuristic {
public int calculate(Node curr, Queue<Node> open);
}
我怎样才能分离出我的启发式以便在运行时对我的队列进行排序?
【问题讨论】: