source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/ShapeConstrainedRegressionProblemData.cs @ 17961

Last change on this file since 17961 was 17961, checked in by gkronber, 5 months ago

#3073: propagate variableRanges from ShapeConstrainedRegressionProblemData to RegressionProblemData

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [StorableType("8D44EABE-2D52-4501-B62D-5E28FB4CFEAE")]
33  [Item("ShapeConstrainedProblemData", "Represents an item containing all data defining a regression problem with shape constraints.")]
34  public class ShapeConstrainedRegressionProblemData : RegressionProblemData, IShapeConstrainedRegressionProblemData {
35    protected const string ShapeConstraintsParameterName = "ShapeConstraints";
36
37    private static double[,] kozaF1 = new double[,] {
38          {2.017885919, -1.449165046},
39          {1.30060506,  -1.344523885},
40          {1.147134798, -1.317989331},
41          {0.877182504, -1.266142284},
42          {0.852562452, -1.261020794},
43          {0.431095788, -1.158793317},
44          {0.112586002, -1.050908405},
45          {0.04594507,  -1.021989402},
46          {0.042572879, -1.020438113},
47          {-0.074027291,  -0.959859562},
48          {-0.109178553,  -0.938094706},
49          {-0.259721109,  -0.803635355},
50          {-0.272991057,  -0.387519561},
51          {-0.161978191,  -0.193611001},
52          {-0.102489983,  -0.114215349},
53          {-0.01469968, -0.014918985},
54          {-0.008863365,  -0.008942626},
55          {0.026751057, 0.026054094},
56          {0.166922436, 0.14309643},
57          {0.176953808, 0.1504144},
58          {0.190233418, 0.159916534},
59          {0.199800708, 0.166635331},
60          {0.261502822, 0.207600348},
61          {0.30182879,  0.232370249},
62          {0.83763905,  0.468046718}
63    };
64
65    private static readonly Dataset defaultDataset;
66    private static readonly IEnumerable<string> defaultAllowedInputVariables;
67    private static readonly string defaultTargetVariable;
68
69    private static readonly ShapeConstrainedRegressionProblemData emptyProblemData;
70    public new static ShapeConstrainedRegressionProblemData EmptyProblemData {
71      get { return emptyProblemData; }
72    }
73
74    static ShapeConstrainedRegressionProblemData() {
75      defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
76      defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
77      defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
78      defaultAllowedInputVariables = new List<string>() { "x" };
79      defaultTargetVariable = "y";
80      var problemData = new ShapeConstrainedRegressionProblemData();
81      problemData.Parameters.Clear(); // parameters are cleared and added below because we cannot set / change parameters of ProblemData
82      problemData.Name = "Empty Regression ProblemData";
83      problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
84      problemData.isEmpty = true;
85
86      problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
87      problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
88      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
89      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
90      problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
91      problemData.Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, "", new IntervalCollection()));
92      problemData.Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "", new ShapeConstraints()));
93      emptyProblemData = problemData;
94    }
95
96    public IFixedValueParameter<ShapeConstraints> ShapeConstraintsParameter => (IFixedValueParameter<ShapeConstraints>)Parameters[ShapeConstraintsParameterName];
97    public ShapeConstraints ShapeConstraints => ShapeConstraintsParameter.Value;
98
99    [StorableConstructor]
100    protected ShapeConstrainedRegressionProblemData(StorableConstructorFlag _) : base(_) { }
101
102    protected ShapeConstrainedRegressionProblemData(ShapeConstrainedRegressionProblemData original, Cloner cloner) : base(original, cloner) {
103      RegisterEventHandlers();
104    }
105
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      RegisterEventHandlers();
109    }
110    public override IDeepCloneable Clone(Cloner cloner) {
111      if (this == emptyProblemData) return emptyProblemData;
112      return new ShapeConstrainedRegressionProblemData(this, cloner);
113    }
114
115    public ShapeConstrainedRegressionProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable,
116                                                          trainingPartition: new IntRange(0, defaultDataset.Rows),
117                                                          testPartition: new IntRange(0, 0)) { } // no test partition for the demo problem
118
119    public ShapeConstrainedRegressionProblemData(IRegressionProblemData regressionProblemData)
120      : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable,
121          regressionProblemData.TrainingPartition, regressionProblemData.TestPartition, regressionProblemData.Transformations, null, regressionProblemData.VariableRanges) {
122    }
123
124    public ShapeConstrainedRegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
125                                                 IntRange trainingPartition, IntRange testPartition,
126                                                 IEnumerable<ITransformation> transformations = null, ShapeConstraints sc = null, IntervalCollection variableRanges = null)
127    : base(dataset, allowedInputVariables, targetVariable, transformations ?? Enumerable.Empty<ITransformation>(), variableRanges) {
128      TrainingPartition.Start = trainingPartition.Start;
129      TrainingPartition.End = trainingPartition.End;
130      TestPartition.Start = testPartition.Start;
131      TestPartition.End = testPartition.End;
132      if (sc == null) sc = new ShapeConstraints();
133      Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "Specifies the shape constraints for the regression problem.", (ShapeConstraints)sc.Clone()));
134
135      RegisterEventHandlers();
136    }
137
138    private void RegisterEventHandlers() {
139      ShapeConstraints.Changed += ShapeConstraints_Changed;
140      ShapeConstraints.CheckedItemsChanged += ShapeConstraints_Changed;
141      ShapeConstraints.CollectionReset += ShapeConstraints_Changed;
142      ShapeConstraints.ItemsAdded += ShapeConstraints_Changed;
143      ShapeConstraints.ItemsRemoved += ShapeConstraints_Changed;
144      ShapeConstraints.ItemsMoved += ShapeConstraints_Changed;
145      ShapeConstraints.ItemsReplaced += ShapeConstraints_Changed;
146    }
147
148    private void ShapeConstraints_Changed(object sender, EventArgs e) {
149      OnChanged();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.