public override List<Link> FindShortestRouteBetween(string fromCity, string toCity,
                                        TransportModes mode, IProgress<string> reportProgress)
        {
            NotifyObservers(fromCity, toCity, mode);

            List<City> citiesBetween = FindCitiesBetween(fromCity, toCity);
            if (citiesBetween == null || citiesBetween.Count < 1 ||
               routes == null || routes.Count < 1)
                return null;

            if (reportProgress != null) reportProgress.Report("FindCitiesBetween done");

            City source = citiesBetween[0];
            City target = citiesBetween[citiesBetween.Count - 1];

            Dictionary<City, double> dist;
            Dictionary<City, City> previous;
            List<City> cityNodes = FillListOfNodes(citiesBetween, out dist, out previous);
            dist[source] = 0.0;
            if (reportProgress != null) reportProgress.Report("FillListOfNodes done");

            // the actual algorithm
            previous = SearchShortestPath(mode, cityNodes, dist, previous);
            if (reportProgress != null) reportProgress.Report("SearchShortestPath done");

            // create a list with all cities on the route
            List<City> citiesOnRoute = GetCitiesOnRoute(source, target, previous);
            if (reportProgress != null) reportProgress.Report("GetCitiesOnRoute done");

            // prepare final list if links
            List<Link> itinerary = FindPath(citiesOnRoute, mode);
            if (reportProgress != null) reportProgress.Report("FindPath done");

            return itinerary;
        }
Пример #2
0
        public override List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode, IProgress<string> progress = null)
        {
            var citiesBetween = FindCitiesBetween(fromCity, toCity);
            NotifyProgress(progress, "FindCitiesBetween done");
            if (citiesBetween == null || citiesBetween.Count < 1 || routes == null || routes.Count < 1)
            {
                return null;
            }

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];
            NotifyObservers(source, target, mode);
            Dictionary<City, double> dist;
            Dictionary<City, City> previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);
            NotifyProgress(progress, "FillListOfNodes done");
            dist[source] = 0.0;

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);
            NotifyProgress(progress, "Searching shortest path done");

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);
            NotifyProgress(progress, "GetCitiesOnRoute done");
            // prepare final list if links
            List<Link> result = FindPath(citiesOnRoute, mode);
            NotifyProgress(progress, "FindPath done");

            return result;
        }
Пример #3
0
        public Task <List <Link> > GoFindShortestRouteBetween(string p1, string p2, TransportModes transportModes, IProgress <string> progress = null)
        {
            City fromCity = cities.FindCity(p1) ?? new City(p1);
            City toCity   = cities.FindCity(p2) ?? new City(p2);

            return(Task.Run(() => FindShortestRouteBetweenAlgorithm(fromCity, toCity, transportModes, progress)));
        }
Пример #4
0
 protected void NotifyObservers(City fromCity, City toCity, TransportModes mode)
 {
     if (RouteRequestEvent != null)
     {
         RouteRequestEvent(this, new RouteRequestEventArgs(fromCity, toCity, mode));
     }
 }
Пример #5
0
 public City[] FindCities(TransportModes transportMode)
 {
     return routes.Where(r => r.TransportMode == transportMode)
         .Select(c => c.FromCity).Distinct()
         .Union(routes.Where(r => r.TransportMode == transportMode).Select(c => c.ToCity).Distinct())
         .ToArray();
 }
Пример #6
0
        public override List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode)
        {
            OnRaiseRouteRequestEvent(new RouteRequestEventArgs(fromCity, toCity, mode));
            City fromCityObi = cities.FindCity(fromCity);
            City toCityObi = cities.FindCity(toCity);
            var citiesBetween = cities.FindCitiesBetween(fromCityObi, toCityObi);
            if (citiesBetween == null || citiesBetween.Count < 1 || routes == null || routes.Count < 1)
                return null;

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];

            Dictionary<City, double> dist;
            Dictionary<City, City> previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);
            dist[source] = 0.0;

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);

            // prepare final list if links
            return FindPath(citiesOnRoute, mode);
        }
Пример #7
0
        private List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode, IProgress<String> progress = null)
        {
            if (progress != null)progress.Report("Start done");
            if (RouteRequestEvent != null)
                RouteRequestEvent(this, new RouteRequestEventArgs(new City(fromCity, "", 0, 0.0, 0.0), new City(toCity, "", 0, 0.0, 0.0), mode));
            var citiesBetween = FindCitiesBetween(fromCity, toCity);
            if (citiesBetween == null || citiesBetween.Count < 1 || _routes == null || _routes.Count < 1)
                return null;

            if (progress != null) progress.Report("Set origin done");
            var source = citiesBetween[0];
            if (progress != null) progress.Report("Set destination done");
            var target = citiesBetween[citiesBetween.Count - 1];

            if (progress != null) progress.Report("Fill list of nodes done");
            Dictionary<City, double> dist;
            Dictionary<City, City> previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);
            dist[source] = 0.0;

            // the actual algorithm
            if (progress != null) progress.Report("Search shortest path done");
            previous = SearchShortestPath(mode, q, dist, previous);

            // create a list with all cities on the route
            if (progress != null) progress.Report("Get cities on route done");
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);

            // prepare final list if links
            if (progress != null) progress.Report("FindShortestRouteBetween done");

            return FindPath(citiesOnRoute, mode);
        }
