Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/RealVectorFitnessDistanceCorrelationAnalyzer.cs @ 12214

Last change on this file since 12214 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 3.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace 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}
Note: See TracBrowser for help on using the repository browser.