用Java编写的* 8拼图不适用于某些初始状态

时间:2018-04-18 07:27:31

标签: java artificial-intelligence a-star 8-puzzle

我必须使用A *算法和两个启发式算法来实现一个8个益智解算器。第一个启发式只是不合适的瓦片的总和,第二个是目标状态下所有瓦片的曼哈顿距离的总和,我们将其定义为:

0 1 2
3 4 5
6 7 8

我们给出了不同深度的样本测试。我使用第一个启发式的实现传递了所有这些情况,但是第二个启发式一旦达到14的深度就不会通过某些测试用例:

 (52) !Test Case failed! for initialState:
3 1 5 
6 0 7 
8 2 4
Expected depth of 14 but got 16

(12) !Test Case failed! for initialState:
4 1 5 
3 2 7 
0 8 6
Expected depth of 16 but got 18

(39) !Test Case failed! for initialState:
2 5 7 
3 4 1 
6 8 0
Expected depth of 16 but got 18

(还有更多失败的测试,这些只是前三个)因为它似乎适用于我使用第一个启发式的所有情况,我猜测它是第二个启发式的错误。这是我的摘要"节点"类:

public EightPuzzleState(int[] state, EightPuzzleState goalState, EightPuzzleState previousState) {
    this.state = new int[NUM_SPACES];
    try {   
        System.arraycopy(state, 0, this.state, 0, NUM_SPACES);
    }catch(ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
    }
    this.previousState = previousState;
    setCost(goalState);

}
private void setCost(EightPuzzleState goalState) {
    if(goalState == null) {
        System.out.println("Cost is 0- no goal state defined");
        cost = 0;
    }
    else {
        cost = calcCost(goalState);  
    }
}

private int calcCost(EightPuzzleState goalState) {
    int sum = 0;
    for(int i = 0; i < NUM_SPACES; i++) {
        sum+=heuristic(goalState, i);
    }
    if(previousState == null) {
        //System.out.println("No previous parent defined, 0 pathCost");
        pathCost = 0;
    }
    else {
        pathCost = previousState.getPathCost()+1;
    }
    return sum + pathCost;

这是使用第二个启发式的节点类:

    //In EightPuzzleStateH2 class, which extends EightPuzzleState
    @Override
    protected int heuristic(EightPuzzleState goalState, int currentIndex) {
        int currentValue = this.getState()[currentIndex];
        int[] goalStateArray = goalState.getState();
        int i = 0;

        while(currentValue != goalStateArray[i]) { 
            i++;
        }

        return calcManhattenDistance(currentIndex,i);
    }

    private int calcManhattenDistance(int currentIndex, int goalIndex) {
        int xDistance = Math.abs((currentIndex % NUM_SPACES_PER_ROW) - (goalIndex % NUM_SPACES_PER_ROW));
        int yDistance = Math.abs((currentIndex / NUM_SPACES_PER_ROW) - (goalIndex / NUM_SPACES_PER_ROW));
        return (xDistance+yDistance);

    }

任何见解都会有所帮助 - 如果问题不在第二个启发式中,那么自从第一个启发式工作完美无缺时,我真的会感到难过!

1 个答案:

答案 0 :(得分:0)

我能够通过修改我的EightPuzzleState类的哈希码函数来解决这个问题。

此外,在计算启发式时,我在计算中包括了洞,但洞不应包含在成本计算中。这与我遇到的问题无关,但为了其他读者的缘故,我在这里解决它。