Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Random-3.3/RandomEnumerableSampleTest.cs @ 11164

Last change on this file since 11164 was 10465, checked in by gkronber, 11 years ago

#2146: implemented unit test for SampleProportionalWithoutRepetition

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
24using System.Text;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26namespace HeuristicLab.Random.Tests {
27
28  [TestClass()]
29  public class RandomEnumerableSampleTest {
30    [TestMethod]
31    [TestCategory("Problems.Random")]
32    [TestProperty("Time", "short")]
33    public void SampleProportionalWithoutRepetitionTest() {
34      {
35        // select 1 of 100 uniformly (weights = 0)
36        var items = Enumerable.Range(0, 100);
37        var random = new MersenneTwister(31415);
38        var weights = Enumerable.Repeat(0.0, 100);
39        for (int i = 0; i < 1000; i++) {
40          var sample =
41            RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
42          Assert.AreEqual(sample.Count(), 1);
43          Assert.AreEqual(sample.Distinct().Count(), 1);
44        }
45      }
46      {
47        // select 1 of 1 uniformly (weights = 0)
48        var items = Enumerable.Range(0, 1);
49        var random = new MersenneTwister(31415);
50        var weights = Enumerable.Repeat(0.0, 1);
51        for (int i = 0; i < 1000; i++) {
52          var sample =
53            RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
54          Assert.AreEqual(sample.Count(), 1);
55          Assert.AreEqual(sample.Distinct().Count(), 1);
56        }
57      }
58      {
59        // select 1 of 2 non-uniformly (weights = 1, 2)
60        var items = Enumerable.Range(0, 2);
61        var random = new MersenneTwister(31415);
62        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();
66          Assert.AreEqual(sample.Count(), 1);
67          Assert.AreEqual(sample.Distinct().Count(), 1);
68        }
69      }
70      {
71        // select 2 of 2 non-uniformly (weights = 1, 1000)
72        var items = Enumerable.Range(0, 2);
73        var random = new MersenneTwister(31415);
74        var weights = new double[] { 1.0, 1000.0 };
75        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);
80        }
81      }
82      {
83        // select 2 from 1 uniformly (weights = 0), this does not throw an exception but instead returns a sample with 1 element!
84        var items = Enumerable.Range(0, 1);
85        var random = new MersenneTwister(31415);
86        var weights = Enumerable.Repeat(0.0, 1);
87        var sample =
88          RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
89        Assert.AreEqual(sample.Count(), 1);
90      }
91
92      {
93        // select 10 of 100 uniformly (weights = 0)
94        var items = Enumerable.Range(0, 100);
95        var random = new MersenneTwister(31415);
96        var weights = Enumerable.Repeat(0.0, 100);
97        for (int i = 0; i < 1000; i++) {
98          var sample =
99            RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
100          Assert.AreEqual(sample.Count(), 10);
101          Assert.AreEqual(sample.Distinct().Count(), 10);
102        }
103      }
104
105      {
106        // select 100 of 100 uniformly (weights = 0)
107        var items = Enumerable.Range(0, 100);
108        var random = new MersenneTwister(31415);
109        var weights = Enumerable.Repeat(0.0, 100);
110        for (int i = 0; i < 1000; i++) {
111          var sample =
112            RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
113          Assert.AreEqual(sample.Count(), 100);
114          Assert.AreEqual(sample.Distinct().Count(), 100);
115        }
116      }
117
118      {
119        // select 10 of 10 uniformly (weights = 1)
120        var items = Enumerable.Range(0, 10);
121        var random = new MersenneTwister(31415);
122        var weights = Enumerable.Repeat(1.0, 10);
123        for (int i = 0; i < 1000; i++) {
124
125          var sample =
126            RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
127          Assert.AreEqual(sample.Count(), 10);
128          Assert.AreEqual(sample.Distinct().Count(), 10);
129        }
130      }
131
132      {
133        // select 10 of 10 uniformly (weights = 1)
134        // repeat 1000000 times and calculate statistics
135        var items = Enumerable.Range(0, 100);
136        var random = new MersenneTwister(31415);
137        var weights = Enumerable.Repeat(1.0, 100);
138        var selectionCount = new int[100, 100]; // frequency of selecting item at pos
139        for (int i = 0; i < 1000000; i++) {
140          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
141          Assert.AreEqual(sample.Count(), 100);
142          Assert.AreEqual(sample.Distinct().Count(), 100);
143
144          int pos = 0;
145          foreach (var item in sample) {
146            selectionCount[item, pos]++;
147            pos++;
148          }
149        }
150        var sb = new StringBuilder();
151        for (int item = 0; item < 100; item++) {
152          for (int pos = 0; pos < 100; pos++) {
153            sb.AppendFormat("{0} ", selectionCount[item, pos]);
154          }
155          sb.AppendLine();
156        }
157        Console.WriteLine(sb.ToString());
158      }
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.