Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11166


Ignore:
Timestamp:
07/09/14 20:07:51 (10 years ago)
Author:
ascheibe
Message:

#2210 merged missing r10646 and r10503 into stable

Location:
stable
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Random/3.3/RandomEnumerable.cs

    r10743 r11166  
    183183    private static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    184184      var sourceArray = source.ToArray();
    185       var valueArray = PrepareProportional(sourceArray, weights, windowing, inverseProportional);
     185      var valueArray = PrepareProportional(weights, windowing, inverseProportional);
    186186      double total = valueArray.Sum();
    187187
     
    195195    }
    196196    private static IEnumerable<T> SampleProportionalWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    197       var valueArray = PrepareProportional(source.ToArray(), weights, windowing, inverseProportional);
     197      var valueArray = PrepareProportional(weights, windowing, inverseProportional);
    198198      var list = new LinkedList<Tuple<T, double>>(source.Zip(valueArray, Tuple.Create));
    199199      double total = valueArray.Sum();
    200200
    201       while (list.Count > 0) {       
    202         var cur = list.First;       
     201      while (list.Count > 0) {
     202        var cur = list.First;
    203203        double ball = cur.Value.Item2, sum = random.NextDouble() * total; // assert: sum < total. When there is only one item remaining: sum < ball
    204204        while (ball < sum) {
     
    212212    }
    213213
    214     private static double[] PrepareProportional<T>(IList<T> sourceArray, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
     214    private static double[] PrepareProportional(IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    215215      double maxValue = double.MinValue, minValue = double.MaxValue;
    216       double[] valueArray = new double[sourceArray.Count];
    217 
    218       var weightsEnum = weights.GetEnumerator();
    219       for (int i = 0; i < sourceArray.Count && weightsEnum.MoveNext(); i++) {
    220         valueArray[i] = weightsEnum.Current;
     216      double[] valueArray = weights.ToArray();
     217
     218      for (int i = 0; i < valueArray.Length; i++) {
    221219        if (valueArray[i] > maxValue) maxValue = valueArray[i];
    222220        if (valueArray[i] < minValue) minValue = valueArray[i];
    223221      }
    224222      if (minValue == maxValue) {  // all values are equal
    225         for (int i = 0; i < sourceArray.Count; i++) {
     223        for (int i = 0; i < valueArray.Length; i++) {
    226224          valueArray[i] = 1.0;
    227225        }
  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab.Random-3.3/RandomEnumerableSampleTest.cs

    r10465 r11166  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2929  public class RandomEnumerableSampleTest {
    3030    [TestMethod]
    31     [TestCategory("Problems.Random")]
     31    [TestCategory("General")]
    3232    [TestProperty("Time", "short")]
    3333    public void SampleProportionalWithoutRepetitionTest() {
     
    3838        var weights = Enumerable.Repeat(0.0, 100);
    3939        for (int i = 0; i < 1000; i++) {
    40           var sample =
    41             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     40          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    4241          Assert.AreEqual(sample.Count(), 1);
    43           Assert.AreEqual(sample.Distinct().Count(), 1);
    4442        }
    4543      }
     
    5048        var weights = Enumerable.Repeat(0.0, 1);
    5149        for (int i = 0; i < 1000; i++) {
    52           var sample =
    53             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     50          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    5451          Assert.AreEqual(sample.Count(), 1);
    55           Assert.AreEqual(sample.Distinct().Count(), 1);
    5652        }
    5753      }
     
    6157        var random = new MersenneTwister(31415);
    6258        var weights = new double[] { 1.0, 2.0 };
    63         for (int i = 0; i < 1000; i++) {
    64           var sample =
    65             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
     59        var zeroSelected = 0;
     60        for (int i = 0; i < 1000; i++) {
     61          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    6662          Assert.AreEqual(sample.Count(), 1);
    67           Assert.AreEqual(sample.Distinct().Count(), 1);
    68         }
     63          if (sample[0] == 0) zeroSelected++;
     64        }
     65        Assert.IsTrue(zeroSelected > 0 && zeroSelected < 1000);
    6966      }
    7067      {
     
    7471        var weights = new double[] { 1.0, 1000.0 };
    7572        for (int i = 0; i < 1000; i++) {
    76           var sample =
    77             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
    78           Assert.AreEqual(sample.Count(), 1);
    79           Assert.AreEqual(sample.Distinct().Count(), 1);
     73          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
     74          Assert.AreEqual(sample.Count(), 2);
     75          Assert.AreEqual(sample.Distinct().Count(), 2);
    8076        }
    8177      }
     
    8581        var random = new MersenneTwister(31415);
    8682        var weights = Enumerable.Repeat(0.0, 1);
    87         var sample =
    88           RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
     83        var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
    8984        Assert.AreEqual(sample.Count(), 1);
    9085      }
     
    9691        var weights = Enumerable.Repeat(0.0, 100);
    9792        for (int i = 0; i < 1000; i++) {
    98           var sample =
    99             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
     93          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
    10094          Assert.AreEqual(sample.Count(), 10);
    10195          Assert.AreEqual(sample.Distinct().Count(), 10);
     
    109103        var weights = Enumerable.Repeat(0.0, 100);
    110104        for (int i = 0; i < 1000; i++) {
    111           var sample =
    112             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
     105          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
    113106          Assert.AreEqual(sample.Count(), 100);
    114107          Assert.AreEqual(sample.Distinct().Count(), 100);
     
    122115        var weights = Enumerable.Repeat(1.0, 10);
    123116        for (int i = 0; i < 1000; i++) {
    124 
    125           var sample =
    126             RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
     117          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
    127118          Assert.AreEqual(sample.Count(), 10);
    128119          Assert.AreEqual(sample.Distinct().Count(), 10);
     
    132123      {
    133124        // select 10 of 10 uniformly (weights = 1)
     125        var items = Enumerable.Range(0, 10);
     126        var random = new MersenneTwister(31415);
     127        var weights = Enumerable.Repeat(1.0, 10);
     128        for (int i = 0; i < 1000; i++) {
     129          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, false).ToArray();
     130          Assert.AreEqual(sample.Count(), 10);
     131          Assert.AreEqual(sample.Distinct().Count(), 10);
     132        }
     133      }
     134
     135      {
     136        // select 10 of 10 uniformly (weights = 1)
     137        var items = Enumerable.Range(0, 10);
     138        var random = new MersenneTwister(31415);
     139        var weights = Enumerable.Repeat(1.0, 10);
     140        for (int i = 0; i < 1000; i++) {
     141          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, true).ToArray();
     142          Assert.AreEqual(sample.Count(), 10);
     143          Assert.AreEqual(sample.Distinct().Count(), 10);
     144        }
     145      }
     146
     147      {
     148        // select 5 of 10 uniformly (weights = 0..n)
     149        var items = Enumerable.Range(0, 10);
     150        var random = new MersenneTwister(31415);
     151        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     152        for (int i = 0; i < 1000; i++) {
     153          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, false, false).ToArray();
     154          Assert.AreEqual(sample.Count(), 5);
     155          Assert.AreEqual(sample.Distinct().Count(), 5);
     156        }
     157      }
     158
     159      {
     160        // select 5 of 10 uniformly (weights = 0..n)
     161        var items = Enumerable.Range(0, 10);
     162        var random = new MersenneTwister(31415);
     163        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     164        for (int i = 0; i < 1000; i++) {
     165          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, false).ToArray();
     166          Assert.AreEqual(sample.Count(), 5);
     167          Assert.AreEqual(sample.Distinct().Count(), 5);
     168        }
     169      }
     170
     171      {
     172        // select 5 of 10 uniformly (weights = 0..n)
     173        var items = Enumerable.Range(0, 10);
     174        var random = new MersenneTwister(31415);
     175        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     176        for (int i = 0; i < 1000; i++) {
     177          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, true).ToArray();
     178          Assert.AreEqual(sample.Count(), 5);
     179          Assert.AreEqual(sample.Distinct().Count(), 5);
     180        }
     181      }
     182
     183      {
     184        // select 10 of 100 uniformly (weights = 1)
    134185        // repeat 1000000 times and calculate statistics
    135186        var items = Enumerable.Range(0, 100);
Note: See TracChangeset for help on using the changeset viewer.