Compare commits

..

2 Commits

Author SHA1 Message Date
2f24a491b2 Changed optimizer values. 2024-07-24 22:02:37 +02:00
46a79ed419 Modifiable parameters for optimizing,
For optimization, include nodes withing x meters from route
2024-07-24 03:23:36 +02:00
2 changed files with 18 additions and 9 deletions

View File

@ -61,7 +61,7 @@ if (arguments.TryGetValue(pathArg, out string[]? pathValue))
converter.SplitOsmExportIntoRegionFiles(pathValue[0]); converter.SplitOsmExportIntoRegionFiles(pathValue[0]);
} }
Route route = Astar.FindPath(startLat, startLon, endLat, endLon, regionSize, true, importFolderPath: importPath, logger: logger); Route route = new Astar().FindPath(startLat, startLon, endLat, endLon, regionSize, true, importFolderPath: importPath, logger: logger);
if(route.RouteFound) if(route.RouteFound)
Console.WriteLine(route); Console.WriteLine(route);
else else

View File

@ -6,11 +6,14 @@ using OSM_Regions;
namespace astar namespace astar
{ {
public class Astar public class Astar(ValueTuple<float, float, float, float>? optimizingWeights = null, int? explorationDistance = null, int? explorationMultiplier = null)
{ {
private static readonly ValueTuple<float, float, float, float> DefaultPriorityWeights = new(1, 1.4f, 0, 0); private static readonly ValueTuple<float, float, float, float> DefaultPriorityWeights = new(1, 1.4f, 0, 0);
private static readonly ValueTuple<float, float, float, float> OptimizingWeights = new(1, 0, 0, 0.5f); private readonly ValueTuple<float, float, float, float> OptimizingWeights = optimizingWeights ?? new(0, 0.07f, 0, 0);
public static 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) private int ExplorationDistanceFromRoute = explorationDistance ?? 200;
private 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)
{ {
RegionLoader rl = new(regionSize, importFolderPath, logger: logger); RegionLoader rl = new(regionSize, importFolderPath, logger: logger);
Graph graph = Spiral(rl, startLat, startLon, regionSize); Graph graph = Spiral(rl, startLat, startLon, regionSize);
@ -64,9 +67,15 @@ namespace astar
if(meetingEnds is null) if(meetingEnds is null)
return new Route(graph, Array.Empty<Step>().ToList(), false); return new Route(graph, Array.Empty<Step>().ToList(), false);
PriorityQueue<ulong, int> combinedQueue = toVisitStart; List<Node> routeNodes = PathFound(graph, meetingEnds.Value.Item1, meetingEnds.Value.Item2, car).Steps.Select(s => s.Node1).ToList();
while(toVisitEnd.TryDequeue(out ulong id, out int prio)) Dictionary<ulong, int> routeQueue = toVisitStart.UnorderedItems.Select(l => l.Element).Union(toVisitEnd.UnorderedItems.Select(l => l.Element)).Where(id =>
combinedQueue.Enqueue(id, prio); {
Node p = graph.Nodes[id];
return routeNodes.Any(route => route.DistanceTo(p) < ExplorationDistanceFromRoute);
}).ToDictionary(id => id, _ => int.MinValue);
PriorityQueue<ulong, int> combinedQueue = new();
foreach ((ulong key, int value) in routeQueue)
combinedQueue.Enqueue(key, value);
ValueTuple<Node, Node>? newMeetingEnds = Optimize(additionalExploration, graph, combinedQueue, car, rl, priorityHelper, pathing, startNode.Value, endNode.Value, logger); ValueTuple<Node, Node>? newMeetingEnds = Optimize(additionalExploration, graph, combinedQueue, car, rl, priorityHelper, pathing, startNode.Value, endNode.Value, logger);
meetingEnds = newMeetingEnds ?? meetingEnds; meetingEnds = newMeetingEnds ?? meetingEnds;
@ -123,10 +132,10 @@ namespace astar
return null; return null;
} }
private static ValueTuple<Node, Node>? Optimize(float additionalExploration, Graph graph, PriorityQueue<ulong, int> combinedQueue, bool car, RegionLoader rl, PriorityHelper priorityHelper, PathMeasure pathing, Node startNode, Node goalNode, ILogger? logger = null) private ValueTuple<Node, Node>? Optimize(float additionalExploration, Graph graph, PriorityQueue<ulong, int> 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 currentPathLength = graph.Nodes.Values.Count(node => node.PreviousNodeId is not null);
int optimizeAfterFound = (int)(currentPathLength * additionalExploration); //Check another x% of unexplored Paths. int optimizeAfterFound = (int)(combinedQueue.Count * additionalExploration); //Check another x% of unexplored Paths.
logger?.LogInformation($"Path found (explored {currentPathLength} Nodes). Optimizing route. (exploring {optimizeAfterFound} additional Nodes)"); logger?.LogInformation($"Path found (explored {currentPathLength} Nodes). Optimizing route. (exploring {optimizeAfterFound} additional Nodes)");
ValueTuple<Node, Node>? newMeetingEnds = null; ValueTuple<Node, Node>? newMeetingEnds = null;
while (optimizeAfterFound-- > 0 && combinedQueue.Count > 0) while (optimizeAfterFound-- > 0 && combinedQueue.Count > 0)