Free cookie consent management tool by TermsFeed Policy Generator

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, 5 years ago

#2971: made branch compile with current version of trunk

File size: 6.8 KB
RevLine 
[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]22using System;
[5505]23using System.Collections.Generic;
[16499]24using System.Diagnostics;
25using System.Linq;
[5505]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16499]30using HeuristicLab.Parameters;
[5505]31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[16628]32using HEAL.Attic;
[5505]33
[5618]34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[16499]35  [Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
[16628]36  [StorableType("AC638211-90FD-448A-BC32-4F26411D8D9D")]
[16499]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
[5505]50    [StorableConstructor]
[16628]51    protected PearsonRSquaredAverageSimilarityEvaluator(StorableConstructorFlag _) : base(_) { }
[16499]52    protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
[5505]53      : base(original, cloner) {
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
[16499]56      return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
[5505]57    }
58
[16499]59    public PearsonRSquaredAverageSimilarityEvaluator() : base() {
60      Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
61    }
[11310]62
[13300]63    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity
[5514]64
[10291]65    public override IOperation InstrumentedApply() {
[5505]66      IEnumerable<int> rows = GenerateRowsToEvaluate();
[5851]67      var solution = SymbolicExpressionTreeParameter.ActualValue;
[11310]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) {
[13670]74        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
[11310]75      }
[12848]76      double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);
[5505]77      QualitiesParameter.ActualValue = new DoubleArray(qualities);
[10291]78      return base.InstrumentedApply();
[5505]79    }
80
[16499]81    public double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, int decimalPlaces) {
[11310]82      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
[12848]83      if (decimalPlaces >= 0)
84        r2 = Math.Round(r2, decimalPlaces);
[16499]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 };
[5505]96    }
[5613]97
98    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
[5722]99      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
[5770]100      EstimationLimitsParameter.ExecutionContext = context;
[8664]101      ApplyLinearScalingParameter.ExecutionContext = context;
[13300]102      // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.
[5722]103
[12848]104      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
[5722]105
106      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
[5770]107      EstimationLimitsParameter.ExecutionContext = null;
[8664]108      ApplyLinearScalingParameter.ExecutionContext = null;
[5722]109
110      return quality;
[5613]111    }
[16499]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    }
[5505]134  }
135}
Note: See TracBrowser for help on using the repository browser.