1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
32 | [Item("Pearson R² & Average Similarity Evaluator", "Calculates the Pearson R² and the average similarity of a symbolic regression solution candidate.")]
|
---|
33 | [StorableType("FE514989-E619-48B8-AC8E-9A2202708F65")]
|
---|
34 | public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
|
---|
35 | private const string StrictSimilarityParameterName = "StrictSimilarity";
|
---|
36 | private const string AverageSimilarityParameterName = "AverageSimilarity";
|
---|
37 |
|
---|
38 | private readonly object locker = new object();
|
---|
39 |
|
---|
40 | private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
|
---|
41 |
|
---|
42 | public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
|
---|
43 | get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ILookupParameter<DoubleValue> AverageSimilarityParameter {
|
---|
47 | get { return (ILookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public bool StrictSimilarity {
|
---|
51 | get { return StrictSimilarityParameter.Value.Value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | [StorableConstructor]
|
---|
55 | protected PearsonRSquaredAverageSimilarityEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
56 | protected PearsonRSquaredAverageSimilarityEvaluator(PearsonRSquaredAverageSimilarityEvaluator original, Cloner cloner)
|
---|
57 | : base(original, cloner) {
|
---|
58 | }
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new PearsonRSquaredAverageSimilarityEvaluator(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public PearsonRSquaredAverageSimilarityEvaluator() : base() {
|
---|
64 | Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
|
---|
65 | Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName));
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity
|
---|
69 |
|
---|
70 | public override IOperation InstrumentedApply() {
|
---|
71 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
72 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
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) {
|
---|
79 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
|
---|
80 | }
|
---|
81 |
|
---|
82 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling);
|
---|
83 |
|
---|
84 | if (DecimalPlaces >= 0)
|
---|
85 | r2 = Math.Round(r2, DecimalPlaces);
|
---|
86 |
|
---|
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);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
|
---|
95 |
|
---|
96 | QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
|
---|
97 | return base.InstrumentedApply();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
101 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
102 | AverageSimilarityParameter.ExecutionContext = context;
|
---|
103 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
104 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
105 |
|
---|
106 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
107 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
108 |
|
---|
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 |
|
---|
120 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
121 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
122 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
123 |
|
---|
124 | return new[] { r2, avgSimilarity };
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|