Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/24/12 17:23:21 (13 years ago)
Author:
abeham
Message:

#1614: worked on GQAP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3/ExtensionMethods.cs

    r7363 r7407  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    2627namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common {
    2728  public static class ExtensionMethods {
     29
     30    /// <summary>
     31    /// Simply enumerates the sequence by moving over all elements.
     32    /// </summary>
     33    /// <typeparam name="T">The type of the items that are to be enumerated.</typeparam>
     34    /// <param name="source">The enumerable that contains the items.</param>
     35    public static void Enumerate<T>(this IEnumerable<T> source) {
     36      var enumerator = source.GetEnumerator();
     37      while (enumerator.MoveNext()) ;
     38    }
     39
     40    /// <summary>
     41    /// Selects an element from an enumerable randomly with equal probability for each element. If the sequence is empty, selected will be false and default(T) will be returned.
     42    /// </summary>
     43    /// <typeparam name="T">The type of the items that are to be selected.</typeparam>
     44    /// <param name="source">The enumerable that contains the items.</param>
     45    /// <param name="random">The random number generator, its NextDouble() method must return a value in the range [0;1).</param>
     46    /// <param name="selected">True if an element was selected, false otherwise (only because of an empty sequence).</param>
     47    /// <returns>The selected item.</returns>
     48    public static T ChooseRandomOrDefault<T>(this IEnumerable<T> source, IRandom random, out bool selected) {
     49      if (source == null) throw new ArgumentNullException("SelectRandomOrDefault: source is null");
     50      uint counter = 1;
     51      selected = false;
     52      T selectedItem = default(T);
     53      foreach (T item in source) {
     54        if (counter * random.NextDouble() < 1.0) { // use multiplication instead of division
     55          selectedItem = item;
     56          selected = true;
     57        }
     58        counter++;
     59      }
     60      return selectedItem;
     61    }
     62
    2863    /// <summary>
    2964    /// Selects an element from an enumerable randomly with equal probability for each element.
     
    3267    /// <param name="source">The enumerable that contains the items.</param>
    3368    /// <param name="random">The random number generator, its NextDouble() method must return a value in the range [0;1).</param>
    34     /// <param name="selected">True if an element was selected, false otherwise (mainly because of an empty sequence).</param>
    3569    /// <returns>The selected item.</returns>
    36     public static T SelectRandomOrDefault<T>(this IEnumerable<T> source, IRandom random, out bool selected) {
    37       int counter = 1;
    38       selected = false;
     70    public static T ChooseRandom<T>(this IEnumerable<T> source, IRandom random) {
     71      if (source == null) throw new ArgumentNullException("SelectRandom: source is null");
     72      if (!source.Any()) throw new ArgumentException("SelectRandom: sequence is empty.");
     73      uint counter = 1;
    3974      T selectedItem = default(T);
    4075      foreach (T item in source) {
    41         if (random.NextDouble() < 1.0 / counter) {
     76        if (counter * random.NextDouble() < 1.0) // use multiplication instead of division
    4277          selectedItem = item;
    43           selected = true;
    44         }
     78        counter++;
    4579      }
    4680      return selectedItem;
Note: See TracChangeset for help on using the changeset viewer.