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

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

View File

@ -30,7 +30,7 @@ public class Server
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);
ValueTuple<Image, Renderer.Bounds> areaDistance = Renderer.DrawPath(
@ -87,10 +87,12 @@ public class Server
if (calcTime.Key.calcTime > result.calcTime)
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 minLon = c1.longitude < c2.longitude ? c1.longitude : c2.longitude;
@ -105,22 +107,21 @@ public class Server
allRegions.GetRegion(new Coordinates(lat, lon));
}
}
Console.WriteLine("Regions Loaded. Rendering.");
ValueTuple<Image, Renderer.Bounds> baseRender = Renderer.DrawArea(allRegions);
return baseRender;
Console.WriteLine("Loaded needed Regions");
return allRegions;
}
private static void TestVariables(string workingDir, Coordinates start, Coordinates finish, int threads)
{
string parentFolder = new DirectoryInfo(workingDir).Parent!.FullName;
RegionManager rm = new (workingDir);
RegionManager rm = LoadRegions(workingDir, start, finish);
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 angle = maxAngle;
@ -128,7 +129,7 @@ public class Server
{
Pathfinder testresult = new Pathfinder(rm, priority, angle).AStar(start,
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));
}));
}
@ -139,7 +140,7 @@ public class Server
DateTime startTime = DateTime.Now;
HashSet<Thread> runningThreads = new();
while (calcThreads.Count > 0)
while (calcThreads.Count > 0 || runningThreads.Count > 0)
{
while (runningThreads.Count < threads && calcThreads.Count > 0)
{
@ -153,7 +154,7 @@ public class Server
if (newCompletedTasks > 0)
{
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}");
}
}
}