/* * 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.IO; using System.Collections.Generic; using System.Text; namespace OpenTraffic { class StaticClient { static int numberOfIteration = 30; static Model.Importer.ImporterModel con = new Model.Importer.ContramNetwork(); static Model.Importer.Native.Native nav = new Model.Importer.Native.Native(); static Model.Importer.Mezzo.Mezzo mez = new Model.Importer.Mezzo.Mezzo(); static int port = 6666; static bool cancel = false; private static String CompareResult(Model.Result.Result r1, Model.Result.Result r2, Model.Model model) { IDictionary stops; stops = new Dictionary(); stops.Add("flow", delegate(Model.Result.Result res, float i, Model.Link l) { return res.GetLinkResultFromTime(i, l).FlowIn * 60 * 60; }); stops.Add("tt", delegate(Model.Result.Result res, float i, Model.Link l) { return res.GetQueuingTime(i, l) + ((res.GetTravelTime(i, l) == 0) ? l.CalculateFreeFlowTravelTime() : res.GetTravelTime(i, l)); }); String str = "AAD:\t"; foreach (KeyValuePair kv in stops) { float value = new Model.StopCriteria.AverageAbsoluteDifference(kv.Value).Compare(r1, r2, model); str += " | " + kv.Key + ": " + value.ToString("f"); } return str; } private static void TestModel(String filename, Model.Importer.ImporterModel imp) { Profile prof = new Profile(); prof.Start("start"); prof.Start("load"); String ext = null; if (imp.GetType() == typeof(Model.Importer.Native.Native)) ext = "otx"; if (imp.GetType() == typeof(Model.Importer.Mezzo.Mezzo)) ext = "mezzo"; if (imp.GetType() == typeof(Model.Importer.ContramNetwork)) ext = "net"; if (ext == null) return; String inFile = filename + "." + ext; String outFile = filename + ".otx"; OpenTraffic.Model.Model m = new Model.Model(); Console.WriteLine("----------------" + filename + "--------------"); imp.LoadFile(new FileInfo(inFile), m); Model.Result.Result origResult = null; if (imp.GetType() == typeof(Model.Importer.ContramNetwork)) { new Model.Importer.ContramRoute().LoadFile(new FileInfo(filename + ".rte"), m); FileInfo fi = new FileInfo(filename + ".res"); if (fi.Exists) { origResult = new Model.Importer.ContramResult().LoadResult(new FileInfo(filename + ".res"), m); origResult.Name = "Contram"; m.ResultStore.AddResult(origResult); } } Network.Client cl = new Network.Client(++port, false); prof.Stop("load"); IDictionary sfs = new Dictionary(); sfs.Add("Shortest: 10%", new Model.SimulatorFactory.QueueFactory(10, 100, new Model.Simulator.RouteAssigner.ShortestPath())); // sfs.Add("Shortest: 10% inc", new Model.SimulatorFactory.QueueFactory(10, 30, // new Model.Simulator.RouteAssigner.ShortestPath())); // sfs.Add("Shortest: -1%", new Model.SimulatorFactory.QueueFactory(-1, 100, // new Model.Simulator.RouteAssigner.ShortestPath())); // sfs.Add("Shortest: -1% inc", new Model.SimulatorFactory.QueueFactory(-1, 30, // new Model.Simulator.RouteAssigner.ShortestPath())); /* sfs.Add("Logit: 100% ", new Model.SimulatorFactory.QueueFactory(100, 100, new Model.Simulator.RouteAssigner.Logit(-1, true))); sfs.Add("Kirchnoff: 100% ", new Model.SimulatorFactory.QueueFactory(100, 100, new Model.Simulator.RouteAssigner.Kirchnoff(-3.5f))); sfs.Add("Logit: 10% ", new Model.SimulatorFactory.QueueFactory(10, 100, new Model.Simulator.RouteAssigner.Logit(-1, true))); sfs.Add("Kirchnoff: 10%", new Model.SimulatorFactory.QueueFactory(10, 100, new Model.Simulator.RouteAssigner.Kirchnoff(-3.5f))); */ for (int t = 0; t < 1; t++) { String name = ""; switch (t) { case 0: m.SignalLight = true; m.GiveAway = true; name = "ALL"; break; case 1: m.SignalLight = true; m.GiveAway = false; name = "SIGNAL"; break; case 2: m.SignalLight = false; m.GiveAway = true; name = "GIVEAWAY"; break; case 3: m.SignalLight = false; m.GiveAway = false; name = "NONE"; break; } List rt = m.Routes; foreach (KeyValuePair kv in sfs) { m.Routes = new List(rt); Console.WriteLine("------------- Start " + kv.Key + " " + name + "--------"); prof.Start("simulation"); prof.Start("init"); cl.StartSimulation(1); m.Partion(new float[] { 1.0f }); Model.Result.Result histResult = m.ResultFactory.CreateResult(); Model.Result.Result firstResult = null; cl.TransferModel(m, new List(), histResult, 1, m.Partition, kv.Value); cl.StartPrecalculation(); while (cl.substate != Network.Client.SUBSTATES.ENDED) cl.Work(); prof.Stop("init"); Model.Result.Result prevRes = null; for (int i = 0; i < numberOfIteration && !cancel; i++) { // Console.WriteLine("Number of Routes" + m.Routes.Count); if (prevRes != null) { cl.SetHistoricalResult(prevRes); } else { // cl.SetHistoricalResult(origResult); } prof.Start("generate routes"); GenerateRoute(cl, m); prof.Stop("generate routes"); prof.Start("init iteration"); cl.StartRestartIteration(); while (cl.substate != Network.Client.SUBSTATES.ENDED) cl.Work(); prof.Stop("init iteration"); // Console.WriteLine("Starting iteration " + i); prof.Start("iteration"); cl.StartIteration(); while (!cancel && cl.substate != Network.Client.SUBSTATES.ENDED) cl.Work(); prof.Stop("iteration"); if (cancel == false) { prof.Start("Comparing and storing result"); Model.Result.Result res = cl.FetchResult(); if (prevRes != null) { prevRes.Name = "Iteration " + i; Console.WriteLine("Prev Iter " + CompareResult(prevRes, res, m)); } prevRes = res; if (firstResult == null) firstResult = res; else Console.WriteLine("First Iter " + CompareResult(firstResult, res, m)); if (origResult != null) Console.WriteLine("Contram " + CompareResult(origResult, prevRes, m)); // histResult.MergeResult(res, 0.2f, m.Links); prof.Stop("Comparing and storing result"); } } if (prevRes != null) { prevRes.CreateRouteResult(m.Vehicle, m); prevRes.Name = "Simulation " + kv.Key + " " + name; m.ResultStore.AddResult(prevRes); } prof.Stop("simulation"); Console.WriteLine(); } } nav.SaveFile(new FileInfo(outFile), m); cl = null; prof.Stop("start"); Console.WriteLine(prof); } private static void printRouteChoice(OpenTraffic.Model.Model m, Model.Result.Result prevRes) { } private static void GenerateRoute(Network.Client cl, Model.Model m) { cl.StartGenerateNewRoutes(); while (cl.substate != Network.Client.SUBSTATES.ENDED) cl.Work(); IList rs = cl.GetRoutes(); // cl.AddRoutes(rs); foreach (Model.Route r in rs) { m.Routes.Add(r); } } private static void TestHeap() { if (!Collections.Heap.TestHeap(150)) return; } static void Main(string[] args) { Model.Model m = new Model.Model(); // TestHeap(); Console.TreatControlCAsInput = false; Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); if (args.Length == 1) { TestModel(args[0], con); } else { // TestModel("C:\\CONTRAM8 Examples\\seatown\\seatown", con); // TestModel("C:\\CONTRAM8 Examples\\seatown_none\\seatown", con); // TestModel("C:\\CONTRAM8 Examples\\seatown_none_1_class\\seatown", con); // TestModel("c:\\Contram8 Examples\\giveaway\\giveaway", con); // TestModel("C:\\CONTRAM8 Examples\\stockholm3\\30A", con); // TestModel("C:\\CONTRAM8 Examples\\mezzo\\stockholm\\stockholm", mez); // TestModel("C:\\CONTRAM8 Examples\\signal_test\\test", con); // TestModel("C:\\CONTRAM8 Examples\\route_assigner\\test", con); // TestModel("C:\\CONTRAM8 Examples\\normal\\test", con); /* TestModel("C:\\CONTRAM8 Examples\\speed_flow\\test", con); TestModel("C:\\CONTRAM8 Examples\\speed_flow_2\\test", con); TestModel("C:\\CONTRAM8 Examples\\speed_flow_3\\test", con); TestModel("C:\\CONTRAM8 Examples\\speed_flow_4\\test", con); */ // Network.Client cl = new Network.Client(6666, false); // Network.Client cl2 = new Network.Client(6665, false); //cl.StartSimulation(1); //cl.TransferModel(m, new List(), null, 0); // new System.Threading.Thread(cl2.TestClient).Start(); // cl.TestServer(new Network.ClientDescription("127.0.0.1", 6665, 1).GetIPEndPoint(),100); } Console.ReadKey(); } static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; cancel = true; } } }