Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.PTSP/3.3/Moves/OneShift/PTSPEstimatedInsertionMoveEvaluator.cs @ 18079

Last change on this file since 18079 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.5 KB
RevLine 
[12261]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12261]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
[13412]22using System;
[12261]23using HeuristicLab.Common;
[12219]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[12219]29
[12261]30namespace HeuristicLab.Problems.PTSP {
[13470]31  [Item("PTSP Estimated Insertion Move Evaluator", "Evaluates an insertion move (1-shift)")]
[16565]32  [StorableType("DAC1CBD1-BF27-4BB4-B7C5-82EC58F7F5C9")]
[13412]33  public class PTSPEstimatedInsertionMoveEvaluator : EstimatedPTSPMoveEvaluator, IPermutationTranslocationMoveOperator {
[12219]34
35    public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
36      get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
37    }
38
39    [StorableConstructor]
[16565]40    protected PTSPEstimatedInsertionMoveEvaluator(StorableConstructorFlag _) : base(_) { }
[13412]41    protected PTSPEstimatedInsertionMoveEvaluator(PTSPEstimatedInsertionMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
42    public PTSPEstimatedInsertionMoveEvaluator()
[12219]43      : base() {
44      Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
[13412]48      return new PTSPEstimatedInsertionMoveEvaluator(this, cloner);
[12219]49    }
50
[13470]51    public static double EvaluateMove(Permutation tour, TranslocationMove move, Func<int, int, double> distance, ItemList<BoolArray> realizations) {
52      var afterMove = (Permutation)tour.Clone();
[13412]53      TranslocationManipulator.Apply(afterMove, move.Index1, move.Index1, move.Index3);
[12219]54      double moveQuality = 0;
[13412]55      var edges = new int[12];
56      var indices = new int[12];
[13470]57      edges[0] = tour.GetCircular(move.Index1 - 1);
58      indices[0] = DecreaseCircularIndex(tour.Length, move.Index1);
59      edges[1] = tour[move.Index1];
[13412]60      indices[1] = move.Index1;
[13470]61      edges[2] = tour[move.Index1];
[13412]62      indices[2] = move.Index1;
[13470]63      edges[3] = tour.GetCircular(move.Index1 + 1);
64      indices[3] = IncreaseCircularIndex(tour.Length, move.Index1);
[13412]65
66      edges[6] = afterMove.GetCircular(move.Index3 - 1);
67      indices[6] = DecreaseCircularIndex(afterMove.Length, move.Index3);
68      edges[7] = afterMove[move.Index3];
69      indices[7] = move.Index3;
70      edges[8] = afterMove[move.Index3];
71      indices[8] = move.Index3;
72      edges[9] = afterMove.GetCircular(move.Index3 + 1);
73      indices[9] = IncreaseCircularIndex(afterMove.Length, move.Index3);
74
75      if (move.Index3 > move.Index1) {
[13470]76        edges[4] = tour[move.Index3];
[13412]77        indices[4] = move.Index3;
[13470]78        edges[5] = tour.GetCircular(move.Index3 + 1);
[13412]79        indices[5] = indices[9];
80        edges[10] = afterMove.GetCircular(move.Index1 - 1);
81        indices[10] = indices[0];
82        edges[11] = afterMove[move.Index1];
83        indices[11] = move.Index1;
84      } else {
[13470]85        edges[4] = tour.GetCircular(move.Index3 - 1);
[13412]86        indices[4] = indices[6];
[13470]87        edges[5] = tour[move.Index3];
[13412]88        indices[5] = move.Index3;
89        edges[10] = afterMove[move.Index1];
90        indices[10] = move.Index1;
91        edges[11] = afterMove.GetCircular(move.Index1 + 1);
92        indices[11] = indices[3];
93      }
94      int[] aPosteriori = new int[12];
95      foreach (var realization in realizations) {
96        for (int i = 0; i < edges.Length; i++) {
97          Permutation tempPermutation;
98          if (i < 6) {
[13470]99            tempPermutation = tour;
[13412]100          } else {
101            tempPermutation = afterMove;
102          }
103          if (realization[edges[i]]) {
104            aPosteriori[i] = edges[i];
105          } else {
106            int j = 1;
107            if (i % 2 == 0) {
108              // find nearest predecessor in realization if source edge
109              while (!realization[tempPermutation.GetCircular(indices[i] - j)]) {
110                j++;
111              }
112              aPosteriori[i] = tempPermutation.GetCircular(indices[i] - j);
113            } else {
114              // find nearest successor in realization if target edge
115              while (!realization[tempPermutation.GetCircular(indices[i] + j)]) {
116                j++;
117              }
118              aPosteriori[i] = tempPermutation.GetCircular(indices[i] + j);
119            }
120          }
121        }
122        if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3]) &&
123          !(aPosteriori[0] == aPosteriori[4] && aPosteriori[1] == aPosteriori[5]) &&
124          !(aPosteriori[2] == aPosteriori[4] && aPosteriori[3] == aPosteriori[5])) {
125          // compute cost difference between the two a posteriori solutions
[13470]126          moveQuality = moveQuality + distance(aPosteriori[6], aPosteriori[7]) + distance(aPosteriori[8], aPosteriori[9]) + distance(aPosteriori[10], aPosteriori[11]);
127          moveQuality = moveQuality - distance(aPosteriori[0], aPosteriori[1]) - distance(aPosteriori[2], aPosteriori[3]) - distance(aPosteriori[4], aPosteriori[5]);
[13412]128        }
129        Array.Clear(aPosteriori, 0, aPosteriori.Length);
130      }
131      // return average of cost differences
132      return moveQuality / realizations.Count;
[12219]133    }
134
[13412]135    private static int DecreaseCircularIndex(int length, int index) {
136      var result = index - 1;
[12261]137      if (result == -1) {
138        result = length - 1;
139      }
140      return result;
141    }
142
[13412]143    private static int IncreaseCircularIndex(int length, int index) {
144      var result = index + 1;
[12261]145      if (result == length + 1) {
146        result = 0;
147      }
148      return result;
149    }
150
[13470]151    protected override double EvaluateMove(Permutation tour, Func<int, int, double> distance, ItemList<BoolArray> realizations) {
152      return EvaluateMove(tour, TranslocationMoveParameter.ActualValue, distance, realizations);
[12219]153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.