Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression/SupportVectorRegressionSolution.cs @ 9333

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

Merged cloning refactoring branch back into trunk (#922)

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