Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs @ 16592

Last change on this file since 16592 was 16592, checked in by chaider, 5 years ago

#2971

  • Added ContainsInterval method in Interval
  • Some renaming
  • Added ConstraintAnalyzer to SymbolicRegressionProblem
  • Added Counter for constraint violations in analyzer
File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Net.Mime;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis.Implementation;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [StorableClass]
35  [Item("RegressionProblemData", "Represents an item containing all data defining a regression problem.")]
36  public class RegressionProblemData : DataAnalysisProblemData, IRegressionProblemData, IStorableContent {
37    protected const string TargetVariableParameterName = "TargetVariable";
38    protected const string VariableRangesParameterName = "VariableRanges";
39    protected const string IntervalConstraintsParameterName = "IntervalConstraints";
40    public string Filename { get; set; }
41
42    #region default data
43    private static double[,] kozaF1 = new double[,] {
44          {2.017885919, -1.449165046},
45          {1.30060506,  -1.344523885},
46          {1.147134798, -1.317989331},
47          {0.877182504, -1.266142284},
48          {0.852562452, -1.261020794},
49          {0.431095788, -1.158793317},
50          {0.112586002, -1.050908405},
51          {0.04594507,  -1.021989402},
52          {0.042572879, -1.020438113},
53          {-0.074027291,  -0.959859562},
54          {-0.109178553,  -0.938094706},
55          {-0.259721109,  -0.803635355},
56          {-0.272991057,  -0.387519561},
57          {-0.161978191,  -0.193611001},
58          {-0.102489983,  -0.114215349},
59          {-0.01469968, -0.014918985},
60          {-0.008863365,  -0.008942626},
61          {0.026751057, 0.026054094},
62          {0.166922436, 0.14309643},
63          {0.176953808, 0.1504144},
64          {0.190233418, 0.159916534},
65          {0.199800708, 0.166635331},
66          {0.261502822, 0.207600348},
67          {0.30182879,  0.232370249},
68          {0.83763905,  0.468046718}
69    };
70    private static readonly Dataset defaultDataset;
71    private static readonly IEnumerable<string> defaultAllowedInputVariables;
72    private static readonly string defaultTargetVariable;
73
74    private static readonly RegressionProblemData emptyProblemData;
75    public static RegressionProblemData EmptyProblemData {
76      get { return emptyProblemData; }
77    }
78
79    static RegressionProblemData() {
80      defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
81      defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
82      defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
83      defaultAllowedInputVariables = new List<string>() { "x" };
84      defaultTargetVariable = "y";
85
86      var problemData = new RegressionProblemData();
87      problemData.Parameters.Clear();
88      problemData.Name = "Empty Regression ProblemData";
89      problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
90      problemData.isEmpty = true;
91
92      problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
93      problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
94      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
95      problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
96      problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
97      problemData.Parameters.Add(new FixedValueParameter<NamedIntervals>(VariableRangesParameterName,"", new NamedIntervals()));
98      problemData.Parameters.Add(new FixedValueParameter<TextValue>(IntervalConstraintsParameterName, "", new TextValue()));
99      emptyProblemData = problemData;
100    }
101    #endregion
102
103    public IConstrainedValueParameter<StringValue> TargetVariableParameter {
104      get { return (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
105    }
106
107    public IFixedValueParameter<NamedIntervals> VariableRangesParameter => (IFixedValueParameter<NamedIntervals>)Parameters[VariableRangesParameterName];
108
109    public NamedIntervals VariableRanges => VariableRangesParameter.Value;
110
111    public IFixedValueParameter<TextValue> IntervalConstraintsParameter => (IFixedValueParameter<TextValue>) Parameters[IntervalConstraintsParameterName];
112    public TextValue IntervalConstraints => IntervalConstraintsParameter.Value;
113
114    public string TargetVariable {
115      get { return TargetVariableParameter.Value.Value; }
116      set {
117        if (value == null) throw new ArgumentNullException("targetVariable", "The provided value for the targetVariable is null.");
118        if (value == TargetVariable) return;
119
120        var matchingParameterValue = TargetVariableParameter.ValidValues.FirstOrDefault(v => v.Value == value);
121        if (matchingParameterValue == null) throw new ArgumentException("The provided value is not valid as the targetVariable.", "targetVariable");
122        TargetVariableParameter.Value = matchingParameterValue;
123      }
124    }
125
126    public IEnumerable<double> TargetVariableValues {
127      get { return Dataset.GetDoubleValues(TargetVariable); }
128    }
129    public IEnumerable<double> TargetVariableTrainingValues {
130      get { return Dataset.GetDoubleValues(TargetVariable, TrainingIndices); }
131    }
132    public IEnumerable<double> TargetVariableTestValues {
133      get { return Dataset.GetDoubleValues(TargetVariable, TestIndices); }
134    }
135
136
137    [StorableConstructor]
138    protected RegressionProblemData(bool deserializing) : base(deserializing) { }
139    [StorableHook(HookType.AfterDeserialization)]
140    private void AfterDeserialization() {
141      RegisterParameterEvents();
142    }
143
144    protected RegressionProblemData(RegressionProblemData original, Cloner cloner)
145      : base(original, cloner) {
146      RegisterParameterEvents();
147    }
148    public override IDeepCloneable Clone(Cloner cloner) {
149      if (this == emptyProblemData) return emptyProblemData;
150      return new RegressionProblemData(this, cloner);
151    }
152
153    public RegressionProblemData()
154      : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
155    }
156    public RegressionProblemData(IRegressionProblemData regressionProblemData)
157      : this(regressionProblemData.Dataset, regressionProblemData.AllowedInputVariables, regressionProblemData.TargetVariable) {
158      TrainingPartition.Start = regressionProblemData.TrainingPartition.Start;
159      TrainingPartition.End = regressionProblemData.TrainingPartition.End;
160      TestPartition.Start = regressionProblemData.TestPartition.Start;
161      TestPartition.End = regressionProblemData.TestPartition.End;
162    }
163
164    public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null)
165      : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
166      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
167      Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
168
169      NamedIntervals namedIntervals = new NamedIntervals();
170      foreach (var variable in variables) {
171        var variableInteval = Interval.GetInterval(dataset.GetDoubleValues(variable.Value));
172        namedIntervals.Add(variable.Value, variableInteval);
173      }
174
175      Parameters.Add(new FixedValueParameter<NamedIntervals>(VariableRangesParameterName, namedIntervals));
176      Parameters.Add(new FixedValueParameter<TextValue>(IntervalConstraintsParameterName, new TextValue()));
177      RegisterParameterEvents();
178    }
179
180    private void RegisterParameterEvents() {
181      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
182    }
183    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
184      OnChanged();
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.