My goal is to debug the A* program.
So whenever a node is opened, I want to display it visually.
Despite all the programming flaws (due to testing purposes only), how do I keep a SortedSet of ArrayList
of Opened Nodes?
In this case, I just keep all the nodes that are opened along the correct path.
How do I display all other opened/discarded nodes as well?
Thanks
Jack
public synchronized Path2 findPath(Node2 from, Node2 to, Unit unit) throws NoPathException {
if (unit == null)
throw new NullPointerException("context is null");
unit.clearOpenedNodes();
try {
Node2 current = null;
openList.clear();
if (useBitSet)
bitSetClosedList.clear();
else intSetClosedList.clear();
int maxOpenSize = 0;
from.transition = null;
from.h = from.getActualTimelessCost(to);
if (from.h < 0)
throw new NoPathException("initial cost: " + from.h);
from.g = 0;
from.f = from.h;
openList.add(from);
// Add backup node to the unit object
PathPoint2 pp = new PathPoint2(from.x, from.z, 0);
pp.f = from.getF();
pp.g = from.getG();
pp.h = from.getH();
unit.AddOpenedNode(pp);
while (! openList.isEmpty()) {
current = openList.first();
if (! openList.remove(current))
assert false;
// find nodes in open list till it is exhausted
if (useBitSet) {
bitSetClosedList.set(current.id);
} else {
intSetClosedList.add(current.id);
}
if (current.x == to.x && current.z == to.z) {
float totalCost = current.g;
List<AStar2.Transition2> transitions = new ArrayList<AStar2.Transition2>();
while (current.transition != null) {
transitions.add(0, current.transition);
current = current.transition.fromNode();
}
return new AStar2.Path2(from, transitions, totalCost);
}
for (Transition2 transition : current.getTransitions()) {
float cost = transition.getCost(unit);
if (cost < 0)
continue;
// new a node out, id will not be the same, so a new node is produced
Node2 neighbour = transition.toNode();
if (useBitSet) {
if (bitSetClosedList.get(neighbour.id))
continue;
} else {
if (intSetClosedList.contains(neighbour.id))
continue;
}
if (openList.contains(neighbour))
{
// check if this path is better
if (current.g + cost < neighbour.g)
{
if (!openList.remove(neighbour))
assert false;
neighbour.transition = transition;
neighbour.g = current.g + cost;
neighbour.f = neighbour.g + neighbour.h;
pp = new PathPoint2(neighbour.x, neighbour.z, 0);
pp.f = neighbour.getF();
pp.g = neighbour.getG();
pp.h = neighbour.getH();
unit.AddOpenedNode(pp);
openList.add(neighbour);
}
} else { // if neighbour in openList
neighbour.transition = transition;
neighbour.g = current.g + cost;
neighbour.h = neighbour.getActualTimelessCost(to);
neighbour.f = neighbour.g + neighbour.h;
// Add backup node to the unit object
pp = new PathPoint2(neighbour.x, neighbour.z, 0);
pp.f = neighbour.getF();
pp.g = neighbour.getG();
pp.h = neighbour.getH();
unit.AddOpenedNode(pp);
openList.add(neighbour);
}
}
}
} finally {}
throw new NoPathException();
}