Original Post
I am currently modifying my A* implementation to include Cooperative mechanisms.
While I start running the app, it begins to freeze after a few secs . When I probe from the debugger, i find the open List is filled with candidates (about 10 seconds with 50000 candidates), then i know something is going very wrong. I check on the scores of each one and find all of them tie at 7 Initially, I design the loop so that when the current tile be the destination, it will break out of the loop. Because all scores are the same, it just chooses the one with the smallest id from the sorted set. Because the smallest id one never be the destination, so the open list continues to stack up. I wonder is my algorithm has some major flaw in it. Here is the attached code:
Could anyone give some advices on how this could be improved?
Or do you spot any bugs in this code snippet? Please let me know
I am currently stuck.
Thanks
Jack
While I start running the app, it begins to freeze after a few secs . When I probe from the debugger, i find the open List is filled with candidates (about 10 seconds with 50000 candidates), then i know something is going very wrong. I check on the scores of each one and find all of them tie at 7 Initially, I design the loop so that when the current tile be the destination, it will break out of the loop. Because all scores are the same, it just chooses the one with the smallest id from the sorted set. Because the smallest id one never be the destination, so the open list continues to stack up. I wonder is my algorithm has some major flaw in it. Here is the attached code:
TimeAStar::Path* TimeAStar::findPath(Node& from, Node& to, CObjects* unit, int depth)
{
bool solved = false;
Node current;
openList.clear();
if (useBitSet)
bitSetClosedList.reset();
else
intSetClosedList.clear();
int maxOpenSize = 0; int maxCloseSize = 0; int reachedDepth = 0;
from.transition = NULL;
from.h = from.getActualTimelessCost(to);
// error correction here
from.g = 0;
from.f = from.h;
from.depth = 0;
openList.insert(from);
while (!openList.empty())
{
current = *openList.begin();
openList.erase(openList.begin());
if (useBitSet)
{
bitSetClosedList.set(current.id);
}
else
{
intSetClosedList.insert(current.id);
}
if (current.depth > reachedDepth)
{
reachedDepth = current.depth;
if (reachedDepth == depth)
{
solved = true;
break;
}
}
if (current.x == to.x && current.z == to.z)
{
solved = true;
break;
}
for (int i = -1; i getCost(unit);
if (cost < 0)
continue;
if (useBitSet)
{
if (bitSetClosedList.at(node.id))
continue;
}
else
{
bool found = intSetClosedList.find(node.id) != intSetClosedList.end();
if (found)
continue;
}
bool contains = openList.find(node) != openList.end();
if (contains)
{
if (current.g + cost < node.g)
{
if (!openList.erase(node))
assert(false);
node.transition = t;
node.g = current.g + cost;
node.f = node.g + node.h;
node.depth = current.depth + 1;
openList.insert(node);
}
}
else
{
// thePath.push(*node);
node.transition = t;
node.g = current.g + cost;
node.h = node.getActualTimelessCost(to);
node.f = node.g + node.h;
node.depth = current.depth + 1;
openList.insert(node);
}
}
}
}
if (solved)
{
float totalCost = current.g;
std::vector transitions;
while (current.transition != NULL)
{
transitions.push_back((Transition*)current.transition);
current = current.transition->FromNode();
}
return new Path(from, transitions, totalCost);
}
}
Could anyone give some advices on how this could be improved?
Or do you spot any bugs in this code snippet? Please let me know
I am currently stuck.
Thanks
Jack