1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HEAL.Attic;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
29 | [StorableType("2998B895-4724-489C-A4CA-9ADD10C7CA49")]
|
---|
30 | [Item("Regression Model", "Base class for all regression models.")]
|
---|
31 | public abstract class RegressionModel : DataAnalysisModel, IRegressionModel {
|
---|
32 | [Storable]
|
---|
33 | private string targetVariable;
|
---|
34 | public string TargetVariable {
|
---|
35 | get { return targetVariable; }
|
---|
36 | set {
|
---|
37 | if (string.IsNullOrEmpty(value) || targetVariable == value) return;
|
---|
38 | targetVariable = value;
|
---|
39 | OnTargetVariableChanged(this, EventArgs.Empty);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected RegressionModel(StorableConstructorFlag _)
|
---|
45 | : base(_) {
|
---|
46 | targetVariable = string.Empty;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected RegressionModel(RegressionModel original, Cloner cloner)
|
---|
50 | : base(original, cloner) {
|
---|
51 | this.targetVariable = original.targetVariable;
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected RegressionModel(string targetVariable)
|
---|
55 | : base("Regression Model") {
|
---|
56 | this.targetVariable = targetVariable;
|
---|
57 | }
|
---|
58 | protected RegressionModel(string targetVariable, string name)
|
---|
59 | : base(name) {
|
---|
60 | this.targetVariable = targetVariable;
|
---|
61 | }
|
---|
62 | protected RegressionModel(string targetVariable, string name, string description)
|
---|
63 | : base(name, description) {
|
---|
64 | this.targetVariable = targetVariable;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public abstract IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows);
|
---|
68 | public abstract IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData);
|
---|
69 |
|
---|
70 | public virtual bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
|
---|
71 | return IsProblemDataCompatible(this, problemData, out errorMessage);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
|
---|
75 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
76 | var regressionProblemData = problemData as IRegressionProblemData;
|
---|
77 | if (regressionProblemData == null)
|
---|
78 | throw new ArgumentException("The problem data is not compatible with this regression model. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
|
---|
79 | return IsProblemDataCompatible(regressionProblemData, out errorMessage);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static bool IsProblemDataCompatible(IRegressionModel model, IRegressionProblemData problemData, out string errorMessage) {
|
---|
83 | if (model == null) throw new ArgumentNullException("model", "The provided model is null.");
|
---|
84 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
85 | errorMessage = string.Empty;
|
---|
86 |
|
---|
87 | if (model.TargetVariable != problemData.TargetVariable)
|
---|
88 | errorMessage = string.Format("The target variable of the model {0} does not match the target variable of the problemData {1}.", model.TargetVariable, problemData.TargetVariable);
|
---|
89 |
|
---|
90 | var evaluationErrorMessage = string.Empty;
|
---|
91 | var datasetCompatible = model.IsDatasetCompatible(problemData.Dataset, out evaluationErrorMessage);
|
---|
92 | if (!datasetCompatible)
|
---|
93 | errorMessage += evaluationErrorMessage;
|
---|
94 |
|
---|
95 | return string.IsNullOrEmpty(errorMessage);
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region events
|
---|
99 | public event EventHandler TargetVariableChanged;
|
---|
100 | private void OnTargetVariableChanged(object sender, EventArgs args) {
|
---|
101 | var changed = TargetVariableChanged;
|
---|
102 | if (changed != null)
|
---|
103 | changed(sender, args);
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 | }
|
---|
107 | }
|
---|