[3652] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3652] | 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 |
|
---|
[4068] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[3652] | 24 | using System.Linq;
|
---|
[4068] | 25 | using HeuristicLab.Analysis;
|
---|
[4722] | 26 | using HeuristicLab.Common;
|
---|
[3652] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[4068] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3652] | 30 | using HeuristicLab.Operators;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5863] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[4068] | 35 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
[3652] | 36 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
| 39 | /// <summary>
|
---|
| 40 | /// "An operator for analyzing the quality of symbolic regression solutions symbolic expression tree encoding."
|
---|
| 41 | /// </summary>
|
---|
[3681] | 42 | [Item("SymbolicRegressionModelQualityAnalyzer", "An operator for analyzing the quality of symbolic regression solutions symbolic expression tree encoding.")]
|
---|
[3652] | 43 | [StorableClass]
|
---|
[5863] | 44 | [NonDiscoverableType]
|
---|
[3996] | 45 | public sealed class SymbolicRegressionModelQualityAnalyzer : SingleSuccessorOperator, ISymbolicRegressionAnalyzer {
|
---|
[3652] | 46 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
| 47 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 48 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 49 | private const string ResultsParameterName = "Results";
|
---|
[3666] | 50 |
|
---|
[3710] | 51 | private const string TrainingMeanSquaredErrorQualityParameterName = "Mean squared error (training)";
|
---|
| 52 | private const string MinTrainingMeanSquaredErrorQualityParameterName = "Min mean squared error (training)";
|
---|
| 53 | private const string MaxTrainingMeanSquaredErrorQualityParameterName = "Max mean squared error (training)";
|
---|
| 54 | private const string AverageTrainingMeanSquaredErrorQualityParameterName = "Average mean squared error (training)";
|
---|
| 55 | private const string BestTrainingMeanSquaredErrorQualityParameterName = "Best mean squared error (training)";
|
---|
[3666] | 56 |
|
---|
[3710] | 57 | private const string TrainingAverageRelativeErrorQualityParameterName = "Average relative error (training)";
|
---|
| 58 | private const string MinTrainingAverageRelativeErrorQualityParameterName = "Min average relative error (training)";
|
---|
| 59 | private const string MaxTrainingAverageRelativeErrorQualityParameterName = "Max average relative error (training)";
|
---|
| 60 | private const string AverageTrainingAverageRelativeErrorQualityParameterName = "Average average relative error (training)";
|
---|
| 61 | private const string BestTrainingAverageRelativeErrorQualityParameterName = "Best average relative error (training)";
|
---|
[3666] | 62 |
|
---|
[3710] | 63 | private const string TrainingRSquaredQualityParameterName = "R² (training)";
|
---|
| 64 | private const string MinTrainingRSquaredQualityParameterName = "Min R² (training)";
|
---|
| 65 | private const string MaxTrainingRSquaredQualityParameterName = "Max R² (training)";
|
---|
| 66 | private const string AverageTrainingRSquaredQualityParameterName = "Average R² (training)";
|
---|
| 67 | private const string BestTrainingRSquaredQualityParameterName = "Best R² (training)";
|
---|
[3666] | 68 |
|
---|
[3710] | 69 | private const string TestMeanSquaredErrorQualityParameterName = "Mean squared error (test)";
|
---|
| 70 | private const string MinTestMeanSquaredErrorQualityParameterName = "Min mean squared error (test)";
|
---|
| 71 | private const string MaxTestMeanSquaredErrorQualityParameterName = "Max mean squared error (test)";
|
---|
| 72 | private const string AverageTestMeanSquaredErrorQualityParameterName = "Average mean squared error (test)";
|
---|
| 73 | private const string BestTestMeanSquaredErrorQualityParameterName = "Best mean squared error (test)";
|
---|
[3666] | 74 |
|
---|
[3710] | 75 | private const string TestAverageRelativeErrorQualityParameterName = "Average relative error (test)";
|
---|
| 76 | private const string MinTestAverageRelativeErrorQualityParameterName = "Min average relative error (test)";
|
---|
| 77 | private const string MaxTestAverageRelativeErrorQualityParameterName = "Max average relative error (test)";
|
---|
| 78 | private const string AverageTestAverageRelativeErrorQualityParameterName = "Average average relative error (test)";
|
---|
| 79 | private const string BestTestAverageRelativeErrorQualityParameterName = "Best average relative error (test)";
|
---|
[3666] | 80 |
|
---|
[3710] | 81 | private const string TestRSquaredQualityParameterName = "R² (test)";
|
---|
| 82 | private const string MinTestRSquaredQualityParameterName = "Min R² (test)";
|
---|
| 83 | private const string MaxTestRSquaredQualityParameterName = "Max R² (test)";
|
---|
| 84 | private const string AverageTestRSquaredQualityParameterName = "Average R² (test)";
|
---|
| 85 | private const string BestTestRSquaredQualityParameterName = "Best R² (test)";
|
---|
[3666] | 86 |
|
---|
[3710] | 87 | private const string RSquaredValuesParameterName = "R²";
|
---|
| 88 | private const string MeanSquaredErrorValuesParameterName = "Mean squared error";
|
---|
| 89 | private const string RelativeErrorValuesParameterName = "Average relative error";
|
---|
[3666] | 90 |
|
---|
[3652] | 91 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 92 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
| 93 |
|
---|
| 94 | #region parameter properties
|
---|
[3681] | 95 | public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 96 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[3652] | 97 | }
|
---|
[3681] | 98 | public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
| 99 | get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
[3652] | 100 | }
|
---|
[3681] | 101 | public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
|
---|
| 102 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
[3652] | 103 | }
|
---|
| 104 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
| 105 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
| 106 | }
|
---|
| 107 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
| 108 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
| 109 | }
|
---|
[3681] | 110 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 111 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 112 | }
|
---|
[3652] | 113 | #endregion
|
---|
[3996] | 114 | #region properties
|
---|
| 115 | public DoubleValue UpperEstimationLimit {
|
---|
| 116 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 117 | }
|
---|
| 118 | public DoubleValue LowerEstimationLimit {
|
---|
| 119 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 120 | }
|
---|
| 121 | #endregion
|
---|
[3652] | 122 |
|
---|
[4722] | 123 | [StorableConstructor]
|
---|
| 124 | private SymbolicRegressionModelQualityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 125 | private SymbolicRegressionModelQualityAnalyzer(SymbolicRegressionModelQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
[3681] | 126 | public SymbolicRegressionModelQualityAnalyzer()
|
---|
[3652] | 127 | : base() {
|
---|
[3659] | 128 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
[3681] | 129 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
|
---|
| 130 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data containing the input varaibles for the symbolic regression problem."));
|
---|
[3652] | 131 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees."));
|
---|
| 132 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees."));
|
---|
[3681] | 133 | Parameters.Add(new ValueLookupParameter<DataTable>(MeanSquaredErrorValuesParameterName, "The data table to collect mean squared error values."));
|
---|
| 134 | Parameters.Add(new ValueLookupParameter<DataTable>(RSquaredValuesParameterName, "The data table to collect R² correlation coefficient values."));
|
---|
| 135 | Parameters.Add(new ValueLookupParameter<DataTable>(RelativeErrorValuesParameterName, "The data table to collect relative error values."));
|
---|
| 136 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
|
---|
[3996] | 137 | }
|
---|
[3652] | 138 |
|
---|
[4722] | 139 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 140 | return new SymbolicRegressionModelQualityAnalyzer(this, cloner);
|
---|
| 141 | }
|
---|
[3681] | 142 |
|
---|
[3996] | 143 | public override IOperation Apply() {
|
---|
[5437] | 144 | double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity;
|
---|
| 145 | double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity;
|
---|
[3996] | 146 | Analyze(SymbolicExpressionTreeParameter.ActualValue, SymbolicExpressionTreeInterpreterParameter.ActualValue,
|
---|
[5437] | 147 | upperEstimationLimit, lowerEstimationLimit, ProblemDataParameter.ActualValue,
|
---|
[3996] | 148 | ResultsParameter.ActualValue);
|
---|
| 149 | return base.Apply();
|
---|
| 150 | }
|
---|
[3681] | 151 |
|
---|
[3996] | 152 | public static void Analyze(IEnumerable<SymbolicExpressionTree> trees, ISymbolicExpressionTreeInterpreter interpreter,
|
---|
| 153 | double upperEstimationLimit, double lowerEstimationLimit,
|
---|
[4468] | 154 | DataAnalysisProblemData problemData, ResultCollection results) {
|
---|
[3996] | 155 | int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
|
---|
[4468] | 156 | IEnumerable<double> originalTrainingValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, problemData.TrainingIndizes);
|
---|
| 157 | IEnumerable<double> originalTestValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, problemData.TestIndizes);
|
---|
[3996] | 158 | List<double> trainingMse = new List<double>();
|
---|
| 159 | List<double> trainingR2 = new List<double>();
|
---|
| 160 | List<double> trainingRelErr = new List<double>();
|
---|
| 161 | List<double> testMse = new List<double>();
|
---|
| 162 | List<double> testR2 = new List<double>();
|
---|
| 163 | List<double> testRelErr = new List<double>();
|
---|
[3652] | 164 |
|
---|
[3996] | 165 | OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
|
---|
| 166 | OnlineMeanAbsolutePercentageErrorEvaluator relErrEvaluator = new OnlineMeanAbsolutePercentageErrorEvaluator();
|
---|
| 167 | OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
|
---|
[3666] | 168 |
|
---|
[3996] | 169 | foreach (var tree in trees) {
|
---|
| 170 | #region training
|
---|
[4468] | 171 | var estimatedTrainingValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndizes);
|
---|
[3996] | 172 | mseEvaluator.Reset();
|
---|
| 173 | r2Evaluator.Reset();
|
---|
| 174 | relErrEvaluator.Reset();
|
---|
| 175 | var estimatedEnumerator = estimatedTrainingValues.GetEnumerator();
|
---|
| 176 | var originalEnumerator = originalTrainingValues.GetEnumerator();
|
---|
| 177 | while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) {
|
---|
| 178 | double estimated = estimatedEnumerator.Current;
|
---|
| 179 | if (double.IsNaN(estimated)) estimated = upperEstimationLimit;
|
---|
| 180 | else estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
|
---|
| 181 | mseEvaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 182 | r2Evaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 183 | relErrEvaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 184 | }
|
---|
| 185 | if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
|
---|
| 186 | throw new InvalidOperationException("Number of elements in estimated and original enumeration doesn't match.");
|
---|
| 187 | }
|
---|
| 188 | trainingMse.Add(mseEvaluator.MeanSquaredError);
|
---|
| 189 | trainingR2.Add(r2Evaluator.RSquared);
|
---|
| 190 | trainingRelErr.Add(relErrEvaluator.MeanAbsolutePercentageError);
|
---|
| 191 | #endregion
|
---|
| 192 | #region test
|
---|
[4468] | 193 | var estimatedTestValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TestIndizes);
|
---|
[3666] | 194 |
|
---|
[3996] | 195 | mseEvaluator.Reset();
|
---|
| 196 | r2Evaluator.Reset();
|
---|
| 197 | relErrEvaluator.Reset();
|
---|
| 198 | estimatedEnumerator = estimatedTestValues.GetEnumerator();
|
---|
| 199 | originalEnumerator = originalTestValues.GetEnumerator();
|
---|
| 200 | while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) {
|
---|
| 201 | double estimated = estimatedEnumerator.Current;
|
---|
| 202 | if (double.IsNaN(estimated)) estimated = upperEstimationLimit;
|
---|
| 203 | else estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
|
---|
| 204 | mseEvaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 205 | r2Evaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 206 | relErrEvaluator.Add(originalEnumerator.Current, estimated);
|
---|
| 207 | }
|
---|
| 208 | if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
|
---|
| 209 | throw new InvalidOperationException("Number of elements in estimated and original enumeration doesn't match.");
|
---|
| 210 | }
|
---|
| 211 | testMse.Add(mseEvaluator.MeanSquaredError);
|
---|
| 212 | testR2.Add(r2Evaluator.RSquared);
|
---|
| 213 | testRelErr.Add(relErrEvaluator.MeanAbsolutePercentageError);
|
---|
| 214 | #endregion
|
---|
| 215 | }
|
---|
[3710] | 216 |
|
---|
[3996] | 217 | AddResultTableValues(results, MeanSquaredErrorValuesParameterName, "mean squared error (training)", trainingMse.Min(), trainingMse.Average(), trainingMse.Max());
|
---|
| 218 | AddResultTableValues(results, MeanSquaredErrorValuesParameterName, "mean squared error (test)", testMse.Min(), testMse.Average(), testMse.Max());
|
---|
| 219 | AddResultTableValues(results, RelativeErrorValuesParameterName, "mean relative error (training)", trainingRelErr.Min(), trainingRelErr.Average(), trainingRelErr.Max());
|
---|
| 220 | AddResultTableValues(results, RelativeErrorValuesParameterName, "mean relative error (test)", testRelErr.Min(), testRelErr.Average(), testRelErr.Max());
|
---|
| 221 | AddResultTableValues(results, RSquaredValuesParameterName, "Pearson's R² (training)", trainingR2.Min(), trainingR2.Average(), trainingR2.Max());
|
---|
| 222 | AddResultTableValues(results, RSquaredValuesParameterName, "Pearson's R² (test)", testR2.Min(), testR2.Average(), testR2.Max());
|
---|
[3652] | 223 | }
|
---|
[3681] | 224 |
|
---|
[3996] | 225 | private static void AddResultTableValues(ResultCollection results, string tableName, string valueName, double minValue, double avgValue, double maxValue) {
|
---|
| 226 | if (!results.ContainsKey(tableName)) {
|
---|
| 227 | results.Add(new Result(tableName, new DataTable(tableName)));
|
---|
| 228 | }
|
---|
| 229 | DataTable table = (DataTable)results[tableName].Value;
|
---|
| 230 | AddValue(table, minValue, "Min. " + valueName, string.Empty);
|
---|
| 231 | AddValue(table, avgValue, "Avg. " + valueName, string.Empty);
|
---|
| 232 | AddValue(table, maxValue, "Max. " + valueName, string.Empty);
|
---|
[3681] | 233 | }
|
---|
| 234 |
|
---|
[3996] | 235 | private static void AddValue(DataTable table, double data, string name, string description) {
|
---|
| 236 | DataRow row;
|
---|
| 237 | table.Rows.TryGetValue(name, out row);
|
---|
| 238 | if (row == null) {
|
---|
| 239 | row = new DataRow(name, description);
|
---|
| 240 | row.Values.Add(data);
|
---|
| 241 | table.Rows.Add(row);
|
---|
| 242 | } else {
|
---|
| 243 | row.Values.Add(data);
|
---|
| 244 | }
|
---|
[3681] | 245 | }
|
---|
| 246 |
|
---|
[3996] | 247 |
|
---|
| 248 | private static void SetResultValue(ResultCollection results, string name, double value) {
|
---|
| 249 | if (results.ContainsKey(name))
|
---|
| 250 | results[name].Value = new DoubleValue(value);
|
---|
| 251 | else
|
---|
| 252 | results.Add(new Result(name, new DoubleValue(value)));
|
---|
[3681] | 253 | }
|
---|
[3652] | 254 | }
|
---|
| 255 | }
|
---|