/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.Result { using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; using OpenTraffic.Model.Simulator; [Serializable] public class ResultLink { [XmlAttribute(AttributeName = "LinkId")] public int LinkId { get; set; } [XmlAttribute(AttributeName = "FlowIn")] public float FlowIn { get; set; } [XmlAttribute(AttributeName = "FlowOut")] public float FlowOut { get; set; } [XmlAttribute(AttributeName = "NumberOfCarsOnLink")] public int NumberOfCarsOnLink { get; set; } [XmlAttribute(AttributeName = "NumberOfCarsQueue")] public float NumberOfCarsQueue { get; set; } [XmlAttribute(AttributeName = "Occupied")] public float Occupied { get; set; } [XmlAttribute(AttributeName = "TravelTime")] public float TravelTime { get; set; } [XmlAttribute(AttributeName = "QueuingTime")] public float QueuingTime { get; set; } [XmlAttribute(AttributeName = "EstimatedFlowOut")] public float EstimatedFlowOut { get; set; } private int vehicleIn; private int vehicleInLeft; private int vehicleOut; private float totLinkTime; private float totTravelTime; public ResultLink() { } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] public ResultLink(ResultLink r) { TravelTime = r.TravelTime; FlowIn = r.FlowIn; FlowOut = r.FlowOut; NumberOfCarsOnLink = r.NumberOfCarsOnLink; NumberOfCarsQueue = r.NumberOfCarsQueue; EstimatedFlowOut = r.EstimatedFlowOut; QueuingTime = r.QueuingTime; Occupied = r.Occupied; LinkId = r.LinkId; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] public ResultLink(TrafficModelLink l) { TravelTime = 0.0f; LinkId = l.Id; } public ResultLink(int id) { TravelTime = 0.0f; LinkId = id; } public void RegisterArrival(Vehicle v, float t) { if (v != null) { vehicleInLeft++; vehicleIn++; v.LastTimePassedNode = t; } } public void RegisterDepartureOutBin(Vehicle v) { if (v != null) { vehicleOut++; } } public void RegisterStartQueing(Vehicle v, float t) { if (v != null) { totTravelTime += t - v.LastTimePassedNode; } } public void RegisterDepartureInBin(Vehicle v, float t) { if (v != null) { vehicleInLeft--; totLinkTime += t - v.LastTimePassedNode; } } public void RegisterLinkStatus(ILinkSimulator l) { if (l != null) { NumberOfCarsOnLink = l.GetNumberOfVehicles(); NumberOfCarsQueue = l.GetNumberOfVehiclesInQueue(); Occupied = l.GetRatioOccupied(); EstimatedFlowOut = l.GetEstimatedFlow(); } } public bool CloseBin(float startTime, float stopTime, float simulationStopTime, bool blockForeever) { FlowIn = vehicleIn / (stopTime - startTime); FlowOut = vehicleOut / (stopTime - startTime); float extraQueingTime = vehicleInLeft * (simulationStopTime - startTime); if (!blockForeever) { if (vehicleIn != 0) { TravelTime = totTravelTime / vehicleIn; QueuingTime = (extraQueingTime + totLinkTime - totTravelTime) / vehicleIn; } } else { TravelTime = 0; QueuingTime = simulationStopTime - startTime; } if (vehicleInLeft != 0) return false; return true; } public void Merge(ResultLink r, float factor, TrafficModelLink l) { if (l != null) { if (r == null) { TravelTime = TravelTime * factor + l.CalculateFreeFlowTravelTime() * (1 - factor); QueuingTime = QueuingTime * factor; } else { TravelTime = TravelTime * factor + r.TravelTime * (1 - factor); QueuingTime = QueuingTime * factor + r.TravelTime * (1 - factor); } } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public delegate float ResultLinkFunction(ResultLink rl, TrafficModelLink l); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly TrafficProperty FlowInProperty = new TrafficProperty("Flow In", delegate(ResultLink rl, TrafficModelLink l) { return rl.FlowIn; }); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly TrafficProperty FlowOutProperty = new TrafficProperty("Flow Out", delegate(ResultLink rl, TrafficModelLink l) { return rl.FlowOut; }); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly TrafficProperty TravelTimeProperty = new TrafficProperty("Travel Time", delegate(ResultLink rl, TrafficModelLink l) { return rl.TravelTime; }); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly TrafficProperty QueueTimeProperty = new TrafficProperty("Queue Time", delegate(ResultLink rl, TrafficModelLink l) { return rl.QueuingTime; }); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly TrafficProperty TotalTravelTimeProperty = new TrafficProperty("Total TravelTime", delegate(ResultLink rl, TrafficModelLink l) { float t = rl.TravelTime; if (t == 0.0f) { t = l.CalculateFreeFlowTravelTime(); } return rl.QueuingTime + t; }); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public class TrafficProperty { private string key; public ResultLinkFunction function { get; private set; } public TrafficProperty(string key, ResultLinkFunction f) { this.key = key; this.function = f; } public float GetValue(ResultLink rl, TrafficModelLink l) { return function(rl, l); } public override string ToString() { return key; } } } }