while(!open.isEmpty()&& !solutionFound){
Node selected=open.poll();//fifo
State estado=selected.getState();
estado.toString();
this.exploredNodes++;
if(!explored.contains(selected.getState()) ){
if(problem.testGoal(selected.getState())){
actionSequence=recoverPath(selected, inicial);//return array with solutions
solutionFound=true;
}
//totalCost++;
successors=getSuccessors(selected);
for(Node successor : successors){
//if(!explored.contains(successor))
open.add(successor);
}
explored.add(selected.getState());
}
}
我正在尝试检查所选节点的状态是否在节点的哈希集中,并且如果它已经在其中,则它不应执行任何操作。
问题是它总是返回false。因此进行无限比较。
@Override
public boolean equals(Object anotherState) {
if(anotherState instanceof MazeState)return false;
if(this.life!=((MazeState)anotherState).life)return false;
if (this.position.x!=((MazeState)anotherState).position.x)return false;
if (this.position.y!=((MazeState)anotherState).position.y)return false;
if (!this.cheeses.containsAll(((MazeState)anotherState).cheeses))return false;
return true;
}
@Override
public int hashCode() {
return Objects.hash(this.position,this.life,this.cheeses);
这是我的equals和hashCode的实现,我认为这是很好的,因为它们比较了State的所有属性。
任何提示将不胜感激。
答案 0 :(得分:1)
我认为您想在此处应用NOT检查...。因此,以下方法将起作用
if(!(anotherState instanceof MazeState)) return false;
此外,即使Cheeses集合中元素的顺序不同,containsAll检查也会报告两个对象相等。