AStar/astar/Astar.cs

274 lines
15 KiB
C#
Raw Permalink Normal View History

using astar.PathingHelper;
using Microsoft.Extensions.Logging;
2024-07-22 04:56:22 +02:00
using Graph.Utils;
using OSM_Graph.Enums;
2024-07-22 04:56:22 +02:00
using OSM_Regions;
2022-05-05 02:00:55 +02:00
namespace astar
{
2024-07-29 22:02:44 +02:00
public class Astar(ValueTuple<float, float, float, float>? priorityWeights = null, int? explorationMultiplier = null, float? nonPriorityRoadSpeedPenalty = null, RegionLoader? regionLoader = null)
2022-05-05 02:00:55 +02:00
{
2024-07-29 22:02:44 +02:00
private readonly ValueTuple<float, float, float, float> DefaultPriorityWeights = priorityWeights ?? new(0.6f, 1.05f, 0, 0);
private readonly int _explorationMultiplier = explorationMultiplier ?? 120;
private readonly float _nonPriorityRoadSpeedPenalty = nonPriorityRoadSpeedPenalty ?? 0.85f;
private RegionLoader? rl = regionLoader;
public Route FindPath(float startLat, float startLon, float endLat, float endLon, float regionSize, bool car = true, PathMeasure pathing = PathMeasure.Distance, string? importFolderPath = null, ILogger? logger = null)
2022-05-06 00:02:28 +02:00
{
2024-07-29 22:02:44 +02:00
rl ??= new(regionSize, importFolderPath, logger: logger);
2024-07-22 04:56:22 +02:00
Graph graph = Spiral(rl, startLat, startLon, regionSize);
Graph endRegion = Spiral(rl, endLat, endLon, regionSize);
graph.ConcatGraph(endRegion);
KeyValuePair<ulong, Node> startNode = graph.ClosestNodeToCoordinates(startLat, startLon, car);
2024-07-22 04:56:22 +02:00
startNode.Value.PreviousIsFromStart = true;
startNode.Value.PreviousNodeId = startNode.Key;
startNode.Value.Metric = 0f;
2024-07-22 04:56:22 +02:00
KeyValuePair<ulong, Node> endNode = graph.ClosestNodeToCoordinates(endLat, endLon, car);
2024-07-22 04:56:22 +02:00
endNode.Value.PreviousIsFromStart = false;
endNode.Value.PreviousNodeId = endNode.Key;
endNode.Value.Metric = 0f;
2024-07-22 04:56:22 +02:00
double totalDistance = NodeUtils.DistanceBetween(startNode.Value, endNode.Value);
PriorityHelper priorityHelper = new(totalDistance, SpeedHelper.GetTheoreticalMaxSpeed(car));
2024-07-22 04:56:22 +02:00
logger?.Log(LogLevel.Information,
"From {0:00.00000}#{1:000.00000} to {2:00.00000}#{3:000.00000} Great-Circle {4:00000.00}km",
startNode.Value.Lat, startNode.Value.Lon, endNode.Value.Lat, endNode.Value.Lon, totalDistance / 1000);
2024-07-22 04:56:22 +02:00
PriorityQueue<ulong, int> toVisitStart = new();
toVisitStart.Enqueue(startNode.Key, 0);
PriorityQueue<ulong, int> toVisitEnd = new();
toVisitEnd.Enqueue(endNode.Key, 0);
2024-07-22 04:56:22 +02:00
ValueTuple<Node, Node>? meetingEnds = null;
2024-07-22 04:56:22 +02:00
while (toVisitStart.Count > 0 && toVisitEnd.Count > 0)
2022-05-06 00:02:28 +02:00
{
for (int i = 0; i < Math.Min(toVisitStart.Count * 0.5, 50) && meetingEnds is null; i++)
2022-05-06 00:02:28 +02:00
{
ulong closestEndNodeId = toVisitEnd.UnorderedItems.MinBy(node => graph.Nodes[node.Element].DistanceTo(graph.Nodes[toVisitStart.Peek()])).Element;
Node closestEndNode = graph.Nodes[closestEndNodeId];
2024-07-29 23:07:14 +02:00
meetingEnds = ExploreSide(true, graph, toVisitStart, priorityHelper, closestEndNode, car, DefaultPriorityWeights, pathing, logger);
2022-11-01 05:08:09 +01:00
}
2024-07-22 04:56:22 +02:00
for (int i = 0; i < Math.Min(toVisitEnd.Count * 0.5, 50) && meetingEnds is null; i++)
2024-07-22 04:56:22 +02:00
{
ulong closestStartNodeId = toVisitStart.UnorderedItems.MinBy(node => graph.Nodes[node.Element].DistanceTo(graph.Nodes[toVisitEnd.Peek()])).Element;
Node closestStartNode = graph.Nodes[closestStartNodeId];
2024-07-29 23:07:14 +02:00
meetingEnds = ExploreSide(false, graph, toVisitEnd, priorityHelper, closestStartNode, car, DefaultPriorityWeights, pathing, logger);
2024-07-22 04:56:22 +02:00
}
if (meetingEnds is not null)
break;
logger?.LogDebug($"toVisit-Queues: {toVisitStart.Count} {toVisitStart.UnorderedItems.MinBy(i => i.Priority).Priority} {toVisitEnd.Count} {toVisitEnd.UnorderedItems.MinBy(i => i.Priority).Priority}");
2022-11-13 14:14:55 +01:00
}
if(meetingEnds is null)
return new Route(graph, Array.Empty<Step>().ToList(), false);
Queue<ulong> routeQueue = new();
2024-07-29 22:02:44 +02:00
toVisitStart.EnqueueRange(toVisitEnd.UnorderedItems);
while(toVisitStart.Count > 0)
{
2024-07-29 22:02:44 +02:00
routeQueue.Enqueue(toVisitStart.Dequeue());
}
2024-07-29 22:02:44 +02:00
int optimizeAfterFound = graph.Nodes.Count(n => n.Value.PreviousNodeId is not null) * _explorationMultiplier; //Check another x% of unexplored Paths.
2024-07-29 23:07:14 +02:00
List<ValueTuple<Node, Node>> newMeetingEnds = Optimize(graph, routeQueue, optimizeAfterFound, car, rl, pathing, logger);
List<Route> routes = newMeetingEnds.Select(end => PathFound(graph, end.Item1, end.Item2, car)).ToList();
routes.Add(PathFound(graph, meetingEnds.Value.Item1, meetingEnds.Value.Item2, car));
2024-07-29 23:07:14 +02:00
return routes.MinBy(route =>
{
if (pathing is PathMeasure.Distance)
return route.Distance;
return route.Time.Ticks;
})!;
2022-11-13 14:14:55 +01:00
}
2024-07-29 23:07:14 +02:00
private ValueTuple<Node, Node>? ExploreSide(bool fromStart, Graph graph, PriorityQueue<ulong, int> toVisit, PriorityHelper priorityHelper, Node goalNode, bool car, ValueTuple<float,float,float,float> ratingWeights, PathMeasure pathing, ILogger? logger = null)
{
ulong currentNodeId = toVisit.Dequeue();
Node currentNode = graph.Nodes[currentNodeId];
logger?.LogDebug($"Distance to goal {currentNode.DistanceTo(goalNode):00000.00}m");
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNode.Neighbors)
{
2024-07-29 23:07:14 +02:00
LoadNeighbor(graph, neighborId, wayId.Key, rl!, logger);
OSM_Graph.Way way = graph.Ways[wayId.Key];
byte speed = SpeedHelper.GetSpeed(way, car);
if(!IsNeighborReachable(speed, wayId.Value, fromStart, way, car))
2024-07-24 00:23:06 +02:00
continue;
2024-07-25 02:26:42 +02:00
if (car && !way.IsPriorityRoad())
speed = (byte)(speed * _nonPriorityRoadSpeedPenalty);
Node neighborNode = graph.Nodes[neighborId];
2024-07-29 15:17:47 +02:00
if (neighborNode.PreviousIsFromStart == !fromStart) //Check if we found the opposite End
return fromStart ? new(currentNode, neighborNode) : new(neighborNode, currentNode);
float metric = (currentNode.Metric ?? float.MaxValue) + (pathing is PathMeasure.Distance
? (float)currentNode.DistanceTo(neighborNode)
: (float)currentNode.DistanceTo(neighborNode) / speed);
if (neighborNode.PreviousNodeId is null || neighborNode.Metric > metric)
{
neighborNode.PreviousNodeId = currentNodeId;
neighborNode.Metric = metric;
neighborNode.PreviousIsFromStart = fromStart;
toVisit.Enqueue(neighborId,
priorityHelper.CalculatePriority(currentNode, neighborNode, goalNode, speed, ratingWeights));
}
}
return null;
}
2024-07-29 23:07:14 +02:00
private List<ValueTuple<Node, Node>> Optimize(Graph graph, Queue<ulong> combinedQueue, int optimizeAfterFound, bool car, RegionLoader rl, PathMeasure pathing, ILogger? logger = null)
{
int currentPathLength = graph.Nodes.Values.Count(node => node.PreviousNodeId is not null);
logger?.LogInformation($"Path found (explored {currentPathLength} Nodes). Optimizing route. (exploring {optimizeAfterFound} additional Nodes)");
2024-07-29 23:07:14 +02:00
List<ValueTuple<Node, Node>> newMeetingEnds = new();
while (optimizeAfterFound-- > 0 && combinedQueue.Count > 0)
{
ulong currentNodeId = combinedQueue.Dequeue();
Node currentNode = graph.Nodes[currentNodeId];
bool fromStart = (bool)currentNode.PreviousIsFromStart!;
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNode.Neighbors)
{
2024-07-29 04:23:42 +02:00
LoadNeighbor(graph, neighborId, wayId.Key, rl, logger);
OSM_Graph.Way way = graph.Ways[wayId.Key];
byte speed = SpeedHelper.GetSpeed(way, car);
if(!IsNeighborReachable(speed, wayId.Value, fromStart, way, car))
continue;
if (car && !way.IsPriorityRoad())
speed = (byte)(speed * _nonPriorityRoadSpeedPenalty);
Node neighborNode = graph.Nodes[neighborId];
if (neighborNode.PreviousIsFromStart is not null &&
neighborNode.PreviousIsFromStart != fromStart) //Check if we found the opposite End
{
2024-07-29 23:07:14 +02:00
newMeetingEnds.Add(fromStart ? new(currentNode, neighborNode) : new(neighborNode, currentNode));
}
float metric = (currentNode.Metric ?? float.MaxValue) + (pathing is PathMeasure.Distance
? (float)currentNode.DistanceTo(neighborNode)
: (float)currentNode.DistanceTo(neighborNode) / speed);
if (neighborNode.PreviousNodeId is null || (neighborNode.PreviousIsFromStart == fromStart && neighborNode.Metric > metric))
{
neighborNode.PreviousNodeId = currentNodeId;
neighborNode.Metric = metric;
neighborNode.PreviousIsFromStart = fromStart;
combinedQueue.Enqueue(neighborId);
}
logger?.LogTrace($"Neighbor {neighborId} {neighborNode}");
logger?.LogDebug($"Optimization Contingent: {optimizeAfterFound}/{combinedQueue.Count}");
}
}
logger?.LogDebug($"Nodes in Queue after Optimization: {combinedQueue.Count}");
return newMeetingEnds;
}
2024-07-23 17:07:31 +02:00
private static Route PathFound(Graph graph, Node fromStart, Node fromEnd, bool car = true, ILogger? logger = null)
2022-11-13 14:14:55 +01:00
{
logger?.LogInformation("Path found!");
2024-07-22 04:56:22 +02:00
List<Step> path = new();
2024-07-23 17:07:31 +02:00
OSM_Graph.Way toNeighbor = graph.Ways[fromStart.Neighbors.First(n => graph.Nodes[n.Key] == fromEnd).Value.Key];
path.Add(new Step(fromStart, fromEnd, (float)fromStart.DistanceTo(fromEnd), SpeedHelper.GetSpeed(toNeighbor, car)));
2024-07-22 04:56:22 +02:00
Node current = fromStart;
while (current.Metric != 0f)
2022-11-13 14:14:55 +01:00
{
2024-07-23 17:07:31 +02:00
Node previous = graph.Nodes[(ulong)current.PreviousNodeId!];
OSM_Graph.Way previousToCurrent = graph.Ways[previous.Neighbors.First(n => graph.Nodes[n.Key] == current).Value.Key];
Step step = new(previous, current, (float)previous.DistanceTo(current), SpeedHelper.GetSpeed(previousToCurrent, car));
2024-07-22 04:56:22 +02:00
path.Add(step);
2024-07-23 17:07:31 +02:00
current = previous;
2022-11-13 14:14:55 +01:00
}
2024-07-22 04:56:22 +02:00
path.Reverse();//Since we go from the middle backwards until here
current = fromEnd;
while (current.Metric != 0f)
2022-11-13 14:14:55 +01:00
{
2024-07-23 17:07:31 +02:00
Node next = graph.Nodes[(ulong)current.PreviousNodeId!];
OSM_Graph.Way currentToNext = graph.Ways[current.Neighbors.First(n => graph.Nodes[n.Key] == next).Value.Key];
Step step = new(current, next, (float)current.DistanceTo(next), SpeedHelper.GetSpeed(currentToNext, car));
2024-07-22 04:56:22 +02:00
path.Add(step);
2024-07-23 17:07:31 +02:00
current = next;
2022-11-13 14:14:55 +01:00
}
Route r = new (graph, path, true);
logger?.LogInformation(r.ToString());
return r;
2022-11-13 14:14:55 +01:00
}
2024-07-29 04:23:42 +02:00
private static bool IsNeighborReachable(byte speed, bool wayDirection, bool fromStart, OSM_Graph.Way way, bool car)
{
if(speed < 1)
return false;
if(!way.AccessPermitted())
return false;
if(wayDirection && way.GetDirection() == (fromStart ? WayDirection.Backwards : WayDirection.Forwards) && car)
return false;
if(!wayDirection && way.GetDirection() == (fromStart ? WayDirection.Forwards : WayDirection.Backwards) && car)
return false;
return true;
}
private static void LoadNeighbor(Graph graph, ulong neighborId, ulong wayId, RegionLoader rl, ILogger? logger = null)
{
if (!graph.ContainsNode(neighborId))
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
if (!graph.ContainsWay(wayId))
{
logger?.LogDebug("Loading way... This will be slow.");
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
graph.ConcatGraph(Graph.FromGraph(g));
}
}
2024-07-22 04:56:22 +02:00
private static Graph Spiral(RegionLoader loader, float lat, float lon, float regionSize)
2022-11-13 14:14:55 +01:00
{
2024-07-22 04:56:22 +02:00
Graph? ret = Graph.FromGraph(loader.LoadRegionFromCoordinates(lat, lon));
int iteration = 1;
while (ret is null)
2022-11-13 14:14:55 +01:00
{
2024-07-22 04:56:22 +02:00
for (int x = -iteration; x <= iteration; x++)
{
Graph? g1 = Graph.FromGraph(loader.LoadRegionFromCoordinates(lat + x * regionSize, lon - iteration * regionSize));
Graph? g2 = Graph.FromGraph(loader.LoadRegionFromCoordinates(lat + x * regionSize, lon + iteration * regionSize));
if (ret is not null)
{
ret.ConcatGraph(g1);
ret.ConcatGraph(g2);
}
else if (ret is null && g1 is not null)
{
ret = g1;
ret.ConcatGraph(g2);
}else if (ret is null && g2 is not null)
ret = g2;
}
for (int y = -iteration + 1; y < iteration; y++)
{
Graph? g1 = Graph.FromGraph(loader.LoadRegionFromCoordinates(lat - iteration * regionSize, lon + y * regionSize));
Graph? g2 = Graph.FromGraph(loader.LoadRegionFromCoordinates(lat + iteration * regionSize, lon + y * regionSize));
if (ret is not null)
{
ret.ConcatGraph(g1);
ret.ConcatGraph(g2);
}
else if (ret is null && g1 is not null)
{
ret = g1;
ret.ConcatGraph(g2);
}else if (ret is null && g2 is not null)
ret = g2;
}
iteration++;
2022-11-13 14:14:55 +01:00
}
2024-07-22 04:56:22 +02:00
return ret;
2022-11-13 14:14:55 +01:00
}
2022-05-05 02:00:55 +02:00
}
}