Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/SupportVectorMachineModelEvaluator.cs @ 4678

Last change on this file since 4678 was 4678, checked in by gkronber, 13 years ago

Refactored cloning in DataAnalysis plugins. #922

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using SVM;
28using System.Collections.Generic;
29using System.Linq;
30using HeuristicLab.Common;
31
32namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
33  [StorableClass]
34  [Item("SupportVectorMachineModelEvaluator", "Represents a operator that evaluates a support vector machine model on a data set.")]
35  public class SupportVectorMachineModelEvaluator : SingleSuccessorOperator {
36    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
37    private const string ModelParameterName = "SupportVectorMachineModel";
38    private const string SamplesStartParameterName = "SamplesStart";
39    private const string SamplesEndParameterName = "SamplesEnd";
40    private const string ValuesParameterName = "Values";
41
42    #region parameter properties
43    public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
44      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
45    }
46    public IValueLookupParameter<IntValue> SamplesStartParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
48    }
49    public IValueLookupParameter<IntValue> SamplesEndParameter {
50      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
51    }
52    public ILookupParameter<SupportVectorMachineModel> SupportVectorMachineModelParameter {
53      get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[ModelParameterName]; }
54    }
55    public ILookupParameter<DoubleMatrix> ValuesParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters[ValuesParameterName]; }
57    }
58    #endregion
59    #region properties
60    public DataAnalysisProblemData DataAnalysisProblemData {
61      get { return DataAnalysisProblemDataParameter.ActualValue; }
62    }
63    public SupportVectorMachineModel SupportVectorMachineModel {
64      get { return SupportVectorMachineModelParameter.ActualValue; }
65    }
66    public IntValue SamplesStart {
67      get { return SamplesStartParameter.ActualValue; }
68    }
69    public IntValue SamplesEnd {
70      get { return SamplesEndParameter.ActualValue; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    protected SupportVectorMachineModelEvaluator(bool deserializing) : base(deserializing) { }
76    protected SupportVectorMachineModelEvaluator(SupportVectorMachineModelEvaluator original, Cloner cloner) : base(original, cloner) { }
77    public SupportVectorMachineModelEvaluator()
78      : base() {
79      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
80      Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
81      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition on which the SVM model should be evaluated."));
82      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition on which the SVM model should be evaluated."));
83      Parameters.Add(new LookupParameter<DoubleMatrix>(ValuesParameterName, "A matrix of original values of the target variable and output values of the SVM model."));
84    }
85
86    public override IOperation Apply() {
87      int start = SamplesStart.Value;
88      int end = SamplesEnd.Value;
89      IEnumerable<int> rows =
90        Enumerable.Range(start, end - start)
91        .Where(i => i < DataAnalysisProblemData.TestSamplesStart.Value || DataAnalysisProblemData.TestSamplesEnd.Value <= i);
92
93      ValuesParameter.ActualValue = new DoubleMatrix(Evaluate(SupportVectorMachineModel, DataAnalysisProblemData, rows));
94      return base.Apply();
95    }
96
97    public static double[,] Evaluate(SupportVectorMachineModel model, DataAnalysisProblemData problemData, IEnumerable<int> rowIndices) {
98      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, rowIndices);
99      SVM.Problem scaledProblem = model.RangeTransform.Scale(problem);
100
101      int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
102
103      double[,] values = new double[scaledProblem.Count, 2];
104      var rowEnumerator = rowIndices.GetEnumerator();
105      for (int i = 0; i < scaledProblem.Count; i++) {
106        rowEnumerator.MoveNext();
107        values[i, 0] = problemData.Dataset[rowEnumerator.Current, targetVariableIndex];
108        values[i, 1] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
109      }
110
111      return values;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.