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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HEAL.Attic;
|
---|
29 |
|
---|
30 | namespace 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), true));
|
---|
63 | Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true));
|
---|
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 | }
|
---|