Changeset 10646
- Timestamp:
- 03/21/14 17:40:36 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab.Random-3.3/RandomEnumerableSampleTest.cs
r10465 r10646 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 3Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 29 29 public class RandomEnumerableSampleTest { 30 30 [TestMethod] 31 [TestCategory(" Problems.Random")]31 [TestCategory("General")] 32 32 [TestProperty("Time", "short")] 33 33 public void SampleProportionalWithoutRepetitionTest() { … … 38 38 var weights = Enumerable.Repeat(0.0, 100); 39 39 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(); 42 41 Assert.AreEqual(sample.Count(), 1); 43 Assert.AreEqual(sample.Distinct().Count(), 1);44 42 } 45 43 } … … 50 48 var weights = Enumerable.Repeat(0.0, 1); 51 49 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(); 54 51 Assert.AreEqual(sample.Count(), 1); 55 Assert.AreEqual(sample.Distinct().Count(), 1);56 52 } 57 53 } … … 61 57 var random = new MersenneTwister(31415); 62 58 var weights = new double[] { 1.0, 2.0 }; 63 for (int i = 0; i < 1000; i++) {64 var sample =65 59 var zeroSelected = 0; 60 for (int i = 0; i < 1000; i++) { 61 var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray(); 66 62 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); 69 66 } 70 67 { … … 74 71 var weights = new double[] { 1.0, 1000.0 }; 75 72 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); 80 76 } 81 77 } … … 85 81 var random = new MersenneTwister(31415); 86 82 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(); 89 84 Assert.AreEqual(sample.Count(), 1); 90 85 } … … 96 91 var weights = Enumerable.Repeat(0.0, 100); 97 92 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(); 100 94 Assert.AreEqual(sample.Count(), 10); 101 95 Assert.AreEqual(sample.Distinct().Count(), 10); … … 109 103 var weights = Enumerable.Repeat(0.0, 100); 110 104 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(); 113 106 Assert.AreEqual(sample.Count(), 100); 114 107 Assert.AreEqual(sample.Distinct().Count(), 100); … … 122 115 var weights = Enumerable.Repeat(1.0, 10); 123 116 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(); 127 118 Assert.AreEqual(sample.Count(), 10); 128 119 Assert.AreEqual(sample.Distinct().Count(), 10); … … 132 123 { 133 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(); 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) 134 185 // repeat 1000000 times and calculate statistics 135 186 var items = Enumerable.Range(0, 100);
Note: See TracChangeset
for help on using the changeset viewer.