Changeset 11166
- Timestamp:
- 07/09/14 20:07:51 (10 years ago)
- Location:
- stable
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10503,10646
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Random/3.3/RandomEnumerable.cs
r10743 r11166 183 183 private static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) { 184 184 var sourceArray = source.ToArray(); 185 var valueArray = PrepareProportional( sourceArray,weights, windowing, inverseProportional);185 var valueArray = PrepareProportional(weights, windowing, inverseProportional); 186 186 double total = valueArray.Sum(); 187 187 … … 195 195 } 196 196 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); 198 198 var list = new LinkedList<Tuple<T, double>>(source.Zip(valueArray, Tuple.Create)); 199 199 double total = valueArray.Sum(); 200 200 201 while (list.Count > 0) { 202 var cur = list.First; 201 while (list.Count > 0) { 202 var cur = list.First; 203 203 double ball = cur.Value.Item2, sum = random.NextDouble() * total; // assert: sum < total. When there is only one item remaining: sum < ball 204 204 while (ball < sum) { … … 212 212 } 213 213 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) { 215 215 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++) { 221 219 if (valueArray[i] > maxValue) maxValue = valueArray[i]; 222 220 if (valueArray[i] < minValue) minValue = valueArray[i]; 223 221 } 224 222 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++) { 226 224 valueArray[i] = 1.0; 227 225 } -
stable/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Tests merged: 10646
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Tests/HeuristicLab.Random-3.3/RandomEnumerableSampleTest.cs
r10465 r11166 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.