/* * 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.TrafficGenerator { using System; using System.Collections.Generic; [Serializable] public class NonProbOD : ODTrafficGenerator { public override List CreateTraffic(float stopTime) { List vehicles = new List(); Random rand = new Random(316); foreach (Demand d in Demands) { Vehicle.lastId = 0; //Console.WriteLine(); // XXX bool debug = false; /* if (d.Origin.Id == 17 && d.Destination.Id == 2) { debug = true; Console.Write(d + ", "); }*/ //XXX rand = new Random(d.Origin.Id * 10000 + d.Destination.Id); //Console.WriteLine(d.Origin.Id + " => " + d.Destination.Id ); for (int i = 0; i < d.Demands.Length; i++) { if (d.Demands[i] != 0) { double ncars = rand.NextDouble(); TimeSlice times = Times[i]; double delta = (times.StopTime - times.StartTime); // xxx ncars += d.Demands[i] * delta; double meanDistance = delta / Math.Ceiling(ncars + 1); double time = times.StartTime; time += meanDistance * rand.NextDouble(); if (debug) Console.Write(i + ":" + time + "#"); for (int j = 0; j < Math.Floor(ncars); j++) { System.Diagnostics.Debug.Assert(time <= times.StopTime); if (debug) Console.Write(time+", "); Vehicle vehicle = new Vehicle(d.Origin, d.Destination, (float)time, null, d.Class); vehicles.Add(vehicle); // ncars--; time += meanDistance; } if (debug) Console.WriteLine(); } } } return vehicles; } public override AbstractTrafficGenerator SplitDemand(IDictionary part, int id) { ODTrafficGenerator gen=null; if (part != null) { gen = new NonProbOD(); foreach (Demand d in Demands) { if (part[d.Origin.Id] == id) { gen.Demands.Add(d); } } gen.Times = Times; } return gen; } } }