/* * 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. */ using System; using System.Collections.Generic; using System.Text; using OpenTraffic.Model.Route; namespace OpenTraffic.Model.Simulator.RouteAssigner { [Serializable] public abstract class TrafficRouteAssigner : ICloneable { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] protected Dictionary>> RoutesLookup { get; private set; } protected Result.ResultData inResult { get; private set; } protected Random rand { get; private set; } private float percent; private float percentUnAssigned; protected TrafficRouteAssigner() { Clear(); } public abstract Object Clone(); public void Clear() { rand = new Random(32); RoutesLookup = new Dictionary>>(); inResult = null; percent = 0.0f; percentUnAssigned = 0.0f; } public void AddRoute(List routes) { if (routes != null) { foreach (RouteContainer r in routes) { addRoute(r); } } } public virtual void init( Result.ResultData inResult1, float percent1, float percentUnassigned1) { this.inResult = inResult1; this.percent = percent1; this.percentUnAssigned = percentUnassigned1; } protected abstract void FillInRoute(Vehicle v); private void addRoute(RouteContainer r) { if (!RoutesLookup.ContainsKey(r.Source.Source)) { RoutesLookup.Add(r.Source.Source, new Dictionary>()); } Dictionary> d = RoutesLookup[r.Source.Source]; if (!d.ContainsKey(r.Target.Target)) { d.Add(r.Target.Target, new List()); } d[r.Target.Target].Add(r); } public void AssignRoutes(List vehicles) { if (vehicles != null) { int failed = 0; int changed = 0; int realChanged = 0; int i = 0; foreach (Vehicle v in vehicles) { i++; if (i > vehicles.Count / 80) { i = 0; Console.Write("*"); } if (RoutesLookup.ContainsKey(v.Origin) && RoutesLookup[v.Origin].ContainsKey(v.Destination)) { // xxx //rand = new Random(v.Id * 1000000 + v.Origin.Id * 1000 + v.Destination.Id + it); bool skip = (v.trafficRoute != null && rand.NextDouble() > percent); if (v.trafficRoute == null && rand.NextDouble() > percentUnAssigned) skip = true; // if (v.Route == null) skip = false; TrafficRoute oldRoute = v.trafficRoute; if (!skip) { changed++; /* xxxx if (RoutesLookup[v.Origin][v.Destination].Count == 1) { RouteContainer r = RoutesLookup[v.Origin][v.Destination][0]; if (r.Allowed(v.Class) && v.Route != r) { v.Route = r; } } else*/ { FillInRoute(v); } if (v != null) { if (!v.trafficRoute.Equals(oldRoute)) { realChanged++; } } FillInRoute(v); } } if (v.trafficRoute == null) { failed++; } else { v.Reset(); } } Console.Write("Routes: Vehicles={0:6d} | ", vehicles.Count); Console.Write("Failed={0:6d} ({1:3F}%) | ", failed, failed * 100.0f / vehicles.Count); Console.Write("Tried={0:6d} ({1:3F}%) | ", changed, changed * 100.0f / vehicles.Count); Console.WriteLine("Changed={0:6d} ({1:3F}%)", realChanged, realChanged * 100.0f / vehicles.Count); } else { Console.WriteLine("Routes: No Vehicles!"); } } /* public void FillInRoutesResult() { foreach (KeyValuePair>> start in RoutesLookup) { foreach (KeyValuePair> stop in RoutesLookup) { foreach (Route r in stop.Value) { } } } }*/ } }