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

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

#2175: Additional calculation of results for EuroCast 2015.

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