[12380] | 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using System;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.PTSP {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// An operator to evaluate inversion moves (2-opt).
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("PTSPInversionMovePathEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public class PTSPEstimatedInversionEvaluator : PTSPMoveEvaluator, IPermutationInversionMoveOperator {
|
---|
| 37 | public override Type EvaluatorType {
|
---|
| 38 | get { return typeof(PTSPEstimatedInversionEvaluator); }
|
---|
| 39 | }
|
---|
| 40 | public ILookupParameter<InversionMove> InversionMoveParameter {
|
---|
| 41 | get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
| 47 | protected PTSPEstimatedInversionEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 48 | protected PTSPEstimatedInversionEvaluator(PTSPEstimatedInversionEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 49 | public PTSPEstimatedInversionEvaluator()
|
---|
| 50 | : base() {
|
---|
| 51 | Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new PTSPEstimatedInversionEvaluator(this, cloner);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, PTSPEstimatedInversionEvaluator evaluator) {
|
---|
| 59 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
| 60 | int edge1target = permutation[move.Index1];
|
---|
| 61 | int edge2source = permutation[move.Index2];
|
---|
| 62 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
| 63 | if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
|
---|
| 64 | double moveQuality = 0;
|
---|
| 65 | // remove two edges
|
---|
| 66 | moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
| 67 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
| 68 | moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
| 69 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 70 | // add two edges
|
---|
| 71 | moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
| 72 | coordinates[edge2source, 0], coordinates[edge2source, 1]);
|
---|
| 73 | moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
|
---|
| 74 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 75 | return moveQuality;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public static double EvaluateByDistanceMatrix(Permutation permutation, InversionMove move, DistanceMatrix distanceMatrix, ItemList<ItemList<IntValue>> realizations) {
|
---|
| 79 | double moveQuality = 0;
|
---|
| 80 | int[] edges = new int[4];
|
---|
| 81 | int[] indices = new int[4];
|
---|
| 82 | edges[0] = permutation.GetCircular(move.Index1 - 1);
|
---|
| 83 | indices[0] = move.Index1-1;
|
---|
| 84 | if (indices[0] == -1) indices[0] = permutation.Length - 1;
|
---|
| 85 | edges[1] = permutation[move.Index1];
|
---|
| 86 | indices[1] = move.Index1;
|
---|
| 87 | edges[2] = permutation[move.Index2];
|
---|
| 88 | indices[2] = move.Index2;
|
---|
| 89 | edges[3] = permutation.GetCircular(move.Index2 + 1);
|
---|
| 90 | indices[3] = move.Index2+1;
|
---|
| 91 | if (indices[3] == permutation.Length + 1) indices[3] = 0;
|
---|
| 92 | int[] aPosteriori = new int[4];
|
---|
| 93 | foreach (ItemList<IntValue> realization in realizations) {
|
---|
| 94 | for (int i = 0; i < edges.Length; i++) {
|
---|
| 95 | if (realization[edges[i]].Value == 1) {
|
---|
| 96 | aPosteriori[i] = edges[i];
|
---|
| 97 | } else {
|
---|
| 98 | int j=1;
|
---|
| 99 | if (i%2==0) {
|
---|
| 100 | // find nearest predecessor in realization if source edge
|
---|
| 101 | while(realization[permutation.GetCircular(indices[i]-j)].Value==0) {
|
---|
| 102 | j++;
|
---|
| 103 | }
|
---|
| 104 | aPosteriori[i] = permutation.GetCircular(indices[i] - j);
|
---|
| 105 | } else {
|
---|
| 106 | // find nearest successor in realization if target edge
|
---|
| 107 | while(realization[permutation.GetCircular(indices[i]+j)].Value==0) {
|
---|
| 108 | j++;
|
---|
| 109 | }
|
---|
| 110 | aPosteriori[i] = permutation.GetCircular(indices[i] + j);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | // compute cost difference between the two a posteriori solutions
|
---|
| 115 | if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3])) {
|
---|
| 116 | moveQuality = moveQuality + distanceMatrix[aPosteriori[0], aPosteriori[2]] + distanceMatrix[aPosteriori[1], aPosteriori[3]] - distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]];
|
---|
| 117 | }
|
---|
| 118 | Array.Clear(aPosteriori, 0, aPosteriori.Length);
|
---|
| 119 | }
|
---|
| 120 | // return average of cost differences
|
---|
[12799] | 121 | return moveQuality / realizations.Count;
|
---|
[12380] | 122 | }
|
---|
| 123 |
|
---|
| 124 | protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
|
---|
| 125 | return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
|
---|
| 129 | return EvaluateByDistanceMatrix(permutation, InversionMoveParameter.ActualValue, distanceMatrix, RealizationsParameter.Value);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | protected override double CalculateDistance(double x1, double y1, double x2, double y2) {
|
---|
| 133 | return Math.Round(Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|