1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
31 | /// <summary>
|
---|
32 | /// A base class for operators which evaluate TSP solutions.
|
---|
33 | /// </summary>
|
---|
34 | [Item("TSPMoveEvaluator", "A base class for operators which evaluate TSP moves.")]
|
---|
35 | [StorableClass]
|
---|
36 | public abstract class TSPPathMoveEvaluator : TSPMoveEvaluator, ITSPPathMoveEvaluator {
|
---|
37 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
38 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
39 | }
|
---|
40 | public ILookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
41 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
|
---|
44 | get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
|
---|
45 | }
|
---|
46 | public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
47 | get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected TSPPathMoveEvaluator()
|
---|
51 | : base() {
|
---|
52 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
53 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The city's coordinates."));
|
---|
54 | Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
55 | Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false."));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IOperation Apply() {
|
---|
59 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
60 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
61 | double relativeQualityDifference = 0;
|
---|
62 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
63 | DoubleMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
64 | if (distanceMatrix == null) {
|
---|
65 | distanceMatrix = CalculateDistanceMatrix(coordinates);
|
---|
66 | DistanceMatrixParameter.ActualValue = distanceMatrix;
|
---|
67 | }
|
---|
68 | relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
|
---|
69 | } else relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
|
---|
70 | DoubleValue moveQuality = MoveQualityParameter.ActualValue;
|
---|
71 | if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
|
---|
72 | else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
|
---|
73 | return base.Apply();
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
|
---|
77 | protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix);
|
---|
78 | protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
|
---|
79 |
|
---|
80 | private DoubleMatrix CalculateDistanceMatrix(DoubleMatrix c) {
|
---|
81 | DoubleMatrix distanceMatrix = new DoubleMatrix(c.Rows, c.Rows);
|
---|
82 | for (int i = 0; i < distanceMatrix.Rows; i++) {
|
---|
83 | for (int j = 0; j < distanceMatrix.Columns; j++)
|
---|
84 | distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
|
---|
85 | }
|
---|
86 | return distanceMatrix;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|