Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/02/15 16:08:21 (9 years ago)
Author:
gkronber
Message:

#2283: several major extensions for grammatical optimization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/Extensions.cs

    r11727 r11730  
    1515      return xsArr[rand.Next(xsArr.Length)];
    1616    }
     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();
     22
     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];
     29      }
     30    }
    1731  }
    1832}
Note: See TracChangeset for help on using the changeset viewer.