Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16904 was 16904, checked in by chaider, 5 years ago

#2971 Renamed NamedIntervals to IntervalCollection

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