Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/RSquaredEvaluator.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.7 KB
Line 
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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
31
32namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
33  [Item("RSquaredEvaluator", "")]
34  [StorableClass]
35  public class RSquaredEvaluator : ParameterizedNamedItem, IGrammarEnumerationEvaluator {
36    private readonly string OptimizeConstantsParameterName = "Optimize Constants";
37    private readonly string ApplyLinearScalingParameterName = "Apply Linear Scaling";
38    private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations";
39
40    #region parameter properties
41    public IFixedValueParameter<BoolValue> OptimizeConstantsParameter {
42      get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
43    }
44
45    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
46      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
47    }
48
49    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
50      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
51    }
52
53    public bool OptimizeConstants {
54      get { return OptimizeConstantsParameter.Value.Value; }
55      set { OptimizeConstantsParameter.Value.Value = value; }
56    }
57
58    public bool ApplyLinearScaling {
59      get { return ApplyLinearScalingParameter.Value.Value; }
60      set { ApplyLinearScalingParameter.Value.Value = value; }
61    }
62
63    public int ConstantOptimizationIterations {
64      get { return ConstantOptimizationIterationsParameter.Value.Value; }
65      set { ConstantOptimizationIterationsParameter.Value.Value = value; }
66    }
67    #endregion
68
69    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
70
71    public RSquaredEvaluator() {
72      Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false)));
73      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Apply linear scaling on the tree model during evaluation.", new BoolValue(false)));
74      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10)));
75    }
76
77    [StorableConstructor]
78    protected RSquaredEvaluator(bool deserializing) : base(deserializing) { }
79
80    protected RSquaredEvaluator(RSquaredEvaluator original, Cloner cloner) : base(original, cloner) {
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new RSquaredEvaluator(this, cloner);
85    }
86
87    public double Evaluate(IRegressionProblemData problemData, Grammar grammar, SymbolList sentence) {
88      var tree = grammar.ParseSymbolicExpressionTree(sentence);
89      return Evaluate(problemData, tree, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);
90    }
91
92    public double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree) {
93      return Evaluate(problemData, tree, OptimizeConstants, ConstantOptimizationIterations, ApplyLinearScaling);
94    }
95
96    public static double Evaluate(IRegressionProblemData problemData, ISymbolicExpressionTree tree, bool optimizeConstants = true, int maxIterations = 10, bool applyLinearScaling = false) {
97      double r2;
98
99      // TODO: Initialize constant values randomly
100      // TODO: Restarts
101      if (optimizeConstants) {
102        r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
103          tree,
104          problemData,
105          problemData.TrainingIndices,
106          applyLinearScaling: applyLinearScaling,
107          maxIterations: maxIterations,
108          updateVariableWeights: false,
109          updateConstantsInTree: true);
110
111        foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) {
112          ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode;
113          if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) {
114            constTreeNode.Value = 0.0;
115          }
116        }
117      } else {
118        r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(expressionTreeLinearInterpreter,
119          tree,
120          double.MinValue,
121          double.MaxValue,
122          problemData,
123          problemData.TrainingIndices,
124          applyLinearScaling: applyLinearScaling);
125      }
126      return double.IsNaN(r2) ? 0.0 : r2;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.