Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VectorData/VectorDataTestOne.cs @ 17448

Last change on this file since 17448 was 17448, checked in by pfleck, 4 years ago

#3040 Replaced own Vector with MathNet.Numerics Vector.

File size: 6.1 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;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Core;
27using HeuristicLab.Problems.DataAnalysis;
28using HeuristicLab.Random;
29using MathNet.Numerics.Statistics;
30
31using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
32
33namespace HeuristicLab.Problems.Instances.DataAnalysis {
34  public abstract class VectorDataTestBase : ArtificialRegressionDataDescriptor {
35
36    protected const int Rows = 100;
37
38    public override string Description { get { return ""; } }
39
40    protected override string TargetVariable { get { return "Y"; } }
41    protected override string[] VariableNames { get { return new string[] { "X1", "X2", "X3", "V1", "V2", "Y" }; } }
42    protected override string[] AllowedInputVariables { get { return new string[] { "X1", "X2", "X3", "V1", "V2" }; } }
43    protected override int TrainingPartitionStart { get { return 0; } }
44    protected override int TrainingPartitionEnd { get { return Rows * 3 / 4; } }
45    protected override int TestPartitionStart { get { return TrainingPartitionEnd; } }
46    protected override int TestPartitionEnd { get { return Rows; } }
47
48    public int Seed { get; private set; }
49
50    protected VectorDataTestBase()
51      : this((int)DateTime.Now.Ticks) { }
52    protected VectorDataTestBase(int seed)
53      : base() {
54      Seed = seed;
55    }
56
57
58    protected override List<List<double>> GenerateValues() { return null; }
59    protected override List<IList> GenerateValuesExtended() {
60      var rand = new MersenneTwister((uint)Seed);
61
62      double x1, x2, x3;
63      DoubleVector v1, v2;
64      double y;
65
66      var x1Column = new List<double>(Rows);
67      var x2Column = new List<double>(Rows);
68      var x3Column = new List<double>(Rows);
69      var v1Column = new List<DoubleVector>(Rows);
70      var v2Column = new List<DoubleVector>(Rows);
71      var yColumn = new List<double>(Rows);
72
73      var vectorLengths = GetVectorLengths(rand);
74      for (int i = 0; i < Rows; i++) {
75        x1 = rand.NextDouble(-2, 2);
76        x2 = rand.NextDouble(2, 6);
77        x3 = rand.NextDouble(0, 1);
78        int v1Length = vectorLengths[0][i];
79        int v2Length = vectorLengths[1][i];
80        v1 = rand.NextDoubleVector(-2, 2, v1Length);
81        v2 = rand.NextDoubleVector(3, 5, v2Length);
82
83        y = x1 * v1.Sum() + x2 * v2.Mean();
84
85        x1Column.Add(x1);
86        x2Column.Add(x2);
87        x3Column.Add(x3);
88        v1Column.Add(v1);
89        v2Column.Add(v2);
90        yColumn.Add(y);
91      }
92
93      return new List<IList> { x1Column, x2Column, x3Column, v1Column, v2Column, yColumn };
94    }
95
96    protected abstract List<int>[] GetVectorLengths(IRandom rand);
97  }
98
99  public class VectorDataTestOneA : VectorDataTestBase {
100    public override string Name {
101      get { return "Vector Data Test - I a (Y = X1 * sum(V1) + X2 * avg(V2)"; }
102    }
103
104    public VectorDataTestOneA() : base() { }
105    public VectorDataTestOneA(int seed) : base(seed) { }
106
107    protected override List<int>[] GetVectorLengths(IRandom rand) {
108      // always same length
109      const int length = 5;
110      return new List<int>[2] {
111        Enumerable.Repeat(length, Rows).ToList(),
112        Enumerable.Repeat(length, Rows).ToList()
113      };
114    }
115  }
116
117  public class VectorDataTestOneB : VectorDataTestBase {
118    public override string Name { get { return "Vector Data Test - I b (Y = X1 * sum(V1) + X2 * avg(V2)"; } }
119
120    public VectorDataTestOneB() : base() { }
121    public VectorDataTestOneB(int seed) : base(seed) { }
122
123    protected override List<int>[] GetVectorLengths(IRandom rand) {
124      // length between length 4 and 8, same row always the same length
125      var lengths = Enumerable.Range(0, Rows).Select(i => rand.Next(4, 8)).ToList();
126      return new List<int>[2] {
127        Enumerable.Range(0, Rows).Select(i => lengths[i]).ToList(),
128        Enumerable.Range(0, Rows).Select(i => lengths[i]).ToList()
129      };
130    }
131  }
132
133  public class VectorDataTestOneC : VectorDataTestBase {
134    public override string Name { get { return "Vector Data Test - I c (Y = X1 * sum(V1) + X2 * avg(V2)"; } }
135
136    public VectorDataTestOneC() : base() { }
137    public VectorDataTestOneC(int seed) : base(seed) { }
138
139    protected override List<int>[] GetVectorLengths(IRandom rand) {
140      // length between length 4 and 8; each feature is same length
141      // force two different lengths
142      int v1Length = rand.Next(4, 8);
143      int v2Length;
144      do {
145        v2Length = rand.Next(4, 8);
146      } while (v1Length != v2Length);
147      return new List<int>[2] {
148        Enumerable.Repeat(v1Length, Rows).ToList(),
149        Enumerable.Repeat(v2Length, Rows).ToList()
150      };
151    }
152  }
153
154  public class VectorDataTestOneD : VectorDataTestBase {
155    public override string Name { get { return "Vector Data Test - I d (Y = X1 * sum(V1) + X2 * avg(V2)"; } }
156
157    public VectorDataTestOneD() : base() { }
158    public VectorDataTestOneD(int seed) : base(seed) { }
159
160    protected override List<int>[] GetVectorLengths(IRandom rand) {
161      // always random between 4 and 8
162      return new List<int>[2] {
163          Enumerable.Range(0, Rows).Select(i => rand.Next(4, 8)).ToList(),
164          Enumerable.Range(0, Rows).Select(i => rand.Next(4, 8)).ToList()
165        };
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.