Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17964 was 17964, checked in by chaider, 3 years ago

#3073 Added sample on start page and default problem in shape constrained regression problem data

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