1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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 


22  using System.Collections.Generic;


23  using System.Linq;


24  using HeuristicLab.Common;


25  using HeuristicLab.Core;


26  using HeuristicLab.Encodings.PermutationEncoding;


27  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


28 


29  namespace 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  where TValue : PermutationInformation {


35 


36  public PermutationSolutionDictionary() {


37  solutionDictionary = new Dictionary<TKey, List<TValue>>(new PermutationWrapperEqualityComparer());


38  }


39 


40  [StorableConstructor]


41  protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { }


42  protected PermutationSolutionDictionary(PermutationSolutionDictionary<TKey, TValue> original, Cloner cloner)


43  : base(original, cloner) {


44  }


45 


46  protected override void InsertKeyValue(TKey key, TValue value) {


47  int diffCnt;


48  Permutation p = key.GetPermutation();


49  Permutation ms = GetMostSimilarSolution(p, out diffCnt);


50 


51  if (diffCnt < ms.Length / 2) {


52  key.StorePartialPermutation(ms, p);


53  }


54 


55  var lst = new List<TValue>();


56  lst.Add(value);


57  solutionDictionary.Add(key, lst);


58  }


59 


60  private Permutation GetMostSimilarSolution(Permutation permutation, out int diffCnt) {


61  Permutation result = null; int min = int.MaxValue;


62  foreach (PermutationWrapper p in solutionDictionary.Keys.Where(x => x.ElementType == ElementType.Complete)) {


63  Permutation pr = p.GetPermutation();


64  int diffs = CountDiffs(permutation, pr);


65  if (diffs < min) {


66  result = pr;


67  min = diffs;


68  }


69  }


70  diffCnt = min;


71  return result;


72  }


73 


74  private int CountDiffs(Permutation x, Permutation y) {


75  int result = 0;


76  // yes we ignore path encoding at the moment


77  for (int i = 0; i < x.Length; i++) {


78  if (x[i] != y[i]) result++;


79  }


80  return result;


81  }


82 


83  public override IDeepCloneable Clone(Cloner cloner) {


84  return new PermutationSolutionDictionary<TKey, TValue>(this, cloner);


85  }


86 


87  public int NumberOfPartialSolutions() {


88  return solutionDictionary.Where(x => x.Key.ElementType == ElementType.Diff).Count();


89  }


90 


91  public int NumberOfCompleteSolutions() {


92  return solutionDictionary.Where(x => x.Key.ElementType == ElementType.Complete).Count();


93  }


94  }


95  }

