- Timestamp:
- 01/19/15 20:09:12 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/Extensions.cs
r11742 r11799 12 12 13 13 public static T SelectRandom<T>(this IEnumerable<T> xs, Random rand) { 14 var xsArr = xs.ToArray();15 return xs Arr[rand.Next(xsArr.Length)];14 var n = xs.Count(); 15 return xs.ElementAt(rand.Next(n)); 16 16 } 17 17 18 public static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, Random random, IEnumerable<double> weights) { 19 var sourceArray = source.ToArray(); 20 var valueArray = weights.ToArray(); 21 double total = valueArray.Sum(); 18 public static T SampleProportional<T>(this IEnumerable<T> elements, Random random, IEnumerable<double> weights) { 19 double total = weights.Sum(); 22 20 23 while (true) { 24 int index = 0; 25 double ball = valueArray[index], sum = random.NextDouble() * total; 26 while (ball < sum) 27 ball += valueArray[++index]; 28 yield return sourceArray[index]; 21 var elemEnumerator = elements.GetEnumerator(); 22 elemEnumerator.MoveNext(); 23 var weightEnumerator = weights.GetEnumerator(); 24 weightEnumerator.MoveNext(); 25 26 var r = random.NextDouble() * total; 27 var agg = weightEnumerator.Current; 28 29 while (agg < r) { 30 weightEnumerator.MoveNext(); 31 elemEnumerator.MoveNext(); 32 agg += weightEnumerator.Current; 29 33 } 34 return elemEnumerator.Current; 30 35 } 31 36 … … 44 49 var y = yEnum.Current; 45 50 s += (x - meanX) * (y - meanY); 46 ssX += Math.Pow(x - meanX, 2);47 ssY += Math.Pow(y - meanY, 2);51 ssX += (x - meanX) * (x - meanX); 52 ssY += (y - meanY) * (y - meanY); 48 53 } 49 54 if (xEnum.MoveNext() | yEnum.MoveNext()) throw new ArgumentException("lengths are not equal"); -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/HeuristicLab.Common.csproj
r11745 r11799 43 43 <Compile Include="ConsoleEx.cs" /> 44 44 <Compile Include="Extensions.cs" /> 45 <Compile Include="MostRecentlyUsedCache.cs" /> 45 46 <Compile Include="Properties\AssemblyInfo.cs" /> 46 47 <Compile Include="Rand.cs" />
Note: See TracChangeset
for help on using the changeset viewer.