Пример #8
0
 private Link FindLink(City c1, City c2, TransportModes mode)
 {
     return(_routes.Find(delegate(Link lnk)
     {
         return (lnk.TransportMode == mode && ((lnk.FromCity == c1 && lnk.ToCity == c2) || (lnk.FromCity == c2 && lnk.ToCity == c1)));
     }));
 }
Пример #9
0
 public City[] FindCities(TransportModes transportMode)
 {
     return routes.Where(r => r.TransportMode == transportMode)
                  .SelectMany(c => new[]{ c.FromCity, c.ToCity})
                  .Distinct()
                  .ToArray();
 }
Пример #10
0
 private Link FindLink(City c1, City c2, TransportModes mode)
 {
     return _routes.Find(delegate(Link lnk)
     {
         return (lnk.TransportMode == mode && ((lnk.FromCity == c1 && lnk.ToCity == c2) || (lnk.FromCity == c2 && lnk.ToCity == c1)));
     });
 }
Пример #11
0
 public City[] FindCities(TransportModes transportMode)
 {
     return(routes.Where(r1 => r1.TransportMode == transportMode)
            .SelectMany(r2 => new[] { r2.FromCity, r2.ToCity })
            .Distinct()
            .ToArray());
 }
Пример #12
0
        // Lab 6.3
        public City[] FindCities(TransportModes transportMode)
        {
            /* before feedback of milestone 3
             * List<City> cityList = new List<City>() ;
             * routes
             *  .Where(lnk => lnk.TransportMode == transportMode)
             *  .ToList()
             *  .ForEach( lnk => {
             *      cityList.Add(lnk.FromCity);
             *      cityList.Add(lnk.ToCity);
             *  });
             *
             * return cityList.Distinct().ToArray();
             */

            // after feedback of milestone 3
            var from = routes
                       .Where(lnk => lnk.TransportMode == transportMode)
                       .Select(lnk => lnk.FromCity);

            return(routes
                   .Where(lnk => lnk.TransportMode == transportMode)
                   .Select(lnk => lnk.ToCity)
                   .Union(from)
                   .ToArray());
        }
Пример #13
0
 public City[] FindCities(TransportModes transportMode)
 {
     return(routes.Where(r => r.TransportMode == transportMode)
            .SelectMany(c => new[] { c.FromCity, c.ToCity })
            .Distinct()
            .ToArray());
 }
        public override List<Link> FindShortestRouteBetween(string fromCity, string toCity,
                                        TransportModes mode, IProgress<string> reportProgress = null)
        {
            List<City> cities = FindCitiesBetween(fromCity, toCity);
            if (cities == null || cities.Count < 1)
            {
                return null;
            }
            List<Link> links = FindAllLinks(cities, mode);
            if (links == null || links.Count < 1)
                return null;

            Setup(cities, links);

            City source = FindCity( fromCity, cities );
            City target = FindCity(toCity, cities);
            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return new List<Link>(); // no path between source and target
            }
            List<City> path = GetIntermediatePath(source, target);

            // must construct route from path
            List<Link> route = new List<Link>();
            route.Add( new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index] ) );
            for (int i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to = path.ElementAt(i + 1);
                route.Add( new Link(from, to, D[from.Index, to.Index]) );
            }
            route.Add( new Link(path.ElementAt(path.Count - 1), target,
                                D[path.ElementAt(path.Count - 1).Index, target.Index]) );
            return route;
        }
Пример #15
0
 protected Link FindLink(City fromC, City toC, TransportModes mode)
 {
     return((from l in routes
             where mode.Equals(l.TransportMode) &&
             ((fromC.Equals(l.FromCity) && toC.Equals(l.ToCity)) ||
              (toC.Equals(l.FromCity) && fromC.Equals(l.ToCity)))
             select new Link(fromC, toC, l.Distance, TransportModes.Rail)).FirstOrDefault());
 }
Пример #16
0
        public virtual List <Link> FindShortestRouteBetween(string from, string to, TransportModes mode)
        {
            // created in order to get it to work
            City fromCity = cities.FindCity(from) ?? new City(from);
            City toCity   = cities.FindCity(to) ?? new City(to);

            return(FindShortestRouteBetween(fromCity, toCity, mode));
        }
