#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.BinaryVectorEncoding; using HeuristicLab.Parameters; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("BinaryVectorFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for binary vector encoding")] [StorableType("BE5474B1-9D8E-4AA0-9236-85C192712A96")] public class BinaryVectorFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IBinaryVectorOperator { #region Parameters public ScopeTreeLookupParameter BinaryVectorParameter { get { return (ScopeTreeLookupParameter)Parameters["BinaryVector"]; } } public LookupParameter BestKnownSolution { get { return (LookupParameter)Parameters["BestKnownSolution"]; } } #endregion [StorableConstructor] protected BinaryVectorFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { } protected BinaryVectorFitnessDistanceCorrelationAnalyzer(BinaryVectorFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { } public BinaryVectorFitnessDistanceCorrelationAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("BinaryVector", "The real encoded solution")); Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution")); } public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorFitnessDistanceCorrelationAnalyzer(this, cloner); } public static double Distance(BinaryVector a, BinaryVector b) { if (a.Length != b.Length) throw new InvalidOperationException("Cannot compare vectors of different lengths"); double nEqualBits = 0; for (int i = 0; i < a.Length; i++) { if (a[i] == b[i]) nEqualBits++; } return Math.Max(a.Length, b.Length) - nEqualBits; } protected override IEnumerable GetDistancesToBestKnownSolution() { if (BinaryVectorParameter.ActualValue == null) return new double[0]; BinaryVector bestKnownValue = BestKnownSolution.ActualValue; if (bestKnownValue == null) return BinaryVectorParameter.ActualValue.Select(v => 0d); return BinaryVectorParameter.ActualValue.Select(v => Distance(v, bestKnownValue)); } } }