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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
36 | using HeuristicLab.Analysis;
|
---|
37 |
|
---|
38 | namespace 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 VariableImpacts {
|
---|
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 (VariableImpacts == null) {
|
---|
83 | VariableImpacts = 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 | VariableImpacts.Rows.Add(new DataRow(inputVariable));
|
---|
87 | results.Add(new Result("Variable Frequencies", VariableImpacts));
|
---|
88 | }
|
---|
89 | foreach (var pair in VariableFrequencyAnalyser.CalculateVariableFrequencies(expressions, inputVariables)) {
|
---|
90 | VariableImpacts.Rows[pair.Key].Values.Add(pair.Value);
|
---|
91 | results["Variable Frequencies"].Value = VariableImpacts;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return base.Apply();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|