Added checks if item already in queue, only update direct-distance if it not already calculated.

This commit is contained in:
glax 2023-04-09 19:22:21 +02:00
parent 2ca4207fd7
commit 9448187452
2 changed files with 16 additions and 14 deletions

View File

@ -27,18 +27,18 @@ public static partial class Pathfinder
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
if (neighbor is not null)
{
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
double newPotentialLength = currentNode.currentPathLength + Utils.DistanceBetween(currentNode, neighbor);
if (newPotentialLength < neighbor.currentPathLength)
{
neighbor.previousPathNode = currentNode;
neighbor.currentPathLength = newPotentialLength;
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
if (neighbor.Equals(goalNode))
{
stop = true;
}
else if(!stop)
if(neighbor.Equals(goalNode))
return GetRouteFromCalc(goalNode, regionManager);
//stop = true;
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)) && !stop)
{
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
}

View File

@ -29,22 +29,24 @@ public static partial class Pathfinder
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
if (neighbor is not null)
{
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
double newPotentialWeight = currentNode.currentPathWeight +
EdgeWeight(currentNode, edge, vehicle, regionManager);
if (newPotentialWeight < neighbor.currentPathWeight)
{
neighbor.previousPathNode = currentNode;
neighbor.currentPathLength = currentNode.currentPathLength + Utils.DistanceBetween(currentNode, neighbor);
neighbor.currentPathWeight = newPotentialWeight;
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
if (neighbor.Equals(goalNode))
if(neighbor.Equals(goalNode))
return GetRouteFromCalc(goalNode, regionManager);
//stop = true;
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)) && !stop)
{
stop = true;
}
else if(!stop)
{
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
toVisit.Enqueue(neighbor, neighbor.currentPathWeight + neighbor.directDistanceToGoal);
}
}
}