source: branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/ConstantsOptimizationEvaluator.cs @ 17193

Last change on this file since 17193 was 17193, checked in by mkommend, 4 months ago

#2974: Merged trunk changes into branch.

File size: 5.8 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 System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
31  [Item("New Constant Optimization Evaluator", "Calculates Pearson R² of a symbolic regression solution and optimizes the constant used.")]
32  [StorableType("B4255C8A-9FFA-42A4-988C-B81911302A04")]
33  public class ConstantsOptimizationEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
34    private const string ConstantOptimizationIterationsParameterName = "ConstantOptimizationIterations";
35    private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
36
37    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
38      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
39    }
40    public IFixedValueParameter<PercentValue> ConstantOptimizationRowsPercentageParameter {
41      get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
42    }
43
44    public IntValue ConstantOptimizationIterations {
45      get { return ConstantOptimizationIterationsParameter.Value; }
46    }
47    public PercentValue ConstantOptimizationRowsPercentage {
48      get { return ConstantOptimizationRowsPercentageParameter.Value; }
49    }
50
51    public override bool Maximization {
52      get { return true; }
53    }
54
55    [StorableConstructor]
56    protected ConstantsOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
57    protected ConstantsOptimizationEvaluator(ConstantsOptimizationEvaluator original, Cloner cloner)
58      : base(original, cloner) {
59    }
60    public ConstantsOptimizationEvaluator()
61      : base() {
62      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
63      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1)));
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new ConstantsOptimizationEvaluator(this, cloner);
68    }
69
70    public override IOperation InstrumentedApply() {
71      var solution = SymbolicExpressionTreeParameter.ActualValue;
72      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
73      var problemData = ProblemDataParameter.ActualValue;
74      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
75      var estimationLimits = EstimationLimitsParameter.ActualValue;
76
77      double quality;
78      var rowsPercentage = ConstantOptimizationRowsPercentage.Value;
79      var constantOptimizationRows = GenerateRowsToEvaluate(rowsPercentage);
80      quality = ConstantsOptimization.LMConstantsOptimizer.OptimizeConstants(solution, problemData.Dataset, problemData.TargetVariable, constantOptimizationRows, applyLinearScaling, ConstantOptimizationIterations.Value);
81      if (quality < 0|| double.IsNaN(quality) || ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
82        var evaluationRows = GenerateRowsToEvaluate();
83        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, evaluationRows, applyLinearScaling);
84      }
85      QualityParameter.ActualValue = new DoubleValue(quality);
86
87      return base.InstrumentedApply();
88    }
89
90    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
91      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
92      EstimationLimitsParameter.ExecutionContext = context;
93      ApplyLinearScalingParameter.ExecutionContext = context;
94
95      // Pearson R² evaluator is used on purpose instead of the const-opt evaluator,
96      // because Evaluate() is used to get the quality of evolved models on
97      // different partitions of the dataset (e.g., best validation model)
98      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
99
100      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
101      EstimationLimitsParameter.ExecutionContext = null;
102      ApplyLinearScalingParameter.ExecutionContext = null;
103
104      return r2;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.