#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.DataAnalysis { /// /// Support vector machine classification data analysis algorithm. /// [Item("Support Vector Classification", "Support vector machine classification data analysis algorithm.")] [Creatable("Data Analysis")] [StorableClass] public sealed class SupportVectorClassification : FixedDataAnalysisAlgorithm { private const string SvmTypeParameterName = "SvmType"; private const string KernelTypeParameterName = "KernelType"; private const string CostParameterName = "Cost"; private const string NuParameterName = "Nu"; private const string GammaParameterName = "Gamma"; #region parameter properties public IValueParameter SvmTypeParameter { get { return (IValueParameter)Parameters[SvmTypeParameterName]; } } public IValueParameter KernelTypeParameter { get { return (IValueParameter)Parameters[KernelTypeParameterName]; } } public IValueParameter NuParameter { get { return (IValueParameter)Parameters[NuParameterName]; } } public IValueParameter CostParameter { get { return (IValueParameter)Parameters[CostParameterName]; } } public IValueParameter GammaParameter { get { return (IValueParameter)Parameters[GammaParameterName]; } } #endregion #region properties public StringValue SvmType { get { return SvmTypeParameter.Value; } } public StringValue KernelType { get { return KernelTypeParameter.Value; } } public DoubleValue Nu { get { return NuParameter.Value; } } public DoubleValue Cost { get { return CostParameter.Value; } } public DoubleValue Gamma { get { return GammaParameter.Value; } } #endregion [StorableConstructor] private SupportVectorClassification(bool deserializing) : base(deserializing) { } private SupportVectorClassification(SupportVectorClassification original, Cloner cloner) : base(original, cloner) { } public SupportVectorClassification() : base() { Problem = new ClassificationProblem(); List svrTypes = (from type in new List { "NU_SVC", "EPSILON_SVC" } select new StringValue(type).AsReadOnly()) .ToList(); ItemSet svrTypeSet = new ItemSet(svrTypes); List kernelTypes = (from type in new List { "LINEAR", "POLY", "SIGMOID", "RBF" } select new StringValue(type).AsReadOnly()) .ToList(); ItemSet kernelTypeSet = new ItemSet(kernelTypes); Parameters.Add(new ConstrainedValueParameter(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0])); Parameters.Add(new ConstrainedValueParameter(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3])); Parameters.Add(new ValueParameter(NuParameterName, "The value of the nu parameter nu-SVC.", new DoubleValue(0.5))); Parameters.Add(new ValueParameter(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0))); Parameters.Add(new ValueParameter(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0))); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } public override IDeepCloneable Clone(Cloner cloner) { return new SupportVectorClassification(this, cloner); } #region support vector classification protected override void Run() { IClassificationProblemData problemData = Problem.ProblemData; IEnumerable selectedInputVariables = problemData.AllowedInputVariables; var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value); Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution)); } public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable allowedInputVariables, string svmType, string kernelType, double cost, double nu, double gamma) { Dataset dataset = problemData.Dataset; string targetVariable = problemData.TargetVariable; int start = problemData.TrainingPartition.Start; int end = problemData.TrainingPartition.End; IEnumerable rows = Enumerable.Range(start, end - start); //extract SVM parameters from scope and set them SVM.Parameter parameter = new SVM.Parameter(); parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true); parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true); parameter.C = cost; parameter.Nu = nu; parameter.Gamma = gamma; parameter.CacheSize = 500; parameter.Probability = false; SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues); return new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); } #endregion } }