Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs @ 3962

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

Fixed #784 (ProblemInjector should be changed to read variable names instead of indexes for input and target variables)

File size: 6.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 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      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
74      ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true);
75      var inputVariableNames = from x in inputVariables
76                               select ((StringData)x).Data;
77      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
78      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
79      IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false);
80      int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data;
81      IntData minTimeOffsetData = GetVariableValue<IntData>("MinTimeOffset", scope, true, false);
82      int minTimeOffset = minTimeOffsetData == null ? 0 : minTimeOffsetData.Data;
83      string svmType = GetVariableValue<StringData>("SVMType", scope, true).Data;
84      string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data;
85
86      //extract SVM parameters from scope and set them
87      SVM.Parameter parameter = new SVM.Parameter();
88      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
89      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), svmKernelType, true);
90      parameter.C = GetVariableValue<DoubleData>("SVMCost", scope, true).Data;
91      parameter.Nu = GetVariableValue<DoubleData>("SVMNu", scope, true).Data;
92      parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
93      parameter.CacheSize = 500;
94      parameter.Probability = false;
95
96      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariableIndex, inputVariableNames, start, end, minTimeOffset, maxTimeOffset);
97      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
98      SVM.Problem scaledProblem = rangeTransform.Scale(problem);
99
100      SVM.Model model = StartTraining(scaledProblem, parameter);
101      if (!abortRequested) {
102        //persist variables in scope
103        SVMModel modelData = new SVMModel();
104        modelData.Model = model;
105        modelData.RangeTransform = rangeTransform;
106        scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
107        return null;
108      } else {
109        return new AtomicOperation(this, scope);
110      }
111    }
112
113    private SVM.Model StartTraining(SVM.Problem scaledProblem, SVM.Parameter parameter) {
114      SVM.Model model = null;
115      lock (locker) {
116        if (!abortRequested) {
117          trainingThread = new Thread(() => {
118            model = SVM.Training.Train(scaledProblem, parameter);
119          });
120          trainingThread.Start();
121        }
122      }
123      if (!abortRequested) {
124        trainingThread.Join();
125        trainingThread = null;
126      }
127      return model;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.