[8352] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[8410] | 28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[8352] | 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
| 35 | [Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public class MutationPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
| 38 | private const string ResultsParameterName = "Results";
|
---|
| 39 | private const string GenerationsParameterName = "Generations";
|
---|
| 40 |
|
---|
| 41 | #region IAnalyzer Members
|
---|
| 42 | public bool EnabledByDefault {
|
---|
| 43 | get { return true; }
|
---|
| 44 | }
|
---|
| 45 | #endregion
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | #region Parameter properties
|
---|
| 49 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 50 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
| 53 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
| 54 | }
|
---|
| 55 | public ILookupParameter<DoubleValue> QualityAfterCrossoverParameter {
|
---|
| 56 | get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterCrossover"]; }
|
---|
| 57 | }
|
---|
| 58 | public ILookupParameter<DoubleValue> QualityAfterMutationParameter {
|
---|
| 59 | get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterMutation"]; }
|
---|
| 60 | }
|
---|
[8410] | 61 | public ILookupParameter<Permutation> PermutationBeforeMutationParameter {
|
---|
| 62 | get { return (ILookupParameter<Permutation>)Parameters["PermutationBeforeMutation"]; }
|
---|
| 63 | }
|
---|
| 64 | public ILookupParameter<Permutation> PermutationAfterMutationParameter {
|
---|
| 65 | get { return (ILookupParameter<Permutation>)Parameters["PermutationAfterMutation"]; }
|
---|
| 66 | }
|
---|
[8739] | 67 | public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
|
---|
| 68 | get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
|
---|
| 69 | }
|
---|
| 70 | public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 71 | get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
| 72 | }
|
---|
[8352] | 73 | #endregion
|
---|
| 74 |
|
---|
| 75 | #region Properties
|
---|
| 76 | public ResultCollection Results {
|
---|
| 77 | get { return ResultsParameter.ActualValue; }
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
[8505] | 81 | [Storable]
|
---|
[8501] | 82 | private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
|
---|
[8505] | 83 | [Storable]
|
---|
[8501] | 84 | private DataTableHelper avgDataTableHelper;
|
---|
[8505] | 85 | [Storable]
|
---|
[8501] | 86 | private int cnt = 0, lastGeneration = 0;
|
---|
[8505] | 87 | [Storable]
|
---|
[8501] | 88 | private List<double> qualityPoints = new List<double>();
|
---|
[8352] | 89 |
|
---|
| 90 | [StorableConstructor]
|
---|
| 91 | private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[8501] | 92 | private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
|
---|
| 93 | : base(original, cloner) {
|
---|
| 94 | diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
|
---|
| 95 | qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
|
---|
| 96 | avgDataTableHelper = (DataTableHelper)original.avgDataTableHelper.Clone(cloner);
|
---|
| 97 | cnt = original.cnt;
|
---|
| 98 | lastGeneration = original.lastGeneration;
|
---|
| 99 | qualityPoints = new List<double>(original.qualityPoints);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[8352] | 102 | public MutationPerformanceAnalyzer()
|
---|
| 103 | : base() {
|
---|
| 104 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
| 105 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
| 106 |
|
---|
| 107 | Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterCrossover", "The evaluated quality of the child solution."));
|
---|
| 108 | QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
|
---|
| 109 |
|
---|
| 110 | Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterMutation", "The evaluated quality of the child solution."));
|
---|
| 111 | QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
|
---|
[8410] | 112 |
|
---|
| 113 | Parameters.Add(new LookupParameter<Permutation>("PermutationBeforeMutation"));
|
---|
[8411] | 114 | PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
|
---|
[8410] | 115 |
|
---|
| 116 | Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
|
---|
[8411] | 117 | PermutationAfterMutationParameter.ActualName = "TSPTour";
|
---|
[8498] | 118 |
|
---|
[8739] | 119 | Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
|
---|
| 120 | Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
|
---|
| 121 |
|
---|
[8511] | 122 | diversityPlotHelper = new ScatterPlotHelper(false, true);
|
---|
| 123 | qualityPlotHelper = new ScatterPlotHelper(false, true);
|
---|
[8498] | 124 | avgDataTableHelper = new DataTableHelper();
|
---|
[8352] | 125 | }
|
---|
| 126 |
|
---|
| 127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 128 | return new MutationPerformanceAnalyzer(this, cloner);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public override IOperation Apply() {
|
---|
[8411] | 132 | Point2D<double> curPoint, divPoint;
|
---|
[8498] | 133 |
|
---|
[8739] | 134 | if (SimilarityCalculatorParameter.Value == null) {
|
---|
| 135 | SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[8352] | 138 | var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
|
---|
| 139 | var qualityM = QualityAfterMutationParameter.ActualValue.Value;
|
---|
[8411] | 140 | var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
|
---|
| 141 | var permutationAfter = PermutationAfterMutationParameter.ActualValue;
|
---|
[8352] | 142 |
|
---|
[8661] | 143 | if (permutationBefore != null) {
|
---|
| 144 | qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
|
---|
| 145 | diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
|
---|
[8797] | 146 | avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", new string[] { "Average Mutation Performance per Generation" });
|
---|
[8498] | 147 |
|
---|
[8739] | 148 | Scope permutationBeforeScope = new Scope();
|
---|
| 149 | Scope permutationAfterScope = new Scope();
|
---|
| 150 | permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationBefore));
|
---|
| 151 | permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationAfter));
|
---|
| 152 |
|
---|
| 153 | divPoint = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
|
---|
[8661] | 154 | curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
|
---|
| 155 | qualityPoints.Add(curPoint.Y);
|
---|
[8352] | 156 |
|
---|
[8661] | 157 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
[8352] | 158 |
|
---|
[8661] | 159 | qualityPlotHelper.AddPoint(curGenStr, curPoint);
|
---|
| 160 | diversityPlotHelper.AddPoint(curGenStr, divPoint);
|
---|
[8352] | 161 |
|
---|
[8661] | 162 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
| 163 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
| 164 | double avg = qualityPoints.Average();
|
---|
| 165 | avgDataTableHelper.AddPoint(avg);
|
---|
| 166 | Reset();
|
---|
| 167 | }
|
---|
| 168 | } else {
|
---|
[8499] | 169 | Reset();
|
---|
[8352] | 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | return base.Apply();
|
---|
| 174 | }
|
---|
[8499] | 175 |
|
---|
| 176 | private void Reset() {
|
---|
| 177 | cnt = 0;
|
---|
| 178 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
| 179 | qualityPoints.Clear();
|
---|
| 180 | }
|
---|
[8352] | 181 | }
|
---|
| 182 | }
|
---|