[7849] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7849] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Random;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
[12740] | 27 | internal static class ValueGenerator {
|
---|
[7849] | 28 | private static FastRandom rand = new FastRandom();
|
---|
| 29 |
|
---|
[8224] | 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 | }
|
---|
[7849] | 44 | }
|
---|
| 45 |
|
---|
[8224] | 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++)
|
---|
[7849] | 55 | yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[12740] | 58 | // Generate the cartesian product.
|
---|
| 59 | // The result is transposed, therefore the inner lists represent a column of values instead of a combination-pair.
|
---|
[7849] | 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 | }
|
---|