【发布时间】:2019-03-28 20:12:16
【问题描述】:
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 的所有属性。
任何提示将不胜感激。
【问题讨论】:
-
错字:大概
if(anotherState instanceof MazeState)return false;应该检查它是否不是实例 -
另外,
cheeses是什么?如果这个集合包含你定义的对象,你还需要为cheese's实现equals(Object o)