1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace 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 | if (evaluations > 0) {
|
---|
91 | var dataTable = QualityPerEvaluationsParameter.ActualValue;
|
---|
92 | var values = dataTable.Rows["First-hit Graph"].Values;
|
---|
93 |
|
---|
94 | var newEntry = Tuple.Create(evaluations, bestQuality);
|
---|
95 |
|
---|
96 | if (values.Count == 0) {
|
---|
97 | values.Add(newEntry); // record the first data
|
---|
98 | values.Add(Tuple.Create(evaluations, bestQuality)); // last entry records max number of evaluations
|
---|
99 | return base.Apply();
|
---|
100 | }
|
---|
101 |
|
---|
102 | var improvement = values.Last().Item2 != bestQuality;
|
---|
103 | if (improvement) {
|
---|
104 | values[values.Count - 1] = newEntry; // record the improvement
|
---|
105 | values.Add(Tuple.Create(evaluations, bestQuality)); // last entry records max number of evaluations
|
---|
106 | } else {
|
---|
107 | values[values.Count - 1] = Tuple.Create(evaluations, bestQuality); // the last entry is updated
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return base.Apply();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|