Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs @ 16529

Last change on this file since 16529 was 16529, checked in by gkronber, 6 years ago

#2520: merged r16491:16528 from trunk to persistence branch

File size: 6.7 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;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
34  [Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
35  [StorableClass]
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
49    [StorableConstructor]
50    protected PearsonRSquaredAverageSimilarityEvaluator(bool deserializing) : base(deserializing) { }
51    protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
52      : base(original, cloner) {
53    }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
56    }
57
58    public PearsonRSquaredAverageSimilarityEvaluator() : base() {
59      Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
60    }
61
62    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity
63
64    public override IOperation InstrumentedApply() {
65      IEnumerable<int> rows = GenerateRowsToEvaluate();
66      var solution = SymbolicExpressionTreeParameter.ActualValue;
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) {
73        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
74      }
75      double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);
76      QualitiesParameter.ActualValue = new DoubleArray(qualities);
77      return base.InstrumentedApply();
78    }
79
80    public double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, int decimalPlaces) {
81      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
82      if (decimalPlaces >= 0)
83        r2 = Math.Round(r2, decimalPlaces);
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 };
95    }
96
97    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
98      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
99      EstimationLimitsParameter.ExecutionContext = context;
100      ApplyLinearScalingParameter.ExecutionContext = context;
101      // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.
102
103      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
104
105      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
106      EstimationLimitsParameter.ExecutionContext = null;
107      ApplyLinearScalingParameter.ExecutionContext = null;
108
109      return quality;
110    }
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    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.