Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs @ 2754

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

Implemented #812 (Static methods for SVM operators).

File size: 7.3 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using System.Threading;
30using SVM;
31
32namespace HeuristicLab.SupportVectorMachines {
33  public class SupportVectorCreator : OperatorBase {
34    private Thread trainingThread;
35    private object locker = new object();
36    private bool abortRequested = false;
37
38    public SupportVectorCreator()
39      : base() {
40      //Dataset infos
41      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
42      AddVariableInfo(new VariableInfo("TargetVariable", "Name of the target variable", typeof(StringData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("InputVariables", "List of allowed input variable names", typeof(ItemList), VariableKind.In));
44      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("MaxTimeOffset", "(optional) Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("MinTimeOffset", "(optional) Minimal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
48
49      //SVM parameters
50      AddVariableInfo(new VariableInfo("SVMType", "String describing which SVM type is used. Valid inputs are: C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR",
51        typeof(StringData), VariableKind.In));
52      AddVariableInfo(new VariableInfo("SVMKernelType", "String describing which SVM kernel is used. Valid inputs are: LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED",
53        typeof(StringData), VariableKind.In));
54      AddVariableInfo(new VariableInfo("SVMCost", "Cost parameter (C) of C-SVC, epsilon-SVR and nu-SVR", typeof(DoubleData), VariableKind.In));
55      AddVariableInfo(new VariableInfo("SVMNu", "Nu parameter of nu-SVC, one-class SVM and nu-SVR", typeof(DoubleData), VariableKind.In));
56      AddVariableInfo(new VariableInfo("SVMGamma", "Gamma parameter in kernel function", typeof(DoubleData), VariableKind.In));
57      AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.New | VariableKind.Out));
58    }
59
60    public override void Abort() {
61      abortRequested = true;
62      lock (locker) {
63        if (trainingThread != null && trainingThread.ThreadState == ThreadState.Running) {
64          trainingThread.Abort();
65        }
66      }
67    }
68
69    public override IOperation Apply(IScope scope) {
70      abortRequested = false;
71      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
72      string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
73      ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true);
74      var inputVariableNames = from x in inputVariables
75                               select ((StringData)x).Data;
76      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
77      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
78      IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false);
79      int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data;
80      IntData minTimeOffsetData = GetVariableValue<IntData>("MinTimeOffset", scope, true, false);
81      int minTimeOffset = minTimeOffsetData == null ? 0 : minTimeOffsetData.Data;
82      string svmType = GetVariableValue<StringData>("SVMType", scope, true).Data;
83      string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data;
84
85      double cost = GetVariableValue<DoubleData>("SVMCost", scope, true).Data;
86      double nu = GetVariableValue<DoubleData>("SVMNu", scope, true).Data;
87      double gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
88
89      SVMModel modelData = null;
90      lock (locker) {
91        if (!abortRequested) {
92          trainingThread = new Thread(() => {
93            modelData = TrainModel(dataset, targetVariable, inputVariableNames,
94                                   start, end, minTimeOffset, maxTimeOffset,
95                                   svmType, svmKernelType,
96                                   cost, nu, gamma);
97          });
98          trainingThread.Start();
99        }
100      }
101      if (!abortRequested) {
102        trainingThread.Join();
103        trainingThread = null;
104      }
105
106
107      if (!abortRequested) {
108        //persist variables in scope
109        scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
110        return null;
111      } else {
112        return new AtomicOperation(this, scope);
113      }
114    }
115
116    public static SVMModel TrainRegressionModel(
117      Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
118      int start, int end,
119      double cost, double nu, double gamma) {
120      return TrainModel(dataset, targetVariable, inputVariables, start, end, 0, 0, "NU_SVR", "RBF", cost, nu, gamma);
121    }
122
123    public static SVMModel TrainModel(
124      Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
125      int start, int end,
126      int minTimeOffset, int maxTimeOffset,
127      string svmType, string kernelType,
128      double cost, double nu, double gamma) {
129      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
130
131      //extract SVM parameters from scope and set them
132      SVM.Parameter parameter = new SVM.Parameter();
133      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
134      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
135      parameter.C = cost;
136      parameter.Nu = nu;
137      parameter.Gamma = gamma;
138      parameter.CacheSize = 500;
139      parameter.Probability = false;
140
141
142      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariableIndex, inputVariables, start, end, minTimeOffset, maxTimeOffset);
143      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
144      SVM.Problem scaledProblem = rangeTransform.Scale(problem);
145      var model = new SVMModel();
146
147      model.Model = SVM.Training.Train(scaledProblem, parameter);
148      model.RangeTransform = rangeTransform;
149
150      return model;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.