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

Last change on this file since 18034 was 18034, checked in by chaider, 11 months ago

#3073 Moved VariableRanges from RegressionProblemData to DataAnalysisProblemData to make it also available in classification problems

File size: 8.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    #region default data
38    private static double[,] sigmoid = new double[,] {
39      {1.00, 0.09, 0.01390952},
40      {1.10, 0.11, 0.048256016},
41      {1.20, 0.14, 0.010182641},
42      {1.30, 0.17, 0.270361269},
43      {1.40, 0.20, 0.091503971},
44      {1.50, 0.24, 0.338157191},
45      {1.60, 0.28, 0.328508579},
46      {1.70, 0.34, 0.21867684},
47      {1.80, 0.40, 0.34515433},
48      {1.90, 0.46, 0.562746903},
49      {2.00, 0.54, 0.554800831},
50      {2.10, 0.62, 0.623018787},
51      {2.20, 0.71, 0.626224329},
52      {2.30, 0.80, 0.909006688},
53      {2.40, 0.90, 0.92514929},
54      {2.50, 1.00, 1.097199936},
55      {2.60, 1.10, 1.138309608},
56      {2.70, 1.20, 1.087880692},
57      {2.80, 1.29, 1.370491683},
58      {2.90, 1.38, 1.422048792},
59      {3.00, 1.46, 1.505242141},
60      {3.10, 1.54, 1.684790135},
61      {3.20, 1.60, 1.480232277},
62      {3.30, 1.66, 1.577412501},
63      {3.40, 1.72, 1.664822534},
64      {3.50, 1.76, 1.773580664},
65      {3.60, 1.80, 1.941034478},
66      {3.70, 1.83, 1.730361986},
67      {3.80, 1.86, 1.9785952},
68      {3.90, 1.89, 1.946698641},
69      {4.00, 1.91, 1.766502803},
70      {4.10, 1.92, 1.847756843},
71      {4.20, 1.94, 1.894506213},
72      {4.30, 1.95, 2.029194724},
73      {4.40, 1.96, 2.01830679},
74      {4.50, 1.96, 1.924316332},
75      {4.60, 1.97, 1.971354792},
76      {4.70, 1.98, 1.85665728},
77      {4.80, 1.98, 1.831400496},
78      {4.90, 1.98, 2.057843156},
79      {5.00, 1.99, 2.128769896},
80    };
81
82    private static readonly Dataset defaultDataset;
83    private static readonly IEnumerable<string> defaultAllowedInputVariables;
84    private static readonly string defaultTargetVariable;
85    private static readonly ShapeConstraints defaultShapeConstraints;
86    private static readonly IntervalCollection defaultVariableRanges;
87
88    private static readonly ShapeConstrainedRegressionProblemData emptyProblemData;
89    public new static ShapeConstrainedRegressionProblemData EmptyProblemData => emptyProblemData;
90
91    static ShapeConstrainedRegressionProblemData() {
92      defaultDataset = new Dataset(new string[] { "x", "y", "y_noise" }, sigmoid) {
93        Name = "Sigmoid function for shape-constrained symbolic regression.",
94        Description = "f(x) = 1 + tanh(x - 2.5)"
95      };
96      defaultAllowedInputVariables = new List<string>() { "x" };
97      defaultTargetVariable = "y_noise";
98      defaultShapeConstraints = new ShapeConstraints {
99        new ShapeConstraint(new Interval(0, 2), 1.0),
100        new ShapeConstraint("x", 1, new Interval(0, double.PositiveInfinity), 1.0)
101      };
102      defaultVariableRanges = defaultDataset.GetVariableRanges();
103      defaultVariableRanges.SetInterval("x", new Interval(0, 6));
104
105      var problemData = new ShapeConstrainedRegressionProblemData();
106      problemData.Parameters.Clear();
107      problemData.Name = "Empty Regression ProblemData";
108      problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
109      problemData.isEmpty = true;
110
111      problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
112      problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
113      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 20).AsReadOnly()));
114      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(20, 40).AsReadOnly()));
115      problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
116      problemData.Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, "", new IntervalCollection()));
117      problemData.Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "", new ShapeConstraints()));
118      emptyProblemData = problemData;
119    }
120    #endregion
121
122    public IFixedValueParameter<ShapeConstraints> ShapeConstraintParameter => (IFixedValueParameter<ShapeConstraints>)Parameters[ShapeConstraintsParameterName];
123    public ShapeConstraints ShapeConstraints => ShapeConstraintParameter.Value;
124
125    [StorableConstructor]
126    protected ShapeConstrainedRegressionProblemData(StorableConstructorFlag _) : base(_) { }
127
128    protected ShapeConstrainedRegressionProblemData(ShapeConstrainedRegressionProblemData original, Cloner cloner) : base(original, cloner) {
129      RegisterEventHandlers();
130    }
131
132    [StorableHook(HookType.AfterDeserialization)]
133    private void AfterDeserialization() {
134      RegisterEventHandlers();
135    }
136    public override IDeepCloneable Clone(Cloner cloner) {
137      return this == emptyProblemData ? emptyProblemData : new ShapeConstrainedRegressionProblemData(this, cloner);
138    }
139
140    public ShapeConstrainedRegressionProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable,
141                                                          trainingPartition: new IntRange(0, defaultDataset.Rows),
142                                                          testPartition: new IntRange(0, 0), sc: defaultShapeConstraints, variableRanges: defaultVariableRanges) { } // no test partition for the demo problem
143
144    public ShapeConstrainedRegressionProblemData(IRegressionProblemData regressionProblemData)
145      : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable,
146          regressionProblemData.TrainingPartition, regressionProblemData.TestPartition, regressionProblemData.Transformations,
147          (regressionProblemData is ShapeConstrainedRegressionProblemData) ? ((ShapeConstrainedRegressionProblemData)regressionProblemData).ShapeConstraints : null,
148          regressionProblemData.VariableRanges) {
149    }
150
151    public ShapeConstrainedRegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
152                                                 IntRange trainingPartition, IntRange testPartition,
153                                                 IEnumerable<ITransformation> transformations = null, ShapeConstraints sc = null, IntervalCollection variableRanges = null)
154    : base(dataset, allowedInputVariables, targetVariable, transformations ?? Enumerable.Empty<ITransformation>()) {
155      TrainingPartition.Start = trainingPartition.Start;
156      TrainingPartition.End = trainingPartition.End;
157      TestPartition.Start = testPartition.Start;
158      TestPartition.End = testPartition.End;
159      if (sc == null) sc = new ShapeConstraints();
160      Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "Specifies the shape constraints for the regression problem.", (ShapeConstraints)sc.Clone()));
161
162      RegisterEventHandlers();
163    }
164
165    private void RegisterEventHandlers() {
166      ShapeConstraints.Changed += ShapeConstraints_Changed;
167      ShapeConstraints.CheckedItemsChanged += ShapeConstraints_Changed;
168      ShapeConstraints.CollectionReset += ShapeConstraints_Changed;
169      ShapeConstraints.ItemsAdded += ShapeConstraints_Changed;
170      ShapeConstraints.ItemsRemoved += ShapeConstraints_Changed;
171      ShapeConstraints.ItemsMoved += ShapeConstraints_Changed;
172      ShapeConstraints.ItemsReplaced += ShapeConstraints_Changed;
173    }
174
175    private void ShapeConstraints_Changed(object sender, EventArgs e) {
176      OnChanged();
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.