Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/RegressionProblemData.cs @ 5554

Last change on this file since 5554 was 5554, checked in by mkommend, 13 years ago

#1418: Refactored RegressionProblemData and related classes.

File size: 5.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [StorableClass]
33  public sealed class RegressionProblemData : DataAnalysisProblemData,
34    IRegressionProblemData {
35    private const string TargetVariableParameterName = "Target variable";
36
37    #region default data
38    private static double[,] kozaF1 = new double[,] {
39          {2.017885919, -1.449165046},
40          {1.30060506,  -1.344523885},
41          {1.147134798, -1.317989331},
42          {0.877182504, -1.266142284},
43          {0.852562452, -1.261020794},
44          {0.431095788, -1.158793317},
45          {0.112586002, -1.050908405},
46          {0.04594507,  -1.021989402},
47          {0.042572879, -1.020438113},
48          {-0.074027291,  -0.959859562},
49          {-0.109178553,  -0.938094706},
50          {-0.259721109,  -0.803635355},
51          {-0.272991057,  -0.387519561},
52          {-0.161978191,  -0.193611001},
53          {-0.102489983,  -0.114215349},
54          {-0.01469968, -0.014918985},
55          {-0.008863365,  -0.008942626},
56          {0.026751057, 0.026054094},
57          {0.166922436, 0.14309643},
58          {0.176953808, 0.1504144},
59          {0.190233418, 0.159916534},
60          {0.199800708, 0.166635331},
61          {0.261502822, 0.207600348},
62          {0.30182879,  0.232370249},
63          {0.83763905,  0.468046718}
64    };
65    private static Dataset defaultDataset;
66    private static IEnumerable<string> defaultAllowedInputVariables;
67    private static string defaultTargetVariable;
68
69    static RegressionProblemData() {
70      defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
71      defaultAllowedInputVariables = new List<string>() { "x" };
72      defaultTargetVariable = "y";
73    }
74    #endregion
75
76    #region parameter properties
77    public IValueParameter<StringValue> TargetVariableParameter {
78      get { return (IValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
79    }
80    #endregion
81    #region propeties
82    public string TargetVariable {
83      get { return TargetVariableParameter.Value.Value; }
84    }
85    #endregion
86
87    [StorableConstructor]
88    private RegressionProblemData(bool deserializing) : base(deserializing) { }
89    private RegressionProblemData(RegressionProblemData original, Cloner cloner)
90      : base(original, cloner) {
91      RegisterEventHandlers();
92    }
93    public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblemData(this, cloner); }
94
95    public RegressionProblemData()
96      : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
97    }
98
99    public RegressionProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
100      : base(dataset, allowedInputVariables) {
101      if (!dataset.VariableNames.Contains(targetVariable))
102        throw new ArgumentException("The target variable must be present in the dataset");
103
104      ConstrainedValueParameter<StringValue> targetVariableParameter = new ConstrainedValueParameter<StringValue>(TargetVariableParameterName);
105      foreach (var variableName in dataset.VariableNames) {
106        StringValue targetVariableValue = new StringValue(variableName);
107        targetVariableParameter.ValidValues.Add(targetVariableValue);
108        if (variableName == targetVariable)
109          targetVariableParameter.Value = targetVariableValue;
110      }
111
112      Parameters.Add(targetVariableParameter);
113      RegisterEventHandlers();
114    }
115
116
117
118    [StorableHook(HookType.AfterDeserialization)]
119    private void AfterDeserialization() {
120      RegisterEventHandlers();
121    }
122
123    #region event propagation
124    private void RegisterEventHandlers() {
125      TargetVariableParameter.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
126      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
127    }
128
129    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
130      TargetVariableParameter.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
131      OnChanged();
132    }
133
134    private void TargetVariable_ValueChanged(object sender, EventArgs e) {
135      OnChanged();
136    }
137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.