Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.3/SupportVectorMachine.cs @ 4410

Last change on this file since 4410 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Evaluators;
31using HeuristicLab.Problems.DataAnalysis.Regression.SupportVectorRegression;
32using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
33
34namespace HeuristicLab.Algorithms.DataAnalysis {
35  /// <summary>
36  /// A support vector machine.
37  /// </summary>
38  [Item("Support Vector Machine", "Support vector machine data analysis algorithm.")]
39  [Creatable("Data Analysis")]
40  [StorableClass]
41  public sealed class SupportVectorMachine : EngineAlgorithm {
42    private const string TrainingSamplesStartParameterName = "Training start";
43    private const string TrainingSamplesEndParameterName = "Training end";
44    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
45    private const string SvmTypeParameterName = "SvmType";
46    private const string KernelTypeParameterName = "KernelType";
47    private const string CostParameterName = "Cost";
48    private const string NuParameterName = "Nu";
49    private const string GammaParameterName = "Gamma";
50    private const string EpsilonParameterName = "Epsilon";
51
52    private const string ModelParameterName = "SupportVectorMachineModel";
53    #region Problem Properties
54    public override Type ProblemType {
55      get { return typeof(DataAnalysisProblem); }
56    }
57    public new DataAnalysisProblem Problem {
58      get { return (DataAnalysisProblem)base.Problem; }
59      set { base.Problem = value; }
60    }
61    #endregion
62
63    #region parameter properties
64    public IValueParameter<IntValue> TrainingSamplesStartParameter {
65      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
66    }
67    public IValueParameter<IntValue> TrainingSamplesEndParameter {
68      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
69    }
70    public IValueParameter<StringValue> SvmTypeParameter {
71      get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
72    }
73    public IValueParameter<StringValue> KernelTypeParameter {
74      get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
75    }
76    public IValueParameter<DoubleValue> NuParameter {
77      get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
78    }
79    public IValueParameter<DoubleValue> CostParameter {
80      get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
81    }
82    public IValueParameter<DoubleValue> GammaParameter {
83      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
84    }
85    public IValueParameter<DoubleValue> EpsilonParameter {
86      get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
87    }
88    #endregion
89
90    [Storable]
91    private SupportVectorMachineModelCreator solutionCreator;
92    [Storable]
93    private SupportVectorMachineModelEvaluator evaluator;
94    [Storable]
95    private SimpleMSEEvaluator mseEvaluator;
96    [Storable]
97    private BestSupportVectorRegressionSolutionAnalyzer analyzer;
98    public SupportVectorMachine()
99      : base() {
100      #region svm types
101      StringValue cSvcType = new StringValue("C_SVC").AsReadOnly();
102      StringValue nuSvcType = new StringValue("NU_SVC").AsReadOnly();
103      StringValue eSvrType = new StringValue("EPSILON_SVR").AsReadOnly();
104      StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
105      ItemSet<StringValue> allowedSvmTypes = new ItemSet<StringValue>();
106      allowedSvmTypes.Add(cSvcType);
107      allowedSvmTypes.Add(nuSvcType);
108      allowedSvmTypes.Add(eSvrType);
109      allowedSvmTypes.Add(nuSvrType);
110      #endregion
111      #region kernel types
112      StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
113      StringValue linearKernelType = new StringValue("LINEAR").AsReadOnly();
114      StringValue polynomialKernelType = new StringValue("POLY").AsReadOnly();
115      StringValue sigmoidKernelType = new StringValue("SIGMOID").AsReadOnly();
116      ItemSet<StringValue> allowedKernelTypes = new ItemSet<StringValue>();
117      allowedKernelTypes.Add(rbfKernelType);
118      allowedKernelTypes.Add(linearKernelType);
119      allowedKernelTypes.Add(polynomialKernelType);
120      allowedKernelTypes.Add(sigmoidKernelType);
121      #endregion
122      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, "The first index of the data set partition to use for training."));
123      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, "The last index of the data set partition to use for training."));
124      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", allowedSvmTypes, nuSvrType));
125      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", allowedKernelTypes, rbfKernelType));
126      Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC, one-class SVM and nu-SVR.", new DoubleValue(0.5)));
127      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC, epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
128      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
129      Parameters.Add(new ValueLookupParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter (only for epsilon-SVR).", new DoubleValue(1.0)));
130
131      solutionCreator = new SupportVectorMachineModelCreator();
132      evaluator = new SupportVectorMachineModelEvaluator();
133      mseEvaluator = new SimpleMSEEvaluator();
134      analyzer = new BestSupportVectorRegressionSolutionAnalyzer();
135
136      OperatorGraph.InitialOperator = solutionCreator;
137      solutionCreator.Successor = evaluator;
138      evaluator.Successor = mseEvaluator;
139      mseEvaluator.Successor = analyzer;
140
141      Initialize();
142    }
143    [StorableConstructor]
144    private SupportVectorMachine(bool deserializing) : base(deserializing) { }
145
146    public override IDeepCloneable Clone(Cloner cloner) {
147      SupportVectorMachine clone = (SupportVectorMachine)base.Clone(cloner);
148      clone.solutionCreator = (SupportVectorMachineModelCreator)cloner.Clone(solutionCreator);
149      clone.evaluator = (SupportVectorMachineModelEvaluator)cloner.Clone(evaluator);
150      clone.mseEvaluator = (SimpleMSEEvaluator)cloner.Clone(mseEvaluator);
151      clone.analyzer = (BestSupportVectorRegressionSolutionAnalyzer)cloner.Clone(analyzer);
152      clone.Initialize();
153      return clone;
154    }
155
156    public override void Prepare() {
157      if (Problem != null) base.Prepare();
158    }
159
160    protected override void Problem_Reset(object sender, EventArgs e) {
161      UpdateAlgorithmParameters();
162      base.Problem_Reset(sender, e);
163    }
164
165    #region Events
166    protected override void OnProblemChanged() {
167      solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
168      evaluator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
169      analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
170      UpdateAlgorithmParameters();
171      Problem.Reset += new EventHandler(Problem_Reset);
172      base.OnProblemChanged();
173    }
174
175    #endregion
176
177    #region Helpers
178    [StorableHook(HookType.AfterDeserialization)]
179    private void Initialize() {
180      solutionCreator.SvmTypeParameter.ActualName = SvmTypeParameter.Name;
181      solutionCreator.KernelTypeParameter.ActualName = KernelTypeParameter.Name;
182      solutionCreator.CostParameter.ActualName = CostParameter.Name;
183      solutionCreator.GammaParameter.ActualName = GammaParameter.Name;
184      solutionCreator.NuParameter.ActualName = NuParameter.Name;
185      solutionCreator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
186      solutionCreator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
187
188      evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
189      evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
190      evaluator.SupportVectorMachineModelParameter.ActualName = solutionCreator.SupportVectorMachineModelParameter.ActualName;
191      evaluator.ValuesParameter.ActualName = "Training values";
192
193      mseEvaluator.ValuesParameter.ActualName = "Training values";
194      mseEvaluator.MeanSquaredErrorParameter.ActualName = "Training MSE";
195
196      analyzer.SupportVectorRegressionModelParameter.ActualName = solutionCreator.SupportVectorMachineModelParameter.ActualName;
197      analyzer.SupportVectorRegressionModelParameter.Depth = 0;
198      analyzer.QualityParameter.ActualName = mseEvaluator.MeanSquaredErrorParameter.ActualName;
199      analyzer.QualityParameter.Depth = 0;
200
201      if (Problem != null) {
202        solutionCreator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
203        evaluator.DataAnalysisProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
204        analyzer.ProblemDataParameter.ActualName = Problem.DataAnalysisProblemDataParameter.Name;
205        Problem.Reset += new EventHandler(Problem_Reset);
206      }
207    }
208
209    private void UpdateAlgorithmParameters() {
210      TrainingSamplesStartParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesStart;
211      TrainingSamplesEndParameter.ActualValue = Problem.DataAnalysisProblemData.TrainingSamplesEnd;
212    }
213    #endregion
214  }
215}
Note: See TracBrowser for help on using the repository browser.