#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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("QAPPermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")] [StorableClass] public class QAPPermutationFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IPermutationOperator { #region Parameters public ScopeTreeLookupParameter PermutationParameter { get { return (ScopeTreeLookupParameter)Parameters["Permutation"]; } } public LookupParameter BestKnownSolution { get { return (LookupParameter)Parameters["BestKnownSolution"]; } } public ILookupParameter WeightsParameter { get { return (ILookupParameter)Parameters["Weights"]; } } public ILookupParameter DistancesParameter { get { return (ILookupParameter)Parameters["Distances"]; } } #endregion [StorableConstructor] protected QAPPermutationFitnessDistanceCorrelationAnalyzer(bool deserializing) : base(deserializing) { } protected QAPPermutationFitnessDistanceCorrelationAnalyzer(QAPPermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { } public QAPPermutationFitnessDistanceCorrelationAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("Permutation", "The permutation encoded solution")); Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution")); Parameters.Add(new LookupParameter("Weights", "The weights matrix.")); Parameters.Add(new LookupParameter("Distances", "The distances matrix.")); } public override IDeepCloneable Clone(Cloner cloner) { return new QAPPermutationFitnessDistanceCorrelationAnalyzer(this, cloner); } public static double Distance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) { Dictionary alleles = new Dictionary(a.Length * a.Length); int distance = 0; for (int x = 0; x < a.Length; x++) { for (int y = 0; y < a.Length; y++) { string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString(); string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString(); if (alleleA == alleleB) continue; int countA = 1, countB = -1; if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA]; if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB]; if (countA <= 0) distance--; // we've found in A an allele that was present in B else distance++; // we've found in A a new allele alleles[alleleA] = countA; if (countB >= 0) distance--; // we've found in B an allele that was present in A else distance++; // we've found in B a new allele alleles[alleleB] = countB; } } return distance; } protected override IEnumerable GetDistancesToBestKnownSolution() { if (PermutationParameter.ActualName == null) return new double[0]; Permutation bestKnownValue = BestKnownSolution.ActualValue; if (bestKnownValue == null) return PermutationParameter.ActualValue.Select(v => 0d); return PermutationParameter.ActualValue.Select(v => Distance(v, bestKnownValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue)); } } }