Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/TwoOpt/PTSPEstimatedInversionMovePathEvaluator.cs @ 12219

Last change on this file since 12219 was 12219, checked in by apolidur, 9 years ago

#2221: First version of 1-shift and 2-p-opt moves

File size: 6.4 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System;
29
30namespace 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 PTSPEstimatedInversionMovePathEvaluator : PTSPPathMoveEvaluator, IPermutationInversionMoveOperator {
37    public override Type EvaluatorType {
38      get { return typeof(PTSPEstimatedInversionMovePathEvaluator); }
39    }
40    public ILookupParameter<InversionMove> InversionMoveParameter {
41      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
42    }
43
44   
45
46    [StorableConstructor]
47    protected PTSPEstimatedInversionMovePathEvaluator(bool deserializing) : base(deserializing) { }
48    protected PTSPEstimatedInversionMovePathEvaluator(PTSPEstimatedInversionMovePathEvaluator original, Cloner cloner) : base(original, cloner) { }
49    public PTSPEstimatedInversionMovePathEvaluator()
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 PTSPEstimatedInversionMovePathEvaluator(this, cloner);
56    }
57
58    public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, PTSPEstimatedInversionMovePathEvaluator 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        moveQuality += distanceMatrix[aPosteriori[0], aPosteriori[2]] + distanceMatrix[aPosteriori[1], aPosteriori[3]];
116        moveQuality -= distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]];
117      }
118      // return average of cost differences
119      return moveQuality / realizations.Capacity;
120    }
121
122    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
123      return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);
124    }
125
126    protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
127      return EvaluateByDistanceMatrix(permutation, InversionMoveParameter.ActualValue, distanceMatrix, RealizationsParameter.Value);
128    }
129
130    protected override double CalculateDistance(double x1, double y1, double x2, double y2) {
131      return Math.Round(Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.