Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs @ 18242

Last change on this file since 18242 was 18220, checked in by gkronber, 3 years ago

#3136: reintegrated structure-template GP branch into trunk

File size: 6.3 KB
RevLine 
[5505]1#region License Information
2/* HeuristicLab
[17180]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]22using System;
[5505]23using System.Collections.Generic;
[16978]24using HEAL.Attic;
[5505]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16499]29using HeuristicLab.Parameters;
[5505]30
[5618]31namespace 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.")]
[16565]33  [StorableType("FE514989-E619-48B8-AC8E-9A2202708F65")]
[16499]34  public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
35    private const string StrictSimilarityParameterName = "StrictSimilarity";
[16978]36    private const string AverageSimilarityParameterName = "AverageSimilarity";
[16499]37
38    private readonly object locker = new object();
39
[16978]40    private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
41
[16499]42    public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
43      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
44    }
45
[16978]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]
[16565]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)));
[16978]65      Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName));
[16499]66    }
[11310]67
[16978]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();
[18220]72      var tree = 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
[18132]78      if (UseParameterOptimization) {
[18220]79        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
[11310]80      }
[5505]81
[18220]82      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
83        tree, problemData, rows, interpreter, applyLinearScaling,
84        estimationLimits.Lower, estimationLimits.Upper);
[16499]85
[16978]86      if (DecimalPlaces >= 0)
87        r2 = Math.Round(r2, DecimalPlaces);
[16499]88
[16978]89      lock (locker) {
90        if (AverageSimilarityParameter.ActualValue == null) {
91          var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent);
92          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
93          SimilarityCalculator.Execute(context, CancellationToken);
[16499]94        }
95      }
[16978]96      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
[16499]97
[16978]98      QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
99      return base.InstrumentedApply();
[5505]100    }
[5613]101
102    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
[5722]103      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
[16978]104      AverageSimilarityParameter.ExecutionContext = context;
[5770]105      EstimationLimitsParameter.ExecutionContext = context;
[8664]106      ApplyLinearScalingParameter.ExecutionContext = context;
[5722]107
[16978]108      var estimationLimits = EstimationLimitsParameter.ActualValue;
109      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
[5722]110
[18220]111      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
112        tree, problemData, rows,
113        SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
114        applyLinearScaling,
115        estimationLimits.Lower, estimationLimits.Upper);
[16978]116
117      lock (locker) {
118        if (AverageSimilarityParameter.ActualValue == null) {
119          var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent);
120          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
121          SimilarityCalculator.Execute(context, CancellationToken);
122        }
123      }
124      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
125
[5722]126      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
[5770]127      EstimationLimitsParameter.ExecutionContext = null;
[8664]128      ApplyLinearScalingParameter.ExecutionContext = null;
[5722]129
[16978]130      return new[] { r2, avgSimilarity };
[5613]131    }
[5505]132  }
133}
Note: See TracBrowser for help on using the repository browser.