source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LinearLinkageMemPR.cs @ 14477

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

#2701:

  • Added TryGetBy(First|Second) method to BidirectionalDictionary
  • Updated linear linkage encoding
    • Added move generator and moves for shift, merge, split, and extract moves
    • Added unit test (Apply/Undo)
  • Updated MemPR (linear linkage)
    • Added basic tabu walk
  • Fixed bug in MemPR (permutation)
  • Updated Tests project
File size: 12.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System.Threading;
26using HeuristicLab.Algorithms.MemPR.Interfaces;
27using HeuristicLab.Algorithms.MemPR.Util;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Encodings.LinearLinkageEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.MemPR.LinearLinkage {
38  [Item("MemPR (linear linkage)", "MemPR implementation for linear linkage vectors.")]
39  [StorableClass]
40  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
41  public class LinearLinkageMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<LinearLinkageEncoding>, Encodings.LinearLinkageEncoding.LinearLinkage, LinearLinkageMemPRPopulationContext, LinearLinkageMemPRSolutionContext> {
42    private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
43   
44    [StorableConstructor]
45    protected LinearLinkageMemPR(bool deserializing) : base(deserializing) { }
46    protected LinearLinkageMemPR(LinearLinkageMemPR original, Cloner cloner) : base(original, cloner) { }
47    public LinearLinkageMemPR() {
48      foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<LinearLinkageMemPRPopulationContext>>())
49        SolutionModelTrainerParameter.ValidValues.Add(trainer);
50     
51      foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<LinearLinkageMemPRSolutionContext>>()) {
52        LocalSearchParameter.ValidValues.Add(localSearch);
53      }
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new LinearLinkageMemPR(this, cloner);
58    }
59
60    protected override bool Eq(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
61      var s1 = a.Solution;
62      var s2 = b.Solution;
63      if (s1.Length != s2.Length) return false;
64      for (var i = 0; i < s1.Length; i++)
65        if (s1[i] != s2[i]) return false;
66      return true;
67    }
68
69    protected override double Dist(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
70      return HammingSimilarityCalculator.CalculateSimilarity(a.Solution, b.Solution);
71    }
72
73    protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> ToScope(Encodings.LinearLinkageEncoding.LinearLinkage code, double fitness = double.NaN) {
74      var creator = Problem.SolutionCreator as ILinearLinkageCreator;
75      if (creator == null) throw new InvalidOperationException("Can only solve linear linkage encoded problems with MemPR (linear linkage)");
76      return new SingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>(code, creator.LLEParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
77        Parent = Context.Scope
78      };
79    }
80
81    protected override ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> CalculateSubspace(IEnumerable<Encodings.LinearLinkageEncoding.LinearLinkage> solutions, bool inverse = false) {
82      var pop = solutions.ToList();
83      var N = pop[0].Length;
84      var subspace = new bool[N];
85      for (var i = 0; i < N; i++) {
86        var val = pop[0][i];
87        if (inverse) subspace[i] = true;
88        for (var p = 1; p < pop.Count; p++) {
89          if (pop[p][i] != val) subspace[i] = !inverse;
90        }
91      }
92      return new LinearLinkageSolutionSubspace(subspace);
93    }
94
95    protected override int TabuWalk(
96        ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> scope,
97        int maxEvals, CancellationToken token,
98        ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> sub_space = null) {
99      var maximization = Context.Problem.Maximization;
100      var subspace = sub_space is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)sub_space).Subspace : null;
101      var evaluations = 0;
102      var quality = scope.Fitness;
103      var bestQuality = quality;
104      if (double.IsNaN(quality)) {
105        Evaluate(scope, token);
106        quality = scope.Fitness;
107        evaluations++;
108        if (evaluations >= maxEvals) return evaluations;
109      }
110      var currentScope = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)scope.Clone();
111      var current = currentScope.Solution;
112
113      var tabu = new double[current.Length, current.Length];
114      for (var i = 0; i < current.Length; i++) {
115        for (var j = i; j < current.Length; j++) {
116          tabu[i, j] = tabu[j, i] = maximization ? double.MinValue : double.MaxValue;
117        }
118        tabu[i, current[i]] = quality;
119      }
120     
121      // this dictionary holds the last relevant links
122      var links = new BidirectionalDictionary<int, int>();
123      bool allMoveTabu;
124      for (var iter = 0; iter < int.MaxValue; iter++) {
125        allMoveTabu = true;
126        // clear the dictionary before a new pass through the array is made
127        links.Clear();
128        for (var i = 0; i < current.Length; i++) {
129          if (subspace != null && !subspace[i]) {
130            links.RemoveBySecond(i);
131            links.Add(i, current[i]);
132            continue;
133          }
134
135          var next = current[i];
136          foreach (var move in MoveGenerator.GenerateForItem(i, next, links)) {
137            // we intend to break link i -> next
138            var qualityToBreak = tabu[i, next];
139            move.Apply(current);
140            var qualityToRestore = tabu[i, current[i]]; // current[i] is new next
141            Evaluate(currentScope, token);
142            evaluations++;
143            var moveF = currentScope.Fitness;
144            var isNotTabu = FitnessComparer.IsBetter(maximization, moveF, qualityToBreak)
145                         && FitnessComparer.IsBetter(maximization, moveF, qualityToRestore);
146            if (isNotTabu) allMoveTabu = false;
147            var isImprovement = FitnessComparer.IsBetter(maximization, moveF, bestQuality);
148            if (isNotTabu || isImprovement) {
149              if (maximization) {
150                tabu[i, next] = Math.Max(tabu[i, next], moveF);
151                tabu[i, current[i]] = Math.Max(tabu[i, current[i]], moveF);
152              } else {
153                tabu[i, next] = Math.Min(tabu[i, next], moveF);
154                tabu[i, current[i]] = Math.Min(tabu[i, current[i]], moveF);
155              }
156              quality = moveF;
157              if (isImprovement) bestQuality = quality;
158
159              move.UpdateLinks(links);
160              break;
161            } else move.Undo(current);
162            if (evaluations >= maxEvals) break;
163          }
164          links.RemoveBySecond(i);
165          links.Add(i, current[i]);
166          if (evaluations >= maxEvals) break;
167          if (token.IsCancellationRequested) break;
168        }
169        if (allMoveTabu) break;
170        if (evaluations >= maxEvals) break;
171        if (token.IsCancellationRequested) break;
172      }
173      return evaluations;
174    }
175
176    protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Cross(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p1Scope, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p2Scope, CancellationToken token) {
177      var p1 = p1Scope.Solution;
178      var p2 = p2Scope.Solution;
179      var transfered = new bool[p1.Length];
180      var subspace = new bool[p1.Length];
181      var lleeChild = new int[p1.Length];
182      var lleep1 = p1.ToLLEe();
183      var lleep2 = p2.ToLLEe();
184      for (var i = p1.Length - 1; i >= 0; i--) {
185        // Step 1
186        subspace[i] = p1[i] != p2[i];
187        var p1IsEnd = p1[i] == i;
188        var p2IsEnd = p2[i] == i;
189        if (p1IsEnd & p2IsEnd) {
190          transfered[i] = true;
191        } else if (p1IsEnd | p2IsEnd) {
192          transfered[i] = Context.Random.NextDouble() < 0.5;
193        }
194        lleeChild[i] = i;
195
196        // Step 2
197        if (transfered[i]) continue;
198        var end1 = lleep1[i];
199        var end2 = lleep2[i];
200        var containsEnd1 = transfered[end1];
201        var containsEnd2 = transfered[end2];
202        if (containsEnd1 & containsEnd2) {
203          if (Context.Random.NextDouble() < 0.5) {
204            lleeChild[i] = end1;
205          } else {
206            lleeChild[i] = end2;
207          }
208        } else if (containsEnd1) {
209          lleeChild[i] = end1;
210        } else if (containsEnd2) {
211          lleeChild[i] = end2;
212        } else {
213          if (Context.Random.NextDouble() < 0.5) {
214            lleeChild[i] = lleeChild[p1[i]];
215          } else {
216            lleeChild[i] = lleeChild[p2[i]];
217          }
218        }
219      }
220      var child = new Encodings.LinearLinkageEncoding.LinearLinkage(lleeChild.Length);
221      child.FromLLEe(lleeChild);
222     
223      return ToScope(child);
224    }
225
226    protected override void Mutate(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> offspring, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
227      var lle = offspring.Solution;
228      var subset = subspace is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)subspace).Subspace : null;
229      for (var i = 0; i < lle.Length - 1; i++) {
230        if (subset == null || subset[i]) continue; // mutation works against crossover so aims to mutate noTouch points
231        if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
232          subset[i] = true;
233          var index = Context.Random.Next(i, lle.Length);
234          for (var j = index - 1; j >= i; j--) {
235            if (lle[j] == index) index = j;
236          }
237          lle[i] = index;
238          index = i;
239          var idx2 = i;
240          for (var j = i - 1; j >= 0; j--) {
241            if (lle[j] == lle[index]) {
242              lle[j] = idx2;
243              index = idx2 = j;
244            } else if (lle[j] == idx2) idx2 = j;
245          }
246        }
247      }
248    }
249
250    protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Relink(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b, CancellationToken token) {
251      var maximization = Context.Problem.Maximization;
252      if (double.IsNaN(a.Fitness)) {
253        Evaluate(a, token);
254        Context.IncrementEvaluatedSolutions(1);
255      }
256      if (double.IsNaN(b.Fitness)) {
257        Evaluate(b, token);
258        Context.IncrementEvaluatedSolutions(1);
259      }
260      var child = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)a.Clone();
261      var cgroups = child.Solution.GetGroups().Select(x => new HashSet<int>(x)).ToList();
262      var g2 = b.Solution.GetGroups().ToList();
263      var order = Enumerable.Range(0, g2.Count).Shuffle(Context.Random).ToList();
264      ISingleObjectiveSolutionScope <Encodings.LinearLinkageEncoding.LinearLinkage> bestChild = null;
265      for (var j = 0; j < g2.Count; j++) {
266        var g = g2[order[j]];
267        var changed = false;
268        for (var k = j; k < cgroups.Count; k++) {
269          foreach (var f in g) if (cgroups[k].Remove(f)) changed = true;
270          if (cgroups[k].Count == 0) {
271            cgroups.RemoveAt(k);
272            k--;
273          }
274        }
275        cgroups.Insert(0, new HashSet<int>(g));
276        child.Solution.SetGroups(cgroups);
277        if (changed) {
278          Evaluate(child, token);
279          if (bestChild == null || FitnessComparer.IsBetter(maximization, child.Fitness, bestChild.Fitness)) {
280            bestChild = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)child.Clone();
281          }
282        }
283      };
284      return bestChild;
285    }
286  }
287}
Note: See TracBrowser for help on using the repository browser.