Free cookie consent management tool by TermsFeed Policy Generator

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