Free cookie consent management tool by TermsFeed Policy Generator

Ticket #728: VariableImpactCalculators.patch

File VariableImpactCalculators.patch, 57.1 KB (added by mkommend, 15 years ago)
  • HeuristicLab.DataAnalysis/3.2/Dataset.cs

     
    3030
    3131namespace HeuristicLab.DataAnalysis {
    3232  public sealed class Dataset : ItemBase {
    33 
    34     private string name;
    35     private double[] samples;
    36     private int rows;
    37     private int columns;
    3833    private Dictionary<int, Dictionary<int, double>>[] cachedMeans;
    3934    private Dictionary<int, Dictionary<int, double>>[] cachedRanges;
    40     private double[] scalingFactor;
    41     private double[] scalingOffset;
    4235    private bool cachedValuesInvalidated = true;
     36   
     37    public Dataset()
     38      : this(new double[,] { { 0.0 } }) {
     39    }
    4340
    44     private bool fireChangeEvents = true;
    45     public bool FireChangeEvents {
    46       get { return fireChangeEvents; }
    47       set { fireChangeEvents = value; }
     41    public Dataset(double[,] samples) {
     42      Name = "-";
     43      Rows = samples.GetLength(0);
     44      Columns = samples.GetLength(1);
     45      double[] values = new double[Rows * Columns];
     46      int i = 0;
     47      for (int row = 0; row < Rows; row++) {
     48        for (int column = 0; column < columns; column++) {
     49          values[i++] = samples[row, column];
     50        }
     51      }
     52      Samples = values;
     53      fireChangeEvents = true;
    4854    }
    4955
     56    #region Properties
     57    private string name;
    5058    public string Name {
    5159      get { return name; }
    5260      set { name = value; }
    5361    }
    5462
     63    private int rows;
    5564    public int Rows {
    5665      get { return rows; }
    5766      set { rows = value; }
    5867    }
    5968
     69    private int columns;
    6070    public int Columns {
    6171      get { return columns; }
    6272      set {
     
    6777      }
    6878    }
    6979
    70     public double[] ScalingFactor {
    71       get { return scalingFactor; }
    72       set {
    73         if (value.Length != scalingFactor.Length)
    74           throw new ArgumentException("Length of scaling factor array doesn't match number of variables");
    75         scalingFactor = value;
    76       }
     80    private string[] variableNames;
     81    public IEnumerable<string> VariableNames {
     82      get { return variableNames; }
    7783    }
    78     public double[] ScalingOffset {
    79       get { return scalingOffset; }
    80       set {
    81         if (value.Length != scalingOffset.Length)
    82           throw new ArgumentException("Length of scaling offset array doesn't match number of variables");
    83         scalingOffset = value;
    84       }
    85     }
    8684
    87     public double GetValue(int i, int j) {
    88       return samples[columns * i + j];
    89     }
    90 
    91     public void SetValue(int i, int j, double v) {
    92       if (v != samples[columns * i + j]) {
    93         samples[columns * i + j] = v;
    94         cachedValuesInvalidated = true;
    95         if (fireChangeEvents) FireChanged();
    96       }
    97     }
    98 
     85    private double[] samples;
    9986    public double[] Samples {
    10087      get { return samples; }
    10188      set {
     
    11299      }
    113100    }
    114101
    115     private string[] variableNames;
    116     public IEnumerable<string> VariableNames {
    117       get { return variableNames; }
     102    private bool fireChangeEvents = true;
     103    public bool FireChangeEvents {
     104      get { return fireChangeEvents; }
     105      set { fireChangeEvents = value; }
    118106    }
    119107
    120     public Dataset()
    121       : this(new double[,] { { 0.0 } }) {
     108    private double[] scalingFactor;
     109    public double[] ScalingFactor {
     110      get { return scalingFactor; }
     111      set {
     112        if (value.Length != scalingFactor.Length)
     113          throw new ArgumentException("Length of scaling factor array doesn't match number of variables");
     114        scalingFactor = value;
     115      }
    122116    }
    123117
    124     public Dataset(double[,] samples) {
    125       Name = "-";
    126       Rows = samples.GetLength(0);
    127       Columns = samples.GetLength(1);
    128       double[] values = new double[Rows * Columns];
    129       int i = 0;
    130       for (int row = 0; row < Rows; row++) {
    131         for (int column = 0; column < columns; column++) {
    132           values[i++] = samples[row, column];
    133         }
     118    private double[] scalingOffset;
     119    public double[] ScalingOffset {
     120      get { return scalingOffset; }
     121      set {
     122        if (value.Length != scalingOffset.Length)
     123          throw new ArgumentException("Length of scaling offset array doesn't match number of variables");
     124        scalingOffset = value;
    134125      }
    135       Samples = values;
    136       fireChangeEvents = true;
    137126    }
     127    #endregion
    138128
    139 
    140     public string GetVariableName(int variableIndex) {
    141       return variableNames[variableIndex];
     129    #region Modify and get values
     130    public double GetValue(int i, int j) {
     131      return samples[columns * i + j];
    142132    }
    143133
    144     public int GetVariableIndex(string variableName) {
    145       for (int i = 0; i < variableNames.Length; i++) {
    146         if (variableNames[i].Equals(variableName)) return i;
    147       }
    148       throw new ArgumentException("The variable name " + variableName + " was not found.");
    149     }
    150 
    151134    public double[] GetVariableValues(int variableIndex, int start, int end) {
    152135      if (start < 0 || !(start <= end))
    153136        throw new ArgumentException("Start must be between 0 and end (" + end + ").");
     
    172155      return GetVariableValues(variableName, 0, this.rows);
    173156    }
    174157
     158    public void SetValue(int i, int j, double v) {
     159      if (v != samples[columns * i + j]) {
     160        samples[columns * i + j] = v;
     161        cachedValuesInvalidated = true;
     162        if (fireChangeEvents) FireChanged();
     163      }
     164    }
     165
     166    public IEnumerable<double> ReplaceVariableValues(int variableIndex, IEnumerable<double> newValues, int start, int end) {
     167      double[] oldValues = new double[end - start];
     168      for (int i = 0; i < end - start; i++) oldValues[i] = this.GetValue(i + start, variableIndex);
     169      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)");
     170
     171      int index = start;
     172      this.FireChangeEvents = false;
     173      foreach (double v in newValues) {
     174        this.SetValue(index++, variableIndex, v);
     175      }
     176      this.FireChangeEvents = true;
     177      this.FireChanged();
     178      return oldValues;
     179    }
     180
     181    public IEnumerable<double> ReplaceVariableValues(string variableName, IEnumerable<double> newValues, int start, int end) {
     182      return ReplaceVariableValues(this.GetVariableIndex(variableName), newValues, start, end);
     183    }
     184    #endregion
     185
     186    #region Variable name methods
     187    public string GetVariableName(int variableIndex) {
     188      return variableNames[variableIndex];
     189    }
     190
     191    public int GetVariableIndex(string variableName) {
     192      for (int i = 0; i < variableNames.Length; i++) {
     193        if (variableNames[i].Equals(variableName)) return i;
     194      }
     195      throw new ArgumentException("The variable name " + variableName + " was not found.");
     196    }
     197
    175198    public void SetVariableName(int variableIndex, string name) {
    176199      variableNames[variableIndex] = name;
    177200    }
     
    179202    public bool ContainsVariableName(string variableName) {
    180203      return this.variableNames.Contains(variableName);
    181204    }
     205    #endregion
    182206
    183207    public override IView CreateView() {
    184208      return new DatasetView(this);
    185209    }
    186210
     211
     212    #region Variable statistics
     213    public double GetMean(string variableName) {
     214      return GetMean(GetVariableIndex(variableName));
     215    }
     216
     217    public double GetMean(string variableName, int start, int end) {
     218      return GetMean(GetVariableIndex(variableName), start, end);
     219    }
     220
     221    public double GetMean(int column) {
     222      return GetMean(column, 0, Rows);
     223    }
     224
     225    public double GetMean(int column, int start, int end) {
     226      if (cachedValuesInvalidated) CreateDictionaries();
     227      if (!cachedMeans[column].ContainsKey(start) || !cachedMeans[column][start].ContainsKey(end)) {
     228        double[] values = new double[end - start];
     229        for (int sample = start; sample < end; sample++) {
     230          values[sample - start] = GetValue(sample, column);
     231        }
     232        double mean = Statistics.Mean(values);
     233        if (!cachedMeans[column].ContainsKey(start)) cachedMeans[column][start] = new Dictionary<int, double>();
     234        cachedMeans[column][start][end] = mean;
     235        return mean;
     236      } else {
     237        return cachedMeans[column][start][end];
     238      }
     239    }
     240
     241    public double GetRange(string variableName) {
     242      return GetRange(this.GetVariableIndex(variableName));
     243    }
     244
     245    public double GetRange(int column) {
     246      return GetRange(column, 0, Rows);
     247    }
     248
     249    public double GetRange(string variableName, int start, int end) {
     250      return GetRange(this.GetVariableIndex(variableName), start, end);
     251    }
     252
     253    public double GetRange(int column, int start, int end) {
     254      if (cachedValuesInvalidated) CreateDictionaries();
     255      if (!cachedRanges[column].ContainsKey(start) || !cachedRanges[column][start].ContainsKey(end)) {
     256        double[] values = new double[end - start];
     257        for (int sample = start; sample < end; sample++) {
     258          values[sample - start] = GetValue(sample, column);
     259        }
     260        double range = Statistics.Range(values);
     261        if (!cachedRanges[column].ContainsKey(start)) cachedRanges[column][start]= new Dictionary<int, double>();
     262        cachedRanges[column][start][end] = range;
     263        return range;
     264      } else {
     265        return cachedRanges[column][start][end];
     266      }
     267    }
     268
     269    public double GetMaximum(string variableName) {
     270      return GetMaximum(this.GetVariableIndex(variableName));
     271    }
     272
     273    public double GetMaximum(int column) {
     274      return GetMaximum(column, 0, Rows);
     275    }
     276
     277    public double GetMaximum(string variableName, int start, int end) {
     278      return GetMaximum(this.GetVariableIndex(variableName), start, end);
     279    }
     280
     281    public double GetMaximum(int column, int start, int end) {
     282      double max = Double.NegativeInfinity;
     283      for (int i = start; i < end; i++) {
     284        double val = GetValue(i, column);
     285        if (!double.IsNaN(val) && val > max) max = val;
     286      }
     287      return max;
     288    }
     289
     290    public double GetMinimum(string variableName) {
     291      return GetMinimum(GetVariableIndex(variableName));
     292    }
     293
     294    public double GetMinimum(int column) {
     295      return GetMinimum(column, 0, Rows);
     296    }
     297
     298    public double GetMinimum(string variableName, int start, int end) {
     299      return GetMinimum(this.GetVariableIndex(variableName), start, end);
     300    }
     301
     302    public double GetMinimum(int column, int start, int end) {
     303      double min = Double.PositiveInfinity;
     304      for (int i = start; i < end; i++) {
     305        double val = GetValue(i, column);
     306        if (!double.IsNaN(val) && val < min) min = val;
     307      }
     308      return min;
     309    }
     310    #endregion
     311
     312    internal void ScaleVariable(int column) {
     313      if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
     314        double min = GetMinimum(column);
     315        double max = GetMaximum(column);
     316        double range = max - min;
     317        if (range == 0) ScaleVariable(column, 1.0, -min);
     318        else ScaleVariable(column, 1.0 / range, -min);
     319      }
     320      cachedValuesInvalidated = true;
     321      if (fireChangeEvents) FireChanged();
     322    }
     323
     324    internal void ScaleVariable(int column, double factor, double offset) {
     325      scalingFactor[column] = factor;
     326      scalingOffset[column] = offset;
     327      for (int i = 0; i < Rows; i++) {
     328        double origValue = samples[i * columns + column];
     329        samples[i * columns + column] = (origValue + offset) * factor;
     330      }
     331      cachedValuesInvalidated = true;
     332      if (fireChangeEvents) FireChanged();
     333    }
     334
     335    internal void UnscaleVariable(int column) {
     336      if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
     337        for (int i = 0; i < rows; i++) {
     338          double scaledValue = samples[i * columns + column];
     339          samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
     340        }
     341        scalingFactor[column] = 1.0;
     342        scalingOffset[column] = 0.0;
     343      }
     344      cachedValuesInvalidated = true;
     345      if (fireChangeEvents) FireChanged();
     346    }
     347
     348    private void CreateDictionaries() {
     349      // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
     350      cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
     351      cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
     352      for (int i = 0; i < columns; i++) {
     353        cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
     354        cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
     355      }
     356      cachedValuesInvalidated = false;
     357    }
     358
    187359    #region persistence
    188360    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    189361      Dataset clone = new Dataset();
     
    312484      return xs;
    313485    }
    314486    #endregion
    315 
    316     public double GetMean(int column) {
    317       return GetMean(column, 0, Rows);
    318     }
    319 
    320     public double GetMean(int column, int from, int to) {
    321       if (cachedValuesInvalidated) CreateDictionaries();
    322       if (!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
    323         double[] values = new double[to - from];
    324         for (int sample = from; sample < to; sample++) {
    325           values[sample - from] = GetValue(sample, column);
    326         }
    327         double mean = Statistics.Mean(values);
    328         if (!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
    329         cachedMeans[column][from][to] = mean;
    330         return mean;
    331       } else {
    332         return cachedMeans[column][from][to];
    333       }
    334     }
    335 
    336     public double GetRange(int column) {
    337       return GetRange(column, 0, Rows);
    338     }
    339 
    340     public double GetRange(int column, int from, int to) {
    341       if (cachedValuesInvalidated) CreateDictionaries();
    342       if (!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
    343         double[] values = new double[to - from];
    344         for (int sample = from; sample < to; sample++) {
    345           values[sample - from] = GetValue(sample, column);
    346         }
    347         double range = Statistics.Range(values);
    348         if (!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
    349         cachedRanges[column][from][to] = range;
    350         return range;
    351       } else {
    352         return cachedRanges[column][from][to];
    353       }
    354     }
    355 
    356     public double GetMaximum(int column) {
    357       return GetMaximum(column, 0, Rows);
    358     }
    359 
    360     public double GetMaximum(int column, int start, int end) {
    361       double max = Double.NegativeInfinity;
    362       for (int i = start; i < end; i++) {
    363         double val = GetValue(i, column);
    364         if (!double.IsNaN(val) && val > max) max = val;
    365       }
    366       return max;
    367     }
    368 
    369     public double GetMinimum(int column) {
    370       return GetMinimum(column, 0, Rows);
    371     }
    372 
    373     public double GetMinimum(int column, int start, int end) {
    374       double min = Double.PositiveInfinity;
    375       for (int i = start; i < end; i++) {
    376         double val = GetValue(i, column);
    377         if (!double.IsNaN(val) && val < min) min = val;
    378       }
    379       return min;
    380     }
    381 
    382     internal void ScaleVariable(int column) {
    383       if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
    384         double min = GetMinimum(column);
    385         double max = GetMaximum(column);
    386         double range = max - min;
    387         if (range == 0) ScaleVariable(column, 1.0, -min);
    388         else ScaleVariable(column, 1.0 / range, -min);
    389       }
    390       cachedValuesInvalidated = true;
    391       if (fireChangeEvents) FireChanged();
    392     }
    393 
    394     internal void ScaleVariable(int column, double factor, double offset) {
    395       scalingFactor[column] = factor;
    396       scalingOffset[column] = offset;
    397       for (int i = 0; i < Rows; i++) {
    398         double origValue = samples[i * columns + column];
    399         samples[i * columns + column] = (origValue + offset) * factor;
    400       }
    401       cachedValuesInvalidated = true;
    402       if (fireChangeEvents) FireChanged();
    403     }
    404 
    405     internal void UnscaleVariable(int column) {
    406       if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
    407         for (int i = 0; i < rows; i++) {
    408           double scaledValue = samples[i * columns + column];
    409           samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
    410         }
    411         scalingFactor[column] = 1.0;
    412         scalingOffset[column] = 0.0;
    413       }
    414       cachedValuesInvalidated = true;
    415       if (fireChangeEvents) FireChanged();
    416     }
    417 
    418     private void CreateDictionaries() {
    419       // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
    420       cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
    421       cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
    422       for (int i = 0; i < columns; i++) {
    423         cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
    424         cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
    425       }
    426       cachedValuesInvalidated = false;
    427     }
    428487  }
    429488}
  • HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/AlgorithmBase.cs

     
    428428      model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
    429429      // calculate and set variable impacts
    430430      VariableEvaluationImpactCalculator evaluationImpactCalculator = new VariableEvaluationImpactCalculator();
    431       evaluationImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    432       evaluationImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     431      evaluationImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     432      evaluationImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    433433      VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
    434       qualityImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    435       qualityImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     434      qualityImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     435      qualityImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    436436       
    437437
    438438      evaluationImpactCalculator.Apply(bestModelScope);
  • HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableEvaluationImpactCalculator.cs

     
    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 
    22 using HeuristicLab.Core;
    23 using HeuristicLab.Data;
    24 using HeuristicLab.DataAnalysis;
    25 using HeuristicLab.GP.Interfaces;
    26 
    27 namespace HeuristicLab.GP.StructureIdentification {
    28   public class VariableEvaluationImpactCalculator : HeuristicLab.Modeling.VariableEvaluationImpactCalculator {
    29 
    30     public VariableEvaluationImpactCalculator()
    31       : base() {
    32       AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
    33       AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
    34     }
    35 
    36 
    37     protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    38       ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    39       IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    40       evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    41 
    42       double[] result = new double[end - start];
    43       for (int i = start; i < end; i++) {
    44         result[i - start] = evaluator.Evaluate(i);
    45       }
    46 
    47       return result;
    48     }
    49   }
    50 }
  • HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs

     
    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 
    22 using System;
    23 using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    25 using HeuristicLab.DataAnalysis;
    26 using HeuristicLab.GP.Interfaces;
    27 
    28 namespace HeuristicLab.GP.StructureIdentification {
    29   public class VariableQualityImpactCalculator : HeuristicLab.Modeling.VariableQualityImpactCalculator {
    30 
    31     public VariableQualityImpactCalculator()
    32       : base() {
    33       AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
    34       AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
    35     }
    36 
    37     protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    38       ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    39       IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    40       evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    41 
    42       double[,] result = new double[end - start, 2];
    43       for (int i = start; i < end; i++) {
    44         result[i - start, 0] = dataset.GetValue(i, targetVariable);
    45         result[i - start, 1] = evaluator.Evaluate(i);
    46       }
    47 
    48       try {
    49         return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(result);
    50       }
    51       catch (ArgumentException) {
    52         return double.PositiveInfinity;
    53       }
    54     }
    55   }
    56 }
  • HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

     
    144144    <Compile Include="ModelAnalyzerExporter.cs" />
    145145    <Compile Include="Properties\AssemblyInfo.cs" />
    146146    <Compile Include="SymbolicExpressionExporter.cs" />
    147     <Compile Include="Evaluators\VariableEvaluationImpactCalculator.cs" />
    148     <Compile Include="Evaluators\VariableQualityImpactCalculator.cs" />
     147    <Compile Include="VariableNamesExtractor.cs" />
    149148  </ItemGroup>
    150149  <ItemGroup>
    151150    <ProjectReference Include="..\..\HeuristicLab.Core\3.2\HeuristicLab.Core-3.2.csproj">
  • HeuristicLab.GP.StructureIdentification/3.3/VariableNamesExtractor.cs

     
     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.Linq;
     25using System.Text;
     26using HeuristicLab.Core;
     27using HeuristicLab.Data;
     28using HeuristicLab.GP.Interfaces;
     29
     30namespace HeuristicLab.GP.StructureIdentification {
     31  public class VariableNamesExtractor : OperatorBase {
     32    public VariableNamesExtractor()
     33      : base() {
     34      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree from which the used variables should be extracted", typeof(IGeneticProgrammingModel), VariableKind.In));
     35      AddVariableInfo(new VariableInfo("VariableNames", "Extracted variable names from model", typeof(ItemList<StringData>), VariableKind.New | VariableKind.Out));
     36    }
     37
     38    public override string Description {
     39      get { return "Extracts the variable names used in the given function tree!"; }
     40    }
     41
     42    public override IOperation Apply(IScope scope) {
     43      IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
     44      ItemList<StringData> data = GetVariableValue<ItemList<StringData>>("VariableNames", scope, false, false);
     45      if (data == null) {
     46        data = new ItemList<StringData>();
     47        IVariableInfo info = GetVariableInfo("VariableNames");
     48        if (info.Local)
     49          AddVariable(new HeuristicLab.Core.Variable(info.ActualName, data));
     50        else
     51          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), data));
     52      }
     53      data.Clear();
     54
     55      foreach (string s in ExtractVariableNames(model.FunctionTree)) {
     56        data.Add(new StringData(s));
     57      }
     58
     59      return null;
     60    }
     61
     62    public static IEnumerable<string> ExtractVariableNames(IFunctionTree functionTree) {
     63      List<string> names = new List<string>();
     64      Extract(functionTree, names);
     65      return names;
     66    }
     67
     68    private static void Extract(IFunctionTree functionTree, List<string> variableNames) {
     69      if (functionTree is VariableFunctionTree) {
     70        VariableFunctionTree v = (VariableFunctionTree)functionTree;
     71        if (!variableNames.Contains(v.VariableName))
     72          variableNames.Add(v.VariableName);
     73      }
     74
     75      foreach (IFunctionTree child in functionTree.SubTrees) {
     76        Extract(child, variableNames);
     77      }
     78    }
     79  }
     80}
  • HeuristicLab.LinearRegression/3.2/LinearRegression.cs

     
    236236      testVAF.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
    237237      #endregion
    238238
    239       HeuristicLab.GP.StructureIdentification.VariableEvaluationImpactCalculator evalImpactCalc = new HeuristicLab.GP.StructureIdentification.VariableEvaluationImpactCalculator();
    240       evalImpactCalc.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    241       evalImpactCalc.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    242       evalImpactCalc.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
    243       HeuristicLab.Modeling.VariableQualityImpactCalculator qualImpactCalc = new HeuristicLab.GP.StructureIdentification.VariableQualityImpactCalculator();
    244       qualImpactCalc.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    245       qualImpactCalc.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    246       qualImpactCalc.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
     239      VariableEvaluationImpactCalculator evalImpactCalc = new VariableEvaluationImpactCalculator();
     240      evalImpactCalc.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     241      evalImpactCalc.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     242      //evalImpactCalc.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
     243      VariableQualityImpactCalculator qualImpactCalc = new VariableQualityImpactCalculator();
     244      qualImpactCalc.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     245      qualImpactCalc.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     246      //qualImpactCalc.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
    247247      seqProc.AddSubOperator(trainingMSE);
    248248      seqProc.AddSubOperator(validationMSE);
    249249      seqProc.AddSubOperator(testMSE);
  • HeuristicLab.Modeling/3.2/HeuristicLab.Modeling-3.2.csproj

     
    8686    <Compile Include="DoubleExtensions.cs" />
    8787    <Compile Include="IAnalyzerModel.cs" />
    8888    <Compile Include="MatrixCreator.cs" />
    89     <Compile Include="VariableImpactCalculatorBase.cs" />
    9089    <Compile Include="VariableEvaluationImpactCalculator.cs" />
    9190    <Compile Include="IPredictor.cs" />
    9291    <Compile Include="SimpleEvaluatorBase.cs" />
  • HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs

     
    2929using System.Linq;
    3030
    3131namespace HeuristicLab.Modeling {
    32   public abstract class VariableEvaluationImpactCalculator : VariableImpactCalculatorBase<double[]> {
    33     public override string OutputVariableName {
    34       get { return "VariableEvaluationImpacts"; }
     32  public class VariableEvaluationImpactCalculator : OperatorBase {
     33
     34    public VariableEvaluationImpactCalculator()
     35      : base() {
     36      AddVariableInfo(new VariableInfo("Predictor", "The predictor used to evaluate the model", typeof(IPredictor), VariableKind.In));
     37      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
     38      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
     39      AddVariableInfo(new VariableInfo("InputVariableNames", "Names of used variables in the model (optional)", typeof(ItemList<StringData>), VariableKind.In));
     40      AddVariableInfo(new VariableInfo("SamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In));
     41      AddVariableInfo(new VariableInfo("SamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In));
     42      AddVariableInfo(new VariableInfo("VariableEvaluationImpacts", "VariableEvaluationImpacts", typeof(ItemList), VariableKind.New));
    3543    }
    3644
    3745    public override string Description {
    3846      get { return @"Calculates the impact of all allowed input variables on the model outputs using evaluator supplied as suboperator."; }
    3947    }
    4048
    41     private double[,] CombineOutputs(double[] referenceOutputs, double[] newOutputs) {
    42       if (referenceOutputs.Length != newOutputs.Length) throw new InvalidProgramException();
    43       double[,] result = new double[referenceOutputs.Length, 2];
    44       for (int i = 0; i < referenceOutputs.Length; i++) {
    45         result[i, 0] = referenceOutputs[i];
    46         result[i, 1] = newOutputs[i];
     49    public override IOperation Apply(IScope scope) {
     50      IPredictor predictor = GetVariableValue<IPredictor>("Predictor", scope, true);
     51      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     52      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     53      string targetVariableName = dataset.GetVariableName(targetVariable);
     54      ItemList<StringData> inputVariableNames = GetVariableValue<ItemList<StringData>>("InputVariableNames", scope, true, false);
     55      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
     56      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
     57
     58      Dictionary<string, double> evaluationImpacts;
     59      if (inputVariableNames == null)
     60        evaluationImpacts = Calculate(dataset, predictor, targetVariableName, start, end);
     61      else
     62        evaluationImpacts = Calculate(dataset, predictor, targetVariableName, inputVariableNames.Select(iv => iv.Data), start, end);
     63
     64      ItemList variableImpacts = new ItemList();
     65      foreach (KeyValuePair<string, double> p in evaluationImpacts) {
     66        if (p.Key != targetVariableName) {
     67          ItemList row = new ItemList();
     68          row.Add(new StringData(p.Key));
     69          row.Add(new DoubleData(p.Value));
     70          variableImpacts.Add(row);
     71        }
    4772      }
    48       return result;
     73
     74      scope.AddVariable(new Variable(scope.TranslateName("VariableEvaluationImpacts"), variableImpacts));
     75      return null;
     76
    4977    }
     78    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, int start, int end) {
     79      return Calculate(dataset, predictor, targetVariableName, null, start, end);
     80    }
    5081
    51     protected override double CalculateImpact(double[] referenceValue, double[] newValue) {
     82    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, IEnumerable<string> inputVariableNames, int start, int end) {
     83      Dictionary<string, double> evaluationImpacts = new Dictionary<string, double>();
     84      Dataset dirtyDataset = (Dataset)dataset.Clone();
     85      double[] referenceValues = predictor.Predict(dataset, start, end);
     86
     87      double mean;
     88      IEnumerable<double> oldValues;
     89      double[] newValues;
     90      IEnumerable<string> variables;
     91      if (inputVariableNames != null)
     92        variables = inputVariableNames;
     93      else
     94        variables = dataset.VariableNames;
     95
     96      foreach (string variableName in variables) {
     97        if (variableName != targetVariableName) {
     98          mean = dataset.GetMean(variableName, start, end);
     99          oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
     100          newValues = predictor.Predict(dirtyDataset, start, end);
     101          evaluationImpacts[variableName] = CalculateMSE(referenceValues, newValues);
     102          dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
     103        }
     104      }
     105
     106      double impactsSum = evaluationImpacts.Values.Sum();
     107      if (impactsSum.IsAlmost(0.0)) impactsSum = 1.0;
     108      foreach (KeyValuePair<string, double> p in evaluationImpacts.ToList())
     109        evaluationImpacts[p.Key] = p.Value / impactsSum;
     110
     111      return evaluationImpacts;
     112    }
     113
     114    private static double CalculateMSE(double[] referenceValues, double[] newValues) {
    52115      try {
    53         return SimpleMSEEvaluator.Calculate(CombineOutputs(referenceValue, newValue));
     116        return SimpleMSEEvaluator.Calculate(MatrixCreator<double>.CreateMatrix(referenceValues, newValues));
    54117      }
    55118      catch (ArgumentException) {
    56119        return double.PositiveInfinity;
    57120      }
    58121    }
    59 
    60     protected override double[] CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    61       return GetOutputs(scope, dataset, targetVariable, start, end);
    62     }
    63 
    64     protected override double[] PostProcessImpacts(double[] impacts) {
    65       double mseSum = impacts.Sum();
    66       if (mseSum.IsAlmost(0.0)) mseSum = 1.0;
    67       for (int i = 0; i < impacts.Length; i++) {
    68         impacts[i] = impacts[i] / mseSum;
    69       }
    70       return impacts;
    71     }
    72 
    73     private bool IsAlmost(double x, double y) {
    74       return Math.Abs(x - y) < 1.0E-12;
    75     }
    76 
    77     protected abstract double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end);
    78122  }
    79123}
  • HeuristicLab.Modeling/3.2/VariableImpactCalculatorBase.cs

     
    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 
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    28 using HeuristicLab.DataAnalysis;
    29 using System.Linq;
    30 
    31 namespace 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 }
  • HeuristicLab.Modeling/3.2/VariableQualityImpactCalculator.cs

     
    2929using System.Linq;
    3030
    3131namespace HeuristicLab.Modeling {
    32   public abstract class VariableQualityImpactCalculator : VariableImpactCalculatorBase<double> {
     32  public class VariableQualityImpactCalculator : OperatorBase {
     33
     34    public VariableQualityImpactCalculator()
     35      : base() {
     36      AddVariableInfo(new VariableInfo("Predictor", "The predictor used to evaluate the model", typeof(IPredictor), VariableKind.In));
     37      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
     38      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
     39      AddVariableInfo(new VariableInfo("InputVariableNames", "Names of used variables in the model (optional)", typeof(ItemList<StringData>), VariableKind.In));
     40      AddVariableInfo(new VariableInfo("SamplesStart", "SamplesStart", typeof(IntData), VariableKind.In));
     41      AddVariableInfo(new VariableInfo("SamplesEnd", "SamplesEnd", typeof(IntData), VariableKind.In));
     42      AddVariableInfo(new VariableInfo("VariableQualityImpacts", "VariableQualityImpacts", typeof(ItemList), VariableKind.New));
     43    }
     44
    3345    public override string Description {
    3446      get { return @"Calculates the impact of all allowed input variables on the quality of the model using evaluator supplied as suboperator."; }
    3547    }
    3648
    37     public override string OutputVariableName {
    38       get { return "VariableQualityImpacts"; }
     49    public override IOperation Apply(IScope scope) {
     50      IPredictor predictor = GetVariableValue<IPredictor>("Predictor", scope, true);
     51      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     52      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     53      string targetVariableName = dataset.GetVariableName(targetVariable);
     54      ItemList<StringData> inputVariableNames = GetVariableValue<ItemList<StringData>>("InputVariableNames", scope, true, false);
     55      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
     56      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
     57
     58      Dictionary<string, double> qualityImpacts;
     59      if (inputVariableNames == null)
     60        qualityImpacts = Calculate(dataset, predictor, targetVariableName, start, end);
     61      else
     62        qualityImpacts = Calculate(dataset, predictor, targetVariableName, inputVariableNames.Select(iv => iv.Data), start, end);
     63
     64      ItemList variableImpacts = new ItemList();
     65      foreach (KeyValuePair<string, double> p in qualityImpacts) {
     66        if (p.Key != targetVariableName) {
     67          ItemList row = new ItemList();
     68          row.Add(new StringData(p.Key));
     69          row.Add(new DoubleData(p.Value));
     70          variableImpacts.Add(row);
     71        }
     72      }
     73
     74      scope.AddVariable(new Variable(scope.TranslateName("VariableQualityImpacts"), variableImpacts));
     75      return null;
    3976    }
    4077
    41     protected override double CalculateImpact(double referenceValue, double newValue) {
     78    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, int start, int end) {
     79      return Calculate(dataset, predictor, targetVariableName, null, start, end);
     80    }
     81
     82    public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, IEnumerable<string> inputVariableNames, int start, int end) {
     83      Dictionary<string, double> evaluationImpacts = new Dictionary<string, double>();
     84      Dataset dirtyDataset = (Dataset)dataset.Clone();
     85
     86      double[] predictedValues = predictor.Predict(dataset, start, end);
     87      double[] targetValues = dataset.GetVariableValues(targetVariableName, start, end);
     88
     89      double oldMSE = CalculateMSE(predictedValues, targetValues);
     90      double newMSE;
     91
     92      double mean;
     93      IEnumerable<double> oldValues;
     94      IEnumerable<string> variables;
     95      if (inputVariableNames != null)
     96        variables = inputVariableNames;
     97      else
     98        variables = dataset.VariableNames;
     99
     100      foreach (string variableName in variables) {
     101        if (variableName != targetVariableName) {
     102          mean = dataset.GetMean(variableName, start, end);
     103          oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
     104          predictedValues = predictor.Predict(dirtyDataset, start, end);
     105          newMSE = CalculateMSE(predictedValues, targetValues);
     106          evaluationImpacts[variableName] = newMSE / oldMSE;
     107          dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
     108        }
     109      }
     110
     111      return evaluationImpacts;
     112    }
     113
     114    private static double CalculateImpact(double referenceValue, double newValue) {
    42115      return newValue / referenceValue;
    43116    }
    44117
    45     protected override double CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    46       return CalculateQuality(scope, dataset, targetVariable, start, end);
     118    private static double CalculateMSE(double[] referenceValues, double[] newValues) {
     119      try {
     120        return SimpleMSEEvaluator.Calculate(MatrixCreator<double>.CreateMatrix(referenceValues, newValues));
     121      }
     122      catch (ArgumentException) {
     123        return double.PositiveInfinity;
     124      }
    47125    }
    48 
    49     protected abstract double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end);
    50126  }
    51127}
  • HeuristicLab.SupportVectorMachines/3.2/HeuristicLab.SupportVectorMachines-3.2.csproj

     
    9696    <Compile Include="SVMModelView.Designer.cs">
    9797      <DependentUpon>SVMModelView.cs</DependentUpon>
    9898    </Compile>
    99     <Compile Include="VariableEvaluationImpactCalculator.cs" />
    100     <Compile Include="VariableQualityImpactCalculator.cs" />
    10199  </ItemGroup>
    102100  <ItemGroup>
    103101    <None Include="HeuristicLab.snk" />
  • HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs

     
    403403      SequentialSubScopesProcessor seqSubScopeProc = new SequentialSubScopesProcessor();
    404404      SequentialProcessor seqProc = new SequentialProcessor();
    405405      VariableEvaluationImpactCalculator evalImpactCalc = new VariableEvaluationImpactCalculator();
    406       evalImpactCalc.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    407       evalImpactCalc.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    408       evalImpactCalc.GetVariableInfo("SVMModel").ActualName = "Model";
     406      evalImpactCalc.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     407      evalImpactCalc.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     408      //evalImpactCalc.GetVariableInfo("SVMModel").ActualName = "Model";
    409409      VariableQualityImpactCalculator qualImpactCalc = new VariableQualityImpactCalculator();
    410       qualImpactCalc.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
    411       qualImpactCalc.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    412       qualImpactCalc.GetVariableInfo("SVMModel").ActualName = "Model";
     410      qualImpactCalc.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
     411      qualImpactCalc.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
     412      //qualImpactCalc.GetVariableInfo("SVMModel").ActualName = "Model";
    413413
    414414      seqProc.AddSubOperator(evalImpactCalc);
    415415      seqProc.AddSubOperator(qualImpactCalc);
  • HeuristicLab.SupportVectorMachines/3.2/VariableEvaluationImpactCalculator.cs

     
    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 
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    28 using HeuristicLab.DataAnalysis;
    29 using System.Linq;
    30 
    31 namespace HeuristicLab.SupportVectorMachines {
    32   public class VariableEvaluationImpactCalculator : HeuristicLab.Modeling.VariableEvaluationImpactCalculator {
    33 
    34     public VariableEvaluationImpactCalculator()
    35       : base() {
    36       AddVariableInfo(new VariableInfo("SVMModel", "The model that should be evaluated", typeof(SVMModel), VariableKind.In));
    37     }
    38 
    39 
    40     protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    41       SVMModel model = GetVariableValue<SVMModel>("SVMModel", scope, true);
    42       SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end);
    43       SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, model.RangeTransform);
    44 
    45       double[] values = new double[end - start];
    46       for (int i = 0; i < end - start; i++) {
    47         values[i] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
    48       }
    49       return values;
    50     }
    51   }
    52 }
  • HeuristicLab.SupportVectorMachines/3.2/VariableQualityImpactCalculator.cs

     
    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 
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    28 using HeuristicLab.DataAnalysis;
    29 using System.Linq;
    30 
    31 namespace HeuristicLab.SupportVectorMachines {
    32   public class VariableQualityImpactCalculator : HeuristicLab.Modeling.VariableQualityImpactCalculator {
    33 
    34     public VariableQualityImpactCalculator()
    35       : base() {
    36       AddVariableInfo(new VariableInfo("SVMModel", "The model that should be evaluated", typeof(SVMModel), VariableKind.In));
    37     }
    38 
    39     protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end) {
    40       SVMModel model = GetVariableValue<SVMModel>("SVMModel", scope, true);
    41       SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end);
    42       SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, model.RangeTransform);
    43 
    44       double[,] values = new double[end - start, 2];
    45       for (int i = 0; i < end - start; i++) {
    46         values[i, 0] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
    47         values[i, 1] = dataset.GetValue(start + i, targetVariable);
    48       }
    49 
    50       try { return HeuristicLab.Modeling.SimpleMSEEvaluator.Calculate(values); }
    51       catch (ArgumentException) { return double.PositiveInfinity; }
    52     }
    53   }
    54 }