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 | public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth) {
|
---|
32 | int steps = (int)Math.Round(((end - start) / stepWidth) + 1);
|
---|
33 | for (int i = 0; i < steps; i++)
|
---|
34 | yield return start + i * stepWidth;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static IEnumerable<double> GenerateUniformDistributedValues(int amount, double start, double end) {
|
---|
38 | for (int i = 0; i < amount; i++)
|
---|
39 | yield return rand.NextDouble() * (end - start) + start;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static IEnumerable<double> GenerateNormalDistributedValues(int amount, double mu, double sigma) {
|
---|
43 | for (int i = 0; i < amount; i++)
|
---|
44 | yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // iterative approach
|
---|
48 | public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
|
---|
49 | List<List<double>> allCombinations = new List<List<double>>();
|
---|
50 | if (lists.Count < 1) {
|
---|
51 | return allCombinations;
|
---|
52 | }
|
---|
53 |
|
---|
54 | List<IEnumerator<double>> enumerators = new List<IEnumerator<double>>();
|
---|
55 | foreach (var list in lists) {
|
---|
56 | allCombinations.Add(new List<double>());
|
---|
57 | enumerators.Add(list.GetEnumerator());
|
---|
58 | }
|
---|
59 |
|
---|
60 | bool finished = !enumerators.All(x => x.MoveNext());
|
---|
61 |
|
---|
62 | while (!finished) {
|
---|
63 | GetCurrentCombination(enumerators, allCombinations);
|
---|
64 | finished = MoveNext(enumerators, lists);
|
---|
65 | }
|
---|
66 | return allCombinations;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
|
---|
70 | int cur = enumerators.Count - 1;
|
---|
71 | while (cur >= 0 && !enumerators[cur].MoveNext()) {
|
---|
72 | enumerators[cur] = lists[cur].GetEnumerator();
|
---|
73 | enumerators[cur].MoveNext();
|
---|
74 | cur--;
|
---|
75 | }
|
---|
76 | return cur < 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | private static void GetCurrentCombination(List<IEnumerator<double>> enumerators, List<List<double>> allCombinations) {
|
---|
80 | for (int i = 0; i < enumerators.Count(); i++) {
|
---|
81 | allCombinations[i].Add(enumerators[i].Current);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | //recursive approach
|
---|
86 | /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
|
---|
87 | int cur = 0;
|
---|
88 | List<double> curCombination = new List<double>();
|
---|
89 | List<List<double>> allCombinations = new List<List<double>>();
|
---|
90 | for (int i = 0; i < lists.Count; i++) {
|
---|
91 | allCombinations.Add(new List<double>());
|
---|
92 | }
|
---|
93 | if (lists.Count() > cur) {
|
---|
94 | foreach (var item in lists[cur]) {
|
---|
95 | curCombination.Clear();
|
---|
96 | curCombination.Add(item);
|
---|
97 | GetCombination(lists, cur + 1, curCombination, allCombinations);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | return allCombinations;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private static void GetCombination(List<List<double>> lists, int cur, List<double> curCombinations, List<List<double>> allCombinations) {
|
---|
104 | if (lists.Count > cur) {
|
---|
105 | foreach (var item in lists[cur]) {
|
---|
106 | if (curCombinations.Count > cur) {
|
---|
107 | curCombinations.RemoveAt(cur);
|
---|
108 | }
|
---|
109 | curCombinations.Add(item);
|
---|
110 | GetCombination(lists, cur + 1, curCombinations, allCombinations);
|
---|
111 | }
|
---|
112 | } else {
|
---|
113 | for (int i = 0; i < curCombinations.Count; i++) {
|
---|
114 | allCombinations[i].Add(curCombinations[i]);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } */
|
---|
118 |
|
---|
119 | //original
|
---|
120 | /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> sets) {
|
---|
121 |
|
---|
122 | var combinations = new List<List<double>>();
|
---|
123 |
|
---|
124 | foreach (var value in sets[0])
|
---|
125 | combinations.Add(new List<double> { value });
|
---|
126 |
|
---|
127 | foreach (var set in sets.Skip(1))
|
---|
128 | combinations = AddListToCombinations(combinations, set);
|
---|
129 |
|
---|
130 | IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, sets.Count)
|
---|
131 | select (from list in combinations
|
---|
132 | select list.ElementAt(i)));
|
---|
133 |
|
---|
134 | return res;
|
---|
135 | }
|
---|
136 |
|
---|
137 | private static List<List<double>> AddListToCombinations
|
---|
138 | (List<List<double>> combinations, List<double> set) {
|
---|
139 | var newCombinations = from value in set
|
---|
140 | from combination in combinations
|
---|
141 | select new List<double>(combination) { value };
|
---|
142 |
|
---|
143 | return newCombinations.ToList();
|
---|
144 | } */
|
---|
145 | }
|
---|
146 | }
|
---|