Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418: Added NonDiscoverableType attribute to outdated analyzers.

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