[5540] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
[5601] | 22 | using System;
|
---|
[5540] | 23 | using System.Collections.Generic;
|
---|
[5559] | 24 | using System.IO;
|
---|
[5540] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[5586] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[5540] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 33 | [StorableClass]
|
---|
[5601] | 34 | [Item("RegressionProblemData", "Represents an item containing all data defining a regression problem.")]
|
---|
[6238] | 35 | public class RegressionProblemData : DataAnalysisProblemData, IRegressionProblemData {
|
---|
[5586] | 36 | private const string TargetVariableParameterName = "TargetVariable";
|
---|
[5540] | 37 |
|
---|
[5554] | 38 | #region default data
|
---|
| 39 | private static double[,] kozaF1 = new double[,] {
|
---|
| 40 | {2.017885919, -1.449165046},
|
---|
| 41 | {1.30060506, -1.344523885},
|
---|
| 42 | {1.147134798, -1.317989331},
|
---|
| 43 | {0.877182504, -1.266142284},
|
---|
| 44 | {0.852562452, -1.261020794},
|
---|
| 45 | {0.431095788, -1.158793317},
|
---|
| 46 | {0.112586002, -1.050908405},
|
---|
| 47 | {0.04594507, -1.021989402},
|
---|
| 48 | {0.042572879, -1.020438113},
|
---|
| 49 | {-0.074027291, -0.959859562},
|
---|
| 50 | {-0.109178553, -0.938094706},
|
---|
| 51 | {-0.259721109, -0.803635355},
|
---|
| 52 | {-0.272991057, -0.387519561},
|
---|
| 53 | {-0.161978191, -0.193611001},
|
---|
| 54 | {-0.102489983, -0.114215349},
|
---|
| 55 | {-0.01469968, -0.014918985},
|
---|
| 56 | {-0.008863365, -0.008942626},
|
---|
| 57 | {0.026751057, 0.026054094},
|
---|
| 58 | {0.166922436, 0.14309643},
|
---|
| 59 | {0.176953808, 0.1504144},
|
---|
| 60 | {0.190233418, 0.159916534},
|
---|
| 61 | {0.199800708, 0.166635331},
|
---|
| 62 | {0.261502822, 0.207600348},
|
---|
| 63 | {0.30182879, 0.232370249},
|
---|
| 64 | {0.83763905, 0.468046718}
|
---|
| 65 | };
|
---|
| 66 | private static Dataset defaultDataset;
|
---|
| 67 | private static IEnumerable<string> defaultAllowedInputVariables;
|
---|
| 68 | private static string defaultTargetVariable;
|
---|
| 69 |
|
---|
| 70 | static RegressionProblemData() {
|
---|
| 71 | defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
|
---|
[5559] | 72 | defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
|
---|
| 73 | defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
|
---|
[5554] | 74 | defaultAllowedInputVariables = new List<string>() { "x" };
|
---|
| 75 | defaultTargetVariable = "y";
|
---|
| 76 | }
|
---|
| 77 | #endregion
|
---|
| 78 |
|
---|
[5586] | 79 | public IValueParameter<StringValue> TargetVariableParameter {
|
---|
| 80 | get { return (IValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
[5540] | 81 | }
|
---|
[5601] | 82 | public string TargetVariable {
|
---|
| 83 | get { return TargetVariableParameter.Value.Value; }
|
---|
[5586] | 84 | }
|
---|
[5540] | 85 |
|
---|
[5554] | 86 | [StorableConstructor]
|
---|
[6238] | 87 | protected RegressionProblemData(bool deserializing) : base(deserializing) { }
|
---|
[5601] | 88 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 89 | private void AfterDeserialization() {
|
---|
| 90 | RegisterParameterEvents();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
[6238] | 94 | protected RegressionProblemData(RegressionProblemData original, Cloner cloner)
|
---|
[5601] | 95 | : base(original, cloner) {
|
---|
| 96 | RegisterParameterEvents();
|
---|
| 97 | }
|
---|
[5554] | 98 | public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblemData(this, cloner); }
|
---|
| 99 |
|
---|
[5540] | 100 | public RegressionProblemData()
|
---|
[5554] | 101 | : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public RegressionProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
|
---|
| 105 | : base(dataset, allowedInputVariables) {
|
---|
[5601] | 106 | var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
|
---|
| 107 | Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
|
---|
[5804] | 108 | RegisterParameterEvents();
|
---|
[5540] | 109 | }
|
---|
| 110 |
|
---|
[5601] | 111 | private void RegisterParameterEvents() {
|
---|
| 112 | TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
| 113 | }
|
---|
| 114 | private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 115 | OnChanged();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | #region Import from file
|
---|
[5559] | 119 | public static RegressionProblemData ImportFromFile(string fileName) {
|
---|
| 120 | TableFileParser csvFileParser = new TableFileParser();
|
---|
| 121 | csvFileParser.Parse(fileName);
|
---|
[5542] | 122 |
|
---|
[5559] | 123 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
| 124 | dataset.Name = Path.GetFileName(fileName);
|
---|
[5554] | 125 |
|
---|
[5559] | 126 | RegressionProblemData problemData = new RegressionProblemData(dataset, dataset.VariableNames.Skip(1), dataset.VariableNames.First());
|
---|
| 127 | problemData.Name = "Data imported from " + Path.GetFileName(fileName);
|
---|
| 128 | return problemData;
|
---|
[5542] | 129 | }
|
---|
[5601] | 130 | #endregion
|
---|
[5540] | 131 | }
|
---|
| 132 | }
|
---|