Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14487 was 14487, checked in by sraggl, 8 years ago

#2701 Merged with trunk to get new version of LinearLinkageEncoding
Improved MoveGenerator

File size: 14.6 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      if (double.IsNaN(quality)) {
104        Evaluate(scope, token);
105        quality = scope.Fitness;
106        evaluations++;
107        if (evaluations >= maxEvals) return evaluations;
108      }
109      var bestQuality = quality;
110      var currentScope = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)scope.Clone();
111      var current = currentScope.Solution;
112      Encodings.LinearLinkageEncoding.LinearLinkage bestOfTheWalk = null;
113      var bestOfTheWalkF = double.NaN;
114
115      var tabu = new double[current.Length, current.Length];
116      for (var i = 0; i < current.Length; i++) {
117        for (var j = i; j < current.Length; j++) {
118          tabu[i, j] = tabu[j, i] = maximization ? double.MinValue : double.MaxValue;
119        }
120        tabu[i, current[i]] = quality;
121      }
122     
123      // this dictionary holds the last relevant links
124      var links = new BidirectionalDictionary<int, int>();
125      Move bestOfTheRest = null;
126      var bestOfTheRestF = double.NaN;
127      var lastAppliedMove = -1;
128      for (var iter = 0; iter < int.MaxValue; iter++) {
129        // clear the dictionary before a new pass through the array is made
130        links.Clear();
131        for (var i = 0; i < current.Length; i++) {
132          if (subspace != null && !subspace[i]) {
133            links.RemoveBySecond(i);
134            links.Add(i, current[i]);
135            continue;
136          }
137
138          var next = current[i];
139
140          if (lastAppliedMove == i) {
141            if (bestOfTheRest != null) {
142              bestOfTheRest.Apply(current);
143              currentScope.Fitness = bestOfTheRestF;
144              quality = bestOfTheRestF;
145              if (maximization) {
146                tabu[i, next] = Math.Max(tabu[i, next], bestOfTheRestF);
147                tabu[i, current[i]] = Math.Max(tabu[i, current[i]], bestOfTheRestF);
148              } else {
149                tabu[i, next] = Math.Min(tabu[i, next], bestOfTheRestF);
150                tabu[i, current[i]] = Math.Min(tabu[i, current[i]], bestOfTheRestF);
151              }
152              if (FitnessComparer.IsBetter(maximization, bestOfTheRestF, bestOfTheWalkF)) {
153                bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
154                bestOfTheWalkF = bestOfTheRestF;
155              }
156              bestOfTheRest = null;
157              bestOfTheRestF = double.NaN;
158              lastAppliedMove = i;
159            } else {
160              lastAppliedMove = -1;
161            }
162            break;
163          } else {
164            foreach (var move in MoveGenerator.GenerateForItem(i, next, links)) {
165              // we intend to break link i -> next
166              var qualityToBreak = tabu[i, next];
167              move.Apply(current);
168              var qualityToRestore = tabu[i, current[i]]; // current[i] is new next
169              Evaluate(currentScope, token);
170              evaluations++;
171              var moveF = currentScope.Fitness;
172              var isNotTabu = FitnessComparer.IsBetter(maximization, moveF, qualityToBreak)
173                              && FitnessComparer.IsBetter(maximization, moveF, qualityToRestore);
174              var isImprovement = FitnessComparer.IsBetter(maximization, moveF, quality);
175              var isAspired = FitnessComparer.IsBetter(maximization, moveF, bestQuality);
176              if ((isNotTabu && isImprovement) || isAspired) {
177                if (maximization) {
178                  tabu[i, next] = Math.Max(tabu[i, next], moveF);
179                  tabu[i, current[i]] = Math.Max(tabu[i, current[i]], moveF);
180                } else {
181                  tabu[i, next] = Math.Min(tabu[i, next], moveF);
182                  tabu[i, current[i]] = Math.Min(tabu[i, current[i]], moveF);
183                }
184                quality = moveF;
185                if (isAspired) bestQuality = quality;
186
187                move.UpdateLinks(links);
188
189                if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheWalkF)) {
190                  bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
191                  bestOfTheWalkF = moveF;
192                }
193
194                bestOfTheRest = null;
195                bestOfTheRestF = double.NaN;
196                lastAppliedMove = i;
197                break;
198              } else {
199                if (isNotTabu) {
200                  if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheRestF)) {
201                    bestOfTheRest = move;
202                    bestOfTheRestF = moveF;
203                  }
204                }
205                move.Undo(current);
206                currentScope.Fitness = quality;
207              }
208              if (evaluations >= maxEvals) break;
209            }
210          }
211          links.RemoveBySecond(i);
212          links.Add(i, current[i]);
213          if (evaluations >= maxEvals) break;
214          if (token.IsCancellationRequested) break;
215        }
216        if (lastAppliedMove == -1) { // no move has been applied
217          if (bestOfTheRest != null) {
218            var i = bestOfTheRest.Item;
219            var next = current[i];
220            bestOfTheRest.Apply(current);
221            currentScope.Fitness = bestOfTheRestF;
222            quality = bestOfTheRestF;
223            if (maximization) {
224              tabu[i, next] = Math.Max(tabu[i, next], bestOfTheRestF);
225              tabu[i, current[i]] = Math.Max(tabu[i, current[i]], bestOfTheRestF);
226            } else {
227              tabu[i, next] = Math.Min(tabu[i, next], bestOfTheRestF);
228              tabu[i, current[i]] = Math.Min(tabu[i, current[i]], bestOfTheRestF);
229            }
230            if (FitnessComparer.IsBetter(maximization, bestOfTheRestF, bestOfTheWalkF)) {
231              bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
232              bestOfTheWalkF = bestOfTheRestF;
233            }
234
235            bestOfTheRest = null;
236            bestOfTheRestF = double.NaN;
237          } else break;
238        }
239        if (evaluations >= maxEvals) break;
240        if (token.IsCancellationRequested) break;
241      }
242      if (bestOfTheWalk != null) {
243        scope.Solution = bestOfTheWalk;
244        scope.Fitness = bestOfTheWalkF;
245      }
246      return evaluations;
247    }
248
249    protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Cross(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p1Scope, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p2Scope, CancellationToken token) {
250      var p1 = p1Scope.Solution;
251      var p2 = p2Scope.Solution;
252      return ToScope(GroupCrossover.Apply(Context.Random, p1, p2));
253    }
254
255    protected override void Mutate(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> offspring, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
256      var lle = offspring.Solution;
257      var subset = subspace is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)subspace).Subspace : null;
258      for (var i = 0; i < lle.Length - 1; i++) {
259        if (subset == null || subset[i]) continue; // mutation works against crossover so aims to mutate noTouch points
260        if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
261          subset[i] = true;
262          var index = Context.Random.Next(i, lle.Length);
263          for (var j = index - 1; j >= i; j--) {
264            if (lle[j] == index) index = j;
265          }
266          lle[i] = index;
267          index = i;
268          var idx2 = i;
269          for (var j = i - 1; j >= 0; j--) {
270            if (lle[j] == lle[index]) {
271              lle[j] = idx2;
272              index = idx2 = j;
273            } else if (lle[j] == idx2) idx2 = j;
274          }
275        }
276      }
277    }
278
279    protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Relink(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b, CancellationToken token) {
280      var maximization = Context.Problem.Maximization;
281      if (double.IsNaN(a.Fitness)) {
282        Evaluate(a, token);
283        Context.IncrementEvaluatedSolutions(1);
284      }
285      if (double.IsNaN(b.Fitness)) {
286        Evaluate(b, token);
287        Context.IncrementEvaluatedSolutions(1);
288      }
289      var child = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)a.Clone();
290      var cgroups = child.Solution.GetGroups().Select(x => new HashSet<int>(x)).ToList();
291      var g2 = b.Solution.GetGroups().ToList();
292      var order = Enumerable.Range(0, g2.Count).Shuffle(Context.Random).ToList();
293      ISingleObjectiveSolutionScope <Encodings.LinearLinkageEncoding.LinearLinkage> bestChild = null;
294      for (var j = 0; j < g2.Count; j++) {
295        var g = g2[order[j]];
296        var changed = false;
297        for (var k = j; k < cgroups.Count; k++) {
298          foreach (var f in g) if (cgroups[k].Remove(f)) changed = true;
299          if (cgroups[k].Count == 0) {
300            cgroups.RemoveAt(k);
301            k--;
302          }
303        }
304        cgroups.Insert(0, new HashSet<int>(g));
305        child.Solution.SetGroups(cgroups);
306        if (changed) {
307          Evaluate(child, token);
308          if (bestChild == null || FitnessComparer.IsBetter(maximization, child.Fitness, bestChild.Fitness)) {
309            bestChild = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)child.Clone();
310          }
311        }
312      };
313      return bestChild;
314    }
315  }
316}
Note: See TracBrowser for help on using the repository browser.