Skip to content

Commit

Permalink
Improved GetBoardings performance (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesVaughan authored Feb 3, 2024
1 parent db6c229 commit 6164b4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
11 changes: 9 additions & 2 deletions TMG.Visum-XTMF1/Export/ExportLineBoardings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 15 additions & 22 deletions TMG.Visum.Test/TestTransitAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<VisumMatrix>> matrices)
Expand Down
40 changes: 27 additions & 13 deletions TMG.Visum/VisumInstance/TransitBoardings.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
namespace TMG.Visum;
using System.Runtime.InteropServices;

namespace TMG.Visum;

public partial class VisumInstance
{

/// <summary>
/// Get the boards by line.
/// This method will overwrite ADDVAL1 for the Lines.
/// </summary>
/// <returns>
/// A list of the transit line names with the
/// corresponding sum of passenger boardings.
/// </returns>
/// <exception cref="VisumException">Throws if there is an error connecting to the VISUM server.</exception>
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();
}
}

Expand Down

0 comments on commit 6164b4f

Please sign in to comment.