Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17514 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.2 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();
[5851]72      var solution = 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
78      if (UseConstantOptimization) {
[13670]79        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
[11310]80      }
[5505]81
[16978]82      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
[16499]83
[16978]84      if (DecimalPlaces >= 0)
85        r2 = Math.Round(r2, DecimalPlaces);
[16499]86
[16978]87      lock (locker) {
88        if (AverageSimilarityParameter.ActualValue == null) {
89          var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent);
90          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
91          SimilarityCalculator.Execute(context, CancellationToken);
[16499]92        }
93      }
[16978]94      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
[16499]95
[16978]96      QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
97      return base.InstrumentedApply();
[5505]98    }
[5613]99
100    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
[5722]101      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
[16978]102      AverageSimilarityParameter.ExecutionContext = context;
[5770]103      EstimationLimitsParameter.ExecutionContext = context;
[8664]104      ApplyLinearScalingParameter.ExecutionContext = context;
[5722]105
[16978]106      var estimationLimits = EstimationLimitsParameter.ActualValue;
107      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
[5722]108
[16978]109      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
110
111      lock (locker) {
112        if (AverageSimilarityParameter.ActualValue == null) {
113          var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent);
114          SimilarityCalculator.StrictSimilarity = StrictSimilarity;
115          SimilarityCalculator.Execute(context, CancellationToken);
116        }
117      }
118      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
119
[5722]120      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
[5770]121      EstimationLimitsParameter.ExecutionContext = null;
[8664]122      ApplyLinearScalingParameter.ExecutionContext = null;
[5722]123
[16978]124      return new[] { r2, avgSimilarity };
[5613]125    }
[5505]126  }
127}
Note: See TracBrowser for help on using the repository browser.