PlotMyFace.Location.GetTotalDistance C# (CSharp) Метод

GetTotalDistance() публичный статический Метод

public static GetTotalDistance ( Location startLocation, Location locations ) : double
startLocation Location
locations Location
Результат double
        public static double GetTotalDistance(Location startLocation, Location[] locations)
        {
            if (startLocation == null)
                throw new ArgumentNullException("startLocation");

            if (locations == null)
                throw new ArgumentNullException("locations");

            if (locations.Length == 0)
                throw new ArgumentException("The locations array must have at least one element.", "locations");

            foreach(var location in locations)
                if (location == null)
                    throw new ArgumentException("The locations array can't contain null values.");

            double result = startLocation.GetDistance(locations[0]);
            int countLess1 = locations.Length-1;
            for(int i=0; i<countLess1; i++)
            {
                var actual = locations[i];
                var next = locations[i+1];

                var distance = actual.GetDistance(next);
                result += distance;
            }

            result += locations[locations.Length-1].GetDistance(startLocation);

            return result;
        }

Usage Example

        public TravellingSalesmanAlgorithm(Location startLocation, Location[] destinations, int populationCount)
        {
            if (startLocation == null)
            {
                throw new ArgumentNullException("startLocation");
            }

            if (destinations == null)
            {
                throw new ArgumentNullException("destinations");
            }

            if (populationCount < 2)
            {
                throw new ArgumentOutOfRangeException("populationCount");
            }

            if (populationCount % 2 != 0)
            {
                throw new ArgumentException("The populationCount parameter must be an even value.", "populationCount");
            }

            _startLocation = startLocation;
            destinations   = (Location[])destinations.Clone();

            foreach (var destination in destinations)
            {
                if (destination == null)
                {
                    throw new ArgumentException("The destinations array can't contain null values.", "destinations");
                }
            }

            // This commented method uses a search of the kind "look for the nearest non visited location".
            // This is rarely the shortest path, yet it is already a "somewhat good" path.
            destinations = _GetFakeShortest(destinations);

            _populationWithDistances = new KeyValuePair <Location[], double> [populationCount];

            // Create initial population.
            for (int solutionIndex = 0; solutionIndex < populationCount; solutionIndex++)
            {
                var newPossibleDestinations = (Location[])destinations.Clone();

                // Try commenting the next 2 lines of code while keeping the _GetFakeShortest active.
                // If you avoid the algorithm from running and press reset, you will see that it always
                // start with a path that seems "good" but is not the best.
                //for(int randomIndex=0; randomIndex<newPossibleDestinations.Length; randomIndex++)
                //RandomProvider.FullyRandomizeLocations(newPossibleDestinations);

                var distance = Location.GetTotalDistance(startLocation, newPossibleDestinations);
                var pair     = new KeyValuePair <Location[], double>(newPossibleDestinations, distance);

                _populationWithDistances[solutionIndex] = pair;
            }

            Array.Sort(_populationWithDistances, _sortDelegate);
        }
All Usage Examples Of PlotMyFace.Location::GetTotalDistance