[5505] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11310] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5505] | 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;
|
---|
[11883] | 27 | using HeuristicLab.Parameters;
|
---|
[5505] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[5618] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[5505] | 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 {
|
---|
[11883] | 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 |
|
---|
[5505] | 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 |
|
---|
[11883] | 52 | public SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator() : base()
|
---|
| 53 | {
|
---|
| 54 | Parameters.Add(new FixedValueParameter<BoolValue>(useConstantOptimizationParameterName, "", new BoolValue(false)));
|
---|
| 55 | }
|
---|
[5505] | 56 |
|
---|
[11883] | 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 |
|
---|
[5514] | 64 | public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
|
---|
| 65 |
|
---|
[10291] | 66 | public override IOperation InstrumentedApply() {
|
---|
[5505] | 67 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5851] | 68 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
[11883] | 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 | }
|
---|
[8664] | 77 | double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
|
---|
[5505] | 78 | QualitiesParameter.ActualValue = new DoubleArray(qualities);
|
---|
[10291] | 79 | return base.InstrumentedApply();
|
---|
[5505] | 80 | }
|
---|
| 81 |
|
---|
[8664] | 82 | public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
|
---|
[11310] | 83 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
|
---|
[8664] | 84 | return new double[2] { r2, solution.Length };
|
---|
[5505] | 85 | }
|
---|
[5613] | 86 |
|
---|
| 87 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[5722] | 88 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
[5770] | 89 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8664] | 90 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[5722] | 91 |
|
---|
[8664] | 92 | double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
|
---|
[5722] | 93 |
|
---|
| 94 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
[5770] | 95 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8664] | 96 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[5722] | 97 |
|
---|
| 98 | return quality;
|
---|
[5613] | 99 | }
|
---|
[5505] | 100 | }
|
---|
| 101 | }
|
---|