Пример #17
0
 /// <summary>
 /// Finds all neighbor cities of a city.
 /// </summary>
 /// <param name="city">source city</param>
 /// <param name="mode">transportation mode</param>
 /// <returns>list of neighbor cities</returns>
 private List <City> FindNeighbours(City city, TransportModes mode)
 {
     return((
                from route in Links
                where route.TransportMode.Equals(mode) && (route.FromCity.Equals(city) || route.ToCity.Equals(city))
                select route.FromCity.Equals(city) ? route.ToCity : route.FromCity
                ).ToList());
 }
Пример #18
0
 public Boolean Equals(City fC, City tC, TransportModes mode)
 {
     if (FromCity.Name == fC.Name && ToCity.Name == tC.Name && TransportMode == mode)
     {
         return(true);
     }
     return(false);
 }
 private List <Link> FindAllLinks(List <City> cities, TransportModes mode)
 {
     if (ExecuteParallel)
     {
         return(FindAllLinksParallel(cities, mode));
     }
     return(Links.Where(r => r.IsIncludedIn(cities)).ToList());
 }
Пример #20
0
        private List<Link> FindAllLinks(List<City> cities, TransportModes mode)
        {
            var links= new List<Link>();
            foreach(Link r in routes)
                if( r.IsIncludedIn( cities ) )
                    links.Add(r);

            return links;
        }
Пример #21
0
        public City[] FindCities(TransportModes transportModes)
        {
            var links      = _routes.Where(p => p.TransportMode == transportModes).ToList();
            var fromCities = links.Select(c => c.FromCity).Distinct().ToList();
            var toCities   = links.Select(d => d.ToCity).Distinct().ToList();

            fromCities.AddRange(toCities);
            return(fromCities.Distinct().ToArray());
        }
Пример #22
0
 private List<Link> FindPath(List<City> citiesOnRoute, TransportModes mode)
 {
     var linkedRoute = new List<Link>();
     for (int i = 0; i < citiesOnRoute.Count - 1; i++)
     {
         var distance = citiesOnRoute[i].Location.Distance(citiesOnRoute[i + 1].Location);
         linkedRoute.Add(new Link(citiesOnRoute[i], citiesOnRoute[i + 1], distance, mode));
     }
     return linkedRoute;
 }
Пример #23
0
 private List<Link> FindPath(List<City> citiesOnRoute, TransportModes mode)
 {
     var ret = new List<Link>();
     for (int i = 0; i < citiesOnRoute.Count - 1; i++)
     {
         var city1 = citiesOnRoute[i];
         var city2 = citiesOnRoute[i + 1];
         ret.Add(new Link(city1, city2, city1.Location.Distance(city2.Location), mode));
     }
     return ret;
 }
Пример #24
0
        private Link FindLink(City u, City n, TransportModes mode)
        {
            var linkToFind             = new Link(u, n, u.Location.Distance(n.Location), mode);
            var linkToFindRevert       = new Link(n, u, n.Location.Distance(u.Location), mode);
            Predicate <Link> predicate = delegate(Link link)
            {
                return(link.Equals(linkToFind) || link.Equals(linkToFindRevert));
            };

            return(routes.Find(predicate));
        }
Пример #25
0
        // Lab 4.1
        private List <Link> FindPath(List <City> citiesOnRoute, TransportModes mode)
        {
            var listOfLinks = new List <Link>();

            for (int i = 0; i < citiesOnRoute.Count - 1; i++)
            {
                listOfLinks.Add(FindLink(citiesOnRoute[i], citiesOnRoute[i + 1], mode));
            }

            return(listOfLinks);
        }
        public override List <Link> FindShortestRouteBetween(string fromCity, string toCity,
                                                             TransportModes mode)
        {
            List <City> floydCities = cities.FindCitiesBetween(cities.FindCity(fromCity), cities.FindCity(toCity));

            if (floydCities == null || floydCities.Count < 1)
            {
                return(null);
            }

            List <Link> links = FindAllLinks(floydCities, mode);

            if (links == null || links.Count < 1)
            {
                return(null);
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long ts0 = stopWatch.ElapsedMilliseconds;

            if (ExecuteParallel)
            {
                SetupParallel(floydCities, links);
            }
            else
            {
                Setup(floydCities, links);
            }

            City source = FindCity(fromCity, floydCities);
            City target = FindCity(toCity, floydCities);

            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return(new List <Link>()); // no path between source and target
            }
            List <City> path = GetIntermediatePath(source, target);

            // must construct route from path
            var route = new List <Link>();

            route.Add(new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index]));
            for (var i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to   = path.ElementAt(i + 1);
                route.Add(new Link(from, to, D[from.Index, to.Index]));
            }
            route.Add(new Link(path.ElementAt(path.Count - 1), target,
                               D[path.ElementAt(path.Count - 1).Index, target.Index]));
            return(route);
        }
