[5505] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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 |
|
---|
[12147] | 22 | using System;
|
---|
[5505] | 23 | using System.Collections.Generic;
|
---|
[17098] | 24 | using HEAL.Attic;
|
---|
[5505] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16499] | 29 | using HeuristicLab.Parameters;
|
---|
[5505] | 30 |
|
---|
[5618] | 31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[16499] | 32 | [Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
|
---|
[17097] | 33 | [StorableType("FE514989-E619-48B8-AC8E-9A2202708F65")]
|
---|
[16499] | 34 | public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
|
---|
| 35 | private const string StrictSimilarityParameterName = "StrictSimilarity";
|
---|
[17098] | 36 | private const string AverageSimilarityParameterName = "AverageSimilarity";
|
---|
[16499] | 37 |
|
---|
| 38 | private readonly object locker = new object();
|
---|
| 39 |
|
---|
[17098] | 40 | private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
|
---|
| 41 |
|
---|
[16499] | 42 | public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
|
---|
| 43 | get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[17098] | 46 | public ILookupParameter<DoubleValue> AverageSimilarityParameter {
|
---|
| 47 | get { return (ILookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[16499] | 50 | public bool StrictSimilarity {
|
---|
| 51 | get { return StrictSimilarityParameter.Value.Value; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[5505] | 54 | [StorableConstructor]
|
---|
[17097] | 55 | protected PearsonRSquaredAverageSimilarityEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[16499] | 56 | protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
|
---|
[5505] | 57 | : base(original, cloner) {
|
---|
| 58 | }
|
---|
| 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16499] | 60 | return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
|
---|
[5505] | 61 | }
|
---|
| 62 |
|
---|
[16499] | 63 | public PearsonRSquaredAverageSimilarityEvaluator() : base() {
|
---|
| 64 | Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
|
---|
[17098] | 65 | Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName));
|
---|
[16499] | 66 | }
|
---|
[11310] | 67 |
|
---|
[17098] | 68 | public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity
|
---|
[5514] | 69 |
|
---|
[10291] | 70 | public override IOperation InstrumentedApply() {
|
---|
[5505] | 71 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5851] | 72 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
[11310] | 73 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 74 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
| 75 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
| 76 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
| 77 |
|
---|
| 78 | if (UseConstantOptimization) {
|
---|
[13670] | 79 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
|
---|
[11310] | 80 | }
|
---|
[5505] | 81 |
|
---|
[17098] | 82 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
|
---|
[16499] | 83 |
|
---|
[17098] | 84 | if (DecimalPlaces >= 0)
|
---|
| 85 | r2 = Math.Round(r2, DecimalPlaces);
|
---|
[16499] | 86 |
|
---|
[17098] | 87 | lock (locker) {
|
---|
| 88 | if (AverageSimilarityParameter.ActualValue == null) {
|
---|
| 89 | var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent);
|
---|
| 90 | SimilarityCalculator.StrictSimilarity = StrictSimilarity;
|
---|
| 91 | SimilarityCalculator.Execute(context, CancellationToken);
|
---|
[16499] | 92 | }
|
---|
| 93 | }
|
---|
[17098] | 94 | var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
|
---|
[16499] | 95 |
|
---|
[17098] | 96 | QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
|
---|
| 97 | return base.InstrumentedApply();
|
---|
[5505] | 98 | }
|
---|
[5613] | 99 |
|
---|
| 100 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[5722] | 101 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
[17098] | 102 | AverageSimilarityParameter.ExecutionContext = context;
|
---|
[5770] | 103 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8664] | 104 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[5722] | 105 |
|
---|
[17098] | 106 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
| 107 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
[5722] | 108 |
|
---|
[17098] | 109 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
|
---|
| 110 |
|
---|
| 111 | lock (locker) {
|
---|
| 112 | if (AverageSimilarityParameter.ActualValue == null) {
|
---|
| 113 | var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent);
|
---|
| 114 | SimilarityCalculator.StrictSimilarity = StrictSimilarity;
|
---|
| 115 | SimilarityCalculator.Execute(context, CancellationToken);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
|
---|
| 119 |
|
---|
[5722] | 120 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
[5770] | 121 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8664] | 122 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[5722] | 123 |
|
---|
[17098] | 124 | return new[] { r2, avgSimilarity };
|
---|
[5613] | 125 | }
|
---|
[5505] | 126 | }
|
---|
| 127 | }
|
---|