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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Support vector machine classification data analysis algorithm.
|
---|
36 | /// </summary>
|
---|
37 | [Item("Support Vector Classification", "Support vector machine classification data analysis algorithm (wrapper for libSVM).")]
|
---|
38 | [Creatable("Data Analysis")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class SupportVectorClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
|
---|
41 | private const string SvmTypeParameterName = "SvmType";
|
---|
42 | private const string KernelTypeParameterName = "KernelType";
|
---|
43 | private const string CostParameterName = "Cost";
|
---|
44 | private const string NuParameterName = "Nu";
|
---|
45 | private const string GammaParameterName = "Gamma";
|
---|
46 |
|
---|
47 | #region parameter properties
|
---|
48 | public IValueParameter<StringValue> SvmTypeParameter {
|
---|
49 | get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
50 | }
|
---|
51 | public IValueParameter<StringValue> KernelTypeParameter {
|
---|
52 | get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<DoubleValue> NuParameter {
|
---|
55 | get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<DoubleValue> CostParameter {
|
---|
58 | get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
|
---|
59 | }
|
---|
60 | public IValueParameter<DoubleValue> GammaParameter {
|
---|
61 | get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 | #region properties
|
---|
65 | public StringValue SvmType {
|
---|
66 | get { return SvmTypeParameter.Value; }
|
---|
67 | }
|
---|
68 | public StringValue KernelType {
|
---|
69 | get { return KernelTypeParameter.Value; }
|
---|
70 | }
|
---|
71 | public DoubleValue Nu {
|
---|
72 | get { return NuParameter.Value; }
|
---|
73 | }
|
---|
74 | public DoubleValue Cost {
|
---|
75 | get { return CostParameter.Value; }
|
---|
76 | }
|
---|
77 | public DoubleValue Gamma {
|
---|
78 | get { return GammaParameter.Value; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 | [StorableConstructor]
|
---|
82 | private SupportVectorClassification(bool deserializing) : base(deserializing) { }
|
---|
83 | private SupportVectorClassification(SupportVectorClassification original, Cloner cloner)
|
---|
84 | : base(original, cloner) {
|
---|
85 | }
|
---|
86 | public SupportVectorClassification()
|
---|
87 | : base() {
|
---|
88 | Problem = new ClassificationProblem();
|
---|
89 |
|
---|
90 | List<StringValue> svrTypes = (from type in new List<string> { "NU_SVC", "C_SVC" }
|
---|
91 | select new StringValue(type).AsReadOnly())
|
---|
92 | .ToList();
|
---|
93 | ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
|
---|
94 | List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
|
---|
95 | select new StringValue(type).AsReadOnly())
|
---|
96 | .ToList();
|
---|
97 | ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
|
---|
98 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
|
---|
99 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
|
---|
100 | Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC.", new DoubleValue(0.5)));
|
---|
101 | Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0)));
|
---|
102 | Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
|
---|
103 | }
|
---|
104 | [StorableHook(HookType.AfterDeserialization)]
|
---|
105 | private void AfterDeserialization() { }
|
---|
106 |
|
---|
107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
108 | return new SupportVectorClassification(this, cloner);
|
---|
109 | }
|
---|
110 |
|
---|
111 | #region support vector classification
|
---|
112 | protected override void Run() {
|
---|
113 | IClassificationProblemData problemData = Problem.ProblemData;
|
---|
114 | IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
|
---|
115 | var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value);
|
---|
116 |
|
---|
117 | Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
|
---|
121 | string svmType, string kernelType, double cost, double nu, double gamma) {
|
---|
122 | Dataset dataset = problemData.Dataset;
|
---|
123 | string targetVariable = problemData.TargetVariable;
|
---|
124 | IEnumerable<int> rows = problemData.TrainingIndizes;
|
---|
125 |
|
---|
126 | //extract SVM parameters from scope and set them
|
---|
127 | SVM.Parameter parameter = new SVM.Parameter();
|
---|
128 | parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
|
---|
129 | parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
|
---|
130 | parameter.C = cost;
|
---|
131 | parameter.Nu = nu;
|
---|
132 | parameter.Gamma = gamma;
|
---|
133 | parameter.CacheSize = 500;
|
---|
134 | parameter.Probability = false;
|
---|
135 |
|
---|
136 | foreach (double c in problemData.ClassValues) {
|
---|
137 | double wSum = 0.0;
|
---|
138 | foreach (double otherClass in problemData.ClassValues) {
|
---|
139 | if (!c.IsAlmost(otherClass)) {
|
---|
140 | wSum += problemData.GetClassificationPenalty(c, otherClass);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | parameter.Weights.Add((int)c, wSum);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
|
---|
148 | SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
|
---|
149 | SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
|
---|
150 | var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
|
---|
151 |
|
---|
152 | return new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 | }
|
---|
156 | }
|
---|