Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418: Added NonDiscoverableType attribute to outdated analyzers.

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