public void Load(TSPData data) { if (data.Coordinates == null && data.Distances == null) { throw new InvalidDataException("The given instance specifies no coordinates or distance matrix!"); } if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2) { throw new 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."); } // Clear old solutions BestKnownSolution = null; Name = data.Name; Description = data.Description; Coordinates = data.Coordinates != null ? new DoubleMatrix(data.Coordinates) : null; if (data.Distances != null) { DistanceMatrix = new DistanceMatrix(data.Distances); } else { DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix()); } StartingPoint = 0; // First city is interpreted as start point TerminalPoint = data.Dimension - 1; // Last city is interpreted als end point PointVisitingCosts = 0; MaximumDistance = DistanceMatrix.Average() * 5.0; // distance from start to end first to last city is interpreted as maximum distance Scores = new DoubleArray(Enumerable.Repeat(1.0, data.Dimension).ToArray()); // all scores are 1 }
public void Load(TSPData data) { if (data.Dimension > 1000) { throw new System.IO.InvalidDataException("Instances with more than 1000 customers are not supported by the QAP."); } var weights = new DoubleMatrix(data.Dimension, data.Dimension); for (int i = 0; i < data.Dimension; i++) { weights[i, (i + 1) % data.Dimension] = 1; } var distances = new DoubleMatrix(data.GetDistanceMatrix()); Name = data.Name; Description = data.Description; Load(weights, distances); if (data.BestKnownQuality.HasValue) { BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value); } EvaluateAndLoadAssignment(data.BestKnownTour); OnReset(); }
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(); }