Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPMultipleGuidesPathRelinker.cs @ 8319

Last change on this file since 8319 was 8319, checked in by jkarder, 12 years ago

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.TravelingSalesman {
34  /// <summary>
35  /// An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy.
36  /// </summary>
37  /// <remarks>
38  /// The operator incrementally changes the initiating solution towards the guiding solution by correcting edges as needed. For each city it choses the best edge from all guiding solutions.
39  /// </remarks>
40  [Item("TSPMultipleGuidesPathRelinker", "An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy.")]
41  [StorableClass]
42  public sealed class TSPMultipleGuidesPathRelinker : SingleObjectivePathRelinker {
43    #region Parameter properties
44    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
45      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
46    }
47    #endregion
48
49    #region Properties
50    public DistanceMatrix DistanceMatrix {
51      get { return DistanceMatrixParameter.ActualValue; }
52    }
53    #endregion
54
55    [StorableConstructor]
56    private TSPMultipleGuidesPathRelinker(bool deserializing) : base(deserializing) { }
57    private TSPMultipleGuidesPathRelinker(TSPMultipleGuidesPathRelinker original, Cloner cloner) : base(original, cloner) { }
58    public TSPMultipleGuidesPathRelinker()
59      : base() {
60      #region Create parameters
61      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
62      #endregion
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new TSPMultipleGuidesPathRelinker(this, cloner);
67    }
68
69    public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, DistanceMatrix distances, PercentValue n) {
70      if (!(initiator is Permutation) || guides.Any(x => !(x is Permutation)))
71        throw new ArgumentException("Cannot relink path because some of the provided solutions have the wrong type.");
72      if (n.Value <= 0.0)
73        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
74
75      Permutation v1 = initiator.Clone() as Permutation;
76      Permutation[] targets = new Permutation[guides.Length];
77      Array.Copy(guides, targets, guides.Length);
78
79      if (targets.Any(x => x.Length != v1.Length))
80        throw new ArgumentException("At least one solution is of different length.");
81
82      IList<Permutation> solutions = new List<Permutation>();
83      for (int i = 0; i < v1.Length; i++) {
84        int currCityIndex = i;
85        int bestCityIndex = (i + 1) % v1.Length;
86        double currDistance = distances[v1[currCityIndex], v1[bestCityIndex]];
87        targets.ToList().ForEach(solution => {
88          var node = solution.Select((x, index) => new { Id = x, Index = index }).First(x => x.Id == v1[currCityIndex]);
89          int pred = solution[(node.Index - 1 + solution.Length) % solution.Length];
90          int succ = solution[(node.Index + 1) % solution.Length];
91          var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = distances[x, node.Id] });
92          if (results.Any(x => x.Distance < currDistance)) {
93            var bestCity = results.OrderBy(x => x.Distance).First();
94            bestCityIndex = v1.Select((x, index) => new { Id = x, Index = index }).First(x => x.Id == bestCity.Id).Index;
95            currDistance = bestCity.Distance;
96          }
97        });
98        Invert(v1, currCityIndex + 1, bestCityIndex);
99        solutions.Add(v1.Clone() as Permutation);
100      }
101
102      IList<IItem> selection = new List<IItem>();
103      if (solutions.Count > 0) {
104        int noSol = (int)(solutions.Count * n.Value);
105        if (noSol <= 0) noSol++;
106        double stepSize = (double)solutions.Count / (double)noSol;
107        for (int i = 0; i < noSol; i++)
108          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
109      }
110
111      return new ItemArray<IItem>(selection);
112    }
113
114    private static void Invert(Permutation sol, int i, int j) {
115      if (i != j)
116        for (int a = 0; a < Math.Abs(i - j) / 2; a++)
117          if (sol[(i + a) % sol.Length] != sol[(j - a + sol.Length) % sol.Length]) {
118            // XOR swap
119            sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length];
120            sol[(j - a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length];
121            sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length];
122          }
123    }
124
125    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
126      if (parents.Length < 2)
127        throw new ArgumentException("The number of parents is smaller than 2.");
128      return Apply(parents[0], parents.Skip(1).ToArray(), DistanceMatrix, n);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.