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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
|
---|
30 | using SVM;
|
---|
31 |
|
---|
32 | namespace 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.VSImageLibrary.Function; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public new SupportVectorMachineModel Model {
|
---|
44 | get { return (SupportVectorMachineModel)base.Model; }
|
---|
45 | set { base.Model = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private List<double> estimatedValues;
|
---|
49 | public override IEnumerable<double> EstimatedValues {
|
---|
50 | get {
|
---|
51 | if (estimatedValues == null) RecalculateEstimatedValues();
|
---|
52 | return estimatedValues;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
57 | get {
|
---|
58 | return GetEstimatedValues(ProblemData.TrainingIndizes);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override IEnumerable<double> EstimatedTestValues {
|
---|
63 | get {
|
---|
64 | return GetEstimatedValues(ProblemData.TestIndizes);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Dataset SupportVectors {
|
---|
69 | get {
|
---|
70 | int nCol = inputVariables.Count;
|
---|
71 | double[,] data = new double[Model.Model.SupportVectorCount, nCol];
|
---|
72 | int row = 0;
|
---|
73 | foreach (var sv in Model.SupportVectors) {
|
---|
74 | for (int col = 0; col < nCol; col++) {
|
---|
75 | data[row, col] = sv[col];
|
---|
76 | }
|
---|
77 | row++;
|
---|
78 | }
|
---|
79 | return new Dataset(inputVariables, data);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [Storable]
|
---|
84 | private List<string> inputVariables;
|
---|
85 |
|
---|
86 | [StorableConstructor]
|
---|
87 | private SupportVectorRegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
88 | private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner)
|
---|
89 | : base(original, cloner) {
|
---|
90 | this.inputVariables = new List<string>(original.inputVariables);
|
---|
91 | }
|
---|
92 | public SupportVectorRegressionSolution() : base() { }
|
---|
93 | public SupportVectorRegressionSolution(DataAnalysisProblemData problemData, SupportVectorMachineModel model, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
|
---|
94 | : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
|
---|
95 | this.Model = model;
|
---|
96 | this.inputVariables = new List<string>(inputVariables);
|
---|
97 | }
|
---|
98 |
|
---|
99 | [StorableHook(HookType.AfterDeserialization)]
|
---|
100 | private void AfterDeserialization() {
|
---|
101 | #region backwards compatibility
|
---|
102 | if (inputVariables == null) inputVariables = ProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value).ToList();
|
---|
103 | #endregion
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
107 | return new SupportVectorRegressionSolution(this, cloner);
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override void RecalculateEstimatedValues() {
|
---|
111 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(ProblemData, Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
112 | SVM.Problem scaledProblem = Scaling.Scale(Model.RangeTransform, problem);
|
---|
113 |
|
---|
114 | estimatedValues = (from row in Enumerable.Range(0, scaledProblem.Count)
|
---|
115 | let prediction = SVM.Prediction.Predict(Model.Model, scaledProblem.X[row])
|
---|
116 | let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, prediction))
|
---|
117 | select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
|
---|
118 | OnEstimatedValuesChanged();
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | private IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
123 | if (estimatedValues == null) RecalculateEstimatedValues();
|
---|
124 | foreach (int row in rows)
|
---|
125 | yield return estimatedValues[row];
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|