diff --git a/astar/Astar.cs b/astar/Astar.cs index eef34a6..c72da5f 100644 --- a/astar/Astar.cs +++ b/astar/Astar.cs @@ -10,10 +10,10 @@ namespace astar { private readonly ValueTuple DefaultPriorityWeights = priorityWeights ?? new(0.75f, 1f, 0.1f, 0); private readonly ValueTuple OptimizingWeights = optimizingWeights ?? new(0, 0.07f, 0, 0); - private int ExplorationDistanceFromRoute = explorationDistance ?? 1200; - private int ExplorationMultiplier = explorationMultiplier ?? 65; + private readonly int _explorationDistanceFromRoute = explorationDistance ?? 1200; + private readonly int _explorationMultiplier = explorationMultiplier ?? 65; - public Route FindPath(float startLat, float startLon, float endLat, float endLon, float regionSize, bool car = true, PathMeasure pathing = PathMeasure.Distance, float additionalExploration = 3, string? importFolderPath = null, ILogger? logger = null) + 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) { RegionLoader rl = new(regionSize, importFolderPath, logger: logger); Graph graph = Spiral(rl, startLat, startLon, regionSize); @@ -77,12 +77,12 @@ namespace astar Dictionary routeQueue = toVisitStart.UnorderedItems.Select(l => l.Element).Union(toVisitEnd.UnorderedItems.Select(l => l.Element)).Where(id => { Node p = graph.Nodes[id]; - return routeNodes.Any(route => route.DistanceTo(p) < ExplorationDistanceFromRoute); + return routeNodes.Any(route => route.DistanceTo(p) < _explorationDistanceFromRoute); }).ToDictionary(id => id, _ => int.MinValue); PriorityQueue combinedQueue = new(); foreach ((ulong key, int value) in routeQueue) combinedQueue.Enqueue(key, value); - ValueTuple? newMeetingEnds = Optimize(additionalExploration, graph, combinedQueue, car, rl, priorityHelper, pathing, startNode.Value, endNode.Value, logger); + ValueTuple? newMeetingEnds = Optimize(graph, combinedQueue, car, rl, priorityHelper, pathing, startNode.Value, endNode.Value, logger); meetingEnds = newMeetingEnds ?? meetingEnds; return PathFound(graph, meetingEnds!.Value.Item1, meetingEnds.Value.Item2, car, logger); @@ -140,10 +140,10 @@ namespace astar return null; } - private ValueTuple? Optimize(float additionalExploration, Graph graph, PriorityQueue combinedQueue, bool car, RegionLoader rl, PriorityHelper priorityHelper, PathMeasure pathing, Node startNode, Node goalNode, ILogger? logger = null) + private ValueTuple? Optimize(Graph graph, PriorityQueue combinedQueue, bool car, RegionLoader rl, PriorityHelper priorityHelper, PathMeasure pathing, Node startNode, Node goalNode, ILogger? logger = null) { int currentPathLength = graph.Nodes.Values.Count(node => node.PreviousNodeId is not null); - int optimizeAfterFound = (int)(combinedQueue.Count * additionalExploration); //Check another x% of unexplored Paths. + int optimizeAfterFound = (int)(combinedQueue.Count * _explorationMultiplier); //Check another x% of unexplored Paths. logger?.LogInformation($"Path found (explored {currentPathLength} Nodes). Optimizing route. (exploring {optimizeAfterFound} additional Nodes)"); ValueTuple? newMeetingEnds = null; while (optimizeAfterFound-- > 0 && combinedQueue.Count > 0) diff --git a/astar/PathingHelper/PriorityHelper.cs b/astar/PathingHelper/PriorityHelper.cs index 8c36da6..899e5ad 100644 --- a/astar/PathingHelper/PriorityHelper.cs +++ b/astar/PathingHelper/PriorityHelper.cs @@ -2,8 +2,6 @@ public class PriorityHelper(double totalDistance, byte maxSpeed) { - private readonly double _totalDistance = totalDistance; - private readonly byte _maxSpeed = maxSpeed; public int CalculatePriority(Node current, Node neighbor, Node goal, byte speed, ValueTuple ratingWeights) { double neighborDistanceToGoal = neighbor.DistanceTo(goal); //we want this to be small @@ -16,10 +14,10 @@ public class PriorityHelper(double totalDistance, byte maxSpeed) neighborDistanceToGoal * neighborDistanceToGoal) / (2 * currentDistanceToGoal * currentDistanceToNeighbor))); - double speedRating = speed * 1.0 / _maxSpeed * 100; + double speedRating = speed * 1.0 / maxSpeed * 100; double angleRating = 100 - (angle < 180 ? angle / 180 : (360 - angle) / 180) * 100; - double distanceImprovedRating = 100 - (neighborDistanceToGoal - currentDistanceToGoal ) / _totalDistance * 100; - double distanceSpeedRating = ((_totalDistance / _maxSpeed) / (neighborDistanceToGoal / speed)) * 100; + double distanceImprovedRating = 100 - (neighborDistanceToGoal - currentDistanceToGoal ) / totalDistance * 100; + double distanceSpeedRating = ((totalDistance / maxSpeed) / (neighborDistanceToGoal / speed)) * 100; return (int)-(speedRating * ratingWeights.Item1 + angleRating * ratingWeights.Item2 + distanceImprovedRating * ratingWeights.Item3 + distanceSpeedRating * ratingWeights.Item4); }