Changeset 3066 for trunk/sources
- Timestamp:
- 03/16/10 16:04:54 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.TSP/3.3
- Files:
-
- 2 deleted
- 4 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 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/HeuristicLab.Problems.TSP-3.3.csproj
r3053 r3066 85 85 <ItemGroup> 86 86 <Compile Include="Evaluators\TSPCoordinatesPathEvaluator.cs" /> 87 <Compile Include="Evaluators\TSPDistanceMatrixPathEvaluator.cs" />88 87 <Compile Include="Evaluators\TSPEvaluator.cs" /> 89 88 <Compile Include="Evaluators\TSPRoundedEuclideanPathEvaluator.cs" /> 90 89 <Compile Include="Interfaces\ITSPCoordinatesPathEvaluator.cs" /> 91 <Compile Include="Interfaces\ITSPDistanceMatrixPathEvaluator.cs" />92 90 <Compile Include="Interfaces\ITSPEvaluator.cs" /> 93 91 <Compile Include="Interfaces\ITSPPathEvaluator.cs" /> -
trunk/sources/HeuristicLab.Problems.TSP/3.3/Interfaces/ITSPCoordinatesPathEvaluator.cs
r3048 r3066 29 29 public interface ITSPCoordinatesPathEvaluator : ITSPPathEvaluator { 30 30 ILookupParameter<DoubleMatrix> CoordinatesParameter { get; } 31 ILookupParameter<DoubleMatrix> DistanceMatrixParameter { get; } 32 ILookupParameter<BoolValue> UseDistanceMatrixParameter { get; } 31 33 } 32 34 } -
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs
r3053 r3066 52 52 get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; } 53 53 } 54 public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter { 55 get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; } 56 } 57 public ValueParameter<BoolValue> UseDistanceMatrixParameter { 58 get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; } 59 } 54 60 public ValueParameter<IPermutationCreator> SolutionCreatorParameter { 55 61 get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; } … … 74 80 set { CoordinatesParameter.Value = value; } 75 81 } 82 public DoubleMatrix DistanceMatrix { 83 get { return DistanceMatrixParameter.Value; } 84 set { DistanceMatrixParameter.Value = value; } 85 } 86 public BoolValue UseDistanceMatrix { 87 get { return UseDistanceMatrixParameter.Value; } 88 set { UseDistanceMatrixParameter.Value = value; } 89 } 76 90 public IPermutationCreator SolutionCreator { 77 91 get { return SolutionCreatorParameter.Value; } … … 108 122 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false))); 109 123 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix(0, 0))); 124 Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 125 Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true))); 110 126 Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator)); 111 127 Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator)); … … 155 171 Coordinates.Reset += new EventHandler(Coordinates_Reset); 156 172 ParameterizeSolutionCreator(); 173 ClearDistanceMatrix(); 157 174 } 158 175 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { 176 ClearDistanceMatrix(); 159 177 } 160 178 private void Coordinates_Reset(object sender, EventArgs e) { 161 179 ParameterizeSolutionCreator(); 180 ClearDistanceMatrix(); 162 181 } 163 182 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { … … 174 193 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 175 194 ParameterizeEvaluator(); 195 ClearDistanceMatrix(); 176 196 OnEvaluatorChanged(); 177 197 } … … 195 215 if (Evaluator is ITSPPathEvaluator) 196 216 ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 197 if (Evaluator is ITSPCoordinatesPathEvaluator) 198 ((ITSPCoordinatesPathEvaluator)Evaluator).CoordinatesParameter.ActualName = CoordinatesParameter.Name; 217 if (Evaluator is ITSPCoordinatesPathEvaluator) { 218 ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator; 219 evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 220 evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 221 evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; 222 } 199 223 } 200 224 private void InitializeOperators() { … … 214 238 } 215 239 } 240 private void ClearDistanceMatrix() { 241 DistanceMatrixParameter.Value = null; 242 } 216 243 #endregion 217 244 }
Note: See TracChangeset
for help on using the changeset viewer.