Пример #27
0
        private List <Link> FindPath(List <City> citiesOnRoute, TransportModes mode)
        {
            var linkedRoute = new List <Link>();

            for (int i = 0; i < citiesOnRoute.Count - 1; i++)
            {
                var distance = citiesOnRoute[i].Location.Distance(citiesOnRoute[i + 1].Location);
                linkedRoute.Add(new Link(citiesOnRoute[i], citiesOnRoute[i + 1], distance, mode));
            }
            return(linkedRoute);
        }
Пример #28
0
        private List <Link> FindPath(List <City> citiesOnRoute, TransportModes mode)
        {
            var ret = new List <Link>();

            for (int i = 0; i < citiesOnRoute.Count - 1; i++)
            {
                var city1 = citiesOnRoute[i];
                var city2 = citiesOnRoute[i + 1];
                ret.Add(new Link(city1, city2, city1.Location.Distance(city2.Location), mode));
            }
            return(ret);
        }
Пример #29
0
        private List<Link> FindPath(List<City> citiesOnRoute, TransportModes mode)
        {
            List<Link> links = new List<Link>();

            for (int i = 0; i < citiesOnRoute.Count - 1; ++i)
            {
                City city1 = citiesOnRoute[i];
                City city2 = citiesOnRoute[i + 1];
                links.Add(FindLink(city1, city2, mode));
            }
            return links;
        }
Пример #30
0
        public City[] FindCities(TransportModes transportMode)
        {
            var fromCities = routes
                             .Where(l => l.TransportMode.Equals(transportMode))
                             .Select(m => m.FromCity);

            var toCities = routes
                           .Where(l => l.TransportMode.Equals(transportMode))
                           .Select(m => m.ToCity);

            return(fromCities.Union(toCities).ToArray <City>());
        }
Пример #31
0
        public City[] FindCities(TransportModes transportMode)
        {
            var fromCities = routes
                    .Where(l => l.TransportMode.Equals(transportMode))
                    .Select(m => m.FromCity);

            var toCities = routes
                    .Where(l => l.TransportMode.Equals(transportMode))
                    .Select(m => m.ToCity);

            return fromCities.Union(toCities).ToArray<City>();
        }
Пример #32
0
        private List <Link> FindAllLinks(List <City> cities, TransportModes mode)
        {
            List <Link> links = new List <Link>();

            foreach (Link r in routes)
            {
                if (r.IsIncludedIn(cities))
                {
                    links.Add(r);
                }
            }
            return(links);
        }
Пример #33
0
        protected List <Link> FindPath(List <City> citiesOnRoute, TransportModes mode)
        {
            var citiesAsLinks = new List <Link>(citiesOnRoute.Count);

            for (var i = 0; i < citiesOnRoute.Count - 1; i++)
            {
                City c1 = citiesOnRoute[i];
                City c2 = citiesOnRoute[i + 1];
                citiesAsLinks.Add(new Link(c1, c2, c1.Location.Distance(c2.Location)));
            }

            return(citiesAsLinks);
        }
Пример #34
0
        protected List <Link> FindPath(List <City> cities, TransportModes mode)
        {
            City        fromCity = cities.First();
            List <Link> links    = cities
                                   .Skip(1)
                                   .Select(c => {
                Link l   = FindLink(fromCity, c, mode);
                fromCity = c;
                return(l);
            })
                                   .Where(c => c != null)
                                   .ToList();

            return(links);
        }
