Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/VariableImpactCalculatorBase.cs @ 2182

Last change on this file since 2182 was 2165, checked in by gkronber, 15 years ago

Removed variable AllowedFeatures in all modeling algorithms. #709

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using System.Linq;
30
31namespace HeuristicLab.Modeling {
32  public abstract class VariableImpactCalculatorBase<T> : OperatorBase {
33    private bool abortRequested = false;
34
35    public override string Description {
36      get { return @"Calculates the impact of all input variables on the model."; }
37    }
38
39    public abstract string OutputVariableName { get; }
40
41    public override void Abort() {
42      abortRequested = true;
43    }
44
45    public VariableImpactCalculatorBase()
46      : base() {
47      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
48      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
49      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In));
51      AddVariableInfo(new VariableInfo(OutputVariableName, OutputVariableName, typeof(ItemList), VariableKind.New));
52    }
53
54    public override IOperation Apply(IScope scope) {
55      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
56      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
57      Dataset dirtyDataset = (Dataset)dataset.Clone();
58      int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
59      int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
60
61      T referenceValue = CalculateValue(scope, dataset, targetVariable, start, end);
62      double[] impacts = new double[dataset.Columns];
63
64      for (int i = 0; i < impacts.Length && !abortRequested; i++) {
65        int currentVariable = i;
66        if (currentVariable != targetVariable) {
67          var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable, CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end);
68          T newValue = CalculateValue(scope, dirtyDataset, targetVariable, start, end);
69          impacts[i] = CalculateImpact(referenceValue, newValue);
70          ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end);
71        }
72      }
73
74      if (!abortRequested) {
75        impacts = PostProcessImpacts(impacts);
76
77        ItemList variableImpacts = new ItemList();
78        for (int i = 0; i < impacts.Length; i++) {
79          int currentVariable = i;
80          if (currentVariable != targetVariable) {
81            ItemList row = new ItemList();
82            row.Add(new StringData(dataset.GetVariableName(currentVariable)));
83            row.Add(new DoubleData(impacts[i]));
84            variableImpacts.Add(row);
85          }
86        }
87
88        scope.AddVariable(new Variable(scope.TranslateName(OutputVariableName), variableImpacts));
89        return null;
90      } else {
91        return new AtomicOperation(this, scope);
92      }
93    }
94
95    protected abstract T CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end);
96
97    protected abstract double CalculateImpact(T referenceValue, T newValue);
98
99    protected virtual double[] PostProcessImpacts(double[] impacts) {
100      return impacts;
101    }
102
103    private IEnumerable<double> ReplaceVariableValues(Dataset ds, int variableIndex, IEnumerable<double> newValues, int start, int end) {
104      double[] oldValues = new double[end - start];
105      for (int i = 0; i < end - start; i++) oldValues[i] = ds.GetValue(i + start, variableIndex);
106      if (newValues.Count() != end - start) throw new ArgumentException("The length of the new values sequence doesn't match the required length (number of replaced values)");
107
108      int index = start;
109      ds.FireChangeEvents = false;
110      foreach (double v in newValues) {
111        ds.SetValue(index++, variableIndex, v);
112      }
113      ds.FireChangeEvents = true;
114      ds.FireChanged();
115      return oldValues;
116    }
117
118    private IEnumerable<double> CalculateNewValues(Dataset ds, int variableIndex, int start, int end) {
119      double mean = ds.GetMean(variableIndex, start, end);
120      return Enumerable.Repeat(mean, end - start);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.