Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5404 was 4973, checked in by gkronber, 14 years ago

Cosmetic changes. #1246

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