Пример #35
0
        /// <summary>
        /// Searches the shortest path for cities and the given links
        /// </summary>
        /// <param name="mode">transportation mode</param>
        /// <param name="q"></param>
        /// <param name="dist"></param>
        /// <param name="previous"></param>
        /// <returns></returns>
        private Dictionary <City, City> SearchShortestPath(TransportModes mode, List <City> q, Dictionary <City, double> dist, Dictionary <City, City> previous)
        {
            while (q.Count > 0)
            {
                City u       = null;
                var  minDist = double.MaxValue;

                // find city u with smallest dist
                // also possible with q.Where(c => dist[c] < minDist)
                foreach (var c in q)
                {
                    if (dist[c] < minDist)
                    {
                        u       = c;
                        minDist = dist[c];
                    }
                }

                if (u != null)
                {
                    q.Remove(u);
                    foreach (var n in FindNeighbours(u, mode))
                    {
                        var l = FindLink(u, n, mode);
                        var d = dist[u];
                        if (l != null)
                        {
                            d += l.Distance;
                        }
                        else
                        {
                            d += double.MaxValue;
                        }

                        if (dist.ContainsKey(n) && d < dist[n])
                        {
                            dist[n]     = d;
                            previous[n] = u;
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            return(previous);
        }
Пример #36
0
        // Lab 4.1
        public Link FindLink(City fromCity, City toCity, TransportModes mode)
        {
            Link link = routes
                        .Where(l => l.Equals(fromCity, toCity, mode) || l.Equals(toCity, fromCity, mode))
                        .DefaultIfEmpty(null)
                        .FirstOrDefault();

            if (link != null)
            {
                return(new Link(fromCity, toCity, link.Distance));
            }
            else
            {
                return(null);
            }
        }
Пример #37
0
        public override List<Link> FindShortestRouteBetween(string fromCity, string toCity,
                                        TransportModes mode)
        {
            List<City> cities = FindCitiesBetween(fromCity, toCity);
            if (cities == null || cities.Count < 1)
            {
                return null;
            }
            List<Link> links = FindAllLinks(cities, mode);
            if (links == null || links.Count < 1)
                return null;
			Stopwatch stopWatch = new Stopwatch();
			stopWatch.Start();
			
            long ts0=stopWatch.ElapsedMilliseconds;

            if (ExecuteParallel)
            {
                SetupParallel(cities, links);
            }
            else
            {
                Setup(cities, links);
            }

            City source = FindCity( fromCity, cities );
            City target = FindCity(toCity, cities);
            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return new List<Link>(); // no path between source and target
            }
            List<City> path = GetIntermediatePath(source, target);

            // must construct route from path
            List<Link> route = new List<Link>();
            route.Add( new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index] ) );
            for (int i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to = path.ElementAt(i + 1);
                route.Add( new Link(from, to, D[from.Index, to.Index]) );
            }
            route.Add( new Link(path.ElementAt(path.Count - 1), target, 
                                D[path.ElementAt(path.Count - 1).Index, target.Index]) );
            return route;

        }
        public override List <Link> FindShortestRouteBetween(string fromCity, string toCity,
                                                             TransportModes mode, IProgress <string> reportProgress)
        {
            List <City> citiesBetween = Cities.FindCitiesBetween(Cities.FindCity(fromCity), Cities.FindCity(toCity));

            if (citiesBetween == null || citiesBetween.Count < 1)
            {
                return(null);
            }

            List <Link> links = FindAllLinks(citiesBetween, mode);

            if (links == null || links.Count < 1)
            {
                return(null);
            }

            /*var stopWatch = new Stopwatch();
             * stopWatch.Start();
             * long ts0=stopWatch.ElapsedMilliseconds;*/

            Setup(citiesBetween, links);

            City source = FindCity(fromCity, citiesBetween);
            City target = FindCity(toCity, citiesBetween);

            if (D[source.Index, target.Index] == Double.MaxValue)
            {
                return(new List <Link>()); // no path between source and target
            }
            List <City> path = GetIntermediatePath(source, target);

            // must construct route from path
            var route = new List <Link>();

            route.Add(new Link(source, path.ElementAt(0), D[source.Index, path.ElementAt(0).Index]));
            for (var i = 0; i < path.Count - 1; i++)
            {
                City from = path.ElementAt(i);
                City to   = path.ElementAt(i + 1);
                route.Add(new Link(from, to, D[from.Index, to.Index]));
            }
            route.Add(new Link(path.ElementAt(path.Count - 1), target,
                               D[path.ElementAt(path.Count - 1).Index, target.Index]));
            return(route);
        }
Пример #39
0
        public List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode,  IProgress<string> reportProgress)
        {
            var from = cities.FindCity(fromCity);
            var to = cities.FindCity(toCity);
            if(reportProgress != null)
                reportProgress.Report("Find cities by name - done");

            if (RouteRequestEvent != null)
            {
                if (from != null && to != null)
                    RouteRequestEvent(this, new RouteRequestEventArgs(from, to, mode));
            }

            var citiesBetween = cities.FindCitiesBetween(from, to);
            if (reportProgress != null)
                reportProgress.Report("Find cities between"+ from.Name + " and " + to.Name + " - done");
            if (citiesBetween == null || citiesBetween.Count < 1 || routes == null || routes.Count < 1)
                return null;

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];

            Dictionary<City, double> dist;
            Dictionary<City, City> previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);
            dist[source] = 0.0;

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);
            if (reportProgress != null)
                reportProgress.Report("Route calculation - done");

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);
            if (reportProgress != null)
                reportProgress.Report("Gather all cities on route - done");

            // prepare final list if links
            var path = FindPath(citiesOnRoute, mode);
            if (reportProgress != null)
                reportProgress.Report("Find path - done");

            return path;
        }
