[10465] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10465] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 26 | namespace HeuristicLab.Random.Tests {
|
---|
| 27 |
|
---|
| 28 | [TestClass()]
|
---|
| 29 | public class RandomEnumerableSampleTest {
|
---|
| 30 | [TestMethod]
|
---|
[10646] | 31 | [TestCategory("General")]
|
---|
[10465] | 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++) {
|
---|
[10646] | 40 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
|
---|
[10465] | 41 | Assert.AreEqual(sample.Count(), 1);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | {
|
---|
| 45 | // select 1 of 1 uniformly (weights = 0)
|
---|
| 46 | var items = Enumerable.Range(0, 1);
|
---|
| 47 | var random = new MersenneTwister(31415);
|
---|
| 48 | var weights = Enumerable.Repeat(0.0, 1);
|
---|
| 49 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 50 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
|
---|
[10465] | 51 | Assert.AreEqual(sample.Count(), 1);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | {
|
---|
| 55 | // select 1 of 2 non-uniformly (weights = 1, 2)
|
---|
| 56 | var items = Enumerable.Range(0, 2);
|
---|
| 57 | var random = new MersenneTwister(31415);
|
---|
| 58 | var weights = new double[] { 1.0, 2.0 };
|
---|
[10646] | 59 | var zeroSelected = 0;
|
---|
[10465] | 60 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 61 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
|
---|
[10465] | 62 | Assert.AreEqual(sample.Count(), 1);
|
---|
[10646] | 63 | if (sample[0] == 0) zeroSelected++;
|
---|
[10465] | 64 | }
|
---|
[10646] | 65 | Assert.IsTrue(zeroSelected > 0 && zeroSelected < 1000);
|
---|
[10465] | 66 | }
|
---|
| 67 | {
|
---|
| 68 | // select 2 of 2 non-uniformly (weights = 1, 1000)
|
---|
| 69 | var items = Enumerable.Range(0, 2);
|
---|
| 70 | var random = new MersenneTwister(31415);
|
---|
| 71 | var weights = new double[] { 1.0, 1000.0 };
|
---|
| 72 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 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);
|
---|
[10465] | 76 | }
|
---|
| 77 | }
|
---|
| 78 | {
|
---|
| 79 | // select 2 from 1 uniformly (weights = 0), this does not throw an exception but instead returns a sample with 1 element!
|
---|
| 80 | var items = Enumerable.Range(0, 1);
|
---|
| 81 | var random = new MersenneTwister(31415);
|
---|
| 82 | var weights = Enumerable.Repeat(0.0, 1);
|
---|
[10646] | 83 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
|
---|
[10465] | 84 | Assert.AreEqual(sample.Count(), 1);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | {
|
---|
| 88 | // select 10 of 100 uniformly (weights = 0)
|
---|
| 89 | var items = Enumerable.Range(0, 100);
|
---|
| 90 | var random = new MersenneTwister(31415);
|
---|
| 91 | var weights = Enumerable.Repeat(0.0, 100);
|
---|
| 92 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 93 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
|
---|
[10465] | 94 | Assert.AreEqual(sample.Count(), 10);
|
---|
| 95 | Assert.AreEqual(sample.Distinct().Count(), 10);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | {
|
---|
| 100 | // select 100 of 100 uniformly (weights = 0)
|
---|
| 101 | var items = Enumerable.Range(0, 100);
|
---|
| 102 | var random = new MersenneTwister(31415);
|
---|
| 103 | var weights = Enumerable.Repeat(0.0, 100);
|
---|
| 104 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 105 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
|
---|
[10465] | 106 | Assert.AreEqual(sample.Count(), 100);
|
---|
| 107 | Assert.AreEqual(sample.Distinct().Count(), 100);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | {
|
---|
| 112 | // select 10 of 10 uniformly (weights = 1)
|
---|
| 113 | var items = Enumerable.Range(0, 10);
|
---|
| 114 | var random = new MersenneTwister(31415);
|
---|
| 115 | var weights = Enumerable.Repeat(1.0, 10);
|
---|
| 116 | for (int i = 0; i < 1000; i++) {
|
---|
[10646] | 117 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
|
---|
| 118 | Assert.AreEqual(sample.Count(), 10);
|
---|
| 119 | Assert.AreEqual(sample.Distinct().Count(), 10);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[10465] | 122 |
|
---|
[10646] | 123 | {
|
---|
| 124 | // 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();
|
---|
[10465] | 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)
|
---|
[10646] | 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)
|
---|
[10465] | 185 | // repeat 1000000 times and calculate statistics
|
---|
| 186 | var items = Enumerable.Range(0, 100);
|
---|
| 187 | var random = new MersenneTwister(31415);
|
---|
| 188 | var weights = Enumerable.Repeat(1.0, 100);
|
---|
| 189 | var selectionCount = new int[100, 100]; // frequency of selecting item at pos
|
---|
| 190 | for (int i = 0; i < 1000000; i++) {
|
---|
| 191 | var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
|
---|
| 192 | Assert.AreEqual(sample.Count(), 100);
|
---|
| 193 | Assert.AreEqual(sample.Distinct().Count(), 100);
|
---|
| 194 |
|
---|
| 195 | int pos = 0;
|
---|
| 196 | foreach (var item in sample) {
|
---|
| 197 | selectionCount[item, pos]++;
|
---|
| 198 | pos++;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | var sb = new StringBuilder();
|
---|
| 202 | for (int item = 0; item < 100; item++) {
|
---|
| 203 | for (int pos = 0; pos < 100; pos++) {
|
---|
| 204 | sb.AppendFormat("{0} ", selectionCount[item, pos]);
|
---|
| 205 | }
|
---|
| 206 | sb.AppendLine();
|
---|
| 207 | }
|
---|
| 208 | Console.WriteLine(sb.ToString());
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|