Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/ProblemInjector.cs @ 2174

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

Implemented #302 (Show variable names instead of var<index> in GP function trees).

  • Added a new operator that chooses a random value from a list of possible values.
  • Changed mutation operator for variables and differentials
  • Changed internal linear representation of function trees to support different types for local data.
File size: 8.4 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 class ProblemInjector : OperatorBase {
33    public override string Description {
34      get { return @"Injects the necessary variables for a data-based modeling problem."; }
35    }
36
37    public ProblemInjector()
38      : base() {
39      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.New));
40      GetVariableInfo("Dataset").Local = true;
41      AddVariable(new Variable("Dataset", new Dataset()));
42
43      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.New));
44      GetVariableInfo("TargetVariable").Local = true;
45      AddVariable(new Variable("TargetVariable", new IntData()));
46
47      AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList<IntData>), VariableKind.New));
48      GetVariableInfo("AllowedFeatures").Local = true;
49      AddVariable(new Variable("AllowedFeatures", new ItemList<IntData>()));
50
51      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.New));
52      GetVariableInfo("TrainingSamplesStart").Local = true;
53      AddVariable(new Variable("TrainingSamplesStart", new IntData()));
54
55      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.New));
56      GetVariableInfo("TrainingSamplesEnd").Local = true;
57      AddVariable(new Variable("TrainingSamplesEnd", new IntData()));
58
59      AddVariableInfo(new VariableInfo("ActualTrainingSamplesStart", "ActualTrainingSamplesStart", typeof(IntData), VariableKind.New));
60      AddVariableInfo(new VariableInfo("ActualTrainingSamplesEnd", "ActualTrainingSamplesEnd", typeof(IntData), VariableKind.New));
61
62      AddVariableInfo(new VariableInfo("ValidationSamplesStart", "ValidationSamplesStart", typeof(IntData), VariableKind.New));
63      GetVariableInfo("ValidationSamplesStart").Local = true;
64      AddVariable(new Variable("ValidationSamplesStart", new IntData()));
65
66      AddVariableInfo(new VariableInfo("ValidationSamplesEnd", "ValidationSamplesEnd", typeof(IntData), VariableKind.New));
67      GetVariableInfo("ValidationSamplesEnd").Local = true;
68      AddVariable(new Variable("ValidationSamplesEnd", new IntData()));
69
70      AddVariableInfo(new VariableInfo("TestSamplesStart", "TestSamplesStart", typeof(IntData), VariableKind.New));
71      GetVariableInfo("TestSamplesStart").Local = true;
72      AddVariable(new Variable("TestSamplesStart", new IntData()));
73
74      AddVariableInfo(new VariableInfo("TestSamplesEnd", "TestSamplesEnd", typeof(IntData), VariableKind.New));
75      GetVariableInfo("TestSamplesEnd").Local = true;
76      AddVariable(new Variable("TestSamplesEnd", new IntData()));
77
78      AddVariableInfo(new VariableInfo("MaxNumberOfTrainingSamples", "Maximal number of training samples to use (optional)", typeof(IntData), VariableKind.In));
79      AddVariableInfo(new VariableInfo("NumberOfInputVariables", "The number of available input variables", typeof(IntData), VariableKind.New));
80      AddVariableInfo(new VariableInfo("InputVariables", "List of input variable names", typeof(ItemList), VariableKind.New));
81    }
82
83    public override IView CreateView() {
84      return new ProblemInjectorView(this);
85    }
86
87    public override IOperation Apply(IScope scope) {
88      AddVariableToScope("TrainingSamplesStart", scope);
89      AddVariableToScope("TrainingSamplesEnd", scope);
90      AddVariableToScope("ValidationSamplesStart", scope);
91      AddVariableToScope("ValidationSamplesEnd", scope);
92      AddVariableToScope("TestSamplesStart", scope);
93      AddVariableToScope("TestSamplesEnd", scope);
94
95      Dataset operatorDataset = (Dataset)GetVariable("Dataset").Value;
96      int targetVariable = ((IntData)GetVariable("TargetVariable").Value).Data;
97      ItemList<IntData> operatorAllowedFeatures = (ItemList<IntData>)GetVariable("AllowedFeatures").Value;
98
99      Dataset scopeDataset = CreateNewDataset(operatorDataset, targetVariable, operatorAllowedFeatures);
100      ItemList inputVariables = new ItemList();
101      for (int i = 1; i < scopeDataset.Columns; i++) {
102        inputVariables.Add(new StringData(scopeDataset.GetVariableName(i)));
103      }
104
105      scope.AddVariable(new Variable(scope.TranslateName("Dataset"), scopeDataset));
106      scope.AddVariable(new Variable(scope.TranslateName("TargetVariable"), new IntData(0)));
107      scope.AddVariable(new Variable(scope.TranslateName("NumberOfInputVariables"), new IntData(scopeDataset.Columns - 1)));
108      scope.AddVariable(new Variable(scope.TranslateName("InputVariables"), inputVariables));
109
110      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
111      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
112
113      var maxTraining = GetVariableValue<IntData>("MaxNumberOfTrainingSamples", scope, true, false);
114      int nTrainingSamples;
115      if (maxTraining != null) {
116        nTrainingSamples = Math.Min(maxTraining.Data, trainingEnd - trainingStart);
117        if (nTrainingSamples <= 0)
118          throw new ArgumentException("Maximal number of training samples must be larger than 0", "MaxNumberOfTrainingSamples");
119      } else {
120        nTrainingSamples = trainingEnd - trainingStart;
121      }
122      scope.AddVariable(new Variable(scope.TranslateName("ActualTrainingSamplesStart"), new IntData(trainingStart)));
123      scope.AddVariable(new Variable(scope.TranslateName("ActualTrainingSamplesEnd"), new IntData(trainingStart + nTrainingSamples)));
124
125
126      return null;
127    }
128
129    private Dataset CreateNewDataset(Dataset operatorDataset, int targetVariable, ItemList<IntData> operatorAllowedFeatures) {
130      int columns = (operatorAllowedFeatures.Count() + 1);
131      double[] values = new double[operatorDataset.Rows * columns];
132
133      for (int i = 0; i < values.Length; i++) {
134        int row = i / columns;
135        int column = i % columns;
136        if (column == 0) {
137          values[i] = operatorDataset.GetValue(row, targetVariable);
138        } else {
139          values[i] = operatorDataset.GetValue(row, operatorAllowedFeatures[column-1].Data);
140        }
141      }
142
143      Dataset ds = new Dataset();
144      ds.Columns = columns;
145      ds.Rows = operatorDataset.Rows;
146      ds.Name = operatorDataset.Name;
147      ds.Samples = values;
148      double[] scalingFactor = new double[columns];
149      double[] scalingOffset = new double[columns];
150      ds.SetVariableName(0, operatorDataset.GetVariableName(targetVariable));
151      scalingFactor[0] = operatorDataset.ScalingFactor[targetVariable];
152      scalingOffset[0] = operatorDataset.ScalingOffset[targetVariable];
153      for (int column = 1; column < columns; column++) {
154        ds.SetVariableName(column, operatorDataset.GetVariableName(operatorAllowedFeatures[column - 1].Data));
155        scalingFactor[column] = operatorDataset.ScalingFactor[operatorAllowedFeatures[column - 1].Data];
156        scalingOffset[column] = operatorDataset.ScalingOffset[operatorAllowedFeatures[column - 1].Data];
157      }
158      ds.ScalingOffset = scalingOffset;
159      ds.ScalingFactor = scalingFactor;
160      return ds;
161    }
162
163    private void AddVariableToScope(string variableName, IScope scope) {
164      scope.AddVariable(new Variable(scope.TranslateName(variableName), (IItem)GetVariable(variableName).Value.Clone()));     
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.