Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 fixed alglib reference and updated year and version information

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 : SolutionDictionary<PermutationWrapper, PermutationInformation> {
33    [Storable]
34    protected Dictionary<PermutationWrapper, List<PermutationInformation>> solutionDictionary;
35
36    public Dictionary<PermutationWrapper, List<PermutationInformation>> SolutionDictionary {
37      get { return solutionDictionary; }
38    }
39
40    [Storable]
41    PermutationEqualityComparer peq;
42
43    public PermutationSolutionDictionary() {
44      solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(new PermutationWrapperEqualityComparer());
45      peq = new PermutationEqualityComparer();
46    }
47
48    [StorableConstructor]
49    protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { }
50    protected PermutationSolutionDictionary(PermutationSolutionDictionary original, Cloner cloner)
51      : base(original, cloner) {
52      this.solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(original.solutionDictionary, new PermutationWrapperEqualityComparer());
53      this.peq = new PermutationEqualityComparer();
54    }
55
56    public override int Size() {
57      return solutionDictionary.Count;
58    }
59
60    public override void Add(PermutationWrapper key, PermutationInformation value) {
61      if (solutionDictionary.Count == 0) {
62        var lst = new List<PermutationInformation>();
63        lst.Add(value);
64        solutionDictionary.Add(key, lst);
65        return;
66      }
67
68      if (ContainsKey(key)) {
69        if (!solutionDictionary[key].Contains(value))
70          solutionDictionary[key].Add(value);
71      } else {
72        InsertKeyValue(key, value);
73      }
74    }
75
76    protected override void InsertKeyValue(PermutationWrapper key, PermutationInformation value) {
77      int diffCnt;
78      Permutation p = key.GetPermutation();
79      Permutation ms = GetMostSimilarSolution(p, out diffCnt);
80
81      if (diffCnt < ms.Length / 2) {
82        key.StorePartialPermutation(ms, p);
83      }
84
85      var lst = new List<PermutationInformation>();
86      lst.Add(value);
87      solutionDictionary.Add(key, lst);
88    }
89
90    private Permutation GetMostSimilarSolution(Permutation permutation, out int diffCnt) {
91      Permutation result = null; int min = int.MaxValue;
92      foreach (PermutationWrapper p in solutionDictionary.Keys.Where(x => x.ElementType == ElementType.Complete)) {
93        Permutation pr = p.GetPermutation();
94        int diffs = CountDiffs(permutation, pr);
95        if (diffs < min) {
96          result = pr;
97          min = diffs;
98        }
99      }
100      diffCnt = min;
101      return result;
102    }
103
104    private int CountDiffs(Permutation x, Permutation y) {
105      int result = 0;
106      // yes we ignore path encoding at the moment
107      for (int i = 0; i < x.Length; i++) {
108        if (x[i] != y[i]) result++;
109      }
110      return result;
111    }
112
113    public PermutationWrapper GetPermutationWrapperOfPermutation(Permutation permutation) {
114      return solutionDictionary.Single(x => peq.GetHashCode(x.Key.GetPermutation()) == peq.GetHashCode(permutation)).Key;
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new PermutationSolutionDictionary(this, cloner);
119    }
120
121    public int NumberOfPartialSolutions() {
122      return solutionDictionary.Where(x => x.Key.ElementType == ElementType.Diff).Count();
123    }
124
125    public int NumberOfCompleteSolutions() {
126      return solutionDictionary.Where(x => x.Key.ElementType == ElementType.Complete).Count();
127    }
128
129    public List<PermutationWrapper> GetSolutionsFromGeneration(int generation) {
130      return solutionDictionary.Where(x => x.Value.Where(y => y.Generation == generation).Count() != 0).Select(x => x.Key).ToList<PermutationWrapper>();
131    }
132
133    public override bool ContainsKey(PermutationWrapper key) {
134      return solutionDictionary.ContainsKey(key);
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.