Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11313 was 11313, checked in by bburlacu, 10 years ago

#2236: Added an overload of the GenerateSteps method accepting a Func<double,double> transform. Added GenerateLogarithmicSteps method for filling an interval with logarithmically spaced points.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Common;
26using HeuristicLab.Random;
27
28namespace HeuristicLab.Problems.Instances.DataAnalysis {
29  public static class ValueGenerator {
30    private static FastRandom rand = new FastRandom();
31
32    /// <summary>
33    /// Generates a sequence of evenly spaced points between start and end (inclusive!).
34    /// </summary>
35    /// <param name="start">The smallest and first value of the sequence.</param>
36    /// <param name="end">The largest and last value of the sequence.</param>
37    /// <param name="stepWidth">The step size between subsequent values.</param>
38    /// <returns>An sequence of values from start to end (inclusive)</returns>
39    public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth) {
40      if (start > end) throw new ArgumentException("start must be less than or equal end.");
41      if (stepWidth <= 0) throw new ArgumentException("stepwith must be larger than zero.", "stepWidth");
42      double x = start;
43      // x<=end could skip the last value because of numerical problems
44      while (x < end || x.IsAlmost(end)) {
45        yield return x;
46        x += stepWidth;
47      }
48    }
49
50    /// <summary>
51    /// Generate a logarithmic sequence between start and end by applying a power-of-10 function to an underlying evenly spaced sequence
52    /// </summary>
53    /// <param name="start">The start of the sequence</param>
54    /// <param name="end">The end of the sequence</param>
55    /// <param name="stepWidth">The stepwidth for the original sequence before the points are transformed</param>
56    /// <returns>A logarithmic sequence from start to end (inclusive)</returns>
57    public static IEnumerable<double> GenerateLogarithmicSteps(double start, double end, double stepWidth) {
58      return GenerateSteps(start, end, stepWidth, x => Math.Pow(10, x));
59    }
60
61    /// <summary>
62    /// Generates a sequence of points between start and end according to given transformation
63    /// </summary>
64    /// <param name="start">The smallest and first value of the sequence.</param>
65    /// <param name="end">The largest and last value of the sequence.</param>
66    /// <param name="stepWidth">The step size between subsequent values (before transform)</param>
67    /// <param name="transform">The transform function</param>
68    /// <returns></returns>
69    public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth, Func<double, double> transform) {
70      return GenerateSteps(start, end, stepWidth).Select(transform).Where(x => x < end || x.IsAlmost(end));
71    }
72
73    /// <summary>
74    /// Generates uniformly distributed values between start and end (inclusive!)
75    /// </summary>
76    /// <param name="n">Number of values to generate.</param>
77    /// <param name="start">The lower value (inclusive)</param>
78    /// <param name="end">The upper value (inclusive)</param>
79    /// <returns>An enumerable including n values in [start, end]</returns>
80    public static IEnumerable<double> GenerateUniformDistributedValues(int n, double start, double end) {
81      for (int i = 0; i < n; i++) {
82        // we need to return a random value including end.
83        // so we cannot use rand.NextDouble() as it returns a value strictly smaller than 1.
84        double r = rand.NextUInt() / (double)uint.MaxValue;    // r \in [0,1]
85        yield return r * (end - start) + start;
86      }
87    }
88
89    /// <summary>
90    /// Generates normally distributed values sampling from N(mu, sigma)
91    /// </summary>
92    /// <param name="n">Number of values to generate.</param>
93    /// <param name="mu">The mu parameter of the normal distribution</param>
94    /// <param name="sigma">The sigma parameter of the normal distribution</param>
95    /// <returns>An enumerable including n values ~ N(mu, sigma)</returns>
96    public static IEnumerable<double> GenerateNormalDistributedValues(int n, double mu, double sigma) {
97      for (int i = 0; i < n; i++)
98        yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
99    }
100
101    // iterative approach
102    public static IEnumerable<IEnumerable<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> lists) {
103      List<List<double>> allCombinations = new List<List<double>>();
104      if (lists.Count < 1) {
105        return allCombinations;
106      }
107
108      List<IEnumerator<double>> enumerators = new List<IEnumerator<double>>();
109      foreach (var list in lists) {
110        allCombinations.Add(new List<double>());
111        enumerators.Add(list.GetEnumerator());
112      }
113
114      bool finished = !enumerators.All(x => x.MoveNext());
115
116      while (!finished) {
117        GetCurrentCombination(enumerators, allCombinations);
118        finished = MoveNext(enumerators, lists);
119      }
120      return allCombinations;
121    }
122
123    private static bool MoveNext(List<IEnumerator<double>> enumerators, List<List<double>> lists) {
124      int cur = enumerators.Count - 1;
125      while (cur >= 0 && !enumerators[cur].MoveNext()) {
126        enumerators[cur] = lists[cur].GetEnumerator();
127        enumerators[cur].MoveNext();
128        cur--;
129      }
130      return cur < 0;
131    }
132
133    private static void GetCurrentCombination(List<IEnumerator<double>> enumerators, List<List<double>> allCombinations) {
134      for (int i = 0; i < enumerators.Count(); i++) {
135        allCombinations[i].Add(enumerators[i].Current);
136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.