Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredTreeComplexityEvaluator.cs @ 12147

Last change on this file since 12147 was 12147, checked in by mkommend, 9 years ago

#2175: Added rounding of quality values to multi-objective sym reg evaluators.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [Item("Pearson R² & Tree Complexity Evaluator", "Calculates the Pearson R² and the tree complexity of a symbolic regression solution.")]
33  [StorableClass]
34  public class PearsonRSquaredTreeComplexityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
35    private const string useConstantOptimizationParameterName = "Use constant optimization";
36    public IFixedValueParameter<BoolValue> UseConstantOptimizationParameter {
37      get { return (IFixedValueParameter<BoolValue>)Parameters[useConstantOptimizationParameterName]; }
38    }
39    public bool UseConstantOptimization {
40      get { return UseConstantOptimizationParameter.Value.Value; }
41      set { UseConstantOptimizationParameter.Value.Value = value; }
42    }
43
44    [StorableConstructor]
45    protected PearsonRSquaredTreeComplexityEvaluator(bool deserializing) : base(deserializing) { }
46    protected PearsonRSquaredTreeComplexityEvaluator(PearsonRSquaredTreeComplexityEvaluator original, Cloner cloner)
47      : base(original, cloner) {
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new PearsonRSquaredTreeComplexityEvaluator(this, cloner);
51    }
52
53    public PearsonRSquaredTreeComplexityEvaluator()
54      : base() {
55      Parameters.Add(new FixedValueParameter<BoolValue>(useConstantOptimizationParameterName, "", new BoolValue(false)));
56    }
57
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      if (!Parameters.ContainsKey(useConstantOptimizationParameterName)) {
61        Parameters.Add(new FixedValueParameter<BoolValue>(useConstantOptimizationParameterName, "", new BoolValue(false)));
62      }
63    }
64
65    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
66
67    public override IOperation InstrumentedApply() {
68      IEnumerable<int> rows = GenerateRowsToEvaluate();
69      var solution = SymbolicExpressionTreeParameter.ActualValue;
70      var problemData = ProblemDataParameter.ActualValue;
71      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
72      var estimationLimits = EstimationLimitsParameter.ActualValue;
73      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
74
75      if (UseConstantOptimization) {
76        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, 5, estimationLimits.Upper, estimationLimits.Lower);
77      }
78      double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
79      QualitiesParameter.ActualValue = new DoubleArray(qualities);
80      return base.InstrumentedApply();
81    }
82
83    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
84      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
85      r2 = Math.Round(r2, 3);
86      return new double[2] { r2, SymbolicDataAnalysisModelComplexityAnalyzer.CalculateComplexity(solution.Root.GetSubtree(0).GetSubtree(0)) };
87    }
88
89    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
90      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
91      EstimationLimitsParameter.ExecutionContext = context;
92      ApplyLinearScalingParameter.ExecutionContext = context;
93
94      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
95
96      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
97      EstimationLimitsParameter.ExecutionContext = null;
98      ApplyLinearScalingParameter.ExecutionContext = null;
99
100      return quality;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.