public void EvaluateAndLoadTour(int[] tour)
        {
            var route = new Permutation(PermutationTypes.RelativeUndirected, tour);

            BestKnownSolution = route;

            double quality;

            if (Evaluator is ITSPDistanceMatrixEvaluator)
            {
                quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
            }
            else if (Evaluator is ITSPCoordinatesPathEvaluator)
            {
                quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
            }
            else
            {
                throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
            }
            BestKnownQuality = new DoubleValue(quality);
        }
 private TSPDistanceMatrixEvaluator(TSPDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner)
 {
 }
        public void Load(TSPData data)
        {
            if (data.Coordinates == null && data.Distances == null)
            {
                throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
            }
            if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att ||
                                                             data.DistanceMeasure == DistanceMeasure.Manhattan ||
                                                             data.DistanceMeasure == DistanceMeasure.Maximum))
            {
                throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
            }
            if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
            {
                throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates.");
            }

            Name        = data.Name;
            Description = data.Description;

            bool clearCoordinates = false, clearDistanceMatrix = false;

            if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
            {
                Coordinates = new DoubleMatrix(data.Coordinates);
            }
            else
            {
                clearCoordinates = true;
            }

            TSPEvaluator evaluator;

            if (data.DistanceMeasure == DistanceMeasure.Att ||
                data.DistanceMeasure == DistanceMeasure.Manhattan ||
                data.DistanceMeasure == DistanceMeasure.Maximum)
            {
                evaluator         = new TSPDistanceMatrixEvaluator();
                UseDistanceMatrix = new BoolValue(true);
                DistanceMatrix    = new DistanceMatrix(data.GetDistanceMatrix());
            }
            else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null)
            {
                evaluator         = new TSPDistanceMatrixEvaluator();
                UseDistanceMatrix = new BoolValue(true);
                DistanceMatrix    = new DistanceMatrix(data.Distances);
            }
            else
            {
                clearDistanceMatrix = true;
                UseDistanceMatrix   = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
                switch (data.DistanceMeasure)
                {
                case DistanceMeasure.Euclidean:
                    evaluator = new TSPEuclideanPathEvaluator();
                    break;

                case DistanceMeasure.RoundedEuclidean:
                    evaluator = new TSPRoundedEuclideanPathEvaluator();
                    break;

                case DistanceMeasure.UpperEuclidean:
                    evaluator = new TSPUpperEuclideanPathEvaluator();
                    break;

                case DistanceMeasure.Geo:
                    evaluator = new TSPGeoPathEvaluator();
                    break;

                default:
                    throw new InvalidDataException("An unknown distance measure is given in the instance!");
                }
            }
            evaluator.QualityParameter.ActualName = "TSPTourLength";
            Evaluator = evaluator;

            // reset them after assigning the evaluator
            if (clearCoordinates)
            {
                Coordinates = null;
            }
            if (clearDistanceMatrix)
            {
                DistanceMatrix = null;
            }

            BestKnownSolution = null;
            BestKnownQuality  = null;

            if (data.BestKnownTour != null)
            {
                try {
                    EvaluateAndLoadTour(data.BestKnownTour);
                } catch (InvalidOperationException) {
                    if (data.BestKnownQuality.HasValue)
                    {
                        BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
                    }
                }
            }
            else if (data.BestKnownQuality.HasValue)
            {
                BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
            }
            OnReset();
        }
 private TSPDistanceMatrixEvaluator(TSPDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }