1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using System.Collections;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Analysis;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
35 |
|
---|
36 | public class SymbolicDataAnalysisVariableImpactAnalyzer<T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
|
---|
37 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
38 | where U : class, IDataAnalysisProblemData {
|
---|
39 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
40 | private const string VariableImpactsDataTableResultName = "Variable Impacts";
|
---|
41 |
|
---|
42 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
43 | get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected SymbolicDataAnalysisVariableImpactAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
48 | protected SymbolicDataAnalysisVariableImpactAnalyzer(SymbolicDataAnalysisVariableImpactAnalyzer<T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisVariableImpactAnalyzer<T,U>(this, cloner); }
|
---|
50 |
|
---|
51 | public SymbolicDataAnalysisVariableImpactAnalyzer() :base(){
|
---|
52 | Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IOperation Apply() {
|
---|
56 | var rows = GenerateRowsToEvaluate().ToArray();
|
---|
57 | if (!rows.Any()) throw new ArgumentException();
|
---|
58 |
|
---|
59 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
60 | var problemData = ProblemDataParameter.ActualValue;
|
---|
61 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
62 | var trees = SymbolicExpressionTreeParameter.ActualValue.ToArray();
|
---|
63 |
|
---|
64 | var originalTreeEvaluations = trees.Select(t => interpreter.GetSymbolicExpressionTreeValues(t, problemData.Dataset, rows).LimitToRange(estimationLimits.Lower,estimationLimits.Upper).ToArray()).ToArray();
|
---|
65 |
|
---|
66 | List<IList> variableValues = new List<IList>();
|
---|
67 | foreach(var variable in problemData.AllowedInputVariables) {
|
---|
68 | variableValues.Add(problemData.Dataset.GetDoubleValues(variable,rows).ToList());
|
---|
69 | }
|
---|
70 | Dictionary<string, List<double>> variableImpacts = new Dictionary<string, List<double>>();
|
---|
71 |
|
---|
72 | //calculation of variable impacts per tree
|
---|
73 | for(int i=0; i < problemData.AllowedInputVariables.Count(); i++) {
|
---|
74 | var variableName = problemData.AllowedInputVariables.ElementAt(i);
|
---|
75 | var variableOrginalValues = variableValues[i];
|
---|
76 | var variableReplacedValues = CalculateReplacementValues(problemData, variableName, rows, rows.Length).ToList();
|
---|
77 |
|
---|
78 | variableValues[i] = variableReplacedValues;
|
---|
79 | var modifiedDataset = new Dataset(problemData.AllowedInputVariables, variableValues);
|
---|
80 |
|
---|
81 | for(int t =0; t< trees.Length; t++) {
|
---|
82 | var tree = trees[t];
|
---|
83 |
|
---|
84 | var treeEvaluation = interpreter.GetSymbolicExpressionTreeValues(tree, modifiedDataset,Enumerable.Range(0,rows.Length)).LimitToRange(estimationLimits.Lower,estimationLimits.Upper);
|
---|
85 | OnlineCalculatorError error;
|
---|
86 | var regressionProblemData = (IRegressionProblemData) problemData;
|
---|
87 | var modifiedR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTreeEvaluations[t], treeEvaluation, out error);
|
---|
88 | //var modifiedR2 = OnlinePearsonsRSquaredCalculator.Calculate(problemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable,rows), treeEvaluation, out error);
|
---|
89 | //var originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(problemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows), originalTreeEvaluations[t], out error);
|
---|
90 |
|
---|
91 | if (error != OnlineCalculatorError.None) modifiedR2 = 0.0;
|
---|
92 |
|
---|
93 | if (!variableImpacts.ContainsKey(variableName)) variableImpacts[variableName] = new List<double>();
|
---|
94 | variableImpacts[variableName].Add(1 - modifiedR2);
|
---|
95 | }
|
---|
96 | variableValues[i] = variableOrginalValues;
|
---|
97 | }
|
---|
98 |
|
---|
99 | //create data table and store average impacts
|
---|
100 |
|
---|
101 | var results = ResultCollectionParameter.ActualValue;
|
---|
102 | if (!results.ContainsKey(VariableImpactsDataTableResultName)) {
|
---|
103 | var dataTableResult = new DataTable("Variable Impacts", "TODO");
|
---|
104 | foreach(var variableName in variableImpacts.Keys)
|
---|
105 | dataTableResult.Rows.Add(new DataRow(variableName));
|
---|
106 |
|
---|
107 | results.Add(new Result("Variable Impacts",dataTableResult));
|
---|
108 | }
|
---|
109 |
|
---|
110 | var dataTable = (DataTable)results[VariableImpactsDataTableResultName].Value;
|
---|
111 |
|
---|
112 | foreach (var pair in variableImpacts) {
|
---|
113 | dataTable.Rows[pair.Key].Values.Add(pair.Value.Average());
|
---|
114 | }
|
---|
115 |
|
---|
116 | return base.Apply();
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected IEnumerable<double> CalculateReplacementValues(U problemData, string variableName, IEnumerable<int> rows, int rowCount) {
|
---|
120 | var mean = problemData.Dataset.GetDoubleValues(variableName, problemData.TrainingIndices).Average();
|
---|
121 | return Enumerable.Repeat(mean, rowCount);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|