试图在java

时间:2018-05-19 07:40:41

标签: java infinite-loop a-star heuristics 8-puzzle

我已经尝试了两天才能让这个跑步,但我不能为我的生命而努力。 我试图做的是使用A星算法和曼哈顿启发式解决8个难题。

这是A星代码的一部分:

                        public void solve(){

    State s = duplicateState(stateToSolve);
    StringState ss = new StringState(s);
    makeQ(ss.getStringState(),null,s);//add the first State to queue
    //s.setG(0);
    //s.setF(s.gettG()+heuristicTwo(ss.getStringState(),goal));
    s.setF(heuristicTwo(ss.toString(),goal));
    while(!states.isEmpty()){
        LinkedList<State>child=new LinkedList<State>();
        State state = lowestF(states);
        StringState pre = new StringState(state);
        if (goalReached(state)&&!solved){
            s = state;
            solved=true;
            System.out.println("#########\n# Solved #\n#########");
            break;
        }
        //explore(state);
        child=neighbours(child,state);
        for(int i=0;i<child.size();i++)
        {
            child.get(i).setTotalCost(state.gettG()+findDistance(state,child.get(i)));
            if(open.containsKey(child.get(i)))
            {
                if(child.get(i).gettG()<=child.get(i).getTotalCost())
                    System.out.println(child.get(i).getSolution());
                    continue;
            }
            else if(close.containsKey(child.get(i)))
            {
                if(child.get(i).gettG()<=child.get(i).getTotalCost())
                    continue;
                System.out.println(child.get(i).getSolution());
                StringState next = new StringState(child.get(i));
                makeQ(next.getStringState(),pre.getStringState(),child.get(i));
                close.remove(child.get(i));
            }
            else
            {
                System.out.println(child.get(i).getSolution());
                StringState next = new StringState(child.get(i));
                makeQ(next.getStringState(),pre.getStringState(),child.get(i));
                child.get(i).setH(heuristicTwo(next.getStringState(),goal));
            }
            child.get(i).setG(child.get(i).getTotalCost());

        }
        close.put(ss.getStringState(),ss.getStringState());



    }
    solution = s.getSolution();
    if (solution.equals("")||solution.equals(null))
        System.out.println("no solution");
    else{
    System.out.println("Astar");
    System.out.println(solution);
    }
}

StringState只是获取拼图板的状态并转换为字符串。  在这里使代码的其他部分更清晰:

              private Queue<State> states;
private State stateToSolve;
boolean solved=false;
private final String goal = "0123456789ABCDEF";
private final String goal2 = "123456789ABCDEF0";

private Map<String,String> close;//to keep track of previous checked status
private Map<String, String> open;
private String solution;

//-1,-1 for decrement of coordinates
private final int up = -4;
private final int down = 4;
private final int left = -1;
private final int right = 1;

//CONSTRUCTOR
public Astar(State s){
    solution = "";
    states = new LinkedList<State>();
    stateToSolve = duplicateState(s);
    close = new HashMap<String, String>();
    open = new HashMap<String, String>();
    solve();

}

所有邻居功能都是采取电路板的状态并返回电路板的邻居 - 意味着将空白磁贴向左/右/向上/向下移动(如果可能)并在执行后返回电路板状态它。 所以邻居是新的州。

我面临的问题(我在运行程序后意识到并尝试在某些点上打印它的状态)是它进入无限循环第2章移动例如,当我试图找到字符串的路径时str1 =&#34; 1 2 0 3 4 5 6 7 8 9 ABCDE F&#34 ;; 答案应该是左边的 但是我得到了

左 左

左 右

左 向下

左 左

左 右

左 向下

左 左

它只是继续无限... 任何帮助,将不胜感激.. ((我正在使用这个 - pseudo code

0 个答案:

没有答案