Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.3/SupportVectorMachine.cs @ 5625

Last change on this file since 5625 was 5625, checked in by mkommend, 14 years ago

#1418: Reorganized branch and removed CreateAble-Attribute from outdated classes.

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