Beispiel #1
0
 public Route(ICityPlace start, ICityPlace end, IReadOnlyCollection <ICityRoad> roads)
 {
     Start = start;
     End   = end;
     Roads = roads;
     new RouteValidator().ValidateAndThrow(this);
 }
Beispiel #2
0
 public DijkstraResolve(ISet <DijkstraVertex> dijkstraVertices,
                        ICityMap cityMap, ICityPlace startPlace)
 {
     _dijkstraVertices = dijkstraVertices;
     CityMap           = cityMap;
     StartPlace        = startPlace;
 }
Beispiel #3
0
 public CityRoad(ICityPlace firstPlace, ICityPlace secondPlace, RoadRank rank)
 {
     FirstPlace  = firstPlace;
     SecondPlace = secondPlace;
     Rank        = rank;
     CityRoadValidator.Instance.ValidateAndThrow(this);
 }
Beispiel #4
0
        public DijkstraVertex(ICityPlace cityPlace)
        {
            CityPlace = cityPlace;

            IsVisited         = false;
            EdgesWeightSum    = double.MaxValue;
            PreviousCityPlace = null;
        }
Beispiel #5
0
        public ICityRoad GetRoadBetween(ICityPlace firstCityPlace, ICityPlace secondCityPlace)
        {
            var road = Roads.FirstOrDefault(x =>
                                            x.FirstPlace == firstCityPlace && x.SecondPlace == secondCityPlace ||
                                            x.FirstPlace == secondCityPlace && x.SecondPlace == firstCityPlace);

            return(road);
        }
Beispiel #6
0
        public IEnumerable <ICityPlace> GetNeighborCityPlaces(ICityPlace cityPlace)
        {
            var roads = GetRoadsFrom(cityPlace).ToArray();

            return(roads.Select(x => x.SecondPlace)
                   .Union(roads.Select(x => x.FirstPlace))
                   .Distinct()
                   .Except(new[] { cityPlace }));
        }
Beispiel #7
0
        public IEnumerable <ICityRoad> GetRoadsFrom(ICityPlace cityPlace)
        {
            if (!Places.Contains(cityPlace))
            {
                throw new ArgumentException("Местоположение не принадлжеит городу.");
            }

            return(Roads
                   .Where(x => x.FirstPlace == cityPlace || x.SecondPlace == cityPlace)
                   .Distinct());
        }
Beispiel #8
0
        /// <summary>
        /// Находит ближайший путь до указанного местоположения.
        /// </summary>
        /// <param name="cityPlace">Местоположение до которого необходимо получить ближайший путь.</param>
        /// <returns>Путь <see cref="IRoute"/>.</returns>
        public IRoute FindShortRouteTo(ICityPlace cityPlace)
        {
            if (!CityMap.Places.Contains(cityPlace))
            {
                throw new ArgumentException("Местоположение не относится к текущему городу.");
            }

            var roads = new List <ICityRoad>();

            var vertex = _dijkstraVertices.GetDijkstraVertex(cityPlace);

            while (vertex.CityPlace != StartPlace)
            {
                var road = CityMap.GetRoadBetween(vertex.CityPlace, vertex.PreviousCityPlace);
                roads.Add(road);
                vertex = _dijkstraVertices.GetDijkstraVertex(vertex.PreviousCityPlace);
            }

            roads.Reverse();
            return(new Route(StartPlace, cityPlace, roads));
        }
Beispiel #9
0
        public DijkstraResolve Resolve(ICityMap cityMap, ICityPlace startPoint)
        {
            _cityMap  = cityMap;
            _vertexes = InitDijkstraVertexes(cityMap.Places);

            _vertexes
            .First(x => x.CityPlace == startPoint)
            .EdgesWeightSum = 0;

            while (true)
            {
                var current = FindUnvisitedVertexWithMinSum();
                if (current == null)
                {
                    break;
                }

                SetOptimalWaysForNeighbor(current);
            }

            return(new DijkstraResolve(_vertexes, cityMap, startPoint));
        }
Beispiel #10
0
 public static DijkstraVertex GetDijkstraVertex(this IEnumerable <DijkstraVertex> vertexes,
                                                ICityPlace cityPlace)
 {
     return(vertexes.First(x => x.CityPlace == cityPlace));
 }
Beispiel #11
0
 public SelfWeightedCityRoad(ICityPlace firstPlace, ICityPlace secondPlace, double weight)
 {
     FirstPlace  = firstPlace;
     SecondPlace = secondPlace;
     Weight      = weight;
 }