[3651] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Linq;
|
---|
[4068] | 23 | using HeuristicLab.Analysis;
|
---|
[3651] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3651] | 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
[3681] | 33 | [Item("BestSymbolicRegressionSolutionAnalyzer", "An operator for analyzing the best solution of symbolic regression problems given in symbolic expression tree encoding.")]
|
---|
[3651] | 34 | [StorableClass]
|
---|
[3892] | 35 | public sealed class BestSymbolicRegressionSolutionAnalyzer : RegressionSolutionAnalyzer, ISymbolicRegressionAnalyzer {
|
---|
[3651] | 36 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 37 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
[3892] | 38 | private const string BestSolutionInputvariableCountResultName = "Variables used by best solution";
|
---|
[3905] | 39 | private const string VariableFrequenciesParameterName = "VariableFrequencies";
|
---|
| 40 | private const string VariableImpactsResultName = "Integrated variable frequencies";
|
---|
[3651] | 41 | private const string BestSolutionParameterName = "BestSolution";
|
---|
[4195] | 42 | private const string BestSolutionComplexity = "Best solution complexity";
|
---|
[3651] | 43 |
|
---|
[3892] | 44 | #region parameter properties
|
---|
[3681] | 45 | public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 46 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[3651] | 47 | }
|
---|
[3681] | 48 | public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
| 49 | get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
[3651] | 50 | }
|
---|
| 51 | public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
|
---|
| 52 | get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
|
---|
| 53 | }
|
---|
[3905] | 54 | public ILookupParameter<DataTable> VariableFrequenciesParameter {
|
---|
| 55 | get { return (ILookupParameter<DataTable>)Parameters[VariableFrequenciesParameterName]; }
|
---|
| 56 | }
|
---|
[3892] | 57 | #endregion
|
---|
| 58 | #region properties
|
---|
| 59 | public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
| 60 | get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
|
---|
[3651] | 61 | }
|
---|
[3892] | 62 | public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {
|
---|
| 63 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
[3651] | 64 | }
|
---|
[3905] | 65 | public DataTable VariableFrequencies {
|
---|
| 66 | get { return VariableFrequenciesParameter.ActualValue; }
|
---|
| 67 | }
|
---|
[3892] | 68 | #endregion
|
---|
[3651] | 69 |
|
---|
[3681] | 70 | public BestSymbolicRegressionSolutionAnalyzer()
|
---|
[3651] | 71 | : base() {
|
---|
[3659] | 72 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
[3681] | 73 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
|
---|
[3905] | 74 | Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
|
---|
[3651] | 75 | Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[3905] | 78 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 79 | private void Initialize() {
|
---|
| 80 | if (!Parameters.ContainsKey(VariableFrequenciesParameterName)) {
|
---|
| 81 | Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3892] | 85 | protected override DataAnalysisSolution UpdateBestSolution() {
|
---|
| 86 | double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity;
|
---|
| 87 | double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity;
|
---|
[3651] | 88 |
|
---|
[3892] | 89 | int i = Quality.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
[3651] | 90 |
|
---|
[3892] | 91 | if (BestSolutionQualityParameter.ActualValue == null || BestSolutionQualityParameter.ActualValue.Value > Quality[i].Value) {
|
---|
| 92 | var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
|
---|
[3915] | 93 | SymbolicExpressionTree[i]);
|
---|
[4475] | 94 | var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit);
|
---|
[3996] | 95 | solution.Name = BestSolutionParameterName;
|
---|
| 96 | solution.Description = "Best solution on validation partition found over the whole run.";
|
---|
[3651] | 97 | BestSolutionParameter.ActualValue = solution;
|
---|
[3892] | 98 | BestSolutionQualityParameter.ActualValue = Quality[i];
|
---|
[3996] | 99 | BestSymbolicRegressionSolutionAnalyzer.UpdateSymbolicRegressionBestSolutionResults(solution, ProblemData, Results, VariableFrequencies);
|
---|
[3651] | 100 | }
|
---|
[3892] | 101 | return BestSolutionParameter.ActualValue;
|
---|
[3651] | 102 | }
|
---|
| 103 |
|
---|
[3996] | 104 | public static void UpdateBestSolutionResults(SymbolicRegressionSolution bestSolution, DataAnalysisProblemData problemData, ResultCollection results, IntValue currentGeneration, DataTable variableFrequencies) {
|
---|
| 105 | RegressionSolutionAnalyzer.UpdateBestSolutionResults(bestSolution, problemData, results, currentGeneration);
|
---|
| 106 | UpdateSymbolicRegressionBestSolutionResults(bestSolution, problemData, results, variableFrequencies);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private static void UpdateSymbolicRegressionBestSolutionResults(SymbolicRegressionSolution bestSolution, DataAnalysisProblemData problemData, ResultCollection results, DataTable variableFrequencies) {
|
---|
| 110 | if (results.ContainsKey(BestSolutionInputvariableCountResultName)) {
|
---|
| 111 | results[BestSolutionInputvariableCountResultName].Value = new IntValue(bestSolution.Model.InputVariables.Count());
|
---|
| 112 | results[VariableImpactsResultName].Value = CalculateVariableImpacts(variableFrequencies);
|
---|
[4195] | 113 | var sizeTable = (DataTable)results[BestSolutionComplexity].Value;
|
---|
| 114 | sizeTable.Rows["Best solution size"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Size);
|
---|
| 115 | sizeTable.Rows["Best solution height"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Height);
|
---|
| 116 | sizeTable.Rows["Best solution variables"].Values.Add(bestSolution.Model.InputVariables.Count());
|
---|
[3996] | 117 | } else {
|
---|
| 118 | results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(bestSolution.Model.InputVariables.Count())));
|
---|
| 119 | results.Add(new Result(VariableImpactsResultName, CalculateVariableImpacts(variableFrequencies)));
|
---|
[4195] | 120 | var sizeTable = new DataTable("Best solution complexity");
|
---|
| 121 | sizeTable.Rows.Add(new DataRow("Best solution size"));
|
---|
| 122 | sizeTable.Rows.Add(new DataRow("Best solution height"));
|
---|
| 123 | sizeTable.Rows.Add(new DataRow("Best solution variables"));
|
---|
| 124 | sizeTable.Rows["Best solution size"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Size);
|
---|
| 125 | sizeTable.Rows["Best solution height"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Height);
|
---|
| 126 | sizeTable.Rows["Best solution variables"].Values.Add(bestSolution.Model.InputVariables.Count());
|
---|
| 127 | results.Add(new Result(BestSolutionComplexity, sizeTable));
|
---|
[3996] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | private static DoubleMatrix CalculateVariableImpacts(DataTable variableFrequencies) {
|
---|
| 133 | if (variableFrequencies != null) {
|
---|
| 134 | var impacts = new DoubleMatrix(variableFrequencies.Rows.Count, 1, new string[] { "Impact" }, variableFrequencies.Rows.Select(x => x.Name));
|
---|
[3922] | 135 | impacts.SortableView = true;
|
---|
[3905] | 136 | int rowIndex = 0;
|
---|
[3996] | 137 | foreach (var dataRow in variableFrequencies.Rows) {
|
---|
[3905] | 138 | string variableName = dataRow.Name;
|
---|
[4125] | 139 | impacts[rowIndex++, 0] = dataRow.Values.Average();
|
---|
[3905] | 140 | }
|
---|
| 141 | return impacts;
|
---|
| 142 | } else return new DoubleMatrix(1, 1);
|
---|
| 143 | }
|
---|
[3651] | 144 | }
|
---|
| 145 | }
|
---|