Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2226 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
RevLine 
[645]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;
[1856]26using HeuristicLab.Core;
[645]27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
[2162]29using System.Linq;
[645]30
[1856]31namespace HeuristicLab.Modeling {
32  public class ProblemInjector : OperatorBase {
[645]33    public override string Description {
[1856]34      get { return @"Injects the necessary variables for a data-based modeling problem."; }
[645]35    }
36
[1252]37    public ProblemInjector()
[645]38      : base() {
[1856]39      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.New));
[645]40      GetVariableInfo("Dataset").Local = true;
[1856]41      AddVariable(new Variable("Dataset", new Dataset()));
[645]42
[1856]43      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.New));
[645]44      GetVariableInfo("TargetVariable").Local = true;
[1856]45      AddVariable(new Variable("TargetVariable", new IntData()));
[645]46
[1856]47      AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList<IntData>), VariableKind.New));
[645]48      GetVariableInfo("AllowedFeatures").Local = true;
[1856]49      AddVariable(new Variable("AllowedFeatures", new ItemList<IntData>()));
[645]50
[1856]51      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.New));
[645]52      GetVariableInfo("TrainingSamplesStart").Local = true;
[1856]53      AddVariable(new Variable("TrainingSamplesStart", new IntData()));
[645]54
[1856]55      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.New));
[645]56      GetVariableInfo("TrainingSamplesEnd").Local = true;
[1856]57      AddVariable(new Variable("TrainingSamplesEnd", new IntData()));
[645]58
[2161]59      AddVariableInfo(new VariableInfo("ActualTrainingSamplesStart", "ActualTrainingSamplesStart", typeof(IntData), VariableKind.New));
60      AddVariableInfo(new VariableInfo("ActualTrainingSamplesEnd", "ActualTrainingSamplesEnd", typeof(IntData), VariableKind.New));
61
[1856]62      AddVariableInfo(new VariableInfo("ValidationSamplesStart", "ValidationSamplesStart", typeof(IntData), VariableKind.New));
[645]63      GetVariableInfo("ValidationSamplesStart").Local = true;
[1856]64      AddVariable(new Variable("ValidationSamplesStart", new IntData()));
[645]65
[1856]66      AddVariableInfo(new VariableInfo("ValidationSamplesEnd", "ValidationSamplesEnd", typeof(IntData), VariableKind.New));
[645]67      GetVariableInfo("ValidationSamplesEnd").Local = true;
[1856]68      AddVariable(new Variable("ValidationSamplesEnd", new IntData()));
[645]69
[1856]70      AddVariableInfo(new VariableInfo("TestSamplesStart", "TestSamplesStart", typeof(IntData), VariableKind.New));
[645]71      GetVariableInfo("TestSamplesStart").Local = true;
[1856]72      AddVariable(new Variable("TestSamplesStart", new IntData()));
[645]73
[1856]74      AddVariableInfo(new VariableInfo("TestSamplesEnd", "TestSamplesEnd", typeof(IntData), VariableKind.New));
[645]75      GetVariableInfo("TestSamplesEnd").Local = true;
[1856]76      AddVariable(new Variable("TestSamplesEnd", new IntData()));
[2161]77
78      AddVariableInfo(new VariableInfo("MaxNumberOfTrainingSamples", "Maximal number of training samples to use (optional)", typeof(IntData), VariableKind.In));
[2165]79      AddVariableInfo(new VariableInfo("NumberOfInputVariables", "The number of available input variables", typeof(IntData), VariableKind.New));
[2174]80      AddVariableInfo(new VariableInfo("InputVariables", "List of input variable names", typeof(ItemList), VariableKind.New));
[645]81    }
82
[1856]83    public override IView CreateView() {
[1252]84      return new ProblemInjectorView(this);
[645]85    }
86
[1856]87    public override IOperation Apply(IScope scope) {
[2161]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
[2162]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);
[2174]100      ItemList inputVariables = new ItemList();
101      for (int i = 1; i < scopeDataset.Columns; i++) {
102        inputVariables.Add(new StringData(scopeDataset.GetVariableName(i)));
103      }
[2162]104
[2174]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));
[2162]109
[2161]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;
[645]121      }
[2161]122      scope.AddVariable(new Variable(scope.TranslateName("ActualTrainingSamplesStart"), new IntData(trainingStart)));
123      scope.AddVariable(new Variable(scope.TranslateName("ActualTrainingSamplesEnd"), new IntData(trainingStart + nTrainingSamples)));
[2174]124
125
[645]126      return null;
127    }
[2161]128
[2162]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
[2161]163    private void AddVariableToScope(string variableName, IScope scope) {
[2174]164      scope.AddVariable(new Variable(scope.TranslateName(variableName), (IItem)GetVariable(variableName).Value.Clone()));     
[2161]165    }
[645]166  }
167}
Note: See TracBrowser for help on using the repository browser.