#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
[Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
[StorableClass]
public class MutationPerformanceAnalyzer : InitializableOperator, IStatefulItem {
private const string ResultsParameterName = "Results";
private const string GenerationsParameterName = "Generations";
#region Parameter properties
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters[ResultsParameterName]; }
}
public ILookupParameter GenerationsParameter {
get { return (ILookupParameter)Parameters[GenerationsParameterName]; }
}
public ILookupParameter QualityAfterCrossoverParameter {
get { return (ILookupParameter)Parameters["QualityAfterCrossover"]; }
}
public ILookupParameter QualityAfterMutationParameter {
get { return (ILookupParameter)Parameters["QualityAfterMutation"]; }
}
public ILookupParameter PermutationBeforeMutationParameter {
get { return (ILookupParameter)Parameters["PermutationBeforeMutation"]; }
}
public ILookupParameter PermutationAfterMutationParameter {
get { return (ILookupParameter)Parameters["PermutationAfterMutation"]; }
}
public ILookupParameter> OperatorsParameter {
get { return (ILookupParameter>)Parameters["Operators"]; }
}
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public IValueParameter SimilarityCalculatorParameter {
get { return (IValueParameter)Parameters["SimilarityCalculator"]; }
}
public ILookupParameter BestKnownQualityParameter {
get { return (ILookupParameter)Parameters["BestKnownQuality"]; }
}
public ILookupParameter WorstKnownQualityParameter {
get { return (ILookupParameter)Parameters["WorstKnownQuality"]; }
}
#endregion
#region Properties
public ResultCollection Results {
get { return ResultsParameter.ActualValue; }
}
#endregion
[Storable]
private DataTableHelper successHelper;
[Storable]
private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
[Storable]
private int cnt = 0, lastGeneration = 0, success = 0;
[Storable]
private bool scalingFinished = false;
[StorableConstructor]
private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
: base(original, cloner) {
diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
cnt = original.cnt;
lastGeneration = original.lastGeneration;
success = original.success;
scalingFinished = original.scalingFinished;
}
public MutationPerformanceAnalyzer()
: base() {
Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection where the analysis values should be stored."));
Parameters.Add(new LookupParameter(GenerationsParameterName, "Nr of generations."));
Parameters.Add(new LookupParameter("QualityAfterCrossover", "The evaluated quality of the child solution."));
QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
Parameters.Add(new LookupParameter("QualityAfterMutation", "The evaluated quality of the child solution."));
QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
Parameters.Add(new LookupParameter("PermutationBeforeMutation"));
PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
Parameters.Add(new LookupParameter("PermutationAfterMutation"));
PermutationAfterMutationParameter.ActualName = "TSPTour";
Parameters.Add(new ValueParameter("SimilarityCalculator"));
Parameters.Add(new LookupParameter>("Operators", "The operators and items that the problem provides to the algorithms."));
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, false otherwise"));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this problem."));
Parameters.Add(new LookupParameter("WorstKnownQuality", "The quality of the worst known solution of this problem."));
diversityPlotHelper = new ScatterPlotHelper(false, true, false, true);
qualityPlotHelper = new ScatterPlotHelper(false, true, true, true);
successHelper = new DataTableHelper();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new MutationPerformanceAnalyzer(this, cloner);
}
protected override void InitializeAction() {
if (SimilarityCalculatorParameter.Value == null) {
SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType().FirstOrDefault();
}
qualityPlotHelper.InitializePlot(Results, "Mutation Performance compared to parent", "Solution Index", "Absolut Quality Difference");
diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
successHelper.InitializeChart(Results, "Successfull Mutations", new string[] { "Successfull Mutations per Generation" });
Reset();
}
public override IOperation Apply() {
Initialize();
Point2D curPoint, divPoint;
var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
var qualityM = QualityAfterMutationParameter.ActualValue.Value;
var solutionBefore = PermutationBeforeMutationParameter.ActualValue;
var solutionAfter = PermutationAfterMutationParameter.ActualValue;
Scope permutationBeforeScope = new Scope();
Scope permutationAfterScope = new Scope();
permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionBefore));
permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionAfter));
divPoint = new Point2D(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
curPoint = new Point2D(cnt++, qualityCX - qualityM);
string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
qualityPlotHelper.AddPoint(curGenStr, curPoint);
diversityPlotHelper.AddPoint(curGenStr, divPoint);
if (GenerationsParameter.ActualValue.Value == lastGeneration) {
CountSuccess(qualityCX, qualityM);
}
if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
scalingFinished = true;
double bkQuality = BestKnownQualityParameter.ActualValue.Value;
double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
if (MaximizationParameter.ActualValue.Value) {
if (qualityPlotHelper.Max == double.MinValue) {
qualityPlotHelper.Max = bkQuality - wkQuality;
qualityPlotHelper.Min = 0;
}
} else {
if (qualityPlotHelper.Min == double.MaxValue) {
qualityPlotHelper.Max = wkQuality - bkQuality;
qualityPlotHelper.Min = 0;
}
}
}
if (GenerationsParameter.ActualValue.Value != 0) {
if (GenerationsParameter.ActualValue.Value > lastGeneration) {
if (cnt > 1) {
successHelper.AddPoint((double)success / (cnt - 1));
} else {
successHelper.AddPoint(0.0);
}
Reset();
CountSuccess(qualityCX, qualityM);
}
}
return base.Apply();
}
private void Reset() {
cnt = 0;
lastGeneration = GenerationsParameter.ActualValue.Value;
success = 0;
}
public override void ClearState() {
qualityPlotHelper.CleanUp();
diversityPlotHelper.CleanUp();
successHelper.CleanUpAndCompressData();
scalingFinished = false;
}
private void CountSuccess(double qualityCX, double qualityM) {
if (!MaximizationParameter.ActualValue.Value && qualityCX > qualityM) {
success++;
}
if (MaximizationParameter.ActualValue.Value && qualityCX < qualityM) {
success++;
}
}
}
}