Compare commits

...

7 Commits

Author SHA1 Message Date
6b5dddb1e3 Prevent method from returning before all threads are finished.
Include calculating threads in output.
2023-04-23 16:03:39 +02:00
fea0ecf17b remove exisiting results. 2023-04-23 16:02:55 +02:00
30b29aa25c whitespace and readability 2023-04-23 15:27:23 +02:00
73e7daffd7 Formatting of finding best variables 2023-04-23 15:27:06 +02:00
7b88616373 Preload Regions 2023-04-23 15:26:29 +02:00
2799db162d readability 2023-04-23 15:09:22 +02:00
9301e948b0 Filename-formatting 2023-04-23 15:09:04 +02:00
2 changed files with 20 additions and 14 deletions

View File

@ -136,8 +136,12 @@ public class Pathfinder
if (priority == 0) if (priority == 0)
return double.MaxValue; return double.MaxValue;
double distance = Utils.DistanceBetween(neighborNode, goalNode);
double speed = regionManager.GetSpeedForEdge(currentNode, edge.wayId, _speedType); double speed = regionManager.GetSpeedForEdge(currentNode, edge.wayId, _speedType);
double roadPriority = priority * roadPriorityFactor;
double angle = 0; double angle = 0;
if (_cameFromDict!.ContainsKey(currentNode)) if (_cameFromDict!.ContainsKey(currentNode))
{ {
@ -151,12 +155,13 @@ public class Pathfinder
angle = nodeAngle / 180; angle = nodeAngle / 180;
} }
double roadPriority = priority * roadPriorityFactor; return distance / (speed * angle + roadPriority + 1);
return Utils.DistanceBetween(neighborNode, goalNode) / (speed * angle + roadPriority + 1);
} }
public void SaveResult(string path) public void SaveResult(string path)
{ {
if(File.Exists(path))
File.Delete(path);
FileStream fs = new (path, FileMode.CreateNew); FileStream fs = new (path, FileMode.CreateNew);
JsonSerializer.Serialize(fs, pathResult, JsonSerializerOptions.Default); JsonSerializer.Serialize(fs, pathResult, JsonSerializerOptions.Default);
fs.Dispose(); fs.Dispose();

View File

@ -30,7 +30,7 @@ public class Server
GetShortestRoute("D:"); GetShortestRoute("D:");
/* /*
ValueTuple<Image, Renderer.Bounds> area = RenderAreaBaseImage(workingDir, start, finish); ValueTuple<Image, Renderer.Bounds> area = Renderer.DrawArea(LoadRegions(workingDir, start, finish));
area.Item1.Save(@"D:\Base.png", ImageFormat.Png); area.Item1.Save(@"D:\Base.png", ImageFormat.Png);
ValueTuple<Image, Renderer.Bounds> areaDistance = Renderer.DrawPath( ValueTuple<Image, Renderer.Bounds> areaDistance = Renderer.DrawPath(
@ -87,10 +87,12 @@ public class Server
if (calcTime.Key.calcTime > result.calcTime) if (calcTime.Key.calcTime > result.calcTime)
calcTime = new KeyValuePair<PathResult, string>(result, filePath); calcTime = new KeyValuePair<PathResult, string>(result, filePath);
} }
Console.WriteLine($"Shortest: {shortest.Key.distance} {shortest.Value}\nFastest: {shortest.Key.weight} {fastest.Value}\nCalcTime: {calcTime.Key.calcTime} {calcTime.Value}"); Console.WriteLine($"\nShortest:\t{shortest.Key.distance:0.0} {shortest.Key.weight:0.00} {shortest.Key.calcTime} {shortest.Value}\n" +
$"Fastest:\t{fastest.Key.distance:0.0} {fastest.Key.weight:0.00} {fastest.Key.calcTime} {fastest.Value}\n" +
$"CalcTime:\t{calcTime.Key.distance:0.0} {calcTime.Key.weight:0.00} {calcTime.Key.calcTime} {calcTime.Value}");
} }
private static ValueTuple<Image, Renderer.Bounds> RenderAreaBaseImage(string workingDir, Coordinates c1, Coordinates c2) private static RegionManager LoadRegions(string workingDir, Coordinates c1, Coordinates c2)
{ {
float minLat = c1.latitude < c2.latitude ? c1.latitude : c2.latitude; float minLat = c1.latitude < c2.latitude ? c1.latitude : c2.latitude;
float minLon = c1.longitude < c2.longitude ? c1.longitude : c2.longitude; float minLon = c1.longitude < c2.longitude ? c1.longitude : c2.longitude;
@ -105,22 +107,21 @@ public class Server
allRegions.GetRegion(new Coordinates(lat, lon)); allRegions.GetRegion(new Coordinates(lat, lon));
} }
} }
Console.WriteLine("Regions Loaded. Rendering."); Console.WriteLine("Loaded needed Regions");
ValueTuple<Image, Renderer.Bounds> baseRender = Renderer.DrawArea(allRegions); return allRegions;
return baseRender;
} }
private static void TestVariables(string workingDir, Coordinates start, Coordinates finish, int threads) private static void TestVariables(string workingDir, Coordinates start, Coordinates finish, int threads)
{ {
string parentFolder = new DirectoryInfo(workingDir).Parent!.FullName; string parentFolder = new DirectoryInfo(workingDir).Parent!.FullName;
RegionManager rm = new (workingDir); RegionManager rm = LoadRegions(workingDir, start, finish);
Queue<Thread> calcThreads = new(); Queue<Thread> calcThreads = new();
for (double roadLevelPriority = 0.02; roadLevelPriority > 0; roadLevelPriority -= 0.001) for (double roadLevelPriority = 0.016; roadLevelPriority < 0.02; roadLevelPriority += 0.0002)
{ {
for (double maxAngle = 5; maxAngle < 45; maxAngle += 5) for (double maxAngle = 25; maxAngle < 35; maxAngle += 1)
{ {
double priority = roadLevelPriority; double priority = roadLevelPriority;
double angle = maxAngle; double angle = maxAngle;
@ -128,7 +129,7 @@ public class Server
{ {
Pathfinder testresult = new Pathfinder(rm, priority, angle).AStar(start, Pathfinder testresult = new Pathfinder(rm, priority, angle).AStar(start,
finish, Tag.SpeedType.car); finish, Tag.SpeedType.car);
string fileName = $"angle{angle:0.000}_level{priority:0.000}.result"; string fileName = $"angle{angle:00}_level{priority:0.0000}.result";
testresult.SaveResult(Path.Join(parentFolder, fileName)); testresult.SaveResult(Path.Join(parentFolder, fileName));
})); }));
} }
@ -139,7 +140,7 @@ public class Server
DateTime startTime = DateTime.Now; DateTime startTime = DateTime.Now;
HashSet<Thread> runningThreads = new(); HashSet<Thread> runningThreads = new();
while (calcThreads.Count > 0) while (calcThreads.Count > 0 || runningThreads.Count > 0)
{ {
while (runningThreads.Count < threads && calcThreads.Count > 0) while (runningThreads.Count < threads && calcThreads.Count > 0)
{ {
@ -153,7 +154,7 @@ public class Server
if (newCompletedTasks > 0) if (newCompletedTasks > 0)
{ {
TimeSpan elapsedTime = DateTime.Now - startTime; TimeSpan elapsedTime = DateTime.Now - startTime;
Console.WriteLine($"To calculate: {calcThreads.Count}/{totalTasks} Time Average: {(elapsedTime)/(completedTasks)} Elapsed: {elapsedTime} Remaining: {(elapsedTime)/(completedTasks)*calcThreads.Count}"); Console.WriteLine($"To calculate: {calcThreads.Count}(+{runningThreads.Count})/{totalTasks} Time Average: {(elapsedTime)/(completedTasks)} Elapsed: {elapsedTime} Remaining: {(elapsedTime)/(completedTasks)*calcThreads.Count}");
} }
} }
} }