Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Regression/3.4/ValueGenerator.cs @ 7770

Last change on this file since 7770 was 7770, checked in by sforsten, 12 years ago

#1784:

  • added some regions for readability
  • added import and export methods in DataAnalysisProblem and SymbolicDataAnalysisProblem to reduce code duplication
  • added a recursive and an iterative approach without many linq expression to generate all combinations of list elements in ValueGenerator
File size: 5.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Random;
26
27namespace HeuristicLab.Problems.Instances.Regression {
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        enumerators.Add(list.GetEnumerator());
57      }
58
59      bool finished = !enumerators.All(x => x.MoveNext());
60
61      while (!finished) {
62        allCombinations.Add(GetCurrentCombination(enumerators));
63        finished = MoveNext(enumerators, lists);
64      }
65
66      IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, lists.Count)
67                                              select (from list in allCombinations
68                                                      select list.ElementAt(i)));
69      return res;
70    }
71
72    private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
73      int cur = enumerators.Count - 1;
74      while (cur >= 0 && !enumerators[cur].MoveNext()) {
75        enumerators[cur] = lists[cur].GetEnumerator();
76        enumerators[cur].MoveNext();
77        cur--;
78      }
79      return cur < 0;
80    }
81
82    private static List<double> GetCurrentCombination(List<IEnumerator<double>> enumerators) {
83      List<double> combination = new List<double>();
84      foreach (var enumerator in enumerators) {
85        combination.Add(enumerator.Current);
86      }
87      return combination;
88    }
89
90    //recursive approach
91    /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
92      int cur = 0;
93      List<double> curCombination = new List<double>();
94      List<List<double>> allCombinations = new List<List<double>>();
95      if (lists.Count() > cur) {
96        foreach (var item in lists[cur]) {
97          curCombination.Clear();
98          curCombination.Add(item);
99          GetCombination(lists, cur + 1, curCombination, allCombinations);
100        }
101      }
102
103      IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, lists.Count)
104                                              select (from list in allCombinations
105                                                      select list.ElementAt(i)));
106
107      return res;
108    }
109
110    private static void GetCombination(List<List<double>> lists, int cur, List<double> curCombinations, List<List<double>> allCombinations) {
111      if (lists.Count > cur) {
112        foreach (var item in lists[cur]) {
113          if (curCombinations.Count > cur) {
114            curCombinations.RemoveAt(cur);
115          }
116          curCombinations.Add(item);
117          GetCombination(lists, cur + 1, curCombinations, allCombinations);
118        }
119      } else {
120        allCombinations.Add(new List<double>(curCombinations));
121      }
122    }    */
123
124    //original
125    /*public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> sets) {
126
127      var combinations = new List<List<double>>();
128
129      foreach (var value in sets[0])
130        combinations.Add(new List<double> { value });
131
132      foreach (var set in sets.Skip(1))
133        combinations = AddListToCombinations(combinations, set);
134
135      IEnumerable<IEnumerable<double>> res = (from i in Enumerable.Range(0, sets.Count)
136                                              select (from list in combinations
137                                                      select list.ElementAt(i)));
138
139      return res;
140    }
141
142    private static List<List<double>> AddListToCombinations
143         (List<List<double>> combinations, List<double> set) {
144      var newCombinations = from value in set
145                            from combination in combinations
146                            select new List<double>(combination) { value };
147
148      return newCombinations.ToList();
149    }    */
150  }
151}
Note: See TracBrowser for help on using the repository browser.