ReadRoutes() public method

Reads a list of links from the given file. Reads only links where the cities exist.
public ReadRoutes ( string filename ) : int
filename string name of links file
return int
Ejemplo n.º 1
0
        public async Task TestFindShortestRouteBetweenAsyncProgress()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new Routes(cities);
            routes.ReadRoutes(LinksTestFile);

            // do synchronous execution
            var linksExpected = routes.FindShortestRouteBetween("Basel", "Zürich", TransportMode.Rail);

            // do asynchronous execution
            var messages = new List<string>();
            var progress = new Progress<string>(msg => messages.Add(msg));
            var linksActual = await routes.FindShortestRouteBetweenAsync("Basel", "Zürich", TransportMode.Rail, progress);

            // let pending tasks execute
            await Task.Yield();

            // ensure that at least 5 progress calls are made
            Assert.IsTrue(messages.Distinct().Count()>=5, "Less than 5 distinct progress messages");

            // ensure that all progress messages end with " done"
            Assert.IsTrue(messages.All(m => m.EndsWith(" done")),
                string.Format("Progress message \"{0}\" does not end with \" done\"",
                    messages.FirstOrDefault(m => !m.EndsWith(" done"))));
        }
Ejemplo n.º 2
0
        public void TestFindCitiesByTransportMode()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab3.txt");
            var routes = new Routes(cities);
            routes.ReadRoutes(@"linksTestDataLab3.txt");

            City[] citiesByMode = routes.FindCities(TransportMode.Rail);
            Assert.AreEqual(11, citiesByMode.Length);

            City[] emptyCitiesByMode = routes.FindCities(TransportMode.Bus);
            Assert.AreEqual(0, emptyCitiesByMode.Length);
        }
Ejemplo n.º 3
0
        public void TestRouteParallelSpeed()
        {
            var cities = new Cities();
            cities.ReadCities("citiesTestDataLab12.txt");
            var routes = new Routes(cities);
            routes.ReadRoutes("linksTestDataLab12.txt");

            //warmup
            routes.FindAllShortestRoutesParallel();

            //execute and measure time
            var timeA = DateTime.Now;
            List<List<Link>> allRoutesSerial = routes.FindAllShortestRoutes();
            var timeB = DateTime.Now;
            List<List<Link>> allRoutesParallel = routes.FindAllShortestRoutesParallel();
            var timeC = DateTime.Now;
            var oldAffinity = Process.GetCurrentProcess().ProcessorAffinity;
            Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)1;
            routes.FindAllShortestRoutesParallel();
            Process.GetCurrentProcess().ProcessorAffinity = oldAffinity;
            var timeD = DateTime.Now;

            var factor = (timeC - timeB).TotalSeconds / (timeB - timeA).TotalSeconds;
            var slowdown = (timeD - timeC).TotalSeconds / (timeB - timeA).TotalSeconds;

            Trace.WriteLine($"Factor: {factor:F2}, Slowdown: {slowdown:F2}");

            //parallel execution on a single core shouldn't be much slower
            Assert.IsTrue(slowdown < 1.1);

            switch (Environment.ProcessorCount)
            {
                case 1:
                    break;
                case 2:
                    //expect at least 25% reduction with 2 cores
                    Assert.IsTrue(factor < 0.75);
                    break;
                case 3:
                    //expect at least 35% reduction with 3 cores
                    Assert.IsTrue(factor < 0.65);
                    break;
                default:
                    //expect at least 45% reduction with 4+ cores
                    Assert.IsTrue(factor < 0.55);
                    break;
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var c = new City("Aarau", "Switzerland", 10, 1.1, 2.2);
            //Console.WriteLine(c.Location.GetType());

            var stream = new StringWriter();
            SimpleObjectWriter w = new SimpleObjectWriter(stream);
            w.Next(c);
            var objstr = stream.ToString();
            Console.Write(objstr + "\r\n");

            var stream2 = new StringReader(objstr);
            SimpleObjectReader r = new SimpleObjectReader(stream2);
            var city = r.Next() as City;
            Console.WriteLine(city.Name);
            Console.WriteLine(city.Country);
            Console.WriteLine(city.Population);
            Console.WriteLine(city.Location);
            Console.WriteLine(city.Location.Name);
            Console.WriteLine(city.Location.Latitude);
            Console.WriteLine(city.Location.Longitude);

            Console.Write("Welcome to RoutePlanner {0}\n", Assembly.GetExecutingAssembly().GetName().Version);
            var wayPoint = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
            //Console.WriteLine("{0}: {1}/{2}", wayPoint.Name, wayPoint.Latitude, wayPoint.Longitude);
            Console.WriteLine(wayPoint);
            wayPoint.Name = "";
            Console.WriteLine(wayPoint);
            wayPoint.Name = null;
            Console.WriteLine(wayPoint);

            var cities = new Cities();
            Console.WriteLine(cities.ReadCities(@"data\citiesTestDataLab3.txt"));

            var routes = new Routes(cities);
            var count = routes.ReadRoutes(@"data\linksTestDataLab3.txt");

            var citiesError = new Cities();
            Console.WriteLine(cities.ReadCities(@"data\citiesTestDataLab253.txt"));

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        public async Task TestFindShortestRouteBetweenAsync()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new Routes(cities);
            routes.ReadRoutes(LinksTestFile);

            // do synchronous execution
            var linksExpected = routes.FindShortestRouteBetween("Basel", "Zürich", TransportMode.Rail);

            // do asynchronous execution
            var linksActual = await routes.FindShortestRouteBetweenAsync("Basel", "Zürich", TransportMode.Rail);

            // now test the results
            Assert.IsNotNull(linksActual);
            Assert.AreEqual(linksExpected.Count, linksActual.Count);

            for (int i = 0; i < linksActual.Count; i++)
            {
                Assert.AreEqual(linksExpected[i].FromCity, linksActual[i].FromCity);
                Assert.AreEqual(linksExpected[i].ToCity, linksActual[i].ToCity);
            }
        }
