source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs @ 11009

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