Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Algorithms.DataAnalysis/3.3/SupportVectorMachine.cs @ 10669

Last change on this file since 10669 was 4437, checked in by swagner, 14 years ago

Implemented !IStorableContent separately for each algorithm (#1193)

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