#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.Drawing;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Parameters;
using HEAL.Attic;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("PermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")]
[StorableType("2021A1AA-FE98-4D42-8730-23C5EA552B21")]
public class PermutationFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IPermutationOperator {
#region Parameters
public ScopeTreeLookupParameter PermutationParameter {
get { return (ScopeTreeLookupParameter)Parameters["Permutation"]; }
}
public LookupParameter BestKnownSolution {
get { return (LookupParameter)Parameters["BestKnownSolution"]; }
}
#endregion
[StorableConstructor]
protected PermutationFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
protected PermutationFitnessDistanceCorrelationAnalyzer(PermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
public PermutationFitnessDistanceCorrelationAnalyzer() {
Parameters.Add(new ScopeTreeLookupParameter("Permutation", "The permutation encoded solution"));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution"));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PermutationFitnessDistanceCorrelationAnalyzer(this, cloner);
}
private static Point GetEdge(Permutation a, int index) {
switch (a.PermutationType) {
case PermutationTypes.RelativeDirected:
return new Point(a[index], a[(index + 1) % a.Length]);
case PermutationTypes.RelativeUndirected:
return new Point(
Math.Min(a[index], a[(index + 1) % a.Length]),
Math.Max(a[index], a[(index + 1) % a.Length]));
default:
throw new ArgumentException("cannot derive edge from non-relative permutation type");
}
}
public static double Distance(Permutation a, Permutation b) {
if (a.PermutationType != b.PermutationType)
throw new ArgumentException(
"Cannot calculate distance between different permuatation types: " +
a.PermutationType + " and " + b.PermutationType);
if (a.PermutationType == PermutationTypes.Absolute) {
int nEqual = 0;
for (int i = 0; i < Math.Min(a.Length, b.Length); i++) {
if (a[i] == b[i])
nEqual++;
}
return Math.Max(a.Length, b.Length) - nEqual;
} else {
HashSet edges = new HashSet();
for (int i = 0; i < a.Length; i++)
edges.Add(GetEdge(a, i));
int nCommonEdges = 0;
for (int i = 0; i < b.Length; i++) {
if (edges.Contains(GetEdge(b, i)))
nCommonEdges++;
}
return Math.Max(a.Length, b.Length) - nCommonEdges;
}
}
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));
}
}
}