From 6164b4fe17246321f9993a486b6ede89b6d67660 Mon Sep 17 00:00:00 2001 From: James Vaughan Date: Sat, 3 Feb 2024 18:45:55 -0500 Subject: [PATCH] Improved GetBoardings performance (#35) --- TMG.Visum-XTMF1/Export/ExportLineBoardings.cs | 11 ++++- TMG.Visum.Test/TestTransitAssignment.cs | 37 +++++++---------- TMG.Visum/VisumInstance/TransitBoardings.cs | 40 +++++++++++++------ 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/TMG.Visum-XTMF1/Export/ExportLineBoardings.cs b/TMG.Visum-XTMF1/Export/ExportLineBoardings.cs index fcf1ac3..7279964 100644 --- a/TMG.Visum-XTMF1/Export/ExportLineBoardings.cs +++ b/TMG.Visum-XTMF1/Export/ExportLineBoardings.cs @@ -9,8 +9,15 @@ public sealed class ExportLineBoardings : IVisumTool public void Execute(VisumInstance visumInstance) { - var boardings = visumInstance.GetBoardings(); - + List<(string lineName, float boardings)> boardings; + try + { + boardings = visumInstance.GetBoardings(); + } + catch(VisumException e) + { + throw new XTMFRuntimeException(this, e); + } using var writer = new StreamWriter(SaveTo); writer.WriteLine("LineName,Boardings"); foreach(var boarding in boardings) diff --git a/TMG.Visum.Test/TestTransitAssignment.cs b/TMG.Visum.Test/TestTransitAssignment.cs index e09bf22..cd27fd0 100644 --- a/TMG.Visum.Test/TestTransitAssignment.cs +++ b/TMG.Visum.Test/TestTransitAssignment.cs @@ -70,30 +70,23 @@ public void TestTransitAssignmentWithMultipleDays() public void TestGetLineBoardings() { using var instance = new VisumInstance("TestNetwork.ver"); - try - { - using var transitSegment = instance.GetDemandSegment("X"); - using var transitDemand = instance.CreateDemandMatrix(1, "X demand"); - // Assign 3 demand for all OD. - transitDemand.SetValues(Enumerable.Range(0, 9).Select(_ => 3.0f).ToArray()); - transitSegment.DemandMatrix = transitDemand; - var matrices = instance.ExecuteTransitAssignment(transitSegment, - new PutLoSTypes[] - { + using var transitSegment = instance.GetDemandSegment("X"); + using var transitDemand = instance.CreateDemandMatrix(1, "X demand"); + // Assign 3 demand for all OD. + transitDemand.SetValues(Enumerable.Range(0, 9).Select(_ => 3.0f).ToArray()); + transitSegment.DemandMatrix = transitDemand; + var matrices = instance.ExecuteTransitAssignment(transitSegment, + new PutLoSTypes[] + { PutLoSTypes.PerceivedJourneyTime, PutLoSTypes.JourneyTime, - }, - new HeadwayImpedanceParameters()); - var boardings = instance.GetBoardings(); - Assert.IsNotNull(boardings); - Assert.AreEqual(1, boardings.Count); - Assert.IsTrue(boardings.Sum(line => line.boardings) > 0); - DisposeMatrices(matrices); - } - finally - { - instance.SaveVersionFile("Temp.ver"); - } + }, + new HeadwayImpedanceParameters()); + var boardings = instance.GetBoardings(); + Assert.IsNotNull(boardings); + Assert.AreEqual(1, boardings.Count); + Assert.IsTrue(boardings.Sum(line => line.boardings) > 0); + DisposeMatrices(matrices); } private static void DisposeMatrices(List> matrices) diff --git a/TMG.Visum/VisumInstance/TransitBoardings.cs b/TMG.Visum/VisumInstance/TransitBoardings.cs index ecbbde4..368f929 100644 --- a/TMG.Visum/VisumInstance/TransitBoardings.cs +++ b/TMG.Visum/VisumInstance/TransitBoardings.cs @@ -1,34 +1,48 @@ -namespace TMG.Visum; +using System.Runtime.InteropServices; + +namespace TMG.Visum; public partial class VisumInstance { + /// + /// Get the boards by line. + /// This method will overwrite ADDVAL1 for the Lines. + /// + /// + /// A list of the transit line names with the + /// corresponding sum of passenger boardings. + /// + /// Throws if there is an error connecting to the VISUM server. public List<(string lineName, float boardings)> GetBoardings() { - _lock.EnterReadLock(); + _lock.EnterWriteLock(); try { ObjectDisposedException.ThrowIf(_visum is null, this); - var ret = new List<(string lineName, float boardings)>(); - double boardings = 0.0; + ExecuteEditAttributeInternal(new EditAttributeParameters() + { + NetObjectType = "LINE", + Formula = "[SUMACTIVE:LINEROUTES\\SUMACTIVE:LINEROUTEITEMS\\PASSBOARD(AP)]", + OnlyActive = true, + ResultAttributeName = "ADDVAL1" + }); foreach(ILine line in _visum.Net.Lines) { - foreach(ILineRoute route in line.LineRoutes) - { - foreach(ILineRouteItem item in route.LineRouteItems) - { - boardings += item.GetBoardings(); - } - } + double boardings = 0.0; + boardings = (double)line.AttValue["ADDVAL1"]; ret.Add((line.GetName(), (float)boardings)); } - return ret; } + catch(COMException e) + { + throw new VisumException(e); + } finally { - _lock.ExitReadLock(); + _lock.ExitWriteLock(); } }