Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971 Added AfterDeserialization hook to RegressionProblemData to add VariableRanges and IntervalConstraints

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