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.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
32 |
|
---|
33 | [Item("RealVectorFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for real vector encoding")]
|
---|
34 | [StorableClass]
|
---|
35 | public class RealVectorFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IRealVectorOperator {
|
---|
36 |
|
---|
37 | #region Parameters
|
---|
38 | public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
|
---|
39 | get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
40 | }
|
---|
41 | public LookupParameter<RealVector> BestKnownSolution {
|
---|
42 | get { return (LookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected RealVectorFitnessDistanceCorrelationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
48 | protected RealVectorFitnessDistanceCorrelationAnalyzer(RealVectorFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
49 |
|
---|
50 | public RealVectorFitnessDistanceCorrelationAnalyzer() {
|
---|
51 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The real encoded solution"));
|
---|
52 | Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution"));
|
---|
53 | RealVectorParameter.ActualName = "Point";
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new RealVectorFitnessDistanceCorrelationAnalyzer(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static double Distance(RealVector a, RealVector b) {
|
---|
61 | if (a.Length != b.Length)
|
---|
62 | throw new InvalidOperationException("Cannot compare vectors of different lengths");
|
---|
63 | double sum = 0;
|
---|
64 | for (int i = 0; i < a.Length; i++) {
|
---|
65 | sum += (a[i] - b[i]) * (a[i] - b[i]);
|
---|
66 | }
|
---|
67 | return Math.Sqrt(sum);
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
|
---|
71 | if (this.RealVectorParameter.ActualValue == null)
|
---|
72 | return new double[0];
|
---|
73 | RealVector bestKnownValue = BestKnownSolution.ActualValue;
|
---|
74 | if (bestKnownValue == null)
|
---|
75 | return RealVectorParameter.ActualValue.Select(v => 0d);
|
---|
76 | return RealVectorParameter.ActualValue.Select(v => Distance(v, bestKnownValue));
|
---|
77 | }
|
---|
78 | }
|
---|
79 | } |
---|