Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs @ 16640

Last change on this file since 16640 was 16640, checked in by gkronber, 5 years ago

#2971: merged r16565:16631 from trunk/HeuristicLab.Problems.DataAnalysis to branch/HeuristicLab.Problems.DataAnalysis (resolving all conflicts)

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