Changeset 3066 for trunk/sources/HeuristicLab.Problems.TSP/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs
- Timestamp:
- 03/16/10 16:04:54 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TSP/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs
r3053 r3066 39 39 get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; } 40 40 } 41 public ILookupParameter<DoubleMatrix> DistanceMatrixParameter { 42 get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; } 43 } 44 public ILookupParameter<BoolValue> UseDistanceMatrixParameter { 45 get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; } 46 } 41 47 42 48 protected TSPCoordinatesPathEvaluator() … … 44 50 Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated.")); 45 51 Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.")); 52 Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 53 Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.")); 46 54 } 47 55 48 56 public sealed override IOperation Apply() { 49 Permutation p = PermutationParameter.ActualValue; 50 DoubleMatrix c = CoordinatesParameter.ActualValue; 57 if (UseDistanceMatrixParameter.ActualValue.Value) { 58 Permutation p = PermutationParameter.ActualValue; 59 DoubleMatrix dm = DistanceMatrixParameter.ActualValue; 51 60 52 double length = 0; 53 for (int i = 0; i < p.Length - 1; i++) 54 length += CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]); 55 length += CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]); 56 QualityParameter.ActualValue = new DoubleValue(length); 61 if (dm == null) { // calculate distance matrix 62 DoubleMatrix c = CoordinatesParameter.ActualValue; 57 63 64 dm = new DoubleMatrix(c.Rows, c.Rows); 65 for (int i = 0; i < dm.Rows; i++) { 66 for (int j = 0; j < dm.Columns; j++) 67 dm[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]); 68 } 69 DistanceMatrixParameter.ActualValue = dm; 70 } 71 72 double length = 0; 73 for (int i = 0; i < p.Length - 1; i++) 74 length += dm[p[i], p[i + 1]]; 75 length += dm[p[p.Length - 1], p[0]]; 76 QualityParameter.ActualValue = new DoubleValue(length); 77 } else { 78 Permutation p = PermutationParameter.ActualValue; 79 DoubleMatrix c = CoordinatesParameter.ActualValue; 80 81 double length = 0; 82 for (int i = 0; i < p.Length - 1; i++) 83 length += CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]); 84 length += CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]); 85 QualityParameter.ActualValue = new DoubleValue(length); 86 } 58 87 return base.Apply(); 59 88 }
Note: See TracChangeset
for help on using the changeset viewer.