|
|
|
@ -4,17 +4,30 @@ import java.util.ArrayList; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Set; |
|
|
|
|
import environment.Node; |
|
|
|
|
import environment.World; |
|
|
|
|
|
|
|
|
|
public class AStar { |
|
|
|
|
|
|
|
|
|
private Set<Node> open = new HashSet<Node>(); |
|
|
|
|
private Set<Node> closed = new HashSet<Node>(); |
|
|
|
|
private List<Node> nodes = new ArrayList<Node>(); |
|
|
|
|
|
|
|
|
|
public AStar(Node start, Node goal) { |
|
|
|
|
private World world; |
|
|
|
|
|
|
|
|
|
public AStar(World world) { |
|
|
|
|
|
|
|
|
|
this.world = world; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Node> getNodes() { |
|
|
|
|
List<Node> nodes = new ArrayList<Node>(); |
|
|
|
|
|
|
|
|
|
Set<Node> open = new HashSet<Node>(); |
|
|
|
|
Set<Node> closed = new HashSet<Node>(); |
|
|
|
|
|
|
|
|
|
Node start = new Node(world.getStart().getX(), world.getStart().getY()); |
|
|
|
|
Node stop = new Node(world.getStop().getX(), world.getStop().getY()); |
|
|
|
|
|
|
|
|
|
start.setG(0); |
|
|
|
|
start.setH(manhattanDistance(start, goal)); |
|
|
|
|
start.setH(manhattanDistance(start, stop)); |
|
|
|
|
start.setF(start.getH()); |
|
|
|
|
|
|
|
|
|
open.add(start); |
|
|
|
@ -32,15 +45,22 @@ public class AStar { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (current == goal) { |
|
|
|
|
if (current == stop) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
open.remove(current); |
|
|
|
|
closed.add(current); |
|
|
|
|
|
|
|
|
|
List<Node> neighbors = new ArrayList<Node>(); |
|
|
|
|
neighbors.add(new Node(current.getX() + world.getSquareEdgeLenght(), current.getY())); |
|
|
|
|
neighbors.add(new Node(current.getX() - world.getSquareEdgeLenght(), current.getY())); |
|
|
|
|
neighbors.add(new Node(current.getX(), current.getY() + world.getSquareEdgeLenght())); |
|
|
|
|
neighbors.add(new Node(current.getX(), current.getY() - world.getSquareEdgeLenght())); |
|
|
|
|
current.setNeighbors(neighbors); |
|
|
|
|
|
|
|
|
|
for (Node neighbor : current.getNeighbors()) { |
|
|
|
|
if (neighbor == null) { |
|
|
|
|
if (neighbor == null || isOccupied(neighbor) || isOutsideMap(neighbor)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -53,7 +73,7 @@ public class AStar { |
|
|
|
|
|
|
|
|
|
if (!open.contains(neighbor) && !closed.contains(neighbor)) { |
|
|
|
|
neighbor.setG(nextG); |
|
|
|
|
neighbor.setH(manhattanDistance(neighbor, goal)); |
|
|
|
|
neighbor.setH(manhattanDistance(neighbor, stop)); |
|
|
|
|
neighbor.setF(neighbor.getG() + neighbor.getH()); |
|
|
|
|
neighbor.setParent(current); |
|
|
|
|
open.add(neighbor); |
|
|
|
@ -61,22 +81,29 @@ public class AStar { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node current = goal; |
|
|
|
|
Node current = stop; |
|
|
|
|
while (current.getParent() != null) { |
|
|
|
|
nodes.add(current); |
|
|
|
|
current = current.getParent(); |
|
|
|
|
} |
|
|
|
|
nodes.add(start); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Node> getNodes() { |
|
|
|
|
return nodes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int manhattanDistance(Node node1, Node node2) { |
|
|
|
|
return Math.abs(node1.getX() - node2.getX()) + Math.abs(node1.getY() - node2.getY()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isOccupied(Node node) { |
|
|
|
|
// We need to convert since obstacles in in points not nodes
|
|
|
|
|
Point point = new Point(world, node); |
|
|
|
|
return world.getObstacles().contains(point); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isOutsideMap(Node node) { |
|
|
|
|
return !(node.getX() >= 0 && node.getX() < world.getWorldWidth() && node.getY() >= 0 && node.getY() < world |
|
|
|
|
.getWorldHeight()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|