Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/BestSymbolicRegressionSolutionAnalyzer.cs @ 3925

Last change on this file since 3925 was 3925, checked in by mkommend, 14 years ago

added scaling for variable impacts (ticket #938)

File size: 7.2 KB
Line 
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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using System.Collections.Generic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Problems.DataAnalysis.Evaluators;
37using HeuristicLab.Analysis;
38
39namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
40  [Item("BestSymbolicRegressionSolutionAnalyzer", "An operator for analyzing the best solution of symbolic regression problems given in symbolic expression tree encoding.")]
41  [StorableClass]
42  public sealed class BestSymbolicRegressionSolutionAnalyzer : RegressionSolutionAnalyzer, ISymbolicRegressionAnalyzer {
43    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
44    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
45    private const string BestSolutionInputvariableCountResultName = "Variables used by best solution";
46    private const string VariableFrequenciesParameterName = "VariableFrequencies";
47    private const string VariableImpactsResultName = "Integrated variable frequencies";
48    private const string BestSolutionParameterName = "BestSolution";
49
50    #region parameter properties
51    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
53    }
54    public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
55      get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
56    }
57    public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
58      get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
59    }
60    public ILookupParameter<DataTable> VariableFrequenciesParameter {
61      get { return (ILookupParameter<DataTable>)Parameters[VariableFrequenciesParameterName]; }
62    }
63    #endregion
64    #region properties
65    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
66      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
67    }
68    public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {
69      get { return SymbolicExpressionTreeParameter.ActualValue; }
70    }
71    public DataTable VariableFrequencies {
72      get { return VariableFrequenciesParameter.ActualValue; }
73    }
74    #endregion
75
76    public BestSymbolicRegressionSolutionAnalyzer()
77      : base() {
78      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
79      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
80      Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
81      Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void Initialize() {
86      if (!Parameters.ContainsKey(VariableFrequenciesParameterName)) {
87        Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
88      }
89    }
90
91    protected override DataAnalysisSolution UpdateBestSolution() {
92      double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity;
93      double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity;
94
95      int i = Quality.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
96
97      if (BestSolutionQualityParameter.ActualValue == null || BestSolutionQualityParameter.ActualValue.Value > Quality[i].Value) {
98        var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
99          SymbolicExpressionTree[i]);
100        var solution = new SymbolicRegressionSolution(ProblemData, model, lowerEstimationLimit, upperEstimationLimit);
101
102        BestSolutionParameter.ActualValue = solution;
103        BestSolutionQualityParameter.ActualValue = Quality[i];
104
105        if (Results.ContainsKey(BestSolutionInputvariableCountResultName)) {
106          Results[BestSolutionInputvariableCountResultName].Value = new IntValue(model.InputVariables.Count());
107          Results[VariableImpactsResultName].Value = CalculateVariableImpacts();
108        } else {
109          Results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(model.InputVariables.Count())));
110          Results.Add(new Result(VariableImpactsResultName, CalculateVariableImpacts()));
111        }
112      }
113      return BestSolutionParameter.ActualValue;
114    }
115
116    private DoubleMatrix CalculateVariableImpacts() {
117      if (VariableFrequencies != null) {
118        var impacts = new DoubleMatrix(VariableFrequencies.Rows.Count, 1, new string[] { "Impact" }, VariableFrequencies.Rows.Select(x => x.Name));
119        impacts.SortableView = true;
120        int rowIndex = 0;
121        foreach (var dataRow in VariableFrequencies.Rows) {
122          string variableName = dataRow.Name;
123          double integral = 0;
124          if (dataRow.Values.Count > 1) {
125            double baseline = dataRow.Values.First();
126            integral = (from value in dataRow.Values
127                        select value - baseline)
128                              .Sum();
129            integral /= dataRow.Values.Count;
130          }
131          impacts[rowIndex++, 0] = integral;
132        }
133        return impacts;
134      } else return new DoubleMatrix(1, 1);
135    }
136
137  }
138}
Note: See TracBrowser for help on using the repository browser.