Пример #40
0
        /// <summary>
        /// Finds all neighbor cities of a city.
        /// </summary>
        /// <param name="city">source city</param>
        /// <param name="mode">transportation mode</param>
        /// <returns>list of neighbor cities</returns>
        private List <City> FindNeighbours(City city, TransportModes mode)
        {
            var neighbors = new List <City>();

            foreach (var r in routes)
            {
                if (mode.Equals(r.TransportMode))
                {
                    if (city.Equals(r.FromCity))
                    {
                        neighbors.Add(r.ToCity);
                    }
                    else if (city.Equals(r.ToCity))
                    {
                        neighbors.Add(r.FromCity);
                    }
                }
            }

            return(neighbors);
        }
Пример #41
0
        /// <summary>
        /// Searches the shortest path for cities and the given links
        /// </summary>
        /// <param name="mode">transportation mode</param>
        /// <param name="q"></param>
        /// <param name="dist"></param>
        /// <param name="previous"></param>
        /// <returns></returns>
        private Dictionary<City, City> SearchShortestPath(TransportModes mode, List<City> q, Dictionary<City, double> dist, Dictionary<City, City> previous)
        {
            while (q.Count > 0)
            {
                City u = null;
                var minDist = double.MaxValue;
                // find city u with smallest dist
                foreach (var c in q)
                    if (dist[c] < minDist)
                    {
                        u = c;
                        minDist = dist[c];
                    }

                if (u != null)
                {
                    q.Remove(u);
                    foreach (var n in FindNeighbours(u, mode))
                    {
                        var l = FindLink(u, n, mode);
                        var d = dist[u];
                        if (l != null)
                            d += l.Distance;
                        else
                            d += double.MaxValue;

                        if (dist.ContainsKey(n) && d < dist[n])
                        {
                            dist[n] = d;
                            previous[n] = u;
                        }
                    }
                }
                else
                    break;

            }

            return previous;
        }
Пример #42
0
        public List <Link> FindShortestRouteBetweenAlgorithm(City fromCity, City toCity, TransportModes mode, IProgress <string> progress = null)
        {
            report(progress, "Starting ");
            EmitRouteEvent(this, new RouteRequestEventArgs(fromCity, toCity, mode));

            // We can not make calculations if locations are
            // We can not make calculations if no routes are available
            if (
                fromCity.Location == null ||
                toCity.Location == null ||
                routes == null ||
                routes.Count < 1
                )
            {
                return(null);
            }



            var citiesBetween = cities.FindCitiesBetween(fromCity, toCity);

            report(progress, "FindCitiesBetween()");

            // We can not make calculations if there are no cities between
            if (citiesBetween == null || citiesBetween.Count < 1)
            {
                return(null);
            }

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];

            Dictionary <City, double> dist;
            Dictionary <City, City>   previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);

            dist[source] = 0.0;
            report(progress, "FillListOfNodes()");

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);
            report(progress, "SearchShortestPath()");

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);

            report(progress, "GetCitiesOnRoute()");

            // prepare final list if links
            var paths = FindPath(citiesOnRoute, mode);

            report(progress, "FindPath()");
            return(paths);
        }
Пример #43
0
 // Lab 4, Aufgabe 1
 // Lab04: Dijkstra implementation
 public override List <Link> FindShortestRouteBetween(City fromCity, City toCity, TransportModes mode)
 {
     return(FindShortestRouteBetweenAlgorithm(fromCity, toCity, mode, null));
 }
Пример #44
0
 public City[] FindCities(TransportModes transportModes)
 {
     var links = _routes.Where(p => p.TransportMode == transportModes).ToList();
     var fromCities = links.Select(c => c.FromCity).Distinct().ToList();
     var toCities = links.Select(d => d.ToCity).Distinct().ToList();
     fromCities.AddRange(toCities);
     return fromCities.Distinct().ToArray();
 }
Пример #45
0
 public override List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode)
 {
     return FindShortestRouteBetween(fromCity, toCity, mode, null);
 }
Пример #46
0
 public Link(City fromCity, City toCity, double distance, TransportModes transportMode)
     : this(fromCity, toCity, distance)
 {
     this.transportMode = transportMode;
 }
