Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionVariableFrequencyAnalyzer.cs @ 3922

Last change on this file since 3922 was 3905, checked in by gkronber, 14 years ago

Implemented calculation of variable impacts as integral over relative variable frequencies. #1036

File size: 5.0 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.Analysis;
37
38namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
39  [Item("SymbolicRegressionVariableFrequencyAnalyzer", "An operator for analyzing the variable frequencies of symbolic regression solutions given in symbolic expression tree encoding.")]
40  [StorableClass]
41  public sealed class SymbolicRegressionVariableFrequencyAnalyzer : SingleSuccessorOperator, ISymbolicRegressionAnalyzer {
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43    private const string ProblemDataParameterName = "ProblemData";
44    private const string VariableFrequenciesParameterName = "VariableFrequencies";
45    private const string ResultsParameterName = "Results";
46
47    #region parameter properties
48    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
49      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
50    }
51    public ILookupParameter<DataTable> VariableFrequenciesParameter {
52      get { return (ILookupParameter<DataTable>)Parameters[VariableFrequenciesParameterName]; }
53    }
54    public ILookupParameter<DataAnalysisProblemData> ProblemDataParameter {
55      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
56    }
57    public ILookupParameter<ResultCollection> ResultsParameter {
58      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
59    }
60    #endregion
61    #region properties
62    public DataTable VariableFrequencies {
63      get { return VariableFrequenciesParameter.ActualValue; }
64      set { VariableFrequenciesParameter.ActualValue = value; }
65    }
66    #endregion
67
68    public SymbolicRegressionVariableFrequencyAnalyzer()
69      : base() {
70      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
71      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data containing the input varaibles for the symbolic regression problem."));
72      Parameters.Add(new ValueLookupParameter<DataTable>(VariableFrequenciesParameterName, "The data table to store the variable frequencies."));
73      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
74    }
75
76    public override IOperation Apply() {
77      ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
78      DataAnalysisProblemData problemData = ProblemDataParameter.ActualValue;
79      var inputVariables = problemData.InputVariables.Select(x => x.Value);
80      ResultCollection results = ResultsParameter.ActualValue;
81
82      if (VariableFrequencies == null) {
83        VariableFrequencies = new DataTable("Variable frequencies", "Relative frequency of variable references aggregated over the whole population.");
84        // add a data row for each input variable
85        foreach (var inputVariable in inputVariables)
86          VariableFrequencies.Rows.Add(new DataRow(inputVariable));
87        results.Add(new Result("Variable frequencies", VariableFrequencies));
88      }
89      foreach (var pair in VariableFrequencyAnalyser.CalculateVariableFrequencies(expressions, inputVariables)) {
90        VariableFrequencies.Rows[pair.Key].Values.Add(pair.Value);
91        results["Variable frequencies"].Value = VariableFrequencies;
92      }
93
94      return base.Apply();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.