Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/ParameterAdjustmentProblem/SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer.cs @ 4068

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.5 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.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis.Evaluators;
32
33namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine.ParameterAdjustmentProblem {
34  [Item("SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer", "Collects the parameters and the quality on training and test of the best solution for the SVM parameter adjustment problem.")]
35  [StorableClass]
36  public class SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
37    private const string ParameterVectorParameterName = "ParameterVector";
38    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
39    private const string SvmTypeParameterName = "SvmType";
40    private const string KernelTypeParameterName = "KernelType";
41    private const string QualityParameterName = "Quality";
42    private const string BestSolutionParameterName = "BestSolution";
43    private const string BestSolutionQualityParameterName = "BestSolutionQuality";
44    private const string ResultsParameterName = "Results";
45    private const string BestSolutionResultName = "Best solution (cross-validation)";
46    private const string BestSolutionTrainingMse = "Best solution mean squared error (training)";
47    private const string BestSolutionTestMse = "Best solution mean squared error (test)";
48    private const string BestSolutionNu = "Best nu (cross-validation)";
49    private const string BestSolutionCost = "Best cost (cross-validation)";
50    private const string BestSolutionGamma = "Best gamma (cross-validation)";
51
52
53    #region parameter properties
54    public ILookupParameter<ItemArray<RealVector>> ParameterVectorParameter {
55      get { return (ILookupParameter<ItemArray<RealVector>>)Parameters["ParameterVector"]; }
56    }
57    public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
58      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
59    }
60    public IValueLookupParameter<StringValue> SvmTypeParameter {
61      get { return (IValueLookupParameter<StringValue>)Parameters[SvmTypeParameterName]; }
62    }
63    public IValueLookupParameter<StringValue> KernelTypeParameter {
64      get { return (IValueLookupParameter<StringValue>)Parameters[KernelTypeParameterName]; }
65    }
66    public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
67      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters[QualityParameterName]; }
68    }
69    #endregion
70    #region properties
71    public DataAnalysisProblemData DataAnalysisProblemData {
72      get { return DataAnalysisProblemDataParameter.ActualValue; }
73    }
74    public StringValue SvmType {
75      get { return SvmTypeParameter.Value; }
76    }
77    public StringValue KernelType {
78      get { return KernelTypeParameter.Value; }
79    }
80    public ILookupParameter<SupportVectorMachineModel> BestSolutionParameter {
81      get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[BestSolutionParameterName]; }
82    }
83    public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
84      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
85    }
86    public ILookupParameter<ResultCollection> ResultsParameter {
87      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
88    }
89
90    #endregion
91
92    public SupportVectorMachineParameterAdjustmentBestSolutionAnalyzer()
93      : base() {
94      StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
95      StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
96      Parameters.Add(new ScopeTreeLookupParameter<RealVector>(ParameterVectorParameterName, "The parameters for the SVM encoded as a real vector."));
97      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
98      Parameters.Add(new ValueLookupParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", nuSvrType));
99      Parameters.Add(new ValueLookupParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", rbfKernelType));
100      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The cross validation quality reached with the given parameters."));
101      Parameters.Add(new LookupParameter<SupportVectorMachineModel>(BestSolutionParameterName, "The best support vector solution."));
102      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best support vector model."));
103      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best support vector solution should be stored."));
104    }
105
106    public override IOperation Apply() {
107      var points = ParameterVectorParameter.ActualValue;
108      var qualities = QualityParameter.ActualValue;
109      var bestPoint = points[0];
110      var bestQuality = qualities[0].Value;
111      for (int i = 1; i < points.Length; i++) {
112        if (bestQuality > qualities[i].Value) {
113          bestQuality = qualities[i].Value;
114          bestPoint = points[i];
115        }
116      }
117      ResultCollection results = ResultsParameter.ActualValue;
118      double nu = bestPoint[0];
119      double cost = Math.Pow(2, bestPoint[1]);
120      double gamma = Math.Pow(2, bestPoint[2]);
121      DataAnalysisProblemData problemData = DataAnalysisProblemData;
122
123      SupportVectorMachineModel bestModel = BestSolutionParameter.ActualValue;
124      if (bestModel == null) {
125        bestModel = SupportVectorMachineModelCreator.TrainModel(DataAnalysisProblemData,
126          DataAnalysisProblemData.TrainingSamplesStart.Value, DataAnalysisProblemData.TrainingSamplesEnd.Value,
127          SvmType.Value, KernelType.Value, cost, nu, gamma, 0.0);
128        BestSolutionParameter.ActualValue = bestModel;
129        BestSolutionQualityParameter.ActualValue = new DoubleValue(bestQuality);
130        results.Add(new Result(BestSolutionResultName, bestModel));
131        #region calculate R2,MSE,Rel Error
132        double[] trainingValues = problemData.Dataset.GetVariableValues(
133          problemData.TargetVariable.Value,
134          problemData.TrainingSamplesStart.Value,
135          problemData.TrainingSamplesEnd.Value);
136        double[] testValues = problemData.Dataset.GetVariableValues(
137          problemData.TargetVariable.Value,
138          problemData.TestSamplesStart.Value,
139          problemData.TestSamplesEnd.Value);
140        double[] estimatedTrainingValues = bestModel.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value)
141          .ToArray();
142        double[] estimatedTestValues = bestModel.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value)
143          .ToArray();
144        double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, estimatedTrainingValues);
145        double testMse = SimpleMSEEvaluator.Calculate(testValues, estimatedTestValues);
146        results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse)));
147        results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse)));
148        results.Add(new Result(BestSolutionNu, new DoubleValue(nu)));
149        results.Add(new Result(BestSolutionCost, new DoubleValue(cost)));
150        results.Add(new Result(BestSolutionGamma, new DoubleValue(gamma)));
151        #endregion
152      } else {
153        if (BestSolutionQualityParameter.ActualValue.Value > bestQuality) {
154          bestModel = SupportVectorMachineModelCreator.TrainModel(DataAnalysisProblemData,
155            DataAnalysisProblemData.TrainingSamplesStart.Value, DataAnalysisProblemData.TrainingSamplesEnd.Value,
156            SvmType.Value, KernelType.Value, cost, nu, gamma, 0.0);
157          BestSolutionParameter.ActualValue = bestModel;
158          BestSolutionQualityParameter.ActualValue = new DoubleValue(bestQuality);
159          results[BestSolutionResultName].Value = bestModel;
160          #region calculate R2,MSE,Rel Error
161          double[] trainingValues = problemData.Dataset.GetVariableValues(
162            problemData.TargetVariable.Value,
163            problemData.TrainingSamplesStart.Value,
164            problemData.TrainingSamplesEnd.Value);
165          double[] testValues = problemData.Dataset.GetVariableValues(
166            problemData.TargetVariable.Value,
167            problemData.TestSamplesStart.Value,
168            problemData.TestSamplesEnd.Value);
169          double[] estimatedTrainingValues = bestModel.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value)
170            .ToArray();
171          double[] estimatedTestValues = bestModel.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value)
172            .ToArray();
173          double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, estimatedTrainingValues);
174          double testMse = SimpleMSEEvaluator.Calculate(testValues, estimatedTestValues);
175          results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse);
176          results[BestSolutionTestMse].Value = new DoubleValue(testMse);
177          results[BestSolutionNu].Value = new DoubleValue(nu);
178          results[BestSolutionCost].Value = new DoubleValue(cost);
179          results[BestSolutionGamma].Value = new DoubleValue(gamma);
180          #endregion
181        }
182      }
183
184      return base.Apply();
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.