Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5593 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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