Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs @ 17912

Last change on this file since 17912 was 17911, checked in by gkronber, 4 years ago

#3073 code improvements after reintegration of branch

File size: 9.7 KB
RevLine 
[17579]1#region License Information
[5540]2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[17579]25using HEAL.Attic;
[5540]26using HeuristicLab.Common;
[5586]27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
[5540]30
31namespace HeuristicLab.Problems.DataAnalysis {
[16565]32  [StorableType("EE612297-B1AF-42D2-BF21-AF9A2D42791C")]
[5601]33  [Item("RegressionProblemData", "Represents an item containing all data defining a regression problem.")]
[7134]34  public class RegressionProblemData : DataAnalysisProblemData, IRegressionProblemData, IStorableContent {
[6666]35    protected const string TargetVariableParameterName = "TargetVariable";
[17579]36    protected const string VariableRangesParameterName = "VariableRanges";
[17902]37    protected const string ShapeConstraintsParameterName = "ShapeConstraints";
[7134]38    public string Filename { get; set; }
[5540]39
[5554]40    #region default data
41    private static double[,] kozaF1 = new double[,] {
[15396]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}
[5554]67    };
[6672]68    private static readonly Dataset defaultDataset;
69    private static readonly IEnumerable<string> defaultAllowedInputVariables;
70    private static readonly string defaultTargetVariable;
[5554]71
[6672]72    private static readonly RegressionProblemData emptyProblemData;
[6666]73    public static RegressionProblemData EmptyProblemData {
74      get { return emptyProblemData; }
75    }
76
[5554]77    static RegressionProblemData() {
78      defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
[5559]79      defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
80      defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
[5554]81      defaultAllowedInputVariables = new List<string>() { "x" };
82      defaultTargetVariable = "y";
[6666]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>()));
[17579]95      problemData.Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, "", new IntervalCollection()));
[17902]96      problemData.Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "", new ShapeConstraints()));
[6666]97      emptyProblemData = problemData;
[5554]98    }
99    #endregion
100
[17911]101    #region parameter properties
102    public IConstrainedValueParameter<StringValue> TargetVariableParameter => (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName];
103    public IFixedValueParameter<ShapeConstraints> ShapeConstraintsParameter => (IFixedValueParameter<ShapeConstraints>)Parameters[ShapeConstraintsParameterName];
[17579]104    public IFixedValueParameter<IntervalCollection> VariableRangesParameter => (IFixedValueParameter<IntervalCollection>)Parameters[VariableRangesParameterName];
[17911]105    #endregion
[17579]106
[17911]107    #region properties
[17579]108    public IntervalCollection VariableRanges {
109      get => VariableRangesParameter.Value;
110    }
111
112
[17902]113    public ShapeConstraints ShapeConstraints => ShapeConstraintsParameter.Value;
114
115
[5601]116    public string TargetVariable {
117      get { return TargetVariableParameter.Value.Value; }
[10540]118      set {
119        if (value == null) throw new ArgumentNullException("targetVariable", "The provided value for the targetVariable is null.");
120        if (value == TargetVariable) return;
121
122        var matchingParameterValue = TargetVariableParameter.ValidValues.FirstOrDefault(v => v.Value == value);
123        if (matchingParameterValue == null) throw new ArgumentException("The provided value is not valid as the targetVariable.", "targetVariable");
124        TargetVariableParameter.Value = matchingParameterValue;
125      }
[5586]126    }
[17911]127    public IEnumerable<double> TargetVariableValues => Dataset.GetDoubleValues(TargetVariable);
128    public IEnumerable<double> TargetVariableTrainingValues => Dataset.GetDoubleValues(TargetVariable, TrainingIndices);
129    public IEnumerable<double> TargetVariableTestValues => Dataset.GetDoubleValues(TargetVariable, TestIndices);
130    #endregion
[5540]131
[13766]132
133
[5554]134    [StorableConstructor]
[16565]135    protected RegressionProblemData(StorableConstructorFlag _) : base(_) { }
[5601]136    [StorableHook(HookType.AfterDeserialization)]
137    private void AfterDeserialization() {
[17579]138      if (!Parameters.ContainsKey(VariableRangesParameterName)) {
[17911]139        var intervalCollection = Dataset.GetIntervals();
[17579]140        Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, intervalCollection));
141      }
[17911]142      if (Parameters.ContainsKey("IntervalConstraints")) {
[17902]143        var param = (IFixedValueParameter<ShapeConstraints>)Parameters["IntervalConstraints"];
144        Parameters.Remove(param);
145        Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, param.Value));
146      }
147      if (!Parameters.ContainsKey(ShapeConstraintsParameterName)) {
148        Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, new ShapeConstraints()));
149      }
150
[5601]151      RegisterParameterEvents();
152    }
153
[6238]154    protected RegressionProblemData(RegressionProblemData original, Cloner cloner)
[5601]155      : base(original, cloner) {
156      RegisterParameterEvents();
157    }
[6666]158    public override IDeepCloneable Clone(Cloner cloner) {
159      if (this == emptyProblemData) return emptyProblemData;
160      return new RegressionProblemData(this, cloner);
161    }
[5554]162
[5540]163    public RegressionProblemData()
[5554]164      : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
165    }
[8528]166    public RegressionProblemData(IRegressionProblemData regressionProblemData)
167      : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable) {
168      TrainingPartition.Start = regressionProblemData.TrainingPartition.Start;
169      TrainingPartition.End = regressionProblemData.TrainingPartition.End;
170      TestPartition.Start = regressionProblemData.TestPartition.Start;
171      TestPartition.End = regressionProblemData.TestPartition.End;
172    }
[5554]173
[17911]174    public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
[17902]175      IEnumerable<ITransformation> transformations = null,
176      IntervalCollection variableRanges = null,
177      ShapeConstraints shapeConstraints = null)
[11114]178      : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
[5601]179      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
180      Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
[17902]181      if (variableRanges == null) {
[17911]182        variableRanges = Dataset.GetIntervals();
[17902]183      }
184      Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, variableRanges));
[17911]185
[17902]186      if (shapeConstraints == null) {
187        shapeConstraints = new ShapeConstraints();
[17911]188      }
[17902]189      Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, shapeConstraints));
[5804]190      RegisterParameterEvents();
[5540]191    }
[5601]192    private void RegisterParameterEvents() {
[17911]193      TargetVariableParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
194      // VariableRanges and ShapeConstraints are fixed parameters
[5601]195    }
[17911]196    private void Parameter_ValueChanged(object sender, EventArgs e) {
[5601]197      OnChanged();
198    }
[5540]199  }
200}
Note: See TracBrowser for help on using the repository browser.