Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/ValueGenerator.cs @ 17511

Last change on this file since 17511 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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