Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17649 was 17647, checked in by chaider, 4 years ago

#3075

  • Added possibility to add noise to the feynman instances
  • Sorted instances by name
File size: 3.8 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 Feynman5 : FeynmanDescriptor {
9    private readonly int testSamples;
10    private readonly int trainingSamples;
11
12    public Feynman5() : this((int) DateTime.Now.Ticks, 10000, 10000, null) { }
13
14    public Feynman5(int seed) {
15      Seed            = seed;
16      trainingSamples = 10000;
17      testSamples     = 10000;
18      noiseRatio      = null;
19    }
20
21    public Feynman5(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("Feynman I.9.18 G*m1*m2/((x2-x1)**2+(y2-y1)**2+(z2-z1)**2) | {0} samples | noise ({1})",
31          trainingSamples, noiseRatio == null ? "no noise" : noiseRatio.ToString());
32      }
33    }
34
35    protected override string TargetVariable { get { return noiseRatio == null ? "F" : "F_noise"; } }
36
37    protected override string[] VariableNames {
38      get { return new[] {"m1", "m2", "G", "x1", "x2", "y1", "y2", "z1", "z2", noiseRatio == null ? "F" : "F_noise"}; }
39    }
40
41    protected override string[] AllowedInputVariables {
42      get { return new[] {"m1", "m2", "G", "x1", "x2", "y1", "y2", "z1", "z2"}; }
43    }
44
45    public int Seed { get; private set; }
46
47    protected override int TrainingPartitionStart { get { return 0; } }
48    protected override int TrainingPartitionEnd { get { return trainingSamples; } }
49    protected override int TestPartitionStart { get { return trainingSamples; } }
50    protected override int TestPartitionEnd { get { return trainingSamples + testSamples; } }
51
52    protected override List<List<double>> GenerateValues() {
53      var rand = new MersenneTwister((uint) Seed);
54
55      var data = new List<List<double>>();
56      var m1   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
57      var m2   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
58      var G    = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
59      var x1   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 3, 4).ToList();
60      var x2   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
61      var y1   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 3, 4).ToList();
62      var y2   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
63      var z1   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 3, 4).ToList();
64      var z2   = ValueGenerator.GenerateUniformDistributedValues(rand.Next(), TestPartitionEnd, 1, 2).ToList();
65
66      var F = new List<double>();
67
68      data.Add(m1);
69      data.Add(m2);
70      data.Add(G);
71      data.Add(x1);
72      data.Add(x2);
73      data.Add(y1);
74      data.Add(y2);
75      data.Add(z1);
76      data.Add(z2);
77      data.Add(F);
78
79      for (var i = 0; i < m1.Count; i++) {
80        var res = G[i] * m1[i] * m2[i] /
81                  (Math.Pow(x2[i] - x1[i], 2) + Math.Pow(y2[i] - y1[i], 2) + Math.Pow(z2[i] - z1[i], 2));
82        F.Add(res);
83      }
84
85      if (noiseRatio != null) {
86        var F_noise     = new List<double>();
87        var sigma_noise = (double) noiseRatio * F.StandardDeviationPop();
88        F_noise.AddRange(F.Select(md => md + NormalDistributedRandom.NextDouble(rand, 0, sigma_noise)));
89        data.Remove(F);
90        data.Add(F_noise);
91      }
92
93      return data;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.