OSMServer/Server/Server.cs

161 lines
7.2 KiB
C#
Raw Normal View History

using System.Drawing;
using System.Drawing.Imaging;
2023-04-01 01:00:22 +02:00
using OSMDatastructure;
using OSMDatastructure.Graph;
2023-04-01 14:43:22 +02:00
using Pathfinding;
using RenderPath;
2023-04-21 00:43:58 +02:00
using Region = OSMDatastructure.Region;
2023-04-01 01:00:22 +02:00
2023-02-02 22:30:43 +01:00
namespace Server;
2023-02-02 19:03:00 +01:00
public class Server
{
2023-02-08 19:09:54 +01:00
2023-02-02 22:30:43 +01:00
public static void Main(string[] args)
2023-02-02 19:03:00 +01:00
{
ConsoleWriter newConsole = new();
Console.SetOut(newConsole);
Console.SetError(newConsole);
string workingDir = "D:/stuttgart-regbez-latest";
2023-04-23 14:48:00 +02:00
2023-04-13 00:24:33 +02:00
//RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
2023-04-01 14:43:22 +02:00
//RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map");
2023-04-06 14:46:19 +02:00
//RegionConverter.ConvertXMLToRegions("D:/germany-latest.osm", "D:/germany-latest");
2023-04-13 00:24:33 +02:00
Coordinates start = new (48.7933798f, 9.8275859f);
Coordinates finish = new (48.795918f, 9.021618f);
2023-04-21 00:43:58 +02:00
2023-04-23 15:00:22 +02:00
TestVariables(workingDir, start, finish, 16);
GetShortestRoute("D:");
2023-04-23 14:51:13 +02:00
/*
2023-04-23 15:26:29 +02:00
ValueTuple<Image, Renderer.Bounds> area = Renderer.DrawArea(LoadRegions(workingDir, start, finish));
2023-04-23 14:51:13 +02:00
area.Item1.Save(@"D:\Base.png", ImageFormat.Png);
ValueTuple<Image, Renderer.Bounds> areaDistance = Renderer.DrawPath(
PathResult.PathresultFromFile(@"D:\angle0,140_level0,020_same0,160.result"), Image.FromFile(@"D:\Base.png"), area.Item2);
areaDistance.Item1.Save(@"D:\Distance.png", ImageFormat.Png);
2023-04-21 00:43:58 +02:00
2023-04-23 14:51:13 +02:00
ValueTuple<Image, Renderer.Bounds> areaWeight = Renderer.DrawPath(
PathResult.PathresultFromFile(@"D:\angle0,160_level0,020_same0,020.result"), Image.FromFile(@"D:\Base.png"), area.Item2);
areaWeight.Item1.Save(@"D:\Weight.png", ImageFormat.Png);
*/
2023-04-23 15:00:22 +02:00
/*
2023-04-21 00:43:58 +02:00
string parentFolder = new DirectoryInfo(workingDir).Parent!.FullName;
2023-04-23 14:48:00 +02:00
Pathfinder result = new Pathfinder(workingDir, 0.002, 0,
0, 1, 30).AStar(start,
finish, Tag.SpeedType.car);
2023-04-21 00:43:58 +02:00
Console.WriteLine($"Calc-time {result.pathResult!.calcTime} Path-length: {result.pathResult.pathNodes.Count} Visited-nodes: {result.gScore!.Count}");
string fileName = DateTime.Now.ToFileTime().ToString();
2023-04-21 00:43:58 +02:00
string resultFileName = $"{new DirectoryInfo(workingDir).Name}-{fileName}.result";
result.SaveResult(Path.Join(parentFolder, resultFileName));
2023-04-21 00:43:58 +02:00
string renderFileName = $"{new DirectoryInfo(workingDir).Name}-{fileName}.render.png";
Image render = Renderer.DrawPathfinder(result);
#pragma warning disable CA1416
render.Save(Path.Join(parentFolder, renderFileName), ImageFormat.Png);
2023-04-21 00:43:58 +02:00
#pragma warning restore CA1416*/
2023-04-23 14:51:13 +02:00
Console.Beep(400, 100);
}
private static void GetShortestRoute(string directory)
{
DateTime start = DateTime.Now;
HashSet<string> allFiles = Directory.GetFiles(directory).Where(file => file.EndsWith(".result")).ToHashSet();
PathResult first = PathResult.PathresultFromFile(allFiles.First());
KeyValuePair<PathResult, string> shortest = new(first, allFiles.First());
KeyValuePair<PathResult, string> fastest = new(first, allFiles.First());
KeyValuePair<PathResult, string> calcTime = new(first, allFiles.First());
int loaded = 0;
foreach (string filePath in allFiles)
{
PathResult result = PathResult.PathresultFromFile(filePath);
Console.WriteLine($"{loaded++}/{allFiles.Count()} {filePath} " +
$"Time elapsed: {DateTime.Now - start} " +
$"Remaining {((DateTime.Now - start)/loaded)*(allFiles.Count-loaded)}");
if (shortest.Key.distance > result.distance)
shortest = new KeyValuePair<PathResult, string>(result, filePath);
if (fastest.Key.weight > result.weight)
fastest = new KeyValuePair<PathResult, string>(result, filePath);
if (calcTime.Key.calcTime > result.calcTime)
calcTime = new KeyValuePair<PathResult, string>(result, filePath);
}
2023-04-23 15:27:06 +02:00
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}");
2023-02-02 19:03:00 +01:00
}
2023-04-23 15:26:29 +02:00
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;
float maxLat = c1.latitude > c2.latitude ? c1.latitude : c2.latitude;
float maxLon = c1.longitude > c2.longitude ? c1.longitude : c2.longitude;
RegionManager allRegions = new(workingDir);
for (float lat = minLat - Region.RegionSize; lat < maxLat + Region.RegionSize; lat += Region.RegionSize / 2)
{
for (float lon = minLon - Region.RegionSize; lon < maxLon + Region.RegionSize; lon += Region.RegionSize / 2)
{
allRegions.GetRegion(new Coordinates(lat, lon));
}
}
2023-04-23 15:26:29 +02:00
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;
2023-04-21 15:14:02 +02:00
2023-04-23 15:26:29 +02:00
RegionManager rm = LoadRegions(workingDir, start, finish);
Queue<Thread> calcThreads = new();
2023-04-23 15:07:26 +02:00
for (double roadLevelPriority = 0.02; roadLevelPriority > 0; roadLevelPriority -= 0.001)
{
2023-04-23 15:07:26 +02:00
for (double maxAngle = 5; maxAngle < 45; maxAngle += 5)
{
2023-04-23 15:07:26 +02:00
double priority = roadLevelPriority;
double angle = maxAngle;
calcThreads.Enqueue(new Thread(() =>
{
2023-04-23 15:07:26 +02:00
Pathfinder testresult = new Pathfinder(rm, priority, angle).AStar(start,
finish, Tag.SpeedType.car);
2023-04-23 15:09:04 +02:00
string fileName = $"angle{angle:00}_level{priority:0.000}.result";
2023-04-23 15:07:26 +02:00
testresult.SaveResult(Path.Join(parentFolder, fileName));
}));
}
}
int totalTasks = calcThreads.Count;
int completedTasks = 0;
DateTime startTime = DateTime.Now;
HashSet<Thread> runningThreads = new();
while (calcThreads.Count > 0)
{
while (runningThreads.Count < threads && calcThreads.Count > 0)
{
Thread t = calcThreads.Dequeue();
runningThreads.Add(t);
t.Start();
}
int newCompletedTasks = runningThreads.RemoveWhere(thread => !thread.IsAlive);
completedTasks += newCompletedTasks;
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}");
}
}
}
2023-02-02 19:03:00 +01:00
}