Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3075_aifeynman_instances/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Feynman/Feynman1.cs @ 17676

Last change on this file since 17676 was 17674, checked in by gkronber, 4 years ago

#3075 small changes while reviewing

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Random;
6
7namespace HeuristicLab.Problems.Instances.DataAnalysis {
8  public class Feynman1 : FeynmanDescriptor {
9    private readonly int testSamples;
10    private readonly int trainingSamples;
11
12    public Feynman1() : this((int) DateTime.Now.Ticks, 10000, 10000, null) { }
13
14    public Feynman1(int seed) {
15      Seed            = seed;
16      trainingSamples = 10000;
17      testSamples     = 10000;
18      noiseRatio      = null;
19    }
20
21    public Feynman1(int seed, int trainingSamples, int testSamples, double? noiseRatio) {
22      Seed                 = seed;
23      this.trainingSamples = trainingSamples;
24      this.testSamples     = testSamples;
25      this.noiseRatio      = noiseRatio;
26    }
27
28    public override string Name {
29      get {
30        return string.Format("I.6.20a exp(-theta**2/2)/sqrt(2*pi) | {0} samples | noise ({1})", trainingSamples,
31          noiseRatio == null ? "no noise" : noiseRatio.ToString());
32      }
33    }
34
35    protected override string TargetVariable { get { return noiseRatio == null ? "f" : "f_noise"; } }
36    protected override string[] VariableNames { get { return new[] {"theta", noiseRatio == null ? "f" : "f_noise"}; } }
37    protected override string[] AllowedInputVariables { get { return new[] {"theta"}; } }
38
39    public int Seed { get; private set; }
40
41    protected override int TrainingPartitionStart { get { return 0; } }
42    protected override int TrainingPartitionEnd { get { return trainingSamples; } }
43    protected override int TestPartitionStart { get { return trainingSamples; } }
44    protected override int TestPartitionEnd { get { return trainingSamples + testSamples; } }
45
46    protected override List<List<double>> GenerateValues() {
47      var rand = new MersenneTwister((uint) Seed);
48
49      var data  = new List<List<double>>();
50      var theta = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 3).ToList();
51
52      var f = new List<double>();
53
54      data.Add(theta);
55      data.Add(f);
56
57      for (var i = 0; i < theta.Count; i++) {
58        var res = Math.Exp(Math.Pow(-theta[i], 2) / 2) / Math.Sqrt(2 * Math.PI);
59        f.Add(res);
60      }
61
62      if (noiseRatio != null) {
63        var f_noise     = new List<double>();
64        var sigma_noise = (double) noiseRatio * f.StandardDeviationPop();
65        f_noise.AddRange(f.Select(md => md + NormalDistributedRandom.NextDouble(rand, 0, sigma_noise)));
66        data.Remove(f);
67        data.Add(f_noise);
68      }
69
70      return data;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.