Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13325 was 12292, checked in by pfleck, 10 years ago

#2301 Removed the GenerateSteps from the ValueGenerator and put it into the new SequenceGenerator.
Adapted DataAnalysis-Instances and scripts (samples and unit tests).

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