Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MulticlassModeller.cs @ 810

Last change on this file since 810 was 712, checked in by gkronber, 16 years ago

fixed a stupid mistake introduced with r702 #328 (GP evaluation doesn't work in a thread parallel engine).

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