[9711] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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;
|
---|
[9723] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[9711] | 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[9723] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[9711] | 28 |
|
---|
| 29 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
[9723] | 30 | [Item("PermutationSolutionDictionary", "Stores permutations generated by an algorithm.")]
|
---|
| 31 | [StorableClass]
|
---|
[9711] | 32 | public class PermutationSolutionDictionary<TKey, TValue> : SolutionDictionary<TKey, TValue>
|
---|
[9728] | 33 | where TKey : PermutationWrapper
|
---|
| 34 | where TValue : PermutationInformation {
|
---|
[9711] | 35 |
|
---|
| 36 | public PermutationSolutionDictionary() {
|
---|
[9723] | 37 | solutionDictionary = new Dictionary<TKey, List<TValue>>(new PermutationWrapperEqualityComparer());
|
---|
[9711] | 38 | }
|
---|
| 39 |
|
---|
[9723] | 40 | [StorableConstructor]
|
---|
| 41 | protected PermutationSolutionDictionary(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected PermutationSolutionDictionary(PermutationSolutionDictionary<TKey, TValue> original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[9711] | 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);
|
---|
[9723] | 65 | if (diffs < min) {
|
---|
[9711] | 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;
|
---|
[9723] | 76 | // yes we ignore path encoding at the moment
|
---|
[9711] | 77 | for (int i = 0; i < x.Length; i++) {
|
---|
| 78 | if (x[i] != y[i]) result++;
|
---|
| 79 | }
|
---|
| 80 | return result;
|
---|
| 81 | }
|
---|
[9723] | 82 |
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new PermutationSolutionDictionary<TKey, TValue>(this, cloner);
|
---|
| 85 | }
|
---|
[9728] | 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 | }
|
---|
[9711] | 94 | }
|
---|
| 95 | }
|
---|