/* * 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 { using System; using System.Collections.Generic; using System.Xml.Serialization; using OpenTraffic.Model.Graph; using OpenTraffic.Model.Importer.Native; using OpenTraffic.Model.Result; using OpenTraffic.Model.Route; using OpenTraffic.Model.SignalPlans; using OpenTraffic.Model.SpeedFunctions; using OpenTraffic.Model.StopCriteria; using OpenTraffic.Model.TrafficGenerator; [Serializable] public class TrafficModel { #region XmlExport [XmlArray(ElementName = "StopCriterias")] [XmlArrayItem(ElementName = "sc")] public List StopCriterias { get; set; } [XmlArray(ElementName = "BackgroundImages")] [XmlArrayItem(ElementName = "bi")] public List BackgroundImages { get; set; } [XmlArray(ElementName = "Flares")] [XmlArrayItem(ElementName = "fl")] public List Flares { get; set; } [XmlArray(ElementName = "LinkCategories")] [XmlArrayItem(ElementName = "lc")] public List LinkCategories { get; set; } [XmlArray(ElementName = "VehicleClasses")] [XmlArrayItem(ElementName = "vc")] public List VehicleClasses { get; set; } [XmlArray(ElementName = "Routes")] [XmlArrayItem(ElementName = "rt")] public List Routes { get; set; } [XmlArray(ElementName = "IntersectionNodes")] [XmlArrayItem(ElementName = "is")] public List IntersectionNodes { get; set; } [XmlArray(ElementName = "SignalPlans")] [XmlArrayItem(ElementName = "sp")] public List SignalPlans { get; set; } [XmlArray(ElementName = "SpeedFlows")] [XmlArrayItem(ElementName = "sf")] public List SpeedFlows { get; set; } [XmlArray(ElementName = "OriginalTimeSlice")] [XmlArrayItem(ElementName = "ts")] public List TimeSlices { get; set; } [XmlArray(ElementName = "TrafficModelConnectors")] [XmlArrayItem(ElementName = "tmc")] public List TrafficModelConnectors { get; set; } [XmlArray(ElementName = "TrafficModelLinks")] [XmlArrayItem(ElementName = "tml")] public List TrafficModelLinks { get; set; } [XmlArray(ElementName = "TrafficZones")] [XmlArrayItem(ElementName = "tz")] public List TrafficZones { get; set; } [XmlElement(ElementName = "TrafficGenerator")] public AbstractTrafficGenerator TrafficGenerator { get; set; } [XmlElement(ElementName = "ResultStore")] public ResultStore ResultStore { get; set; } [XmlElement(ElementName = "ResultFactory")] public Factory ResultFactory { get; set; } [XmlAttribute(AttributeName = "StartTime")] public float StartTime { get; set; } [XmlAttribute(AttributeName = "StopTime")] public float StopTime { get; set; } [XmlAttribute(AttributeName = "Giveaway")] public bool Giveaway { get; set; } [XmlAttribute(AttributeName = "SignalLight")] public bool SignalLight { get; set; } [XmlAttribute(AttributeName = "Optimize")] public bool Optimize { get; set; } [XmlAttribute(AttributeName = "AssumeOnlyOneClass")] public bool AssumeOnlyOneClass { get; set; } [XmlAttribute(AttributeName = "Shockwave")] public bool Shockwave { get; set; } #endregion #region XmlIgnore [XmlIgnore] public IDictionary Partition { get; set; } #endregion public TrafficModel() { this.Giveaway = true; this.SignalLight = true; this.Optimize = true; this.AssumeOnlyOneClass = true; this.Shockwave = false; TrafficModelConnectors = new List(); TrafficZones = new List(); VehicleClasses = new List(); Routes = new List(); Partition = new Dictionary(); IntersectionNodes = new List(); TrafficModelLinks = new List(); SpeedFlows = new List(); LinkCategories = new List(); StopCriterias = new List(); BackgroundImages = new List(); ResultStore = new Result.ResultStore(); SignalPlans = new List(); Flares = new List(); TimeSlices = new List(); } /// /// Create a clone of the current object. /// /// Uses the same method as used for saving/restoring native data. /// /// /// New instance of this object. public TrafficModel clone() { byte[] ba = NativeFileImporter.SerializeRaw(this); return NativeFileImporter.DeserializeRaw(ba); ; } /// /// Clear data after load. /// public void PostLoad() { // FILL IN LINKS IN NODE foreach (IntersectionNode n in IntersectionNodes) { n.LinksIn.Clear(); n.LinksOut.Clear(); } foreach (IntersectionNode n in TrafficZones) { n.LinksIn.Clear(); n.LinksOut.Clear(); } foreach (TrafficModelLink l in TrafficModelLinks) { l.Target.LinksIn.Add(l); l.Source.LinksOut.Add(l); } foreach (TrafficModelConnector l in TrafficModelConnectors) { l.Target.LinksIn.Add(l); l.Source.LinksOut.Add(l); } foreach (RouteContainer rc in Routes) { rc.FillInParallellRoutes(); } } public TrafficModel MakeClientModel(int id) { TrafficModel m = new TrafficModel(); m.TrafficModelConnectors = TrafficModelConnectors; m.TrafficModelLinks = TrafficModelLinks; m.TrafficZones = TrafficZones; m.IntersectionNodes = IntersectionNodes; m.VehicleClasses = VehicleClasses; m.SpeedFlows = SpeedFlows; m.LinkCategories = LinkCategories; m.StartTime = StartTime; m.StopTime = StopTime; m.SignalPlans = SignalPlans; m.Flares = Flares; m.Giveaway = Giveaway; m.SignalLight = SignalLight; m.Optimize = Optimize; m.AssumeOnlyOneClass = AssumeOnlyOneClass; m.Shockwave = Shockwave; m.Partition = Partition; m.ResultFactory = ResultFactory; m.TrafficGenerator = TrafficGenerator.SplitDemand(Partition, id); foreach (RouteContainer r in Routes) { if (r.IsWithinPartion(Partition, id)) { m.Routes.Add(r); }; } return m; } public void Reset() { TrafficModelConnectors.Clear(); TrafficZones.Clear(); VehicleClasses.Clear(); Routes.Clear(); Partition.Clear(); IntersectionNodes.Clear(); TrafficModelLinks.Clear(); TrafficGenerator = null; SpeedFlows.Clear(); LinkCategories.Clear(); StopCriterias.Clear(); ResultStore.Clear(); SignalPlans.Clear(); Flares.Clear(); } public void Partion(float[] weightArray) { Graph.Partioning.Partioning p; List weights = new List(weightArray); p = new Graph.Partioning.Partioning(makeGraph(), weights); p.Run(); Partition = p.partition; } public Graph makeGraph() { Graph graph = new Graph(); foreach (IntersectionNode node in IntersectionNodes) { graph.Add(node); } foreach (TrafficZone node in TrafficZones) { graph.Add(node); } foreach (TrafficModelLink link in TrafficModelLinks) { graph.Add(link); } foreach (TrafficModelConnector link in TrafficModelConnectors) { graph.Add(link); } return graph; } public bool StopIteration(Result.ResultData res, Result.ResultData prevRes) { if (prevRes != null) { foreach (AbstractStopCriteria stop in StopCriterias) { if (stop.CheckCriteria(res, prevRes, this)) { return false; } } } return true; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public float GetSmallestFreeFlowTravelTime() { float max = float.MaxValue; foreach (TrafficModelLink l in TrafficModelLinks) { float time = l.CalculateFreeFlowTravelTime(null); if (time != 0) max = time < max ? time : max; } return max; } } }