/* * 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.SpeedFunctions { using System; using System.Xml.Serialization; [Serializable] public class FlowPartlyLinear : SpeedFlow { [XmlIgnore] public override float MinSpeed { get { return vMin; } } [XmlIgnore] public override float MaxDensity { get { return points != null ? points[points.Length - 1].Flow / vMin : 0; } } [XmlIgnore] private FlowPoint[] points; public FlowPartlyLinear() : base(null, null) { } public FlowPartlyLinear(string name, string description) : base(name, description) { } public FlowPartlyLinear(string name, string description, float sMax, float sMin, FlowPoint[] points) : base(name, description) { this.vMax = sMax; this.vMin = sMin; this.points = points; } public override float GetSpeedByDensity(float density) { FlowPoint max = null, min = null; min = new FlowPoint(0, vMax); foreach (FlowPoint p in points) { if (density * p.Speed <= p.Flow) { max = p; break; } min = p; } if (max == null) { min = points[points.Length - 2]; max = points[points.Length - 1]; } System.Diagnostics.Debug.Assert((min != null) && (max != null)); // This could be optimized by using lookup for. We just need to be careful // with the maxcapacity case. // (max.Flow-min.Flow)*(max.Speed-min.Speed) // Precalculate..... float alpha = (max.Speed - min.Speed) / (max.Flow - min.Flow); float speed = (min.Speed - alpha * min.Flow) / (1 - alpha * density); speed = Math.Max(speed, vMin); return speed; } } }