[5505] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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;
|
---|
[16499] | 24 | using System.Diagnostics;
|
---|
| 25 | using System.Linq;
|
---|
[5505] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16499] | 30 | using HeuristicLab.Parameters;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[5505] | 32 |
|
---|
[5618] | 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[16499] | 34 | [Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
|
---|
[16565] | 35 | [StorableType("FE514989-E619-48B8-AC8E-9A2202708F65")]
|
---|
[16499] | 36 | public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
|
---|
| 37 | private const string StrictSimilarityParameterName = "StrictSimilarity";
|
---|
| 38 |
|
---|
| 39 | private readonly object locker = new object();
|
---|
| 40 |
|
---|
| 41 | public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
|
---|
| 42 | get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public bool StrictSimilarity {
|
---|
| 46 | get { return StrictSimilarityParameter.Value.Value; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[5505] | 49 | [StorableConstructor]
|
---|
[16565] | 50 | protected PearsonRSquaredAverageSimilarityEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[16499] | 51 | protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
|
---|
[5505] | 52 | : base(original, cloner) {
|
---|
| 53 | }
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16499] | 55 | return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
|
---|
[5505] | 56 | }
|
---|
| 57 |
|
---|
[16499] | 58 | public PearsonRSquaredAverageSimilarityEvaluator() : base() {
|
---|
| 59 | Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
|
---|
| 60 | }
|
---|
[11310] | 61 |
|
---|
[13300] | 62 | public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity
|
---|
[5514] | 63 |
|
---|
[10291] | 64 | public override IOperation InstrumentedApply() {
|
---|
[5505] | 65 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5851] | 66 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
[11310] | 67 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 68 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
| 69 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
| 70 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
| 71 |
|
---|
| 72 | if (UseConstantOptimization) {
|
---|
[13670] | 73 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
|
---|
[11310] | 74 | }
|
---|
[12848] | 75 | double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);
|
---|
[5505] | 76 | QualitiesParameter.ActualValue = new DoubleArray(qualities);
|
---|
[10291] | 77 | return base.InstrumentedApply();
|
---|
[5505] | 78 | }
|
---|
| 79 |
|
---|
[16499] | 80 | public double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, int decimalPlaces) {
|
---|
[11310] | 81 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
|
---|
[12848] | 82 | if (decimalPlaces >= 0)
|
---|
| 83 | r2 = Math.Round(r2, decimalPlaces);
|
---|
[16499] | 84 |
|
---|
| 85 | var variables = ExecutionContext.Scope.Variables;
|
---|
| 86 | if (!variables.ContainsKey("AverageSimilarity")) {
|
---|
| 87 | lock (locker) {
|
---|
| 88 | CalculateAverageSimilarities(ExecutionContext.Scope.Parent.SubScopes.Where(x => x.Variables.ContainsKey("SymbolicExpressionTree")).ToArray(), StrictSimilarity);
|
---|
| 89 |
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | double avgSim = ((DoubleValue)variables["AverageSimilarity"].Value).Value;
|
---|
| 94 | return new double[2] { r2, avgSim };
|
---|
[5505] | 95 | }
|
---|
[5613] | 96 |
|
---|
| 97 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[5722] | 98 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
[5770] | 99 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8664] | 100 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[13300] | 101 | // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.
|
---|
[5722] | 102 |
|
---|
[12848] | 103 | double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
|
---|
[5722] | 104 |
|
---|
| 105 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
[5770] | 106 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8664] | 107 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[5722] | 108 |
|
---|
| 109 | return quality;
|
---|
[5613] | 110 | }
|
---|
[16499] | 111 |
|
---|
| 112 | private readonly Stopwatch sw = new Stopwatch();
|
---|
| 113 | public void CalculateAverageSimilarities(IScope[] treeScopes, bool strict) {
|
---|
| 114 | var trees = treeScopes.Select(x => (ISymbolicExpressionTree)x.Variables["SymbolicExpressionTree"].Value).ToArray();
|
---|
| 115 | var similarityMatrix = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(trees, simplify: false, strict: strict);
|
---|
| 116 |
|
---|
| 117 | for (int i = 0; i < treeScopes.Length; ++i) {
|
---|
| 118 | var scope = treeScopes[i];
|
---|
| 119 | var avgSimilarity = 0d;
|
---|
| 120 | for (int j = 0; j < trees.Length; ++j) {
|
---|
| 121 | if (i == j) continue;
|
---|
| 122 | avgSimilarity += similarityMatrix[i, j];
|
---|
| 123 | }
|
---|
| 124 | avgSimilarity /= trees.Length - 1;
|
---|
| 125 |
|
---|
| 126 | if (scope.Variables.ContainsKey("AverageSimilarity")) {
|
---|
| 127 | ((DoubleValue)scope.Variables["AverageSimilarity"].Value).Value = avgSimilarity;
|
---|
| 128 | } else {
|
---|
| 129 | scope.Variables.Add(new Core.Variable("AverageSimilarity", new DoubleValue(avgSimilarity)));
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
[5505] | 133 | }
|
---|
| 134 | }
|
---|