/* * 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.Xml.Serialization; using OpenTraffic.Model.Graph; using OpenTraffic.Model.Route; using OpenTraffic.Model.Simulator; [Serializable] public class ResultData { [XmlElement(ElementName = "Name")] public string Name { get; set; } [XmlAttribute(AttributeName = "Active")] public bool Active { get; set; } [XmlAttribute(AttributeName = "startTime")] public float startTime { get; set; } [XmlAttribute(AttributeName = "stopTime")] public float stopTime { get; set; } [XmlAttribute(AttributeName = "HasTravelTime")] public bool HasTravelTime { get; set; } [XmlAttribute(AttributeName = "HasNumberOfCars")] public bool HasNumberOfCars { get; set; } [XmlAttribute(AttributeName = "HasFlowIn")] public bool HasFlowIn { get; set; } [XmlAttribute(AttributeName = "HasFlowOut")] public bool HasFlowOut { get; set; } [XmlAttribute(AttributeName = "HasQueuingTime")] public bool HasQueuingTime { get; set; } [XmlAttribute(AttributeName = "HasNumberOfVehicleInQueue")] public bool HasNumberOfVehicleInQueue { get; set; } [XmlAttribute(AttributeName = "HasEstimatedFlow")] public bool HasEstimatedFlow { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] [XmlArray(ElementName = "Frames")] [XmlArrayItem(ElementName = "rf")] public List Frames { get; set; } [XmlIgnore] private int NumberOfFrames { get { return this.Frames.Count; } } private int activeFramePos; private int lastIterateBin; private int vehiclesLeft; [XmlIgnore] public int VehiclesLeft { get { return vehiclesLeft; } } public ResultData() { activeFramePos = 0; this.stopTime = 0; this.startTime = 0; Name = "undefined"; Active = true; HasFlowIn = true; HasFlowOut = true; HasNumberOfCars = true; HasTravelTime = true; HasQueuingTime = true; HasEstimatedFlow = true; HasNumberOfVehicleInQueue = true; vehiclesLeft = 0; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] public ResultData(List times) { activeFramePos = 0; Name = "undefined"; Active = true; HasFlowIn = true; HasFlowOut = true; HasNumberOfCars = true; HasTravelTime = true; HasQueuingTime = true; HasEstimatedFlow = true; HasNumberOfVehicleInQueue = true; Frames = new List(times.Count); this.startTime = times[0].StartTime; this.stopTime = times[times.Count - 1].StopTime; for (int i = 0; i < times.Count; i++) { TimeSlice time = times[i]; Frames[i] = new ResultFrame(i, time.StartTime, time.StopTime); } vehiclesLeft = 0; } public ResultData(float start, float stop, float step) { activeFramePos = 0; Name = "undefined"; Active = true; HasFlowIn = true; HasFlowOut = true; HasNumberOfCars = true; HasTravelTime = true; HasQueuingTime = true; HasEstimatedFlow = true; HasNumberOfVehicleInQueue = true; int i = 0; this.startTime = start; this.stopTime = stop; int nFrames = (int)Math.Ceiling((stop - start) / step); Frames = new List(nFrames); for (float f = start; f < stop; f += step) { Frames[i] = new ResultFrame(i, f, Math.Min(stop, f + step)); i++; } vehiclesLeft = 0; } public int GetNumberOfCars(int bin, IGraphEdge link) { if ((bin < 0) || (bin >= NumberOfFrames)) return 0; return Frames[bin].NumberOfCarsOnLink(link); } public float GetTravelTime(int bin, IGraphEdge link) { if ((bin < 0) || (bin >= NumberOfFrames)) return 0; return Frames[bin].GetTravelTime(link); } public float GetNumberOfCars(float time, IGraphEdge link) { int bin = GetBinPos(time); if (bin == -1) return 0.0f; return Frames[bin].NumberOfCarsOnLink(link); } public float GetFlowOut(float time, IGraphEdge l) { int bin = GetBinPos(time); if (bin == -1) return 0.0f; return Frames[bin].GetFlowOut(l); } public float GetFlowIn(int bin, IGraphEdge l) { return Frames[bin].GetFlowIn(l); } public float GetFlowOut(int bin, IGraphEdge l) { return Frames[bin].GetFlowOut(l); } public float GetTravelTime(float time, IGraphEdge l) { int i = GetBinPos(time); if (i == -1) return 0.0f; return Frames[i].GetTravelTime(l); } public float GetNumberOfVehicleInQueue(float time, IGraphEdge l) { return Frames[GetBinPos(time)].GetNumberofVehicleInQueue(l); } public float GetQueuingTime(float time, IGraphEdge l) { int i = GetBinPos(time); if (i == -1) return 0.0f; return Frames[i].GetQueuingTime(l); } public float GetQueuingTime(int bin, IGraphEdge link) { if ((bin < 0) || (bin >= NumberOfFrames)) return 0; return Frames[bin].GetQueuingTime(link); } private int GetBinPos(float time) { if (activeFramePos != -1) { if (Frames[activeFramePos].Within(time)) { return activeFramePos; } } for (int i = activeFramePos + 1; i < Frames.Count; i++) { if (Frames[i].Within(time)) { activeFramePos = i; return i; } } for (int i = 0; (i < activeFramePos) && (i < Frames.Count); i++) { if (Frames[i].Within(time)) { activeFramePos = i; return i; } } return -1; } public ResultLink GetLinkResultFromBin(int bin, IGraphEdge l) { return Frames[bin].GetResultLink(l); } public ResultLink GetLinkResultFromTime(float time, IGraphEdge l) { int i = GetBinPos(time); if (i == -1) return null; return Frames[i].GetResultLink(l); } public ResultLink GetLinkResultFromTime(float time, int id) { int i = GetBinPos(time); if (i == -1) return null; return Frames[i].GetResultLink(id); } public ResultRoute GetRouteResultFromTime(float time, VehicleClass c, TrafficRoute r) { int i = GetBinPos(time); if (i == -1) return null; return Frames[i].GetRoute(c, r); } public void RegisterRouteEnd(Vehicle vehicle, float time) { if (vehicle != null) { vehiclesLeft++; vehicle.StopTime = time; RegisterDeparture(vehicle, time); } } public void RegisterStartQueing(Vehicle vehicle, float time) { if (vehicle != null) { int i = vehicle.LastBinPassedNode;//GetBinPos(v.LastTimePassedNode); if (i == -1) return; Frames[i].RegisterStartQueing(vehicle, time); } } public void RegisterArrival(Vehicle vehicle, float time) { if (vehicle != null) { int i = GetBinPos(time); if (i == -1) return; vehicle.LastBinPassedNode = i; Frames[i].RegisterArrivalInBin(vehicle, time); } } public void RegisterDeparture(Vehicle vehicle, float time) { if (vehicle != null) { int i = vehicle.LastBinPassedNode;//GetBinPos(v.LastTimePassedNode); Frames[i].RegisterDepartureInBin(vehicle, time); i = GetBinPos(time); if (i == -1) return; Frames[i].RegisterDepartureOutBin(vehicle); } } public void MergeResult(ResultData frame, float alpha, List links) { if (frame != null) { for (int i = 0; i < frame.Frames.Count; i++) { Frames[i].Merge(frame.Frames[i], alpha, links); } } } public void AddResult(ResultData src) { if (src != null) { for (int i = 0; i < Frames.Count; i++) { Frames[i].AddFrame(src.Frames[i]); } } } public void Iterate(List links, float time) { if (lastIterateBin != GetBinPos(time)) { lastIterateBin = GetBinPos(time); this.Frames[lastIterateBin].RegisterNetworkStatus(links); } } public void Close(List links) { if (links != null) { float absStopTime = Frames[Frames.Count - 1].StopTime; foreach (ILinkSimulator l in links) { bool blockForeever = false; for (int i = 0; i < Frames.Count; i++) { ResultLink lr = GetLinkResultFromBin(i, l.NetworkLink); if (!lr.CloseBin(Frames[i].StartTime, Frames[i].StopTime, absStopTime, blockForeever)) blockForeever = true; } } } } public void PrintRouteChooseInformation(VehicleClass routeClass, List routes) { if (routeClass != null && routes != null) { foreach (ResultFrame rf in Frames) { if (rf.routes.ContainsKey(routeClass.IdRegister)) { Console.WriteLine("**************" + rf.StartTime + ":" + rf.StopTime); foreach (RouteContainer r in routes) { if (rf.routes[routeClass.IdRegister].ContainsKey(r.Id)) { ResultRoute route = rf.routes[routeClass.IdRegister][r.Id]; Console.WriteLine(r.Id + " [{0:F}% {1:F}s] ", route.split * 100, route.meanTravelTime); } } } } } } public override string ToString() { HasNumberOfVehicleInQueue = true; return Name + " " + ((Active == false) ? "[deactive]" : ""); } public void CreateRouteResult(List vehicles) //, Model m) { Dictionary>[] routeLookup = new Dictionary>[this.Frames.Count]; for (int i = 0; i < routeLookup.Length; i++) routeLookup[i] = new Dictionary>(); if (vehicles != null) { foreach (Vehicle v in vehicles) { if (v.trafficRoute != null) { int i = GetBinPos(v.StartTime); ResultRoute rr = Frames[i].GetRoute(v.routeClass, v.trafficRoute); rr.vehicles++; rr.meanTravelTime += v.StopTime - v.StartTime; ODPair od = v.ODPair; if (!routeLookup[i].ContainsKey(od)) routeLookup[i].Add(od, new Dictionary()); if (!routeLookup[i][od].ContainsKey(rr.routeId)) { routeLookup[i][od].Add(rr.routeId, rr); } } } } for (int i = 0; i < routeLookup.Length; i++) { foreach (KeyValuePair> cl in routeLookup[i]) { float totVehicle = 0; foreach (KeyValuePair rr in cl.Value) { totVehicle += rr.Value.vehicles; } foreach (KeyValuePair rr in cl.Value) { rr.Value.split = rr.Value.vehicles / totVehicle; rr.Value.meanTravelTime = rr.Value.meanTravelTime / rr.Value.vehicles; } } } } } }