Changeset 9754 for branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationSolutionDictionary.cs
- Timestamp:
- 07/25/13 11:14:56 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationSolutionDictionary.cs
r9730 r9754 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 30 31 [Item("PermutationSolutionDictionary", "Stores permutations generated by an algorithm.")] 31 32 [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; } } 35 37 36 38 public PermutationSolutionDictionary() { 37 solutionDictionary = new Dictionary< TKey, List<TValue>>(new PermutationWrapperEqualityComparer());39 solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(new PermutationWrapperEqualityComparer()); 38 40 } 39 41 40 42 [StorableConstructor] 41 43 protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { } 42 protected PermutationSolutionDictionary(PermutationSolutionDictionary <TKey, TValue>original, Cloner cloner)44 protected PermutationSolutionDictionary(PermutationSolutionDictionary original, Cloner cloner) 43 45 : base(original, cloner) { 46 this.solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(original.solutionDictionary, new PermutationWrapperEqualityComparer()); 44 47 } 45 48 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) { 47 70 int diffCnt; 48 71 Permutation p = key.GetPermutation(); … … 53 76 } 54 77 55 var lst = new List< TValue>();78 var lst = new List<PermutationInformation>(); 56 79 lst.Add(value); 57 80 solutionDictionary.Add(key, lst); … … 81 104 } 82 105 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 83 118 public override IDeepCloneable Clone(Cloner cloner) { 84 return new PermutationSolutionDictionary <TKey, TValue>(this, cloner);119 return new PermutationSolutionDictionary(this, cloner); 85 120 } 86 121 … … 96 131 return solutionDictionary.Where(x => x.Value.Where(y => y.Generation == generation).Count() != 0).Select(x => x.Key).ToList<PermutationWrapper>(); 97 132 } 133 134 public override bool ContainsKey(PermutationWrapper key) { 135 return solutionDictionary.ContainsKey(key); 136 } 98 137 } 99 138 }
Note: See TracChangeset
for help on using the changeset viewer.