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 HeuristicLab.Common; |
---|
24 | using HeuristicLab.Core; |
---|
25 | using HeuristicLab.Encodings.PermutationEncoding; |
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; |
---|
27 | |
---|
28 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { |
---|
29 | public enum ElementType { Complete, Diff }; |
---|
30 | |
---|
31 | [Item("PermutationWrapper", "Wrapper for permutations. Can either store a permutation or a diff.")] |
---|
32 | [StorableClass] |
---|
33 | public class PermutationWrapper : Item { |
---|
34 | [Storable] |
---|
35 | public ElementType ElementType { get; set; } |
---|
36 | [Storable] |
---|
37 | public Permutation Permutation { get; set; } |
---|
38 | [Storable] |
---|
39 | public List<int> Diffs { get; set; } |
---|
40 | |
---|
41 | public PermutationWrapper(Permutation p) { |
---|
42 | ElementType = ElementType.Complete; |
---|
43 | Diffs = new List<int>(); |
---|
44 | Permutation = p; |
---|
45 | } |
---|
46 | |
---|
47 | public PermutationWrapper() { |
---|
48 | ElementType = ElementType.Complete; |
---|
49 | Diffs = new List<int>(); |
---|
50 | } |
---|
51 | |
---|
52 | [StorableConstructor] |
---|
53 | protected PermutationWrapper(bool deserializing) : base(deserializing) { } |
---|
54 | protected PermutationWrapper(PermutationWrapper original, Cloner cloner) |
---|
55 | : base(original, cloner) { |
---|
56 | this.ElementType = original.ElementType; |
---|
57 | this.Diffs = new List<int>(original.Diffs); |
---|
58 | this.Permutation = (Permutation)original.Permutation.Clone(cloner); |
---|
59 | } |
---|
60 | |
---|
61 | public Permutation GetPermutation() { |
---|
62 | if (ElementType == ElementType.Complete) { |
---|
63 | return Permutation; |
---|
64 | } else { |
---|
65 | Permutation result = (Permutation)Permutation.Clone(new Cloner()); |
---|
66 | for (int i = 0; i < Diffs.Count; i++) { |
---|
67 | int index = Diffs[i] >> 16; |
---|
68 | int value = Diffs[i] & 0x0000FFFF; |
---|
69 | result[index] = value; |
---|
70 | } |
---|
71 | return result; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | //supports efficient diffs up to a permutation length of 30000 |
---|
76 | public void StorePartialPermutation(Permutation original, Permutation newPermutation) { |
---|
77 | ElementType = ElementType.Diff; |
---|
78 | Diffs = new List<int>(); |
---|
79 | Permutation = original; |
---|
80 | |
---|
81 | for (int i = 0; i < original.Length; i++) { |
---|
82 | if (original[i] != newPermutation[i]) { |
---|
83 | int diff = (i << 16) | newPermutation[i]; |
---|
84 | Diffs.Add(diff); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | public void StoreCompletePermutation(Permutation original) { |
---|
90 | ElementType = ElementType.Complete; |
---|
91 | Diffs = new List<int>(); |
---|
92 | Permutation = original; |
---|
93 | } |
---|
94 | |
---|
95 | public override IDeepCloneable Clone(Cloner cloner) { |
---|
96 | return new PermutationWrapper(this, cloner); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|