Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationSolutionDictionary.cs @ 9723

Last change on this file since 9723 was 9723, checked in by ascheibe, 11 years ago

#1886 worked on solution caching

File size: 3.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
30  [Item("PermutationSolutionDictionary", "Stores permutations generated by an algorithm.")]
31  [StorableClass]
32  public class PermutationSolutionDictionary<TKey, TValue> : SolutionDictionary<TKey, TValue>
33    where TKey : PermutationWrapper {
34
35    public PermutationSolutionDictionary() {
36      solutionDictionary = new Dictionary<TKey, List<TValue>>(new PermutationWrapperEqualityComparer());
37    }
38
39    [StorableConstructor]
40    protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { }
41    protected PermutationSolutionDictionary(PermutationSolutionDictionary<TKey, TValue> original, Cloner cloner)
42      : base(original, cloner) {
43    }
44
45    protected override void InsertKeyValue(TKey key, TValue value) {
46      int diffCnt;
47      Permutation p = key.GetPermutation();
48      Permutation ms = GetMostSimilarSolution(p, out diffCnt);
49
50      if (diffCnt < ms.Length / 2) {
51        key.StorePartialPermutation(ms, p);
52      }
53
54      var lst = new List<TValue>();
55      lst.Add(value);
56      solutionDictionary.Add(key, lst);
57    }
58
59    private Permutation GetMostSimilarSolution(Permutation permutation, out int diffCnt) {
60      Permutation result = null; int min = int.MaxValue;
61      foreach (PermutationWrapper p in solutionDictionary.Keys.Where(x => x.ElementType == ElementType.Complete)) {
62        Permutation pr = p.GetPermutation();
63        int diffs = CountDiffs(permutation, pr);
64        if (diffs < min) {
65          result = pr;
66          min = diffs;
67        }
68      }
69      diffCnt = min;
70      return result;
71    }
72
73    private int CountDiffs(Permutation x, Permutation y) {
74      int result = 0;
75      // yes we ignore path encoding at the moment
76      for (int i = 0; i < x.Length; i++) {
77        if (x[i] != y[i]) result++;
78      }
79      return result;
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new PermutationSolutionDictionary<TKey, TValue>(this, cloner);
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.