Пример #47
0
        private void UpdateMode(TransportModes transportMode)
        {
            // Update the transport mode and stop any riding sounds playing.
            mode = transportMode;
            if (ridingAudioSource.isPlaying)
            {
                ridingAudioSource.Stop();
            }

            if (mode == TransportModes.Horse || mode == TransportModes.Cart)
            {
                // Tell player motor we're riding.
                playerMotor.IsRiding = true;

                // Setup appropriate riding sounds.
                SoundClips sound = (mode == TransportModes.Horse) ? horseRidingSound2 : cartRidingSound;
                ridingAudioSource.clip = dfAudioSource.GetAudioClip((int)sound);

                // Setup appropriate riding textures.
                string textureName = (mode == TransportModes.Horse) ? horseTextureName : cartTextureName;
                for (int i = 0; i < 4; i++)
                {
                    ridingTexures[i] = ImageReader.GetImageData(textureName, 0, i, true, true);
                }
                ridingTexture = ridingTexures[0];

                // Initialise neighing timer.
                neighTime = Time.time + Random.Range(1, 5);
            }
            else
            {
                // Tell player motor we're not riding.
                playerMotor.IsRiding = false;
            }

            if (mode == TransportModes.Ship)
            {
                GameManager.Instance.PlayerMotor.CancelMovement = true;
                SerializablePlayer serializablePlayer = GetComponent <SerializablePlayer>();
                DaggerfallUI.Instance.FadeBehaviour.SmashHUDToBlack();
                StreamingWorld world      = GameManager.Instance.StreamingWorld;
                DFPosition     shipCoords = DaggerfallBankManager.GetShipCoords();

                // Is there recorded position before boarding and is player on the ship?
                if (IsOnShip())
                {
                    // Check for terrain sampler changes. (so don't fall through floor)
                    StreamingWorld.RepositionMethods reposition = StreamingWorld.RepositionMethods.None;
                    if (boardShipPosition.terrainSamplerName != DaggerfallUnity.Instance.TerrainSampler.ToString() ||
                        boardShipPosition.terrainSamplerVersion != DaggerfallUnity.Instance.TerrainSampler.Version)
                    {
                        reposition = StreamingWorld.RepositionMethods.RandomStartMarker;
                        if (DaggerfallUI.Instance.DaggerfallHUD != null)
                        {
                            DaggerfallUI.Instance.DaggerfallHUD.PopupText.AddText("Terrain sampler changed. Repositioning player.");
                        }
                    }
                    // Restore player position from before boarding ship, caching ship scene first.
                    SaveLoadManager.CacheScene(world.SceneName);    // TODO: Should this should move into teleport to support other teleports? Issue only if inside. (e.g. recall)
                    DFPosition mapPixel = MapsFile.WorldCoordToMapPixel(boardShipPosition.worldPosX, boardShipPosition.worldPosZ);
                    world.TeleportToCoordinates(mapPixel.X, mapPixel.Y, reposition);
                    serializablePlayer.RestorePosition(boardShipPosition);
                    boardShipPosition = null;
                    // Restore cached scene (ship is special case, cache will not be cleared)
                    SaveLoadManager.RestoreCachedScene(world.SceneName);
                }
                else
                {
                    // Record current player position before boarding ship, and cache scene. (ship is special case, cache will not be cleared)
                    boardShipPosition = serializablePlayer.GetPlayerPositionData();
                    SaveLoadManager.CacheScene(world.SceneName);
                    // Teleport to the players ship, restoring cached scene.
                    world.TeleportToCoordinates(shipCoords.X, shipCoords.Y, StreamingWorld.RepositionMethods.RandomStartMarker);
                    SaveLoadManager.RestoreCachedScene(world.SceneName);
                }
                DaggerfallUI.Instance.FadeBehaviour.FadeHUDFromBlack();
                mode = TransportModes.Foot;
            }
        }
Пример #48
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="fromCity">Links departing city</param>
 /// <param name="toCity">Links arriving city</param>
 /// <param name="distance">Distance between cities</param>
 /// <param name="transportMode">How link is served</param>
 public Link(City fromCity, City toCity, double distance, TransportModes transportMode)
     : this(fromCity, toCity, distance)
 {
     this.transportMode = transportMode;
 }
Пример #49
0
        public override List <Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode)
        {
            RouteRequestEventArgs routeRequestEventArgs = new RouteRequestEventArgs(fromCity, toCity, mode);

            if (RouteRequestEvent != null)
            {
                RouteRequestEvent(this, routeRequestEventArgs);
            }

            var citiesBetween = FindCitiesBetween(fromCity, toCity);

            if (citiesBetween == null || citiesBetween.Count < 1 || routes == null || routes.Count < 1)
            {
                return(null);
            }

            var source = citiesBetween[0];
            var target = citiesBetween[citiesBetween.Count - 1];

            Dictionary <City, double> dist;
            Dictionary <City, City>   previous;
            var q = FillListOfNodes(citiesBetween, out dist, out previous);

            dist[source] = 0.0;

            // the actual algorithm
            previous = SearchShortestPath(mode, q, dist, previous);

            // create a list with all cities on the route
            var citiesOnRoute = GetCitiesOnRoute(source, target, previous);

            // prepare final list if links
            return(FindPath(citiesOnRoute, mode));
        }
