Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/ValueGenerator.cs @ 18032

Last change on this file since 18032 was 18032, checked in by chaider, 3 years ago

#3075 noise generation method to ValueGenerator; use same method for generating noise in friedman and feynman instances

File size: 4.7 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.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.Problems.Instances.DataAnalysis {
30  internal static class ValueGenerator {
31
32    /// <summary>
33    /// Generates uniformly distributed values between start and end (inclusive!)
34    /// </summary>
35    /// <param name="seed">The seed for the random number generator</param>
36    /// <param name="n">Number of values to generate.</param>
37    /// <param name="start">The lower value (inclusive)</param>
38    /// <param name="end">The upper value (inclusive)</param>
39    /// <returns>An enumerable including n values in [start, end]</returns>
40    public static IEnumerable<double> GenerateUniformDistributedValues(int seed, int n, double start, double end) {
41      var rand = new FastRandom(seed);
42      for (int i = 0; i < n; i++) {
43        // we need to return a random value including end.
44        // so we cannot use rand.NextDouble() as it returns a value strictly smaller than 1.
45        double r = rand.NextUInt() / (double)uint.MaxValue;    // r \in [0,1]
46        yield return r * (end - start) + start;
47      }
48    }
49
50    /// <summary>
51    /// Generates normally distributed values sampling from N(mu, sigma)
52    /// </summary>
53    /// <param name="seed">The seed for the random number generator</param>
54    /// <param name="n">Number of values to generate.</param>
55    /// <param name="mu">The mu parameter of the normal distribution</param>
56    /// <param name="sigma">The sigma parameter of the normal distribution</param>
57    /// <returns>An enumerable including n values ~ N(mu, sigma)</returns>
58    public static IEnumerable<double> GenerateNormalDistributedValues(int seed, int n, double mu, double sigma) {
59      var rand = new FastRandom(seed);
60      for (int i = 0; i < n; i++)
61        yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
62    }
63
64    // Generate the cartesian product.
65    // The result is transposed, therefore the inner lists represent a column of values instead of a combination-pair.
66    public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
67      List<List<double>> allCombinations = new List<List<double>>();
68      if (lists.Count < 1) {
69        return allCombinations;
70      }
71
72      List<IEnumerator<double>> enumerators = new List<IEnumerator<double>>();
73      foreach (var list in lists) {
74        allCombinations.Add(new List<double>());
75        enumerators.Add(list.GetEnumerator());
76      }
77
78      bool finished = !enumerators.All(x => x.MoveNext());
79
80      while (!finished) {
81        GetCurrentCombination(enumerators, allCombinations);
82        finished = MoveNext(enumerators, lists);
83      }
84      return allCombinations;
85    }
86
87    private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
88      int cur = enumerators.Count - 1;
89      while (cur >= 0 && !enumerators[cur].MoveNext()) {
90        enumerators[cur] = lists[cur].GetEnumerator();
91        enumerators[cur].MoveNext();
92        cur--;
93      }
94      return cur < 0;
95    }
96
97    private static void GetCurrentCombination(List<IEnumerator<double>> enumerators, List<List<double>> allCombinations) {
98      for (int i = 0; i < enumerators.Count(); i++) {
99        allCombinations[i].Add(enumerators[i].Current);
100      }
101    }
102
103    public static List<double> GenerateNoise(IEnumerable<double> target, IRandom rand, double? noiseRatio) {
104      if (noiseRatio == null) return null;
105
106      var targetNoise = new List<double>();
107      var targetSigma = target.StandardDeviation();
108      var noisePrng = new NormalDistributedRandomPolar(rand, 0, targetSigma * Math.Sqrt(noiseRatio.Value / (1.0 - noiseRatio.Value)));
109
110      targetNoise.AddRange(target.Select(t => t + noisePrng.NextDouble()).ToList());
111      return targetNoise;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.