Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented base classes for variable impact analysis and implemented specific operators for GP. #644 (Variable impact of CEDMA models should be calculated and stored in the result DB)

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    public override string Description {
34      get { return @"Calculates the impact of all allowed input variables on the model."; }
35    }
36
37    public abstract string OutputVariableName { get; }
38
39    public VariableImpactCalculatorBase()
40      : base() {
41      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
42      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList<IntData>), VariableKind.In));
44      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo(OutputVariableName, OutputVariableName, typeof(ItemList), VariableKind.New));
47    }
48
49    public override IOperation Apply(IScope scope) {
50      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);
51      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
52      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
53      Dataset dirtyDataset = (Dataset)dataset.Clone();
54      int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
55      int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
56
57      T referenceValue = CalculateValue(scope, dataset, targetVariable, start, end);
58      double[] impacts = new double[allowedFeatures.Count];
59
60      for (int i = 0; i < allowedFeatures.Count; i++) {
61        int currentVariable = allowedFeatures[i].Data;
62        var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable , CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end);
63        T newValue = CalculateValue(scope, dirtyDataset, targetVariable, start, end);
64        impacts[i] = CalculateImpact(referenceValue, newValue);
65        ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end);
66      }
67
68      impacts = PostProcessImpacts(impacts);
69
70      ItemList variableImpacts = new ItemList();
71      for (int i = 0; i < allowedFeatures.Count; i++) {
72        int currentVariable = allowedFeatures[i].Data;
73        ItemList row = new ItemList();
74        row.Add(new StringData(dataset.GetVariableName(currentVariable)));
75        row.Add(new DoubleData(impacts[i]));
76        variableImpacts.Add(row);
77      }
78
79      scope.AddVariable(new Variable(scope.TranslateName(OutputVariableName), variableImpacts));
80      return null;
81    }
82
83    protected abstract T CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end);
84
85    protected abstract double CalculateImpact(T referenceValue, T newValue);
86
87    protected virtual double[] PostProcessImpacts(double[] impacts) {
88      return impacts;
89    }
90
91    private IEnumerable<double> ReplaceVariableValues(Dataset ds, int variableIndex, IEnumerable<double> newValues, int start, int end) {
92      double[] oldValues = new double[end - start];
93      for (int i = 0; i < end - start; i++) oldValues[i] = ds.GetValue(i + start, variableIndex);
94      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)");
95
96      int index = start;
97      ds.FireChangeEvents = false;
98      foreach(double v in newValues) {
99        ds.SetValue(index++, variableIndex, v);
100      }
101      ds.FireChangeEvents = true;
102      ds.FireChanged();
103      return oldValues;
104    }
105
106    private IEnumerable<double> CalculateNewValues(Dataset ds, int variableIndex, int start, int end) {
107      double mean = ds.GetMean(variableIndex, start, end);
108      return Enumerable.Repeat(mean, end - start);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.