Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredNestedTreeSizeEvaluator.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
RevLine 
[11407]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;
[11883]28using HeuristicLab.Parameters;
[11407]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [Item("Pearson R² & Nested Tree size Evaluator", "Calculates the Pearson R² and the nested tree size of a symbolic regression solution.")]
33  [StorableClass]
[11883]34  public class PearsonRSquaredNestedTreeSizeEvaluator : 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
[11407]44    [StorableConstructor]
[11883]45    protected PearsonRSquaredNestedTreeSizeEvaluator(bool deserializing) : base(deserializing) { }
46    protected PearsonRSquaredNestedTreeSizeEvaluator(PearsonRSquaredNestedTreeSizeEvaluator original, Cloner cloner)
[11407]47      : base(original, cloner) {
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
[11883]50      return new PearsonRSquaredNestedTreeSizeEvaluator(this, cloner);
[11407]51    }
52
[11883]53    public PearsonRSquaredNestedTreeSizeEvaluator()
54      : base() {
55      Parameters.Add(new FixedValueParameter<BoolValue>(useConstantOptimizationParameterName, "", new BoolValue(false)));
56    }
[11407]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;
[11883]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
[11407]72      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
73      QualitiesParameter.ActualValue = new DoubleArray(qualities);
74      return base.InstrumentedApply();
75    }
76
77    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
78      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
79      return new double[2] { r2, solution.IterateNodesPostfix().Sum(n => n.GetLength()) };
80    }
81
82    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
83      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
84      EstimationLimitsParameter.ExecutionContext = context;
85      ApplyLinearScalingParameter.ExecutionContext = context;
86
87      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
88
89      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
90      EstimationLimitsParameter.ExecutionContext = null;
91      ApplyLinearScalingParameter.ExecutionContext = null;
92
93      return quality;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.