Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/BestSolutionAnalyzer.cs @ 16053

Last change on this file since 16053 was 16053, checked in by bburlacu, 6 years ago

#2886: Refactor RSquaredEvaluator as a standalone ParameterizedNamedItem which is a parameter of the algorithm. Implement BestSolutionAnalyzer analyzer for quality statistics. Add license headers where missing.

File size: 5.8 KB
RevLine 
[16053]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
22using System;
23using System.Diagnostics;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Symbolic;
31using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
32
33namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
34  [Item("Best Solution Analyzer", "Returns the characteristics of the best solution so far.")]
35  [StorableClass]
36  public class BestSolutionAnalyzer : Item, IGrammarEnumerationAnalyzer {
37    public static readonly string BestTrainingQualityResultName = "Best R² (Training)";
38    public static readonly string BestTestQualityResultName = "Best R² (Test)";
39    public static readonly string BestTrainingModelResultName = "Best model (Training)";
40    public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
41    public static readonly string BestComplexityResultName = "Best solution complexity";
42    public static readonly string BestSolutions = "Best solutions";
43
44    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
45
46    public BestSolutionAnalyzer() { }
47
48    [StorableConstructor]
49    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
50
51    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) {
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new BestSolutionAnalyzer(this, cloner);
56    }
57
58    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
59      algorithm.DistinctSentenceGenerated -= AlgorithmDistinctSentenceGenerated;
60    }
61
62    public void Register(GrammarEnumerationAlgorithm algorithm) {
63      algorithm.DistinctSentenceGenerated += AlgorithmDistinctSentenceGenerated;
64    }
65
66    private void AlgorithmDistinctSentenceGenerated(object sender, PhraseAddedEventArgs args) {
67      var algorithm = (GrammarEnumerationAlgorithm)sender;
68      var sentence = args.NewPhrase;
69
70      var results = algorithm.Results;
71      var grammar = algorithm.Grammar;
72      var problemData = algorithm.Problem.ProblemData;
73
74      SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(sentence);
75      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
76
77      double r2 = algorithm.Evaluator.Evaluate(problemData, tree);
78      double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0;
79      if (r2 < bestR2)
80        return;
81
82      var bestComplexity = results.ContainsKey(BestComplexityResultName) ? GetValue<int>(results[BestComplexityResultName].Value) : int.MaxValue;
83      var complexity = sentence.Complexity;
84
85      if (algorithm.BestTrainingSentence == null || r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) {
86        algorithm.BestTrainingSentence = sentence;
87
88        var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, expressionTreeLinearInterpreter);
89        model.Scale(problemData);
90        var bestSolution = model.CreateRegressionSolution(problemData);
91
92        results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(bestSolution.TrainingRSquared));
93        results.AddOrUpdateResult(BestTestQualityResultName, new DoubleValue(bestSolution.TestRSquared));
94        results.AddOrUpdateResult(BestTrainingModelResultName, bestSolution.Model);
95        results.AddOrUpdateResult(BestTrainingSolutionResultName, bestSolution);
96        results.AddOrUpdateResult(BestComplexityResultName, new IntValue(complexity));
97
98        // record best sentence quality & length
99        DataTable dt;
100        if (!results.ContainsKey(BestSolutions)) {
101          var names = new[] { "Quality", "Length", "Complexity", "Timestamp" };
102          dt = new DataTable();
103          foreach (var name in names) {
104            dt.Rows.Add(new DataRow(name) { VisualProperties = { StartIndexZero = true } });
105          }
106          results.AddOrUpdateResult(BestSolutions, dt);
107        }
108        dt = (DataTable)results[BestSolutions].Value;
109        dt.Rows["Quality"].Values.Add(r2);
110        dt.Rows["Length"].Values.Add((double)sentence.Count);
111        dt.Rows["Complexity"].Values.Add(complexity);
112        dt.Rows["Timestamp"].Values.Add(algorithm.ExecutionTime.TotalMilliseconds / 1000d);
113      }
114
115      // stop the algorithm if the best quality was already achieved
116      if (r2.IsAlmost(1d)) {
117        algorithm.Stop();
118      }
119    }
120
121    private T GetValue<T>(IItem value) where T : struct {
122      var v = value as ValueTypeValue<T>;
123      if (v == null)
124        throw new ArgumentException(string.Format("Item is not of type {0}", typeof(ValueTypeValue<T>)));
125      return v.Value;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.