Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs @ 5624

Last change on this file since 5624 was 5624, checked in by gkronber, 13 years ago

#1418 renamed interface for interpreter, worked on solutions and models and implemented SVM regression.

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
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 : RegressionSolution {
38
39    public new SupportVectorRegressionModel Model {
40      get { return (SupportVectorRegressionModel)base.Model; }
41    }
42
43    //IRegressionModel IRegressionSolution.Model {
44    //  get { return model; }
45    //}
46    //IDataAnalysisModel IDataAnalysisSolution.Model {
47    //  get { return model; }
48    //}
49
50    //[Storable]
51    //private IRegressionProblemData problemData;
52    //public IRegressionProblemData ProblemData {
53    //  get { return problemData; }
54    //}
55    //IDataAnalysisProblemData IDataAnalysisSolution.ProblemData {
56    //  get { return ProblemData; }
57    //}
58
59    [Storable]
60    private double lowerEstimationLimit;
61    public double LowerEstimationLimit {
62      get { return lowerEstimationLimit; }
63    }
64
65    [Storable]
66    private double upperEstimationLimit;
67    public double UpperEstimationLimit {
68      get { return upperEstimationLimit; }
69    }
70
71    private List<string> inputVariables;
72    [Storable]
73    private IEnumerable<string> InputVariablesStorable {
74      get { return inputVariables; }
75      set { inputVariables = new List<string>(value); }
76    }
77
78
79    //public event EventHandler ModelChanged;
80
81    //public event EventHandler ProblemDataChanged;
82
83    public Dataset SupportVectors {
84      get { return CalculateSupportVectors(); }
85    }
86
87    //private List<double> estimatedValues;
88
89    [StorableConstructor]
90    private SupportVectorRegressionSolution(bool deserializing) : base(deserializing) { }
91    private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner)
92      : base(original, cloner) {
93      //problemData = cloner.Clone(original.problemData);
94      //model = cloner.Clone(original.model);
95      inputVariables = new List<string>(original.inputVariables);
96      lowerEstimationLimit = original.lowerEstimationLimit;
97      upperEstimationLimit = original.upperEstimationLimit;
98    }
99    public SupportVectorRegressionSolution(SupportVectorRegressionModel model, IRegressionProblemData problemData, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
100      : base(model, problemData) {
101      //this.problemData = problemData;
102      //this.model = model;
103      this.inputVariables = new List<string>(inputVariables);
104      this.lowerEstimationLimit = lowerEstimationLimit;
105      this.upperEstimationLimit = upperEstimationLimit;
106    }
107
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new SupportVectorRegressionSolution(this, cloner);
110    }
111
112    protected override void OnProblemDataChanged(EventArgs e) {
113      Model.Model.SupportVectorIndizes = new int[0];
114      base.OnProblemDataChanged(e);
115    }
116
117    //public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
118    //  if (estimatedValues == null) RecalculateEstimatedValues();
119    //  foreach (int row in rows)
120    //    yield return estimatedValues[row];
121    //}
122
123    //public override IEnumerable<double> EstimatedValues {
124    //  get {
125    //    if (estimatedValues == null) RecalculateEstimatedValues();
126    //    return estimatedValues;
127    //  }
128    //}
129
130    //public override IEnumerable<double> EstimatedTrainingValues {
131    //  get {
132    //    return GetEstimatedValues(ProblemData.TrainingIndizes);
133    //  }
134    //}
135
136    //public override IEnumerable<double> EstimatedTestValues {
137    //  get {
138    //    return GetEstimatedValues(ProblemData.TestIndizes);
139    //  }
140    //}
141
142    private Dataset CalculateSupportVectors() {
143      if (Model.Model.SupportVectorIndizes.Length == 0)
144        return new Dataset(new List<string>(), new double[0, 0]);
145
146      double[,] data = new double[Model.Model.SupportVectorIndizes.Length, ProblemData.Dataset.Columns];
147      for (int i = 0; i < Model.Model.SupportVectorIndizes.Length; i++) {
148        for (int column = 0; column < ProblemData.Dataset.Columns; column++)
149          data[i, column] = ProblemData.Dataset[Model.Model.SupportVectorIndizes[i], column];
150      }
151      return new Dataset(ProblemData.Dataset.VariableNames, data);
152    }
153
154    //protected override void RecalculateEstimatedValues() {
155    //  Dataset dataset = problemData.Dataset;
156    //  string targetVariable = problemData.TargetVariable;
157    //  IEnumerable<string> allowedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
158    //  int start = 0;
159    //  int end = dataset.Rows;
160    //  IEnumerable<int> rows = Enumerable.Range(start, end - start);
161    //  SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
162    //  SVM.Problem scaledProblem = SVM.Scaling.Scale(Model.RangeTransform, problem);
163
164    //  estimatedValues = (from row in Enumerable.Range(0, scaledProblem.Count)
165    //                     let prediction = SVM.Prediction.Predict(Model.Model, scaledProblem.X[row])
166    //                     let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, prediction))
167    //                     select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
168    //}
169  }
170}
Note: See TracBrowser for help on using the repository browser.