Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression/SupportVectorRegressionSolution.cs @ 3858

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

added calculation and view for support vectors (ticket #1009)

File size: 5.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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using System.Collections.Generic;
29using System.Linq;
30using System.Drawing;
31using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
32using SVM;
33
34namespace HeuristicLab.Problems.DataAnalysis.Regression.SupportVectorRegression {
35  /// <summary>
36  /// Represents a support vector solution for a regression problem which can be visualized in the GUI.
37  /// </summary>
38  [Item("SupportVectorRegressionSolution", "Represents a support vector solution for a regression problem which can be visualized in the GUI.")]
39  [StorableClass]
40  public sealed class SupportVectorRegressionSolution : DataAnalysisSolution {
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
43    }
44
45    [Storable]
46    private SupportVectorMachineModel model;
47    public SupportVectorMachineModel Model {
48      get { return model; }
49    }
50
51    public Dataset SupportVectors {
52      get { return CalculateSupportVectors(); }
53    }
54
55    public SupportVectorRegressionSolution() : base() { }
56    public SupportVectorRegressionSolution(DataAnalysisProblemData problemData, SupportVectorMachineModel model, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
57      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
58      this.model = model;
59    }
60
61    protected override void OnProblemDataChanged(EventArgs e) {
62      RecalculateEstimatedValues();
63      model.Model.SupportVectorIndizes = new int[0];
64    }
65
66    private Dataset CalculateSupportVectors() {
67      if (model.Model.SupportVectorIndizes.Length == 0)
68        return new Dataset();
69
70      Dataset dataset = new Dataset(ProblemData.Dataset.VariableNames, new double[model.Model.SupportVectorCount, ProblemData.Dataset.Columns]);
71      for (int i = 0; i < model.Model.SupportVectorIndizes.Length; i++) {
72        for (int column = 0; column < ProblemData.Dataset.Columns; column++)
73          dataset[i, column] = ProblemData.Dataset[model.Model.SupportVectorIndizes[i], column];
74      }
75      return dataset;
76    }
77
78    private void RecalculateEstimatedValues() {
79      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(ProblemData, 0, ProblemData.Dataset.Rows);
80      SVM.Problem scaledProblem = Scaling.Scale(model.RangeTransform, problem);
81
82      estimatedValues = (from row in Enumerable.Range(0, scaledProblem.Count)
83                         let prediction = SVM.Prediction.Predict(model.Model, scaledProblem.X[row])
84                         let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, prediction))
85                         select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
86      OnEstimatedValuesChanged(EventArgs.Empty);
87    }
88
89    private List<double> estimatedValues;
90    public override IEnumerable<double> EstimatedValues {
91      get {
92        if (estimatedValues == null) RecalculateEstimatedValues();
93        return estimatedValues.AsEnumerable();
94      }
95    }
96
97    public override IEnumerable<double> EstimatedTrainingValues {
98      get {
99        if (estimatedValues == null) RecalculateEstimatedValues();
100        int start = ProblemData.TrainingSamplesStart.Value;
101        int n = ProblemData.TrainingSamplesEnd.Value - start;
102        return estimatedValues.Skip(start).Take(n).ToList();
103      }
104    }
105
106    public override IEnumerable<double> EstimatedTestValues {
107      get {
108        if (estimatedValues == null) RecalculateEstimatedValues();
109        int start = ProblemData.TestSamplesStart.Value;
110        int n = ProblemData.TestSamplesEnd.Value - start;
111        return estimatedValues.Skip(start).Take(n).ToList();
112      }
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      SupportVectorRegressionSolution clone = (SupportVectorRegressionSolution)base.Clone(cloner);
117      clone.model = (SupportVectorMachineModel)cloner.Clone(model);
118      return clone;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.