Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification.Classification/3.3/MulticlassModeller.cs @ 2216

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

GP Refactoring #713

  • cleaned code
  • reintegrated GP.Boolean and GP.SantaFe
  • worked on serialization of function trees
File size: 8.2 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 HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.DataAnalysis;
27
28namespace HeuristicLab.GP.StructureIdentification.Classification {
29  public class MulticlassModeller : OperatorBase {
30
31    private const string DATASET = "Dataset";
32    private const string TARGETVARIABLE = "TargetVariable";
33    private const string TARGETCLASSVALUES = "TargetClassValues";
34    private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
35    private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
36    private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
37    private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
38    private const string CLASSAVALUE = "ClassAValue";
39    private const string CLASSBVALUE = "ClassBValue";
40    private const double EPSILON = 1E-6;
41    public override string Description {
42      get { return @"TASK"; }
43    }
44
45    public MulticlassModeller()
46      : base() {
47      AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new dataset parts in the newly created subscopes", typeof(Dataset), VariableKind.In));
48      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "TargetVariable", typeof(IntData), VariableKind.In));
49      AddVariableInfo(new VariableInfo(TARGETCLASSVALUES, "Class values of the target variable in the original dataset and in the new dataset parts", typeof(ItemList<DoubleData>), VariableKind.In | VariableKind.New));
50      AddVariableInfo(new VariableInfo(CLASSAVALUE, "The original class value of the new class A", typeof(DoubleData), VariableKind.New));
51      AddVariableInfo(new VariableInfo(CLASSBVALUE, "The original class value of the new class B", typeof(DoubleData), VariableKind.New));
52      AddVariableInfo(new VariableInfo(TRAININGSAMPLESSTART, "The start of training samples in the original dataset and starts of training samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
53      AddVariableInfo(new VariableInfo(TRAININGSAMPLESEND, "The end of training samples in the original dataset and ends of training samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
54      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESSTART, "The start of validation samples in the original dataset and starts of validation samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
55      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESEND, "The end of validation samples in the original dataset and ends of validation samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
56    }
57
58    public override IOperation Apply(IScope scope) {
59      Dataset origDataset = GetVariableValue<Dataset>(DATASET, scope, true);
60      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
61      ItemList<DoubleData> classValues = GetVariableValue<ItemList<DoubleData>>(TARGETCLASSVALUES, scope, true);
62      int origTrainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
63      int origTrainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
64      int origValidationSamplesStart = GetVariableValue<IntData>(VALIDATIONSAMPLESSTART, scope, true).Data;
65      int origValidationSamplesEnd = GetVariableValue<IntData>(VALIDATIONSAMPLESEND, scope, true).Data;
66      ItemList<DoubleData> binaryClassValues = new ItemList<DoubleData>();
67      binaryClassValues.Add(new DoubleData(0.0));
68      binaryClassValues.Add(new DoubleData(1.0));
69      for (int i = 0; i < classValues.Count - 1; i++) {
70        for (int j = i + 1; j < classValues.Count; j++) {
71          Dataset dataset = new Dataset();
72          dataset.Columns = origDataset.Columns;
73          double classAValue = classValues[i].Data;
74          double classBValue = classValues[j].Data;
75          int trainingSamplesStart;
76          int trainingSamplesEnd;
77          int validationSamplesStart;
78          int validationSamplesEnd;
79
80          trainingSamplesStart = 0;
81          List<double[]> rows = new List<double[]>();
82          for (int k = origTrainingSamplesStart; k < origTrainingSamplesEnd; k++) {
83            double[] row = new double[dataset.Columns];
84            double targetValue = origDataset.GetValue(k, targetVariable);
85            if (IsEqual(targetValue, classAValue)) {
86              for (int l = 0; l < row.Length; l++) {
87                row[l] = origDataset.GetValue(k, l);
88              }
89              row[targetVariable] = 0;
90              rows.Add(row);
91            } else if (IsEqual(targetValue, classBValue)) {
92              for (int l = 0; l < row.Length; l++) {
93                row[l] = origDataset.GetValue(k, l);
94              }
95              row[targetVariable] = 1.0;
96              rows.Add(row);
97            }
98          }
99          trainingSamplesEnd = rows.Count;
100          validationSamplesStart = rows.Count;
101          for (int k = origValidationSamplesStart; k < origValidationSamplesEnd; k++) {
102            double[] row = new double[dataset.Columns];
103            double targetValue = origDataset.GetValue(k, targetVariable);
104            if (IsEqual(targetValue, classAValue)) {
105              for (int l = 0; l < row.Length; l++) {
106                row[l] = origDataset.GetValue(k, l);
107              }
108              row[targetVariable] = 0;
109              rows.Add(row);
110            } else if (IsEqual(targetValue, classBValue)) {
111              for (int l = 0; l < row.Length; l++) {
112                row[l] = origDataset.GetValue(k, l);
113              }
114              row[targetVariable] = 1.0;
115              rows.Add(row);
116            }
117          }
118          validationSamplesEnd = rows.Count;
119
120          dataset.Rows = rows.Count;
121          dataset.Samples = new double[dataset.Rows * dataset.Columns];
122          for (int k = 0; k < dataset.Rows; k++) {
123            for (int l = 0; l < dataset.Columns; l++) {
124              dataset.SetValue(k, l, rows[k][l]);
125            }
126          }
127
128          Scope childScope = new Scope(classAValue + " vs. " + classBValue);
129
130          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(TARGETCLASSVALUES), binaryClassValues));
131          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(CLASSAVALUE), new DoubleData(classAValue)));
132          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(CLASSBVALUE), new DoubleData(classBValue)));
133          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(TRAININGSAMPLESSTART), new IntData(trainingSamplesStart)));
134          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(TRAININGSAMPLESEND), new IntData(trainingSamplesEnd)));
135          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(VALIDATIONSAMPLESSTART), new IntData(validationSamplesStart)));
136          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(VALIDATIONSAMPLESEND), new IntData(validationSamplesEnd)));
137          childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(DATASET), dataset));
138          scope.AddSubScope(childScope);
139        }
140      }
141      return null;
142    }
143
144    private bool IsEqual(double x, double y) {
145      return Math.Abs(x - y) < EPSILON;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.