Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/Moves/OneShift/PTSPEstimatedInsertionMoveEvaluator.cs @ 17533

Last change on this file since 17533 was 17533, checked in by abeham, 4 years ago

#2521: Unified architecture

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