#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; 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"]; } } public ILookupParameter> OperatorsParameter { get { return (ILookupParameter>)Parameters["Operators"]; } } public IValueParameter SimilarityCalculatorParameter { get { return (IValueParameter)Parameters["SimilarityCalculator"]; } } #endregion #region Properties public ResultCollection Results { get { return ResultsParameter.ActualValue; } } #endregion [Storable] private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper; [Storable] private DataTableHelper avgDataTableHelper; [Storable] private int cnt = 0, lastGeneration = 0; [Storable] private List qualityPoints = new List(); [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); avgDataTableHelper = (DataTableHelper)original.avgDataTableHelper.Clone(cloner); cnt = original.cnt; lastGeneration = original.lastGeneration; qualityPoints = new List(original.qualityPoints); } 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.")); diversityPlotHelper = new ScatterPlotHelper(false, true); qualityPlotHelper = new ScatterPlotHelper(false, true); avgDataTableHelper = new DataTableHelper(); } public override IDeepCloneable Clone(Cloner cloner) { return new MutationPerformanceAnalyzer(this, cloner); } public override IOperation Apply() { Point2D curPoint, divPoint; if (SimilarityCalculatorParameter.Value == null) { SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType().FirstOrDefault(); } var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value; var qualityM = QualityAfterMutationParameter.ActualValue.Value; var permutationBefore = PermutationBeforeMutationParameter.ActualValue; var permutationAfter = PermutationAfterMutationParameter.ActualValue; if (permutationBefore != null) { qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference"); diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity"); avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", new string[] { "Average Mutation Performance per Generation" }); Scope permutationBeforeScope = new Scope(); Scope permutationAfterScope = new Scope(); permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationBefore)); permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationAfter)); divPoint = new Point2D(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope)); 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); Reset(); } } else { Reset(); } } return base.Apply(); } private void Reset() { cnt = 0; lastGeneration = GenerationsParameter.ActualValue.Value; qualityPoints.Clear(); } } }