Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/05/12 09:44:06 (12 years ago)
Author:
gkronber
Message:

#1784: fixed uniform sampling. Adapted Nguyen instances.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/ValueGenerator.cs

    r7849 r8224  
    2929    private static FastRandom rand = new FastRandom();
    3030
     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>
    3138    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;
     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      }
    3546    }
    3647
    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;
     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      }
    4062    }
    4163
    42     public static IEnumerable<double> GenerateNormalDistributedValues(int amount, double mu, double sigma) {
    43       for (int i = 0; i < amount; i++)
     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++)
    4473        yield return NormalDistributedRandom.NextDouble(rand, mu, sigma);
    4574    }
     
    82111      }
    83112    }
    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     }    */
    145113  }
    146114}
Note: See TracChangeset for help on using the changeset viewer.