Пример #50
0
        protected Link FindLink(City fromC, City toC, TransportModes mode)
        {

            return (from l in routes
                    where mode.Equals(l.TransportMode)
                    && ((fromC.Equals(l.FromCity) && toC.Equals(l.ToCity))
                         || (toC.Equals(l.FromCity) && fromC.Equals(l.ToCity)))
                    select new Link(fromC, toC, l.Distance, TransportModes.Rail)).FirstOrDefault();
        }
Пример #51
0
 protected void NotifyObservers(City fromCity,City toCity,TransportModes mode) {
     if (RouteRequestEvent != null)
     {
         RouteRequestEvent(this, new RouteRequestEventArgs(fromCity, toCity, mode));
     }
 }
Пример #52
0
        protected List<Link> FindPath(List<City> cities, TransportModes mode)
        {
            City fromCity = cities.First();
            List<Link> links = cities
                .Skip(1)
                .Select(c => {
                    Link l = FindLink(fromCity, c, mode);
                    fromCity = c;
                    return l;
                })
                .Where(c => c != null)
                .ToList(); 

            return links;
        }
        /// <summary>
        /// Searches the shortest path for cities and the given links
        /// </summary>
        /// <param name="mode">transportation mode</param>
        /// <param name="cityNodesList"></param>
        /// <param name="dist"></param>
        /// <param name="previous"></param>
        /// <returns></returns>
        private Dictionary<City, City> SearchShortestPath(TransportModes mode,
                         List<City> cityNodesList, Dictionary<City, double> dist, Dictionary<City, City> previous)
        {
            while (cityNodesList.Count > 0)
            {
                City u = null;
                double minDist = double.MaxValue;
                // find city u with smallest dist
                foreach (City c in cityNodesList)
                {
                    if (dist[c] < minDist)
                    {
                        u = c;
                        minDist = dist[c];
                    }
                }
                if (u != null)
                {
                    cityNodesList.Remove(u);
                    foreach (City n in FindNeighbours(u, mode))
                    {
                        Link l = FindLink(u, n, mode);
                        double d = dist[u];
                        if (l != null)
                        {
                            d += l.Distance;
                        }
                        else
                        {
                            d += double.MaxValue;
                        }
                        if (dist.ContainsKey(n) && d < dist[n])
                        {
                            dist[n] = d;
                            previous[n] = u;
                        }
                    }
                }
                else
                {
                    break;
                }

            }

            return previous;
        }
Пример #54
0
 public RouteRequestEventArgs(City fromCity, City toCity, TransportModes mode)
 {
     FromCity = fromCity;
     ToCity = toCity;
     TransportMode = mode;
 }
Пример #55
0
        /// <summary>
        /// Finds all neighbor cities of a city. 
        /// </summary>
        /// <param name="city">source city</param>
        /// <param name="mode">transportation mode</param>
        /// <returns>list of neighbor cities</returns>
        private List<City> FindNeighbours(City city, TransportModes mode)
        {
            var neighbors = new List<City>();
            foreach (var r in _routes)
                if (mode.Equals(r.TransportMode))
                {
                    if (city.Equals(r.FromCity))
                        neighbors.Add(r.ToCity);
                    else if (city.Equals(r.ToCity))
                        neighbors.Add(r.FromCity);
                }

            return neighbors;
        }
Пример #56
0
 public abstract List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode);
Пример #57
0
 public Task<List<Link>> GoFindShortestRouteBetween(string fromCity, string toCity, TransportModes mode, Progress<string> progress = null)
 {
     return Task.Run(() => FindShortestRouteBetween(fromCity, toCity, mode, progress));
 }
 /// <summary>
 /// Finds all neighbor cities of a city. 
 /// </summary>
 /// <param name="city">source city</param>
 /// <param name="mode">transportation mode</param>
 /// <returns>list of neighbor cities</returns>
 private List<City> FindNeighbours(City city, TransportModes mode)
 {
     List<City> neighbors = new List<City>();
     foreach (Link r in routes)
     {
         if (mode.Equals(r.TransportMode))
         {
             if (city.Equals(r.FromCity))
             {
                 neighbors.Add(r.ToCity);
             }
             else if (city.Equals(r.ToCity))
             {
                 neighbors.Add(r.FromCity);
             }
         }
     }
     return neighbors;
 }
Пример #59
0
 public RouteRequestEventArgs(String fromCity, String toCity, TransportModes mode)
 {
     this.FromCity = fromCity;
     this.ToCity = toCity;
     this.Mode = mode;
 }
Пример #60
0
 public abstract List<Link> FindShortestRouteBetween(string fromCity, string toCity, TransportModes mode, IProgress<string> progress = null);