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 tree = 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 (UseParameterOptimization) {
|
---|
79 | SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
|
---|
80 | }
|
---|
81 |
|
---|
82 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
|
---|
83 | tree, problemData, rows, interpreter, applyLinearScaling,
|
---|
84 | estimationLimits.Lower, estimationLimits.Upper);
|
---|
85 |
|
---|
86 | if (DecimalPlaces >= 0)
|
---|
87 | r2 = Math.Round(r2, DecimalPlaces);
|
---|
88 |
|
---|
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);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
|
---|
97 |
|
---|
98 | QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity });
|
---|
99 | return base.InstrumentedApply();
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
103 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
104 | AverageSimilarityParameter.ExecutionContext = context;
|
---|
105 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
106 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
107 |
|
---|
108 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
109 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
110 |
|
---|
111 | double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
|
---|
112 | tree, problemData, rows,
|
---|
113 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
114 | applyLinearScaling,
|
---|
115 | estimationLimits.Lower, estimationLimits.Upper);
|
---|
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 |
|
---|
126 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
127 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
128 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
129 |
|
---|
130 | return new[] { r2, avgSimilarity };
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|