1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("RegressionProblemData", "Represents an item containing all data defining a regression problem.")]
|
---|
34 | public class RegressionProblemData : DataAnalysisProblemData, IRegressionProblemData, IStorableContent {
|
---|
35 | protected const string TargetVariableParameterName = "TargetVariable";
|
---|
36 | public string Filename { get; set; }
|
---|
37 |
|
---|
38 | #region default data
|
---|
39 | private static double[,] kozaF1 = new double[,] {
|
---|
40 | {2.017885919, -1.449165046},
|
---|
41 | {1.30060506, -1.344523885},
|
---|
42 | {1.147134798, -1.317989331},
|
---|
43 | {0.877182504, -1.266142284},
|
---|
44 | {0.852562452, -1.261020794},
|
---|
45 | {0.431095788, -1.158793317},
|
---|
46 | {0.112586002, -1.050908405},
|
---|
47 | {0.04594507, -1.021989402},
|
---|
48 | {0.042572879, -1.020438113},
|
---|
49 | {-0.074027291, -0.959859562},
|
---|
50 | {-0.109178553, -0.938094706},
|
---|
51 | {-0.259721109, -0.803635355},
|
---|
52 | {-0.272991057, -0.387519561},
|
---|
53 | {-0.161978191, -0.193611001},
|
---|
54 | {-0.102489983, -0.114215349},
|
---|
55 | {-0.01469968, -0.014918985},
|
---|
56 | {-0.008863365, -0.008942626},
|
---|
57 | {0.026751057, 0.026054094},
|
---|
58 | {0.166922436, 0.14309643},
|
---|
59 | {0.176953808, 0.1504144},
|
---|
60 | {0.190233418, 0.159916534},
|
---|
61 | {0.199800708, 0.166635331},
|
---|
62 | {0.261502822, 0.207600348},
|
---|
63 | {0.30182879, 0.232370249},
|
---|
64 | {0.83763905, 0.468046718}
|
---|
65 | };
|
---|
66 | private static readonly Dataset defaultDataset;
|
---|
67 | private static readonly IEnumerable<string> defaultAllowedInputVariables;
|
---|
68 | private static readonly string defaultTargetVariable;
|
---|
69 |
|
---|
70 | private static readonly RegressionProblemData emptyProblemData;
|
---|
71 | public static RegressionProblemData EmptyProblemData {
|
---|
72 | get { return emptyProblemData; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | static RegressionProblemData() {
|
---|
76 | defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
|
---|
77 | defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
|
---|
78 | defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
|
---|
79 | defaultAllowedInputVariables = new List<string>() { "x" };
|
---|
80 | defaultTargetVariable = "y";
|
---|
81 |
|
---|
82 | var problemData = new RegressionProblemData();
|
---|
83 | problemData.Parameters.Clear();
|
---|
84 | problemData.Name = "Empty Regression ProblemData";
|
---|
85 | problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
|
---|
86 | problemData.isEmpty = true;
|
---|
87 |
|
---|
88 | problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
|
---|
89 | problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
|
---|
90 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
91 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
92 | problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
|
---|
93 | emptyProblemData = problemData;
|
---|
94 | }
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | public IConstrainedValueParameter<StringValue> TargetVariableParameter {
|
---|
98 | get { return (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
99 | }
|
---|
100 | public string TargetVariable {
|
---|
101 | get { return TargetVariableParameter.Value.Value; }
|
---|
102 | set {
|
---|
103 | if (value == null) throw new ArgumentNullException("targetVariable", "The provided value for the targetVariable is null.");
|
---|
104 | if (value == TargetVariable) return;
|
---|
105 |
|
---|
106 | var matchingParameterValue = TargetVariableParameter.ValidValues.FirstOrDefault(v => v.Value == value);
|
---|
107 | if (matchingParameterValue == null) throw new ArgumentException("The provided value is not valid as the targetVariable.", "targetVariable");
|
---|
108 | TargetVariableParameter.Value = matchingParameterValue;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public IEnumerable<double> TargetVariableValues {
|
---|
113 | get { return Dataset.GetDoubleValues(TargetVariable); }
|
---|
114 | }
|
---|
115 | public IEnumerable<double> TargetVariableTrainingValues {
|
---|
116 | get { return Dataset.GetDoubleValues(TargetVariable, TrainingIndices); }
|
---|
117 | }
|
---|
118 | public IEnumerable<double> TargetVariableTestValues {
|
---|
119 | get { return Dataset.GetDoubleValues(TargetVariable, TestIndices); }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | [StorableConstructor]
|
---|
124 | protected RegressionProblemData(bool deserializing) : base(deserializing) { }
|
---|
125 | [StorableHook(HookType.AfterDeserialization)]
|
---|
126 | private void AfterDeserialization() {
|
---|
127 | RegisterParameterEvents();
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected RegressionProblemData(RegressionProblemData original, Cloner cloner)
|
---|
131 | : base(original, cloner) {
|
---|
132 | RegisterParameterEvents();
|
---|
133 | }
|
---|
134 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
135 | if (this == emptyProblemData) return emptyProblemData;
|
---|
136 | return new RegressionProblemData(this, cloner);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public RegressionProblemData()
|
---|
140 | : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
|
---|
141 | }
|
---|
142 | public RegressionProblemData(IRegressionProblemData regressionProblemData)
|
---|
143 | : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable) {
|
---|
144 | TrainingPartition.Start = regressionProblemData.TrainingPartition.Start;
|
---|
145 | TrainingPartition.End = regressionProblemData.TrainingPartition.End;
|
---|
146 | TestPartition.Start = regressionProblemData.TestPartition.Start;
|
---|
147 | TestPartition.End = regressionProblemData.TestPartition.End;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null)
|
---|
151 | : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
|
---|
152 | var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
|
---|
153 | Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
|
---|
154 | RegisterParameterEvents();
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void RegisterParameterEvents() {
|
---|
158 | TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
159 | }
|
---|
160 | private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
|
---|
161 | OnChanged();
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
|
---|
165 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
166 | IRegressionProblemData regressionProblemData = problemData as IRegressionProblemData;
|
---|
167 | if (regressionProblemData == null)
|
---|
168 | throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
|
---|
169 |
|
---|
170 | var returnValue = base.IsProblemDataCompatible(problemData, out errorMessage);
|
---|
171 | //check targetVariable
|
---|
172 | if (problemData.InputVariables.All(var => var.Value != TargetVariable)) {
|
---|
173 | errorMessage = string.Format("The target variable {0} is not present in the new problem data.", TargetVariable)
|
---|
174 | + Environment.NewLine + errorMessage;
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 | return returnValue;
|
---|
178 | }
|
---|
179 |
|
---|
180 | public override void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
|
---|
181 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
182 | RegressionProblemData regressionProblemData = problemData as RegressionProblemData;
|
---|
183 | if (regressionProblemData == null)
|
---|
184 | throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
|
---|
185 |
|
---|
186 | base.AdjustProblemDataProperties(problemData);
|
---|
187 | TargetVariable = regressionProblemData.TargetVariable;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|