Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3932 was 3892, checked in by gkronber, 15 years ago

Improved code for analyzers for SVR and symbolic regression. #1009

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