Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/BinaryVectorFitnessDistanceCorrelationAnalyzer.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.BinaryVectorEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HEAL.Attic;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33
34  [Item("BinaryVectorFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for binary vector encoding")]
35  [StorableType("BE5474B1-9D8E-4AA0-9236-85C192712A96")]
36  public class BinaryVectorFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IBinaryVectorOperator {
37
38    #region Parameters
39    public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
40      get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
41    }
42    public LookupParameter<BinaryVector> BestKnownSolution {
43      get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
44    }
45    #endregion
46
47    [StorableConstructor]
48    protected BinaryVectorFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
49    protected BinaryVectorFitnessDistanceCorrelationAnalyzer(BinaryVectorFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
50
51    public BinaryVectorFitnessDistanceCorrelationAnalyzer() {
52      Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The real encoded solution"));
53      Parameters.Add(new LookupParameter<BinaryVector>("BestKnownSolution", "The best known solution"));
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new BinaryVectorFitnessDistanceCorrelationAnalyzer(this, cloner);
58    }
59
60    public static double Distance(BinaryVector a, BinaryVector b) {
61      if (a.Length != b.Length)
62        throw new InvalidOperationException("Cannot compare vectors of different lengths");
63      double nEqualBits = 0;
64      for (int i = 0; i < a.Length; i++) {
65        if (a[i] == b[i])
66          nEqualBits++;
67      }
68      return Math.Max(a.Length, b.Length) - nEqualBits;
69    }
70
71
72    protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
73      if (BinaryVectorParameter.ActualValue == null)
74        return new double[0];
75      BinaryVector bestKnownValue = BestKnownSolution.ActualValue;
76      if (bestKnownValue == null)
77        return BinaryVectorParameter.ActualValue.Select(v => 0d);
78      return BinaryVectorParameter.ActualValue.Select(v => Distance(v, bestKnownValue));
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.