Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/BinaryVectorFitnessDistanceCorrelationAnalyzer.cs @ 17282

Last change on this file since 17282 was 16995, checked in by gkronber, 6 years ago

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