[8352] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9789] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8352] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
| 32 | [Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
|
---|
| 33 | [StorableClass]
|
---|
[8867] | 34 | public class MutationPerformanceAnalyzer : InitializableOperator, IStatefulItem {
|
---|
[8352] | 35 | private const string ResultsParameterName = "Results";
|
---|
| 36 | private const string GenerationsParameterName = "Generations";
|
---|
| 37 |
|
---|
| 38 | #region Parameter properties
|
---|
| 39 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 40 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 41 | }
|
---|
| 42 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
| 43 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
| 44 | }
|
---|
| 45 | public ILookupParameter<DoubleValue> QualityAfterCrossoverParameter {
|
---|
| 46 | get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterCrossover"]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<DoubleValue> QualityAfterMutationParameter {
|
---|
| 49 | get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterMutation"]; }
|
---|
| 50 | }
|
---|
[9045] | 51 | public ILookupParameter<IItem> PermutationBeforeMutationParameter {
|
---|
| 52 | get { return (ILookupParameter<IItem>)Parameters["PermutationBeforeMutation"]; }
|
---|
[8410] | 53 | }
|
---|
[9045] | 54 | public ILookupParameter<IItem> PermutationAfterMutationParameter {
|
---|
| 55 | get { return (ILookupParameter<IItem>)Parameters["PermutationAfterMutation"]; }
|
---|
[8410] | 56 | }
|
---|
[8739] | 57 | public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
|
---|
| 58 | get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
|
---|
| 59 | }
|
---|
[9150] | 60 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 61 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 62 | }
|
---|
[8739] | 63 | public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 64 | get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
| 65 | }
|
---|
[9185] | 66 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 67 | get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 68 | }
|
---|
| 69 | public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
|
---|
| 70 | get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
|
---|
| 71 | }
|
---|
[8352] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region Properties
|
---|
| 75 | public ResultCollection Results {
|
---|
| 76 | get { return ResultsParameter.ActualValue; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
[8505] | 80 | [Storable]
|
---|
[9150] | 81 | private DataTableHelper successHelper;
|
---|
| 82 | [Storable]
|
---|
[8501] | 83 | private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
|
---|
[8505] | 84 | [Storable]
|
---|
[9150] | 85 | private int cnt = 0, lastGeneration = 0, success = 0;
|
---|
[9431] | 86 | [Storable]
|
---|
| 87 | private bool scalingFinished = false;
|
---|
[8352] | 88 |
|
---|
| 89 | [StorableConstructor]
|
---|
| 90 | private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[8501] | 91 | private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
|
---|
| 92 | : base(original, cloner) {
|
---|
| 93 | diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
|
---|
| 94 | qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
|
---|
[9150] | 95 | successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
|
---|
[8501] | 96 | cnt = original.cnt;
|
---|
| 97 | lastGeneration = original.lastGeneration;
|
---|
[9150] | 98 | success = original.success;
|
---|
[9431] | 99 | scalingFinished = original.scalingFinished;
|
---|
[8501] | 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 |
|
---|
[9045] | 113 | Parameters.Add(new LookupParameter<IItem>("PermutationBeforeMutation"));
|
---|
[8411] | 114 | PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
|
---|
[8410] | 115 |
|
---|
[9045] | 116 | Parameters.Add(new LookupParameter<IItem>("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."));
|
---|
[9150] | 121 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
[9185] | 122 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
| 123 | Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
|
---|
[8739] | 124 |
|
---|
[9252] | 125 | diversityPlotHelper = new ScatterPlotHelper(false, true, false, true);
|
---|
| 126 | qualityPlotHelper = new ScatterPlotHelper(false, true, true, true);
|
---|
[9150] | 127 | successHelper = new DataTableHelper();
|
---|
[8352] | 128 | }
|
---|
| 129 |
|
---|
| 130 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 131 | return new MutationPerformanceAnalyzer(this, cloner);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[8867] | 134 | protected override void InitializeAction() {
|
---|
[8739] | 135 | if (SimilarityCalculatorParameter.Value == null) {
|
---|
| 136 | SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
| 137 | }
|
---|
[9341] | 138 | qualityPlotHelper.InitializePlot(Results, "Mutation Performance compared to parent", "Solution Index", "Absolut Quality Difference");
|
---|
[9045] | 139 | diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
|
---|
[9150] | 140 | successHelper.InitializeChart(Results, "Successfull Mutations", new string[] { "Successfull Mutations per Generation" });
|
---|
[8867] | 141 | Reset();
|
---|
| 142 | }
|
---|
[8739] | 143 |
|
---|
[8867] | 144 | public override IOperation Apply() {
|
---|
| 145 | Initialize();
|
---|
| 146 | Point2D<double> curPoint, divPoint;
|
---|
| 147 |
|
---|
[8352] | 148 | var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
|
---|
| 149 | var qualityM = QualityAfterMutationParameter.ActualValue.Value;
|
---|
[9045] | 150 | var solutionBefore = PermutationBeforeMutationParameter.ActualValue;
|
---|
| 151 | var solutionAfter = PermutationAfterMutationParameter.ActualValue;
|
---|
[8352] | 152 |
|
---|
[9045] | 153 | Scope permutationBeforeScope = new Scope();
|
---|
| 154 | Scope permutationAfterScope = new Scope();
|
---|
| 155 | permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionBefore));
|
---|
| 156 | permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionAfter));
|
---|
[8498] | 157 |
|
---|
[9045] | 158 | divPoint = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
|
---|
| 159 | curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
|
---|
[8739] | 160 |
|
---|
[9045] | 161 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
[8867] | 162 |
|
---|
[9045] | 163 | qualityPlotHelper.AddPoint(curGenStr, curPoint);
|
---|
| 164 | diversityPlotHelper.AddPoint(curGenStr, divPoint);
|
---|
[8352] | 165 |
|
---|
[9150] | 166 | if (GenerationsParameter.ActualValue.Value == lastGeneration) {
|
---|
| 167 | CountSuccess(qualityCX, qualityM);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[9431] | 170 | if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
|
---|
| 171 | scalingFinished = true;
|
---|
[9185] | 172 | double bkQuality = BestKnownQualityParameter.ActualValue.Value;
|
---|
| 173 | double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
|
---|
| 174 |
|
---|
| 175 | if (MaximizationParameter.ActualValue.Value) {
|
---|
| 176 | if (qualityPlotHelper.Max == double.MinValue) {
|
---|
| 177 | qualityPlotHelper.Max = bkQuality - wkQuality;
|
---|
| 178 | qualityPlotHelper.Min = 0;
|
---|
| 179 | }
|
---|
| 180 | } else {
|
---|
| 181 | if (qualityPlotHelper.Min == double.MaxValue) {
|
---|
| 182 | qualityPlotHelper.Max = wkQuality - bkQuality;
|
---|
| 183 | qualityPlotHelper.Min = 0;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[9431] | 188 |
|
---|
[9045] | 189 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
| 190 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
[9341] | 191 | if (cnt > 1) {
|
---|
| 192 | successHelper.AddPoint((double)success / (cnt - 1));
|
---|
| 193 | } else {
|
---|
| 194 | successHelper.AddPoint(0.0);
|
---|
| 195 | }
|
---|
[9150] | 196 |
|
---|
[9045] | 197 | Reset();
|
---|
[9150] | 198 | CountSuccess(qualityCX, qualityM);
|
---|
[8352] | 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return base.Apply();
|
---|
| 203 | }
|
---|
[8499] | 204 |
|
---|
| 205 | private void Reset() {
|
---|
| 206 | cnt = 0;
|
---|
| 207 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
[9150] | 208 | success = 0;
|
---|
[8499] | 209 | }
|
---|
[8867] | 210 |
|
---|
| 211 | public override void ClearState() {
|
---|
| 212 | qualityPlotHelper.CleanUp();
|
---|
| 213 | diversityPlotHelper.CleanUp();
|
---|
[9252] | 214 | successHelper.CleanUpAndCompressData();
|
---|
[9431] | 215 | scalingFinished = false;
|
---|
[8867] | 216 | }
|
---|
[9150] | 217 |
|
---|
| 218 | private void CountSuccess(double qualityCX, double qualityM) {
|
---|
| 219 | if (!MaximizationParameter.ActualValue.Value && qualityCX > qualityM) {
|
---|
| 220 | success++;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (MaximizationParameter.ActualValue.Value && qualityCX < qualityM) {
|
---|
| 224 | success++;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[8352] | 227 | }
|
---|
| 228 | }
|
---|