Exemplo n.º 1
0
 public void printCurrentList() {
   PriorityQueue<Node> pq = new PriorityQueue<Node>(nodes);
   AStarNode p;
   System.out.print("\n[");
   while ((p = (AStarNode) pq.poll()) != null) {
     System.out.print("\n");
     p.printNode();
   }
   System.out.println("]");
 }
Exemplo n.º 2
0
 public AStarNode createStartNode(Object state) {
   Position p = (Position) state;
   AStarNode n = new AStarNode(p);
   n.setDistTravelled(0);
   n.setApproxTotalDist(p.distance((Position) getEndingState()));
   List<Position> path = new ArrayList<Position>();
   path.add(p);
   n.setPath(path);
   // System.out.print("createStartNode"); n.printNode();
   return n;
 }
Exemplo n.º 3
0
  public List<Node> expandFunc(Node n) {
    AStarNode node = (AStarNode) n;
    // loop computes the positions you can get to from node
    List<Node> expandedNodes = new ArrayList<Node>();
    List<Position> path = node.getPath();
    Position pos = node.getPosition();
    int x = pos.getX();
    int y = pos.getY();
    // this next pair of loops will create all the possible moves
    // from pos.
    for (int i = -1; i <= 1; i += 2) { // Checks nodes to left and right
      // create the potential next position
      int nextX = x + i;
      int nextY = y;
      // make sure next point is on the grid
      if ((nextX + 1 > grid.length || nextY + 1 > grid[0].length) || (nextX < 0 || nextY < 0))
        continue;
      Position next = new Position(nextX, nextY);
      // System.out.println("considering"+next);
      if (inPath(next, path) || !next.open(grid)) continue;
      // printCurrentList();
      // System.out.println("available"+next);
      AStarNode nodeTemp = new AStarNode(next);

      // update distance travelled
      nodeTemp.setDistTravelled(node.getDistTravelled() + pos.distance(next));
      // update approximate total distance to destination
      // note that we are computing the straight-line
      // heuristic on the fly right here from next to endingState
      nodeTemp.setApproxTotalDist(next.distance((Position) endingState));
      // update internal path
      nodeTemp.updatePath(path);
      expandedNodes.add(nodeTemp); // could have just added
      // them directly to nodelist
    }

    for (int j = -1; j <= 1; j += 2) { // Checks nodes above and below
      // create the potential next position
      int nextX = x;
      int nextY = y + j;
      // make sure next point is on the grid
      if ((nextX + 1 > grid.length || nextY + 1 > grid[0].length) || (nextX < 0 || nextY < 0))
        continue;
      Position next = new Position(nextX, nextY);
      // System.out.println("considering"+next);
      if (inPath(next, path) || !next.open(grid)) continue;
      // printCurrentList();
      // System.out.println("available"+next);
      AStarNode nodeTemp = new AStarNode(next);

      // update distance travelled
      nodeTemp.setDistTravelled(node.getDistTravelled() + pos.distance(next));
      // update approximate total distance to destination
      // note that we are computing the straight-line
      // heuristic on the fly right here from next to endingState
      nodeTemp.setApproxTotalDist(next.distance((Position) endingState));
      // update internal path
      nodeTemp.updatePath(path);
      expandedNodes.add(nodeTemp); // could have just added
      // them directly to nodelist
    }
    return expandedNodes;
  } // end expandFunc
Exemplo n.º 4
0
 public void addNewNode(Queue<Node> old, AStarNode m) {
   for (Node n : old) {
     if (m.getPosition() == ((AStarNode) n).getPosition()) return;
   }
   old.offer(m);
 }