#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.TravelingSalesman;
namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
[Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
[StorableClass]
public class MutationPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
private const string ResultsParameterName = "Results";
private const string GenerationsParameterName = "Generations";
#region IAnalyzer Members
public bool EnabledByDefault {
get { return true; }
}
#endregion
#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"]; }
}
#endregion
#region Properties
public ResultCollection Results {
get { return ResultsParameter.ActualValue; }
}
#endregion
ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
DataTableHelper avgDataTableHelper;
int cnt = 0, lastGeneration = 0;
List qualityPoints = new List();
[StorableConstructor]
private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
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";
diversityPlotHelper = new ScatterPlotHelper();
qualityPlotHelper = new ScatterPlotHelper();
avgDataTableHelper = new DataTableHelper();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new MutationPerformanceAnalyzer(this, cloner);
}
public override IOperation Apply() {
Point2D curPoint, divPoint;
var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
var qualityM = QualityAfterMutationParameter.ActualValue.Value;
var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
var permutationAfter = PermutationAfterMutationParameter.ActualValue;
qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", "Average Mutation Performance per Generation");
divPoint = new Point2D(cnt, TSPSimilarityCalculator.CalculateSimilarity(permutationBefore, permutationAfter));
curPoint = new Point2D(cnt++, qualityCX - qualityM);
qualityPoints.Add(curPoint.Y);
string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
qualityPlotHelper.AddPoint(curGenStr, curPoint);
diversityPlotHelper.AddPoint(curGenStr, divPoint);
if (GenerationsParameter.ActualValue.Value != 0) {
if (GenerationsParameter.ActualValue.Value > lastGeneration) {
double avg = qualityPoints.Average();
avgDataTableHelper.AddPoint(avg);
cnt = 0;
lastGeneration = GenerationsParameter.ActualValue.Value;
qualityPoints.Clear();
}
}
return base.Apply();
}
}
}