1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.PTSP {
|
---|
33 | /// <summary>
|
---|
34 | /// A base class for operators which evaluate TSP solutions.
|
---|
35 | /// </summary>
|
---|
36 | [Item("PTSPMoveEvaluator", "A base class for operators which evaluate TSP moves.")]
|
---|
37 | [StorableClass]
|
---|
38 | public abstract class PTSPMoveEvaluator : SingleSuccessorOperator, IPTSPMoveEvaluator, IMoveOperator {
|
---|
39 |
|
---|
40 | public abstract Type EvaluatorType { get; }
|
---|
41 | public override bool CanChangeName {
|
---|
42 | get { return false; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
46 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
49 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
52 | get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
55 | get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter {
|
---|
58 | get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
62 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
65 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableConstructor]
|
---|
69 | protected PTSPMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
70 | protected PTSPMoveEvaluator(PTSPMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
71 | protected PTSPMoveEvaluator()
|
---|
72 | : base() {
|
---|
73 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
74 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The city's coordinates."));
|
---|
75 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
76 | 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."));
|
---|
77 | Parameters.Add(new ValueParameter<ItemList<ItemList<IntValue>>>("Realizations", "The concrete..."));
|
---|
78 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a TSP solution."));
|
---|
79 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a TSP solution."));
|
---|
80 | }
|
---|
81 |
|
---|
82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
83 | private void AfterDeserialization() {
|
---|
84 | // BackwardsCompatibility3.3
|
---|
85 | #region Backwards compatible code (remove with 3.4)
|
---|
86 | LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as LookupParameter<DoubleMatrix>;
|
---|
87 | if (oldDistanceMatrixParameter != null) {
|
---|
88 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
89 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
90 | DistanceMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override IOperation Apply() {
|
---|
96 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
97 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
98 | double relativeQualityDifference = 0;
|
---|
99 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
100 | DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
101 | if (distanceMatrix == null) {
|
---|
102 | if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
|
---|
103 | distanceMatrix = CalculateDistanceMatrix(coordinates);
|
---|
104 | DistanceMatrixParameter.ActualValue = distanceMatrix;
|
---|
105 | }
|
---|
106 | relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
|
---|
107 | } else {
|
---|
108 | if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
|
---|
109 | relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
|
---|
110 | }
|
---|
111 | DoubleValue moveQuality = MoveQualityParameter.ActualValue;
|
---|
112 | if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
|
---|
113 | else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
|
---|
114 | return base.Apply();
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
|
---|
118 | protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix);
|
---|
119 | protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
|
---|
120 |
|
---|
121 | private DistanceMatrix CalculateDistanceMatrix(DoubleMatrix c) {
|
---|
122 | DistanceMatrix distanceMatrix = new DistanceMatrix(c.Rows, c.Rows);
|
---|
123 | for (int i = 0; i < distanceMatrix.Rows; i++) {
|
---|
124 | for (int j = 0; j < distanceMatrix.Columns; j++)
|
---|
125 | distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
|
---|
126 | }
|
---|
127 | return (DistanceMatrix)distanceMatrix.AsReadOnly();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|