1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Random;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
28 | public static class ValueGenerator {
|
---|
29 | private static FastRandom rand = new FastRandom();
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// Generates a sequence of evenly spaced points between start and end (inclusive!).
|
---|
33 | /// </summary>
|
---|
34 | /// <param name="start">The smallest and first value of the sequence.</param>
|
---|
35 | /// <param name="end">The largest and last value of the sequence.</param>
|
---|
36 | /// <param name="stepWidth">The step size between subsequent values.</param>
|
---|
37 | /// <returns>An sequence of values from start to end (inclusive)</returns>
|
---|
38 | public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth) {
|
---|
39 | if (start > end) throw new ArgumentException("start must be less than or equal end.");
|
---|
40 | if (stepWidth <= 0) throw new ArgumentException("stepwith must be larger than zero.", "stepWidth");
|
---|
41 | double x = start;
|
---|
42 | while (x <= end) {
|
---|
43 | yield return x;
|
---|
44 | x += stepWidth;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Generates uniformly distributed values between start and end (inclusive!)
|
---|
50 | /// </summary>
|
---|
51 | /// <param name="n">Number of values to generate.</param>
|
---|
52 | /// <param name="start">The lower value (inclusive)</param>
|
---|
53 | /// <param name="end">The upper value (inclusive)</param>
|
---|
54 | /// <returns>An enumerable including n values in [start, end]</returns>
|
---|
55 | public static IEnumerable<double> GenerateUniformDistributedValues(int n, double start, double end) {
|
---|
56 | for (int i = 0; i < n; i++) {
|
---|
57 | // we need to return a random value including end.
|
---|
58 | // so we cannot use rand.NextDouble() as it returns a value strictly smaller than 1.
|
---|
59 | double r = rand.NextUInt() / (double)uint.MaxValue; // r \in [0,1]
|
---|
60 | yield return r * (end - start) + start;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Generates normally distributed values sampling from N(mu, sigma)
|
---|
66 | /// </summary>
|
---|
67 | /// <param name="n">Number of values to generate.</param>
|
---|
68 | /// <param name="mu">The mu parameter of the normal distribution</param>
|
---|
69 | /// <param name="sigma">The sigma parameter of the normal distribution</param>
|
---|
70 | /// <returns>An enumerable including n values ~ N(mu, sigma)</returns>
|
---|
71 | public static IEnumerable<double> GenerateNormalDistributedValues(int n, double mu, double sigma) {
|
---|
72 | for (int i = 0; i < n; i++)
|
---|
73 | yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
|
---|
74 | }
|
---|
75 |
|
---|
76 | // iterative approach
|
---|
77 | public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
|
---|
78 | List<List<double>> allCombinations = new List<List<double>>();
|
---|
79 | if (lists.Count < 1) {
|
---|
80 | return allCombinations;
|
---|
81 | }
|
---|
82 |
|
---|
83 | List<IEnumerator<double>> enumerators = new List<IEnumerator<double>>();
|
---|
84 | foreach (var list in lists) {
|
---|
85 | allCombinations.Add(new List<double>());
|
---|
86 | enumerators.Add(list.GetEnumerator());
|
---|
87 | }
|
---|
88 |
|
---|
89 | bool finished = !enumerators.All(x => x.MoveNext());
|
---|
90 |
|
---|
91 | while (!finished) {
|
---|
92 | GetCurrentCombination(enumerators, allCombinations);
|
---|
93 | finished = MoveNext(enumerators, lists);
|
---|
94 | }
|
---|
95 | return allCombinations;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
|
---|
99 | int cur = enumerators.Count - 1;
|
---|
100 | while (cur >= 0 && !enumerators[cur].MoveNext()) {
|
---|
101 | enumerators[cur] = lists[cur].GetEnumerator();
|
---|
102 | enumerators[cur].MoveNext();
|
---|
103 | cur--;
|
---|
104 | }
|
---|
105 | return cur < 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static void GetCurrentCombination(List<IEnumerator<double>> enumerators, List<List<double>> allCombinations) {
|
---|
109 | for (int i = 0; i < enumerators.Count(); i++) {
|
---|
110 | allCombinations[i].Add(enumerators[i].Current);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|