[17388] | 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] | 22 | using System;
|
---|
[5540] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[5586] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[5540] | 30 |
|
---|
| 31 | namespace 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";
|
---|
[17388] | 36 | protected const string ScaleInputsParameterName = "Scale Inputs";
|
---|
[7134] | 37 | public string Filename { get; set; }
|
---|
[5540] | 38 |
|
---|
[5554] | 39 | #region default data
|
---|
| 40 | private static double[,] kozaF1 = new double[,] {
|
---|
[15396] | 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}
|
---|
[5554] | 66 | };
|
---|
[6672] | 67 | private static readonly Dataset defaultDataset;
|
---|
| 68 | private static readonly IEnumerable<string> defaultAllowedInputVariables;
|
---|
| 69 | private static readonly string defaultTargetVariable;
|
---|
[5554] | 70 |
|
---|
[6672] | 71 | private static readonly RegressionProblemData emptyProblemData;
|
---|
[6666] | 72 | public static RegressionProblemData EmptyProblemData {
|
---|
| 73 | get { return emptyProblemData; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[5554] | 76 | static RegressionProblemData() {
|
---|
| 77 | defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
|
---|
[5559] | 78 | defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
|
---|
| 79 | defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
|
---|
[5554] | 80 | defaultAllowedInputVariables = new List<string>() { "x" };
|
---|
| 81 | defaultTargetVariable = "y";
|
---|
[6666] | 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;
|
---|
[5554] | 95 | }
|
---|
| 96 | #endregion
|
---|
| 97 |
|
---|
[8121] | 98 | public IConstrainedValueParameter<StringValue> TargetVariableParameter {
|
---|
| 99 | get { return (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
[5540] | 100 | }
|
---|
[5601] | 101 | public string TargetVariable {
|
---|
| 102 | get { return TargetVariableParameter.Value.Value; }
|
---|
[10540] | 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 | }
|
---|
[5586] | 111 | }
|
---|
[5540] | 112 |
|
---|
[17388] | 113 | public IFixedValueParameter<BoolValue> ScaleInputsParameter {
|
---|
| 114 | get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleInputsParameterName]; }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public bool ScaleInputs {
|
---|
| 118 | get { return ScaleInputsParameter.Value.Value; }
|
---|
| 119 | set { ScaleInputsParameter.Value.Value = value; }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[13766] | 122 | public IEnumerable<double> TargetVariableValues {
|
---|
| 123 | get { return Dataset.GetDoubleValues(TargetVariable); }
|
---|
| 124 | }
|
---|
| 125 | public IEnumerable<double> TargetVariableTrainingValues {
|
---|
| 126 | get { return Dataset.GetDoubleValues(TargetVariable, TrainingIndices); }
|
---|
| 127 | }
|
---|
| 128 | public IEnumerable<double> TargetVariableTestValues {
|
---|
| 129 | get { return Dataset.GetDoubleValues(TargetVariable, TestIndices); }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[5554] | 132 | [StorableConstructor]
|
---|
[16565] | 133 | protected RegressionProblemData(StorableConstructorFlag _) : base(_) { }
|
---|
[5601] | 134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 135 | private void AfterDeserialization() {
|
---|
[17388] | 136 | if (!Parameters.ContainsKey(ScaleInputsParameterName)) {
|
---|
| 137 | Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputsParameterName, "If enabled input features are scaled by a standard transformation (µ=0, σ=1)", new BoolValue(false)));
|
---|
| 138 | }
|
---|
[5601] | 139 | RegisterParameterEvents();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[6238] | 142 | protected RegressionProblemData(RegressionProblemData original, Cloner cloner)
|
---|
[5601] | 143 | : base(original, cloner) {
|
---|
| 144 | RegisterParameterEvents();
|
---|
| 145 | }
|
---|
[6666] | 146 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 147 | if (this == emptyProblemData) return emptyProblemData;
|
---|
| 148 | return new RegressionProblemData(this, cloner);
|
---|
| 149 | }
|
---|
[5554] | 150 |
|
---|
[5540] | 151 | public RegressionProblemData()
|
---|
[5554] | 152 | : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
|
---|
| 153 | }
|
---|
[8528] | 154 | public RegressionProblemData(IRegressionProblemData regressionProblemData)
|
---|
| 155 | : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable) {
|
---|
| 156 | TrainingPartition.Start = regressionProblemData.TrainingPartition.Start;
|
---|
| 157 | TrainingPartition.End = regressionProblemData.TrainingPartition.End;
|
---|
| 158 | TestPartition.Start = regressionProblemData.TestPartition.Start;
|
---|
| 159 | TestPartition.End = regressionProblemData.TestPartition.End;
|
---|
| 160 | }
|
---|
[5554] | 161 |
|
---|
[12509] | 162 | public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null)
|
---|
[11114] | 163 | : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
|
---|
[5601] | 164 | var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
|
---|
| 165 | Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
|
---|
[17388] | 166 | Parameters.Add(new FixedValueParameter<BoolValue>(ScaleInputsParameterName, "If enabled input features are scaled by a standard transformation (µ=0, σ=1)", new BoolValue(false)));
|
---|
[5804] | 167 | RegisterParameterEvents();
|
---|
[5540] | 168 | }
|
---|
| 169 |
|
---|
[5601] | 170 | private void RegisterParameterEvents() {
|
---|
| 171 | TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
[17391] | 172 | ScaleInputsParameter.Value.ValueChanged += new EventHandler(ScaleInputsParameter_ValueChanged);
|
---|
[5601] | 173 | }
|
---|
| 174 | private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 175 | OnChanged();
|
---|
| 176 | }
|
---|
[17391] | 177 |
|
---|
| 178 | private void ScaleInputsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 179 | var transformations = new ShiftStandardDistributionTransformation(Dataset.DoubleVariables);
|
---|
| 180 | transformations.Mean = 0;
|
---|
| 181 | transformations.StandardDeviation = 1;
|
---|
| 182 | var scaling = Transformation.CreateTransformations(transformations, Dataset, TrainingIndices, AllowedInputVariables);
|
---|
| 183 |
|
---|
| 184 | var scaledVariables = AllowedInputVariables.Select((var, i) => new { Variable = var, Data = scaling[i].Apply(Dataset.GetDoubleValues(var)).ToArray() });
|
---|
| 185 | var newDataset = ((Dataset)Dataset).ToModifiable();
|
---|
| 186 |
|
---|
| 187 | foreach (var v in scaledVariables) {
|
---|
| 188 | newDataset.ReplaceVariable(v.Variable, v.Data);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (!Parameters.ContainsKey("Scaled Dataset"))
|
---|
| 192 | Parameters.Add(new FixedValueParameter<Dataset>("Scaled Dataset", newDataset.ToDataset()));
|
---|
| 193 | }
|
---|
[5540] | 194 | }
|
---|
| 195 | }
|
---|