Changeset 10503
 Timestamp:
 02/24/14 16:22:21 (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/sources/HeuristicLab.Random/3.3/RandomEnumerable.cs
r10466 r10503 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 }
Note: See TracChangeset
for help on using the changeset viewer.