我现在正在创建一个A *搜索(wiki page with pseudocode),而且我在最后一个小时左右开始使用启发式方程式。当我认为我终于找到了一个好的时候,我删除了允许我查看访问状态的打印语句。出于某种原因,这使得我的搜索变得慢得多。如果我重新添加打印件,它会再次变快。可能会发生什么?
我甚至尝试改变它打印的内容。无论我打印什么(只要它是2个字符或更多),结果都是一样的。
部分代码:
我事先为凌乱的代码道歉,这是我第一次使用这样的东西:
while(!toVisit.isEmpty()){//toVisit is a set of states that need to be visited
int f = Integer.MAX_VALUE;
State temp;
State visiting = new State();
Iterator<State> it = toVisit.iterator();
while(it.hasNext()){//find state with smallest f value
temp = it.next();
if(temp.getF() < f){
f = temp.getF();
visiting = temp;//should be state with smallest f by end of loop
}
}
System.out.println("Visiting: ");//THIS LINE HERE
//LINE THAT MAGICALY MAKES IT FAST ^^^^
if(numConflicts(visiting.getList()) == 0){//checking if visiting state is the solution
best = visiting.getList();//sets best answer
return visiting;//ends algorithm
}
........
HashSet<State> toVisit = new HashSet<State>();//from Java.util
public ArrayList<Node> State.getList(){return list;}
Node is my own class. It only contains some coordinates
这可以在大约6秒内始终解决问题。如果我将该行更改为不打印或短于约2个字符的内容,则需要20到70秒