Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis/3.3/QualityAnalysis/QualityPerEvaluationsAnalyzer.cs @ 12834

Last change on this file since 12834 was 12834, checked in by abeham, 9 years ago

#2431: Changed analyzer to always output last value (even if quality is equal)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis {
33  [Item("QualityPerEvaluationsAnalyzer", @"Creates a plot of the solution quality with respect to the number of evaluated solutions.")]
34  [StorableClass]
35  public class QualityPerEvaluationsAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
36    public virtual bool EnabledByDefault {
37      get { return false; }
38    }
39
40    public ILookupParameter<DoubleValue> BestQualityParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
42    }
43    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
44      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
45    }
46    public ILookupParameter<IntValue> EvaluatedMovesParameter {
47      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
48    }
49    public IValueLookupParameter<DoubleValue> MoveCostPerSolutionParameter {
50      get { return (IValueLookupParameter<DoubleValue>)Parameters["MoveCostPerSolution"]; }
51    }
52    public IResultParameter<IndexedDataTable<double>> QualityPerEvaluationsParameter {
53      get { return (IResultParameter<IndexedDataTable<double>>)Parameters["QualityPerEvaluations"]; }
54    }
55
56    [StorableConstructor]
57    protected QualityPerEvaluationsAnalyzer(bool deserializing) : base(deserializing) { }
58    protected QualityPerEvaluationsAnalyzer(QualityPerEvaluationsAnalyzer original, Cloner cloner) : base(original, cloner) { }
59    public QualityPerEvaluationsAnalyzer()
60      : base() {
61      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The quality value that should be compared."));
62      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
63      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
64      Parameters.Add(new ValueLookupParameter<DoubleValue>("MoveCostPerSolution", "The cost to evaluate a move as a ratio to the cost of evaluating a solution.", new DoubleValue(1)));
65      Parameters.Add(new ResultParameter<IndexedDataTable<double>>("QualityPerEvaluations", "Data table containing the first hitting graph with evaluations as the x-axis."));
66      QualityPerEvaluationsParameter.DefaultValue = new IndexedDataTable<double>("Quality per Evaluations") {
67        VisualProperties = {
68          XAxisTitle = "Evaluations",
69          YAxisTitle = "Quality"
70        },
71        Rows = { new IndexedDataRow<double>("First-hit Graph") { VisualProperties = {
72          ChartType = DataRowVisualProperties.DataRowChartType.StepLine,
73          LineWidth = 2
74        } } }
75      };
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new QualityPerEvaluationsAnalyzer(this, cloner);
80    }
81
82    public override IOperation Apply() {
83      var bestQuality = BestQualityParameter.ActualValue.Value;
84      var evalSols = EvaluatedSolutionsParameter.ActualValue;
85      var evalMoves = EvaluatedMovesParameter.ActualValue;
86      var evaluations = 0.0;
87      if (evalSols != null) evaluations += evalSols.Value;
88      if (evalMoves != null) evaluations += evalMoves.Value * MoveCostPerSolutionParameter.ActualValue.Value;
89
90      var dataTable = QualityPerEvaluationsParameter.ResultValue;
91      var values = dataTable.Rows["First-hit Graph"].Values;
92      if (evaluations == 0 || values.Count > 0 && evaluations < values.Last().Item1) evaluations = 1;
93      var newEntry = Tuple.Create(evaluations, bestQuality);
94
95      if (values.Count == 0) {
96        values.Add(newEntry);
97        values.Add(Tuple.Create(evaluations, bestQuality)); // duplicate entry that will be replaced
98        return base.Apply();
99      }
100
101      var improvement = values.Last().Item2 != bestQuality;
102      if (improvement) {
103        values[values.Count - 1] = newEntry;
104        values.Add(Tuple.Create(evaluations, bestQuality)); // duplicate entry that will be replaced
105      } else {
106        values[values.Count - 1] = Tuple.Create(evaluations, bestQuality);
107      }
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.