1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
33 |
|
---|
34 | [Item("PermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")]
|
---|
35 | [StorableType("C5CBBD3F-B8FE-41B8-8670-7455D6B15EF1")]
|
---|
36 | public class PermutationFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IPermutationOperator {
|
---|
37 |
|
---|
38 | #region Parameters
|
---|
39 | public ScopeTreeLookupParameter<Permutation> PermutationParameter {
|
---|
40 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
41 | }
|
---|
42 | public LookupParameter<Permutation> BestKnownSolution {
|
---|
43 | get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
44 | }
|
---|
45 | #endregion
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected PermutationFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
49 | protected PermutationFitnessDistanceCorrelationAnalyzer(PermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
50 |
|
---|
51 | public PermutationFitnessDistanceCorrelationAnalyzer() {
|
---|
52 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The permutation encoded solution"));
|
---|
53 | Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution"));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new PermutationFitnessDistanceCorrelationAnalyzer(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static Point GetEdge(Permutation a, int index) {
|
---|
61 | switch (a.PermutationType) {
|
---|
62 | case PermutationTypes.RelativeDirected:
|
---|
63 | return new Point(a[index], a[(index + 1) % a.Length]);
|
---|
64 | case PermutationTypes.RelativeUndirected:
|
---|
65 | return new Point(
|
---|
66 | Math.Min(a[index], a[(index + 1) % a.Length]),
|
---|
67 | Math.Max(a[index], a[(index + 1) % a.Length]));
|
---|
68 | default:
|
---|
69 | throw new ArgumentException("cannot derive edge from non-relative permutation type");
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static double Distance(Permutation a, Permutation b) {
|
---|
74 | if (a.PermutationType != b.PermutationType)
|
---|
75 | throw new ArgumentException(
|
---|
76 | "Cannot calculate distance between different permuatation types: " +
|
---|
77 | a.PermutationType + " and " + b.PermutationType);
|
---|
78 | if (a.PermutationType == PermutationTypes.Absolute) {
|
---|
79 | int nEqual = 0;
|
---|
80 | for (int i = 0; i < Math.Min(a.Length, b.Length); i++) {
|
---|
81 | if (a[i] == b[i])
|
---|
82 | nEqual++;
|
---|
83 | }
|
---|
84 | return Math.Max(a.Length, b.Length) - nEqual;
|
---|
85 | } else {
|
---|
86 | HashSet<Point> edges = new HashSet<Point>();
|
---|
87 | for (int i = 0; i < a.Length; i++)
|
---|
88 | edges.Add(GetEdge(a, i));
|
---|
89 | int nCommonEdges = 0;
|
---|
90 | for (int i = 0; i < b.Length; i++) {
|
---|
91 | if (edges.Contains(GetEdge(b, i)))
|
---|
92 | nCommonEdges++;
|
---|
93 | }
|
---|
94 | return Math.Max(a.Length, b.Length) - nCommonEdges;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
|
---|
99 | if (PermutationParameter.ActualName == null)
|
---|
100 | return new double[0];
|
---|
101 | Permutation bestKnownValue = BestKnownSolution.ActualValue;
|
---|
102 | if (bestKnownValue == null)
|
---|
103 | return PermutationParameter.ActualValue.Select(v => 0d);
|
---|
104 | return PermutationParameter.ActualValue.Select(v => Distance(v, bestKnownValue));
|
---|
105 | }
|
---|
106 | }
|
---|
107 | } |
---|