Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Templates/RegressionProblemInstances/RegressionProblemInstance/RegressionProblemInstanceDescriptor.cs @ 17536

Last change on this file since 17536 was 14365, checked in by gkronber, 8 years ago

added template for regression problem instance providers

File size: 1.9 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Core;
4using HeuristicLab.Problems.DataAnalysis;
5
6
7namespace RegressionProblemInstances {
8  // This is a general descriptor for regression problem instances which produces data for a given target expression
9  public class RegressionProblemInstanceDescriptor : IRegressionProblemInstanceDescriptor {
10    private IRandom rand;
11    private int dim;
12    private Func<double[], double> func;
13    private int numRows;
14
15    public string Name { get; }
16    public string Description { get; }
17
18    public RegressionProblemInstanceDescriptor(string name, string desc, IRandom rand, int numRows, int dim, Func<double[], double> func) {
19      this.Name = name;
20      this.Description = desc;
21      this.rand = rand;
22      this.numRows = numRows;
23      this.dim = dim;
24      this.func = func;
25    }
26
27
28    public IRegressionProblemData GenerateData() {
29      var inputs = Enumerable.Range(1, dim).Select(idx => "x" + idx);
30      var target = "y";
31
32      // generate data
33      var x = new double[dim]; // for evaluating the expression
34      var data = new double[numRows, dim + 1];
35      int i = 0;
36      while (i < numRows) {
37        for (int j = 0; j < dim; j++) {
38          // we use the supplied PRND to generate x independently and identically distributed
39          // the PRND could use any kind of probability distribution
40          x[j] = rand.NextDouble();
41          data[i, j] = x[j];
42        }
43        data[i, dim] = func(x); // y = f(x)
44        // only accept reasonable values
45        if (!double.IsInfinity(data[i, dim]) && !double.IsNaN(data[i, dim])) i++;
46      }
47
48      var ds = new Dataset(inputs.Concat(new string[] { target }), data);
49      ds.Name = Name;
50      var probData = new RegressionProblemData(ds, inputs, target);
51      probData.Name = Name;
52      return probData;
53    }
54
55  }
56}
Note: See TracBrowser for help on using the repository browser.