Ejemplo n.º 6
0
        private long FindRoutes(Routes routes)
        {
            int count = routes.ReadRoutes(@"linksTestDataLab11.txt");

            // test available cities
            Stopwatch timer = new Stopwatch();

            timer.Start();
            List<Link> links = routes.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);
            return timer.ElapsedTicks;
        }
Ejemplo n.º 7
0
        private void TestRouteParallelCorrectness(string _suffix,int _cities,int _routes,int _resultsA,int _resultsB,int _resultsC)
        {
            var cities = new Cities();
            cities.ReadCities("citiesTestDataLab"+_suffix+".txt");
            Assert.AreEqual(_cities, cities.Count);

            var routes = new Routes(cities);
            int count = routes.ReadRoutes("linksTestDataLab"+_suffix+".txt");
            Assert.AreEqual(_routes, count);

            List<List<Link>> allRoutesSerial = routes.FindAllShortestRoutes();
            List<List<Link>> allRoutesParallel = routes.FindAllShortestRoutesParallel();

            //should return all combinations of routes (cities * routes * TransportModes)
            Assert.AreEqual(_resultsA, allRoutesSerial.Count());
            Assert.AreEqual(_resultsA, allRoutesParallel.Count());

            //filter out non-existing routes
            allRoutesSerial = allRoutesSerial.Where(r => r != null).ToList();
            allRoutesParallel = allRoutesParallel.Where(r => r != null).ToList();

            //should've found a subset of valid routes
            Assert.AreEqual(_resultsB, allRoutesSerial.Count());
            Assert.AreEqual(_resultsB, allRoutesParallel.Count());

            //filter out zero-length routes
            allRoutesSerial = allRoutesSerial.Where(r => r.Count()>0).ToList();
            allRoutesParallel = allRoutesParallel.Where(r => r.Count()>0).ToList();

            //should've found a subset of non-zero-length routes
            Assert.AreEqual(_resultsC, allRoutesSerial.Count());
            Assert.AreEqual(_resultsC, allRoutesParallel.Count());

            //sort both lists in a deterministic fashion
            foreach (var list in new[] { allRoutesSerial, allRoutesParallel })
                list.Sort((a, b) =>
                               string.Join(":", a.Select(l => l.FromCity.Name + " " + l.ToCity.Name + " " + l.TransportMode))
                    .CompareTo(string.Join(":", b.Select(l => l.FromCity.Name + " " + l.ToCity.Name + " " + l.TransportMode))));

            //serialize lists to a string
            var txtSerial = string.Join("/", allRoutesSerial.Select(i => string.Join(":", i.Select(l => l.FromCity.Name + " " + l.ToCity.Name + " " + l.TransportMode))));
            var txtParallel = string.Join("/", allRoutesParallel.Select(i => string.Join(":", i.Select(l => l.FromCity.Name + " " + l.ToCity.Name + " " + l.TransportMode))));

            //both algorithms should deliver the same result
            Assert.AreEqual(txtSerial, txtParallel);
        }