1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
28 | [Item("PermutationUnwantedMutationAnalyzer", "An item that calculates unwanted mutations for the permutation encoding.")]
|
---|
29 | [StorableClass]
|
---|
30 | public class PermutationUnwantedMutationAnalyzer : UnwantedMutationAnalyzer {
|
---|
31 | [StorableConstructor]
|
---|
32 | protected PermutationUnwantedMutationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
33 | protected PermutationUnwantedMutationAnalyzer(PermutationUnwantedMutationAnalyzer original, Cloner cloner)
|
---|
34 | : base(original, cloner) { }
|
---|
35 |
|
---|
36 | public PermutationUnwantedMutationAnalyzer()
|
---|
37 | : base() { }
|
---|
38 |
|
---|
39 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
40 | return new PermutationUnwantedMutationAnalyzer(this, cloner);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override double AnalyzeUnwantedMutations(IScope parent1, IScope parent2, IScope child) {
|
---|
44 | Permutation p1 = parent1.Variables[SolutionVariableName].Value as Permutation;
|
---|
45 | Permutation p2 = parent2.Variables[SolutionVariableName].Value as Permutation;
|
---|
46 | Permutation c = child.Variables[SolutionVariableName].Value as Permutation;
|
---|
47 |
|
---|
48 | return AnalyzeUnwantedMutations(p1, p2, c);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private double AnalyzeUnwantedMutations(Permutation parent1, Permutation parent2, Permutation child) {
|
---|
52 | int cnt = 0;
|
---|
53 | int[] edgesP1 = CalculateEdgesVector(parent1);
|
---|
54 | int[] edgesP2 = CalculateEdgesVector(parent2);
|
---|
55 | int[] edgesC = CalculateEdgesVector(child);
|
---|
56 |
|
---|
57 | for (int i = 0; i < edgesP1.Length; i++) {
|
---|
58 | if (edgesC[i] != edgesP1[i] &&
|
---|
59 | edgesC[i] != edgesP2[i] &&
|
---|
60 | edgesP1[edgesC[i]] != i &&
|
---|
61 | edgesP2[edgesC[i]] != i) {
|
---|
62 | cnt += 1;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | //reverse so that it matches the other diversity charts
|
---|
67 | return 1.0 - (((double)cnt) / parent1.Length);
|
---|
68 | }
|
---|
69 |
|
---|
70 | //copied from PermutationEqualityComparer
|
---|
71 | private int[] CalculateEdgesVector(Permutation permutation) {
|
---|
72 | // transform path representation into adjacency representation
|
---|
73 | int[] edgesVector = new int[permutation.Length];
|
---|
74 | for (int i = 0; i < permutation.Length - 1; i++)
|
---|
75 | edgesVector[permutation[i]] = permutation[i + 1];
|
---|
76 | edgesVector[permutation[permutation.Length - 1]] = permutation[0];
|
---|
77 | return edgesVector;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|