Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/RealVectorFitnessDistanceCorrelationAnalyzer.cs @ 16573

Last change on this file since 16573 was 16573, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

File size: 3.4 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;
30using HEAL.Attic;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33
34  [Item("RealVectorFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for real vector encoding")]
35  [StorableType("84EA2C55-F847-4CF9-B966-0E7B9DD8903C")]
36  public class RealVectorFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IRealVectorOperator {
37
38    #region Parameters
39    public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
40      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
41    }
42    public LookupParameter<RealVector> BestKnownSolution {
43      get { return (LookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
44    }
45    #endregion
46
47    [StorableConstructor]
48    protected RealVectorFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
49    protected RealVectorFitnessDistanceCorrelationAnalyzer(RealVectorFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
50
51    public RealVectorFitnessDistanceCorrelationAnalyzer() {
52      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The real encoded solution"));
53      Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution"));
54      RealVectorParameter.ActualName = "Point";
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new RealVectorFitnessDistanceCorrelationAnalyzer(this, cloner);
59    }
60
61    public static double Distance(RealVector a, RealVector b) {
62      if (a.Length != b.Length)
63        throw new InvalidOperationException("Cannot compare vectors of different lengths");
64      double sum = 0;
65      for (int i = 0; i < a.Length; i++) {
66        sum += (a[i] - b[i]) * (a[i] - b[i]);
67      }
68      return Math.Sqrt(sum);
69    }
70
71    protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
72      if (this.RealVectorParameter.ActualValue == null)
73        return new double[0];
74      RealVector bestKnownValue = BestKnownSolution.ActualValue;
75      if (bestKnownValue == null)
76        return RealVectorParameter.ActualValue.Select(v => 0d);
77      return RealVectorParameter.ActualValue.Select(v => Distance(v, bestKnownValue));
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.