A *搜索实施

时间:2014-02-17 22:04:29

标签: java search a-star

我正在尝试用Java实现A * Search算法来查找给定迷宫的路径。迷宫具有起始和目标状态并且可能包含障碍物。

这是我解决给定迷宫的代码:

  public Solver(Maze maze) 
  {

  explored = new HashSet<Square>(); // Set of squares that A* has investigated
  path = new ArrayList<Square>(); // List of squares to the goal
  Square currentSquare = maze.getStart();
  Square goalSquare = maze.getGoal();

  while (currentSquare.equals(goalSquare) != true)
  {
      System.out.println("Im here");


      // Calculate each direction from the current node
      Square left = new Square(currentSquare.getRow(), currentSquare.getColumn()         - 1);
      Square right = new Square(currentSquare.getRow(), currentSquare.getColumn() + 1);
      Square top = new Square(currentSquare.getRow() - 1, currentSquare.getColumn());
      Square bottom = new Square(currentSquare.getRow() + 1, currentSquare.getColumn());

      // Check if each direction is blocked. If it is, we cant go there.
      List<Square> possibilities = new ArrayList<Square>();

      if (maze.isBlocked(left) != true && path.contains(left) != true)
      {
          possibilities.add(left);
      }
      if (maze.isBlocked(right) != true && path.contains(right) != true)
      {
          possibilities.add(right);
      }
      if (maze.isBlocked(top) != true && path.contains(top) != true)
      {
          possibilities.add(top);
      }
      if (maze.isBlocked(bottom) != true && path.contains(bottom) != true)
      {
          possibilities.add(bottom);
      }

      // find which square we should go to next
      Square choice = currentSquare;
      int choicegx =  1000;//path.size();
      int choicehx = 1000;//Math.abs(goalSquare.getRow() - choice.getRow()) + Math.abs(goalSquare.getColumn() - choice.getColumn());
      int choicefx = 1000;//choicegx + choicehx;
      for (Square possibility : possibilities)
      {
          int fx = 0;
          int gx = 0;
          int hx = 0;

          // Calculate gx (distance traveled)
          gx = path.size();

          // Calculate hx (Manhatten distance)
          hx = Math.abs(goalSquare.getRow() - possibility.getRow()) + Math.abs(goalSquare.getColumn() - possibility.getColumn());

          // Calculate fx
          fx = gx + hx;

          if (fx < choicefx)
          { // Possibility is a better choice based on fx
              choicefx = fx;
              choicehx = hx;
              choicegx = gx;
              choice = possibility;
              if (choice.equals(goalSquare) != true && choice.equals(maze.getStart()) != true)
              {
                  explored.add(choice);
              }
          }
          else if (fx == choicefx)
          { // Squares are tied based on fx
              if (hx < choicehx)
              { // Possibility is better based on hx
                  choicefx = fx;
                  choicehx = hx;
                  choicegx = gx;
                  choice = possibility;
                  if (choice.equals(goalSquare) != true && choice.equals(maze.getStart()) != true)
                  {
                      explored.add(choice);
                  }
              }
              else if (hx == choicehx)
              { // Squares are tied based on hx
                  if (possibility.getRow() < choice.getRow())
                  {// Possibility is better based on row
                      choicefx = fx;
                      choicehx = hx;
                      choicegx = gx;
                      choice = possibility;
                      if (choice.equals(goalSquare) != true && choice.equals(maze.getStart()) != true)
                      {
                          explored.add(choice);
                      }
                  }
                  else if (possibility.getRow() == choice.getRow())
                  { // Squares are tied based on row
                      if (possibility.getColumn() < choice.getColumn())
                      { // Squares are tied based on column
                          choicefx = fx;
                          choicehx = hx;
                          choicegx = gx;
                          choice = possibility;
                          if (choice.equals(goalSquare) != true && choice.equals(maze.getStart()) != true)
                          {
                              explored.add(choice);
                          }
                      }
                  }
              }
          }
      }

      // Move to the square we have chosen and add to path
      currentSquare = choice;
      if (currentSquare.equals(goalSquare) != true)
      {
          path.add(currentSquare);
      }

  }
}

我正在尝试更好地设计此部分:

Square choice = currentSquare;
      int choicegx =  1000;//path.size();
      int choicehx = 1000;//Math.abs(goalSquare.getRow() - choice.getRow()) +     Math.abs(goalSquare.getColumn() - choice.getColumn());
      int choicefx = 1000;//choicegx + choicehx;

在本节中,我在查看要选择的正方形之前确定初始fx。最初,我一直在使用当前节点的fx来设置这些值。然而,我接着进入一个搜索问题,从不探索比当前节点更糟糕的节点。我希望始终取得进步,永远不要停留在不是目标的节点上。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试查看Priority queue。它是用于“调度”节点访问的结构,您尚未访问过。当您访问节点时,可以计算可以从当前节点访问的所有节点的gx,然后将节点存储在优先级队列中,按gx值排序,以便节点最低gx位于最前面。

然后从队列顶部挑选下一个要访问的节点,因此它可以是当前节点之一,但如果它比任何其他节点具有更低的gx,它也可以是更旧的节点剩下。这样你就可以随时探索目前最有趣的路径。