source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 4 months ago

#2971: made branch compile with current version of trunk

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