Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/Moves/TwoOpt/PTSPEstimatedInversionMoveEvaluator.cs @ 17253

Last change on this file since 17253 was 17253, checked in by abeham, 5 years ago

#2521: worked on refactoring PTSP

File size: 4.5 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 Inversion Move Evaluator", "Evaluates an inversion move (2-opt) over several realizations of tours by summing up the length of all added edges and subtracting the length of all deleted edges.")]
33  [StorableType("9E418FA4-7721-40D2-9FDC-DB82723F7DBF")]
34  public class PTSPEstimatedInversionMoveEvaluator : EstimatedPTSPMoveEvaluator, IPermutationInversionMoveOperator {
35
36    public ILookupParameter<InversionMove> InversionMoveParameter {
37      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
38    }
39
40    [StorableConstructor]
41    protected PTSPEstimatedInversionMoveEvaluator(StorableConstructorFlag _) : base(_) { }
42    protected PTSPEstimatedInversionMoveEvaluator(PTSPEstimatedInversionMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
43    public PTSPEstimatedInversionMoveEvaluator()
44      : base() {
45      Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new PTSPEstimatedInversionMoveEvaluator(this, cloner);
50    }
51
52    public static double EvaluateMove(Permutation tour, InversionMove move, IProbabilisticTSPData data, IEnumerable<BoolArray> realizations) {
53      double moveQuality = 0;
54      var edges = new int[4];
55      var indices = new int[4];
56      edges[0] = tour.GetCircular(move.Index1 - 1);
57      indices[0] = move.Index1 - 1;
58      if (indices[0] == -1) indices[0] = tour.Length - 1;
59      edges[1] = tour[move.Index1];
60      indices[1] = move.Index1;
61      edges[2] = tour[move.Index2];
62      indices[2] = move.Index2;
63      edges[3] = tour.GetCircular(move.Index2 + 1);
64      indices[3] = move.Index2 + 1;
65      if (indices[3] == tour.Length + 1) indices[3] = 0;
66      var aPosteriori = new int[4];
67      var count = 0;
68      foreach (var realization in realizations) {
69        for (var i = 0; i < edges.Length; i++) {
70          if (realization[edges[i]]) {
71            aPosteriori[i] = edges[i];
72          } else {
73            var j = 1;
74            if (i % 2 == 0) {
75              // find nearest predecessor in realization if source edge
76              while (!realization[tour.GetCircular(indices[i] - j)]) {
77                j++;
78              }
79              aPosteriori[i] = tour.GetCircular(indices[i] - j);
80            } else {
81              // find nearest successor in realization if target edge
82              while (!realization[tour.GetCircular(indices[i] + j)]) {
83                j++;
84              }
85              aPosteriori[i] = tour.GetCircular(indices[i] + j);
86            }
87          }
88        }
89        // compute cost difference between the two a posteriori solutions
90        if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3])) {
91          moveQuality = moveQuality + data.GetDistance(aPosteriori[0], aPosteriori[2]) + data.GetDistance(aPosteriori[1], aPosteriori[3])
92            - data.GetDistance(aPosteriori[0], aPosteriori[1]) - data.GetDistance(aPosteriori[2], aPosteriori[3]);
93        }
94        Array.Clear(aPosteriori, 0, aPosteriori.Length);
95        count++;
96      }
97      // return average of cost differences
98      return moveQuality / count;
99    }
100
101    protected override double EvaluateMove(Permutation tour, IProbabilisticTSPData data, ReadOnlyItemList<BoolArray> realizations) {
102      return EvaluateMove(tour, InversionMoveParameter.ActualValue, data, realizations);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.