Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/19/15 20:09:12 (10 years ago)
Author:
gkronber
Message:

#2283: performance tuning and reactivated random-roll-out policy in sequential search

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  
    1212
    1313    public static T SelectRandom<T>(this IEnumerable<T> xs, Random rand) {
    14       var xsArr = xs.ToArray();
    15       return xsArr[rand.Next(xsArr.Length)];
     14      var n = xs.Count();
     15      return xs.ElementAt(rand.Next(n));
    1616    }
    1717
    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();
    2220
    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;
    2933      }
     34      return elemEnumerator.Current;
    3035    }
    3136
     
    4449        var y = yEnum.Current;
    4550        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);
    4853      }
    4954      if (xEnum.MoveNext() | yEnum.MoveNext()) throw new ArgumentException("lengths are not equal");
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/HeuristicLab.Common.csproj

    r11745 r11799  
    4343    <Compile Include="ConsoleEx.cs" />
    4444    <Compile Include="Extensions.cs" />
     45    <Compile Include="MostRecentlyUsedCache.cs" />
    4546    <Compile Include="Properties\AssemblyInfo.cs" />
    4647    <Compile Include="Rand.cs" />
Note: See TracChangeset for help on using the changeset viewer.