#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("RealVectorFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for real vector encoding")] [StorableType("84EA2C55-F847-4CF9-B966-0E7B9DD8903C")] public class RealVectorFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IRealVectorOperator { #region Parameters public ScopeTreeLookupParameter RealVectorParameter { get { return (ScopeTreeLookupParameter)Parameters["RealVector"]; } } public LookupParameter BestKnownSolution { get { return (LookupParameter)Parameters["BestKnownSolution"]; } } #endregion [StorableConstructor] protected RealVectorFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { } protected RealVectorFitnessDistanceCorrelationAnalyzer(RealVectorFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { } public RealVectorFitnessDistanceCorrelationAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("RealVector", "The real encoded solution")); Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution")); RealVectorParameter.ActualName = "Point"; } public override IDeepCloneable Clone(Cloner cloner) { return new RealVectorFitnessDistanceCorrelationAnalyzer(this, cloner); } public static double Distance(RealVector a, RealVector b) { if (a.Length != b.Length) throw new InvalidOperationException("Cannot compare vectors of different lengths"); double sum = 0; for (int i = 0; i < a.Length; i++) { sum += (a[i] - b[i]) * (a[i] - b[i]); } return Math.Sqrt(sum); } protected override IEnumerable GetDistancesToBestKnownSolution() { if (this.RealVectorParameter.ActualValue == null) return new double[0]; RealVector bestKnownValue = BestKnownSolution.ActualValue; if (bestKnownValue == null) return RealVectorParameter.ActualValue.Select(v => 0d); return RealVectorParameter.ActualValue.Select(v => Distance(v, bestKnownValue)); } } }