Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/13 11:14:56 (11 years ago)
Author:
ascheibe
Message:

#1886 added a caching analyzer for crossover

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationSolutionDictionary.cs

    r9730 r9754  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3031  [Item("PermutationSolutionDictionary", "Stores permutations generated by an algorithm.")]
    3132  [StorableClass]
    32   public class PermutationSolutionDictionary<TKey, TValue> : SolutionDictionary<TKey, TValue>
    33     where TKey : PermutationWrapper
    34     where TValue : PermutationInformation {
     33  public class PermutationSolutionDictionary : SolutionDictionary<PermutationWrapper, PermutationInformation> {
     34    [Storable]
     35    protected Dictionary<PermutationWrapper, List<PermutationInformation>> solutionDictionary;
     36    public Dictionary<PermutationWrapper, List<PermutationInformation>> SolutionDictionary { get { return solutionDictionary; } }
    3537
    3638    public PermutationSolutionDictionary() {
    37       solutionDictionary = new Dictionary<TKey, List<TValue>>(new PermutationWrapperEqualityComparer());
     39      solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(new PermutationWrapperEqualityComparer());
    3840    }
    3941
    4042    [StorableConstructor]
    4143    protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { }
    42     protected PermutationSolutionDictionary(PermutationSolutionDictionary<TKey, TValue> original, Cloner cloner)
     44    protected PermutationSolutionDictionary(PermutationSolutionDictionary original, Cloner cloner)
    4345      : base(original, cloner) {
     46      this.solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(original.solutionDictionary, new PermutationWrapperEqualityComparer());
    4447    }
    4548
    46     protected override void InsertKeyValue(TKey key, TValue value) {
     49    public override int Size() {
     50      return solutionDictionary.Count;
     51    }
     52
     53    public override void Add(PermutationWrapper key, PermutationInformation value) {
     54      if (solutionDictionary.Count == 0) {
     55        var lst = new List<PermutationInformation>();
     56        lst.Add(value);
     57        solutionDictionary.Add(key, lst);
     58        return;
     59      }
     60
     61      if (ContainsKey(key)) {
     62        if (!solutionDictionary[key].Contains(value))
     63          solutionDictionary[key].Add(value);
     64      } else {
     65        InsertKeyValue(key, value);
     66      }
     67    }
     68
     69    protected override void InsertKeyValue(PermutationWrapper key, PermutationInformation value) {
    4770      int diffCnt;
    4871      Permutation p = key.GetPermutation();
     
    5376      }
    5477
    55       var lst = new List<TValue>();
     78      var lst = new List<PermutationInformation>();
    5679      lst.Add(value);
    5780      solutionDictionary.Add(key, lst);
     
    81104    }
    82105
     106    public PermutationWrapper GetPermutationWrapperOfPermutation(Permutation permutation) {
     107      PermutationEqualityComparer peq = new PermutationEqualityComparer();
     108      var res = solutionDictionary.Where(x => peq.GetHashCode(x.Key.GetPermutation()) == peq.GetHashCode(permutation));
     109
     110      //TODO: fix this
     111      if (res.Count() > 1) {
     112        throw new Exception("ERROR: Cannot conatin duplicates!");
     113      }
     114
     115      return res.First().Key;
     116    }
     117
    83118    public override IDeepCloneable Clone(Cloner cloner) {
    84       return new PermutationSolutionDictionary<TKey, TValue>(this, cloner);
     119      return new PermutationSolutionDictionary(this, cloner);
    85120    }
    86121
     
    96131      return solutionDictionary.Where(x => x.Value.Where(y => y.Generation == generation).Count() != 0).Select(x => x.Key).ToList<PermutationWrapper>();
    97132    }
     133
     134    public override bool ContainsKey(PermutationWrapper key) {
     135      return solutionDictionary.ContainsKey(key);
     136    }
    98137  }
    99138}
Note: See TracChangeset for help on using the changeset viewer.