Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredNumberofVariablesEvaluator.cs @ 11883

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

#2175: Additional calculation of results for EuroCast 2015.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
23using System.Linq;
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² & Number of Variables Evaluator", "Calculates the Pearson R² and the number of used variables of a symbolic regression solution.")]
33  [StorableClass]
34  public class PearsonRSquaredNumberOfVariablesEvaluator : 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 PearsonRSquaredNumberOfVariablesEvaluator(bool deserializing) : base(deserializing) { }
46    protected PearsonRSquaredNumberOfVariablesEvaluator(PearsonRSquaredNumberOfVariablesEvaluator original, Cloner cloner)
47      : base(original, cloner) {
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new PearsonRSquaredNumberOfVariablesEvaluator(this, cloner);
51    }
52
53    public PearsonRSquaredNumberOfVariablesEvaluator()
54      : base() {
55      Parameters.Add(new FixedValueParameter<BoolValue>(useConstantOptimizationParameterName, "", new BoolValue(false)));
56    }
57
58    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
59
60    public override IOperation InstrumentedApply() {
61      IEnumerable<int> rows = GenerateRowsToEvaluate();
62      var solution = SymbolicExpressionTreeParameter.ActualValue;
63      var problemData = ProblemDataParameter.ActualValue;
64      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
65      var estimationLimits = EstimationLimitsParameter.ActualValue;
66      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
67
68      if (UseConstantOptimization) {
69        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, 5, estimationLimits.Upper, estimationLimits.Lower);
70      }
71      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
72      QualitiesParameter.ActualValue = new DoubleArray(qualities);
73      return base.InstrumentedApply();
74    }
75
76    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
77      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
78      return new double[2] { r2, solution.IterateNodesPostfix().OfType<VariableTreeNode>().Count() };
79    }
80
81    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
82      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
83      EstimationLimitsParameter.ExecutionContext = context;
84      ApplyLinearScalingParameter.ExecutionContext = context;
85
86      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
87
88      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
89      EstimationLimitsParameter.ExecutionContext = null;
90      ApplyLinearScalingParameter.ExecutionContext = null;
91
92      return quality;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.