From 84350f4be28ab8a0914c89de69bfbfd1077374b7 Mon Sep 17 00:00:00 2001 From: Michael Krisper <michael.krisper@tugraz.at> Date: Fri, 20 May 2016 16:32:32 +0200 Subject: [PATCH] performance Optimizations --- .../Impl/DistanceBasedDrivingCycle.cs | 7 +++-- .../Models/SimulationComponent/Impl/Driver.cs | 12 ++++----- VectoCore/VectoCore/Utils/DelaunayMap.cs | 26 +++++++++++-------- VectoCore/VectoCore/Utils/VectoCSVFile.cs | 5 ++-- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs index 1559a6784e..e083a333f7 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs @@ -279,13 +279,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl CycleIntervalIterator.LeftSample.RoadGradient); } + /// <summary> + /// Progress of the distance in the driving cycle. + /// </summary> public double Progress { get { return _data.Entries.Count > 0 - ? ((CurrentState.Distance - _data.Entries.First().Distance) / - (_data.Entries.Last().Distance - _data.Entries.First().Distance)).Value() + ? (CurrentState.Distance.Value() - _data.Entries.First().Distance.Value()) / + (_data.Entries.Last().Distance.Value() - _data.Entries.First().Distance.Value()) : 0; } } diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Driver.cs index 822d5223fa..58397a0eab 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Driver.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Driver.cs @@ -57,7 +57,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl protected IDriverStrategy DriverStrategy; private Dictionary<string, object> _coastData = new Dictionary<string, object>(20); - private string _currentAction = ""; + public string CurrentAction = ""; //public MeterPerSquareSecond LookaheadDeceleration { get; protected set; } @@ -155,7 +155,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl IResponse previousResponse = null) { IterationStatistics.Increment(this, "Accelerate"); - _currentAction = "Accelerate"; + CurrentAction = "Accelerate"; Log.Debug("DrivingAction Accelerate"); var operatingPoint = ComputeAcceleration(ds, targetVelocity); @@ -235,7 +235,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl public IResponse DrivingActionCoast(Second absTime, Meter ds, MeterPerSecond maxVelocity, Radian gradient) { IterationStatistics.Increment(this, "Coast"); - _currentAction = "Coast"; + CurrentAction = "Coast"; Log.Debug("DrivingAction Coast"); return CoastOrRollAction(absTime, ds, maxVelocity, gradient, false); @@ -251,7 +251,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl /// <returns></returns> public IResponse DrivingActionRoll(Second absTime, Meter ds, MeterPerSecond maxVelocity, Radian gradient) { - _currentAction = "Roll"; + CurrentAction = "Roll"; IterationStatistics.Increment(this, "Roll"); Log.Debug("DrivingAction Roll"); @@ -357,7 +357,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl IResponse previousResponse = null, Meter targetDistance = null) { IterationStatistics.Increment(this, "Brake"); - _currentAction = "Brake"; + CurrentAction = "Brake"; Log.Debug("DrivingAction Brake"); IResponse retVal = null; @@ -765,7 +765,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl /// <returns></returns> public IResponse DrivingActionHalt(Second absTime, Second dt, MeterPerSecond targetVelocity, Radian gradient) { - _currentAction = "Halt"; + CurrentAction = "Halt"; if (!targetVelocity.IsEqual(0) || !DataBus.VehicleStopped) { Log.Error("TargetVelocity ({0}) and VehicleVelocity ({1}) must be zero when vehicle is halting!", targetVelocity, DataBus.VehicleSpeed); diff --git a/VectoCore/VectoCore/Utils/DelaunayMap.cs b/VectoCore/VectoCore/Utils/DelaunayMap.cs index f019b82c25..321c0aa3ba 100644 --- a/VectoCore/VectoCore/Utils/DelaunayMap.cs +++ b/VectoCore/VectoCore/Utils/DelaunayMap.cs @@ -36,6 +36,7 @@ using System.Drawing; using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms.DataVisualization.Charting; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.Models; @@ -100,19 +101,22 @@ namespace TUGraz.VectoCore.Utils foreach (var point in points) { // If the vertex lies inside the circumcircle of a triangle, the edges of this triangle are // added to the edge buffer and the triangle is removed from list. - var point1 = point; - var containerTriangles = triangles.FindAll(t => t.ContainsInCircumcircle(point1)); - triangles = triangles.Except(containerTriangles).ToList(); - // Remove duplicate edges. This leaves the convex hull of the edges. // The edges in this convex hull are oriented counterclockwise! - var allEdges = containerTriangles.SelectMany(t => t.GetEdges()).ToList(); - var groupedEdges = allEdges.GroupBy(edge => edge).ToList(); - var convexHullEdges = groupedEdges.Where(group => group.Count() == 1).Select(group => group.Key).ToList(); - var newTriangles = convexHullEdges.Select(edge => new Triangle(edge.P1, edge.P2, point)).ToList(); + var newTriangles = triangles.Select((t, i) => Tuple.Create(i, t, t.ContainsInCircumcircle(point))) + .Where(t => t.Item3) + .Reverse() + .SelectMany(t => { + triangles.RemoveAt(t.Item1); + return t.Item2.GetEdges(); + }) + .GroupBy(edge => edge) + .Where(group => group.Count() == 1) + .Select(group => new Triangle(group.Key.P1, group.Key.P2, point)).ToList(); triangles.AddRange(newTriangles); + //DrawGraph(pointCount, triangles, superTriangle, xmin, xmax, ymin, ymax, point); pointCount++; @@ -148,18 +152,18 @@ namespace TUGraz.VectoCore.Utils } } - /// <summary> /// Draws the delaunay map (except supertriangle). /// </summary> [Conditional("TRACE")] - private static void DrawGraph(int i, List<Triangle> triangles, Triangle superTriangle, Point[] points, Point lastPoint = null) + private static void DrawGraph(int i, List<Triangle> triangles, Triangle superTriangle, Point[] points, + Point lastPoint = null) { var xmin = points.Min(p => p.X); var xmax = points.Max(p => p.X); var ymin = points.Min(p => p.Y); var ymax = points.Max(p => p.Y); - + using (var chart = new Chart { Width = 1000, Height = 1000 }) { chart.ChartAreas.Add(new ChartArea("main") { AxisX = new Axis { Minimum = Math.Min(xmin, xmin), Maximum = Math.Max(xmax, xmax) }, diff --git a/VectoCore/VectoCore/Utils/VectoCSVFile.cs b/VectoCore/VectoCore/Utils/VectoCSVFile.cs index 8f38a3ad21..2029c8659f 100644 --- a/VectoCore/VectoCore/Utils/VectoCSVFile.cs +++ b/VectoCore/VectoCore/Utils/VectoCSVFile.cs @@ -58,6 +58,7 @@ namespace TUGraz.VectoCore.Utils /// </remarks> public class VectoCSVFile : LoggingObject { + private static readonly Regex HeaderFilter = new Regex(@"\[.*?\]|\<|\>", RegexOptions.Compiled); private const char Delimiter = ','; private const char Comment = '#'; @@ -162,9 +163,7 @@ namespace TUGraz.VectoCore.Utils private static IEnumerable<string> GetColumns(string line, bool fullHeader = false) { if (!fullHeader) { - line = Regex.Replace(line, @"\[.*?\]", ""); - line = line.Replace("<", ""); - line = line.Replace(">", ""); + line = HeaderFilter.Replace(line, ""); } return line.Split(Delimiter).Select(col => col.Trim()); } -- GitLab