Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/13 16:28:49 (11 years ago)
Author:
ascheibe
Message:

#1886 fixed a bug and improved PermutationInformation

Location:
branches/HeuristicLab.Analysis.AlgorithmBehavior
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/UnitTest2.cs

    r9754 r9757  
    3535            break;
    3636          }
    37 
    3837        }
    3938      }
     
    4544            break;
    4645          }
    47 
    4846        }
    4947      }
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/AfterCrossoverSolutionCachingAnalyzer.cs

    r9754 r9757  
    9696      info.ParentList.Add(p1Pw);
    9797      info.ParentList.Add(p2Pw);
    98       solDict.Add(new PermutationWrapper(solution), info);
     98      solDict.Add(new PermutationWrapper((Permutation)solution.Clone(new Cloner())), info);
    9999
    100100      return base.Apply();
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationInformation.cs

    r9728 r9757  
    3737    [Storable]
    3838    public int Generation { get; set; }
     39    [Storable]
     40    PermutationWrapperEqualityComparer pweq;
    3941
    40     public PermutationInformation() { }
     42    public PermutationInformation() {
     43      pweq = new PermutationWrapperEqualityComparer();
     44    }
    4145
    4246    [StorableConstructor]
     
    4751      this.ParentList = new List<PermutationWrapper>(original.ParentList);
    4852      this.Generation = original.Generation;
     53      this.pweq = new PermutationWrapperEqualityComparer();
    4954    }
    5055
     
    5257      return new PermutationInformation(this, cloner);
    5358    }
     59
     60    public override int GetHashCode() {
     61      //use rotating hash, should be enough
     62      //see: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
     63      int h = 0;
     64
     65      h = (h << 4) ^ (h >> 28) ^ ProducedBy.GetHashCode();
     66      h = (h << 4) ^ (h >> 28) ^ Generation.GetHashCode();
     67
     68      if (ParentList != null) {
     69        foreach (var parent in ParentList) {
     70          h = (h << 4) ^ (h >> 28) ^ pweq.GetHashCode(parent);
     71        }
     72      }
     73
     74      return h;
     75    }
     76
     77    public override bool Equals(object obj) {
     78      PermutationInformation pi = obj as PermutationInformation;
     79
     80      if (ProducedBy == pi.ProducedBy && Generation == pi.Generation) {
     81        if (pi.ParentList == null && ParentList == null) return true;
     82        if (pi.ParentList != null && ParentList == null ||
     83          pi.ParentList == null && ParentList != null) return false;
     84        if (pi.ParentList.Count != ParentList.Count) return false;
     85
     86        foreach (var p in ParentList) {
     87          if (!pi.ParentList.Contains(p)) return false;
     88        }
     89        return true;
     90      } else {
     91        return false;
     92      }
     93    }
    5494  }
    5595}
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationSolutionDictionary.cs

    r9754 r9757  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    3433    [Storable]
    3534    protected Dictionary<PermutationWrapper, List<PermutationInformation>> solutionDictionary;
    36     public Dictionary<PermutationWrapper, List<PermutationInformation>> SolutionDictionary { get { return solutionDictionary; } }
     35
     36    public Dictionary<PermutationWrapper, List<PermutationInformation>> SolutionDictionary {
     37      get { return solutionDictionary; }
     38    }
     39
     40    [Storable]
     41    PermutationEqualityComparer peq;
    3742
    3843    public PermutationSolutionDictionary() {
    3944      solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(new PermutationWrapperEqualityComparer());
     45      peq = new PermutationEqualityComparer();
    4046    }
    4147
     
    4551      : base(original, cloner) {
    4652      this.solutionDictionary = new Dictionary<PermutationWrapper, List<PermutationInformation>>(original.solutionDictionary, new PermutationWrapperEqualityComparer());
     53      this.peq = new PermutationEqualityComparer();
    4754    }
    4855
     
    105112
    106113    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;
     114      return solutionDictionary.Single(x => peq.GetHashCode(x.Key.GetPermutation()) == peq.GetHashCode(permutation)).Key;
    116115    }
    117116
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/SolutionCachingAnalyzer.cs

    r9754 r9757  
    9393        info.ProducedBy = ProducedBy.Mutation;
    9494       
    95         solDict.Add(new PermutationWrapper(sol), info);
     95        solDict.Add(new PermutationWrapper((Permutation)sol.Clone(new Cloner())), info);
    9696      }
    9797      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.