Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MulticlassOneVsOneAnalyzer.cs @ 930

Last change on this file since 930 was 702, checked in by gkronber, 16 years ago

fixed #328 by restructuring evaluation operators to remove state in evaluation operators.

File size: 6.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;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.GP.StructureIdentification;
30
[668]31namespace HeuristicLab.GP.StructureIdentification.Classification {
[645]32  public class MulticlassOneVsOneAnalyzer : OperatorBase {
33
34    private const string DATASET = "Dataset";
35    private const string TARGETVARIABLE = "TargetVariable";
36    private const string TARGETCLASSVALUES = "TargetClassValues";
37    private const string SAMPLESSTART = "SamplesStart";
38    private const string SAMPLESEND = "SamplesEnd";
39    private const string CLASSAVALUE = "ClassAValue";
40    private const string CLASSBVALUE = "ClassBValue";
41    private const string BESTMODELLSCOPE = "BestValidationSolution";
42    private const string BESTMODELL = "FunctionTree";
43    private const string VOTES = "Votes";
44    private const string ACCURACY = "Accuracy";
45
46    private const double EPSILON = 1E-6;
47    public override string Description {
48      get { return @"TASK"; }
49    }
50
51    public MulticlassOneVsOneAnalyzer()
52      : base() {
53      AddVariableInfo(new VariableInfo(DATASET, "The dataset to use", typeof(Dataset), VariableKind.In));
54      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "Target variable", typeof(IntData), VariableKind.In));
55      AddVariableInfo(new VariableInfo(TARGETCLASSVALUES, "Class values of the target variable in the original dataset", typeof(ItemList<DoubleData>), VariableKind.In));
56      AddVariableInfo(new VariableInfo(CLASSAVALUE, "The original class value of the class A in the subscope", typeof(DoubleData), VariableKind.In));
57      AddVariableInfo(new VariableInfo(CLASSBVALUE, "The original class value of the class B in the subscope", typeof(DoubleData), VariableKind.In));
58      AddVariableInfo(new VariableInfo(SAMPLESSTART, "The start of samples in the original dataset", typeof(IntData), VariableKind.In));
59      AddVariableInfo(new VariableInfo(SAMPLESEND, "The end of samples in the original dataset", typeof(IntData), VariableKind.In));
60      AddVariableInfo(new VariableInfo(BESTMODELLSCOPE, "The variable containing the scope of the model (incl. meta data)", typeof(IScope), VariableKind.In));
61      AddVariableInfo(new VariableInfo(BESTMODELL, "The variable in the scope of the model that contains the actual model", typeof(BakedFunctionTree), VariableKind.In));
62      AddVariableInfo(new VariableInfo(VOTES, "Array with the votes for each instance", typeof(IntMatrixData), VariableKind.New));
63      AddVariableInfo(new VariableInfo(ACCURACY, "Accuracy of the one-vs-one multi-cass classifier", typeof(DoubleData), VariableKind.New));
64    }
65
66    public override IOperation Apply(IScope scope) {
67      Dataset dataset = GetVariableValue<Dataset>(DATASET, scope, true);
68      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
69      int samplesStart = GetVariableValue<IntData>(SAMPLESSTART, scope, true).Data;
70      int samplesEnd = GetVariableValue<IntData>(SAMPLESEND, scope, true).Data;
71      ItemList<DoubleData> classValues = GetVariableValue<ItemList<DoubleData>>(TARGETCLASSVALUES, scope, true);
72      int[,] votes = new int[samplesEnd - samplesStart, classValues.Count];
73
74      foreach(IScope childScope in scope.SubScopes) {
75        double classAValue = GetVariableValue<DoubleData>(CLASSAVALUE, childScope, true).Data;
76        double classBValue = GetVariableValue<DoubleData>(CLASSBVALUE, childScope, true).Data;
77        IScope bestScope = GetVariableValue<IScope>(BESTMODELLSCOPE, childScope, true);
78        BakedFunctionTree functionTree = GetVariableValue<BakedFunctionTree>(BESTMODELL, bestScope, true);
79
80        BakedTreeEvaluator evaluator = new BakedTreeEvaluator();
[702]81        evaluator.ResetEvaluator(functionTree, dataset, targetVariable, samplesStart, samplesEnd, 1.0);
[645]82
83        for(int i = 0; i < (samplesEnd - samplesStart); i++) {
84          double est = evaluator.Evaluate(i + samplesStart);
85          if(est < 0.5) {
86            CastVote(votes, i, classAValue, classValues);
87          } else {
88            CastVote(votes, i, classBValue, classValues);
89          }
90        }
91      }
92
93      int correctlyClassified = 0;
94      for(int i = 0; i < (samplesEnd - samplesStart); i++) {
95        double originalClassValue = dataset.GetValue(i + samplesStart, targetVariable);
96        double estimatedClassValue = classValues[0].Data;
97        int maxVotes = votes[i, 0];
98        int sameVotes = 0;
99        for(int j = 1; j < classValues[j].Data; j++) {
100          if(votes[i, j] > maxVotes) {
101            maxVotes = votes[i, j];
102            estimatedClassValue = classValues[j].Data;
103            sameVotes = 0;
104          } else if(votes[i, j] == maxVotes) {
105            sameVotes++;
106          }
107        }
108        if(IsEqual(originalClassValue, estimatedClassValue) && sameVotes == 0) correctlyClassified++;
109      }
110
111      double accuracy = correctlyClassified / (double)(samplesEnd - samplesStart);
112
113      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(VOTES), new IntMatrixData(votes)));
114      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(ACCURACY), new DoubleData(accuracy)));
115      return null;
116    }
117
118    private void CastVote(int[,] votes, int sample, double votedClass, ItemList<DoubleData> classValues) {
119      for(int i = 0; i < classValues.Count; i++) {
120        if(IsEqual(classValues[i].Data, votedClass)) votes[sample, i]++;
121      }
122    }
123
124    private bool IsEqual(double x, double y) {
125      return Math.Abs(x - y) < EPSILON;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.