Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7807


Ignore:
Timestamp:
05/14/12 15:59:39 (13 years ago)
Author:
abeham
Message:

#1614, #1848

  • updated extension methods and operators that use them
Location:
branches/GeneralizedQAP
Files:
1 added
8 edited

Legend:

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

    r7593 r7807  
    3030    #region Choosing from a sequence
    3131    /// <summary>
    32     /// Chooses one elements from a sequence under equal chances for each element.
    33     /// </summary>
    34     /// <remarks>
    35     /// Runtime complexity is O(1) for sequences that are <see cref="IList{T}"/> and
     32    /// Chooses one elements from a sequence giving each element an equal chance.
     33    /// </summary>
     34    /// <remarks>
     35    /// Runtime complexity is O(1) for sequences that are of type <see cref="IList{T}"/> and
    3636    /// O(N) for all other.
    3737    /// </remarks>
     38    /// <exception cref="ArgumentException">If the sequence is empty.</exception>
     39    /// <typeparam name="T">The type of the items to be selected.</typeparam>
     40    /// <param name="source">The sequence of elements.</param>
     41    /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
     42    /// <param name="count">The number of items to be selected.</param>
     43    /// <returns>An element that has been chosen randomly from the sequence.</returns>
     44    public static T SampleRandom<T>(this IEnumerable<T> source, IRandom random) {
     45      if (!source.Any()) throw new ArgumentException("sequence is empty.", "source");
     46      return source.SampleRandom(random, 1).First();
     47    }
     48    /// <summary>
     49    /// Chooses <paramref name="count"/> elements from a sequence with repetition with equal chances for each element.
     50    /// </summary>
     51    /// <remarks>
     52    /// Runtime complexity is O(count) for sequences that are <see cref="IList{T}"/> and
     53    /// O(N * count) for all other. No exception is thrown if the sequence is empty.
     54    ///
     55    /// The method is online.
     56    /// </remarks>
    3857    /// <typeparam name="T">The type of the items to be selected.</typeparam>
    3958    /// <param name="source">The sequence of elements.</param>
     
    4160    /// <param name="count">The number of items to be selected.</param>
    4261    /// <returns>A sequence of elements that have been chosen randomly.</returns>
    43     public static T ChooseUniformRandom<T>(this IEnumerable<T> source, IRandom random) {
    44       return source.ChooseUniformRandom(random, 1).First();
    45     }
    46     /// <summary>
    47     /// Chooses <paramref name="count"/> elements from a sequence with repetition under equal chances for each element.
    48     /// </summary>
    49     /// <remarks>
    50     /// Runtime complexity is O(count) for sequences that are <see cref="IList{T}"/> and
    51     /// O(N * count) for all other.
    52     ///
    53     /// The method is online.
    54     /// </remarks>
    55     /// <typeparam name="T">The type of the items to be selected.</typeparam>
    56     /// <param name="source">The sequence of elements.</param>
    57     /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
    58     /// <param name="count">The number of items to be selected.</param>
    59     /// <returns>A sequence of elements that have been chosen randomly.</returns>
    60     public static IEnumerable<T> ChooseUniformRandom<T>(this IEnumerable<T> source, IRandom random, int count) {
    61       if (source == null) throw new ArgumentNullException("source", "sequence is null");
    62       if (!source.Any()) throw new ArgumentException("sequence is empty.", "source");
    63 
     62    public static IEnumerable<T> SampleRandom<T>(this IEnumerable<T> source, IRandom random, int count) {
    6463      var listSource = source as IList<T>;
    65       if (listSource != null)
     64      if (listSource != null) {
    6665        while (count > 0) {
    6766          yield return listSource[random.Next(listSource.Count)];
    6867          count--;
    6968        }
    70 
    71       while (count > 0) {
    72         var enumerator = source.GetEnumerator();
    73         enumerator.MoveNext();
    74         T selectedItem = enumerator.Current;
    75         int counter = 1;
    76         while (enumerator.MoveNext()) {
    77           counter++;
    78           if (counter * random.NextDouble() < 1.0)
    79             selectedItem = enumerator.Current;
    80         }
    81         yield return selectedItem;
    82         count--;
    83       }
    84     }
    85     /// <summary>
    86     /// Chooses <paramref name="count"/> elements from a sequence without repetition under equal chances for each element.
    87     /// </summary>
    88     /// <remarks>
    89     /// Runtime complexity is O(count) for sequences that are <see cref="IList{T}"/> and
    90     /// O(N * count) for all other.
    91     ///
    92     /// The method is online, but the source enumerable is transformed into an array.
     69      } else {
     70        while (count > 0) {
     71          var enumerator = source.GetEnumerator();
     72          enumerator.MoveNext();
     73          T selectedItem = enumerator.Current;
     74          int counter = 1;
     75          while (enumerator.MoveNext()) {
     76            counter++;
     77            if (counter * random.NextDouble() < 1.0)
     78              selectedItem = enumerator.Current;
     79          }
     80          yield return selectedItem;
     81          count--;
     82        }
     83      }
     84    }
     85    /// <summary>
     86    /// Chooses <paramref name="count"/> elements from a sequence without repetition with equal chances for each element.
     87    /// The items are returned in the same order as they appear in the sequence.
     88    /// </summary>
     89    /// <remarks>
     90    /// Runtime complexity is O(N) for all sequences.
     91    /// No exception is thrown if the sequence contains less items than there are to be selected.
     92    ///
     93    /// The method is online.
    9394    /// </remarks>
    9495    /// <typeparam name="T">The type of the items to be selected.</typeparam>
     
    9798    /// <param name="count">The number of items to be selected.</param>
    9899    /// <returns>A sequence of elements that have been chosen randomly.</returns>
    99     public static IEnumerable<T> ChooseUniformRandomWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, int count) {
    100       return source.Shuffle(random).Take(count);
    101     }
    102 
    103     /// <summary>
    104     /// Chooses items out of a sequence with repetition. The chance that an item is selected is proportional or inverse-proportional
    105     /// to the value obtained from <paramref name="valueSelector"/>.
    106     /// </summary>
    107     /// <remarks>
    108     /// In case both <paramref name="maximization"/> and <paramref name="windowing"/> are false values must be &gt; 0,
     100    public static IEnumerable<T> SampleRandomWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, int count) {
     101      int remaining = source.Count();
     102      foreach (var item in source) {
     103        if (random.NextDouble() * remaining < count) {
     104          count--;
     105          yield return item;
     106          if (count <= 0) break;
     107        }
     108        remaining--;
     109      }
     110    }
     111
     112    /// <summary>
     113    /// Chooses elements out of a sequence with repetition. The chance that an item is selected is proportional or inverse-proportional
     114    /// to the <paramref name="weights"/>.
     115    /// </summary>
     116    /// <remarks>
     117    /// In case both <paramref name="inverseProportional"/> and <paramref name="windowing"/> are false values must be &gt; 0,
    109118    /// otherwise an InvalidOperationException is thrown.
    110119    ///
    111     /// The method is online, but internally holds two arrays: One that is the sequence itself and another one for the values. The <paramref name="valueSelector"/>
    112     /// is only applied once for each item.
    113     /// </remarks>
    114     /// <typeparam name="T">The type of the items to be selected.</typeparam>
    115     /// <param name="source">The sequence of elements.</param>
    116     /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
    117     /// <param name="count">The number of items to be selected.</param>
    118     /// <param name="valueSelector">The function that calculates a proportional value for each item.</param>
     120    /// The method internally holds two arrays: One that is the sequence itself and another one for the values.
     121    /// </remarks>
     122    /// <typeparam name="T">The type of the items to be selected.</typeparam>
     123    /// <param name="source">The sequence of elements.</param>
     124    /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
     125    /// <param name="count">The number of items to be selected.</param>
     126    /// <param name="weights">The weight values for the items.</param>
    119127    /// <param name="windowing">Whether to scale the proportional values or not.</param>
    120     /// <param name="maximization">Determines whether to choose proportionally (true) or inverse-proportionally (false).</param>
     128    /// <param name="inverseProportional">Determines whether to choose proportionally (false) or inverse-proportionally (true).</param>
    121129    /// <returns>A sequence of selected items. The sequence might contain the same item more than once.</returns>
    122     public static IEnumerable<T> ChooseProportionalRandom<T>(this IEnumerable<T> source, IRandom random, int count, Func<T, double> valueSelector, bool windowing, bool maximization) {
    123       return source.ChooseProportionalRandom(random, valueSelector, windowing, maximization).Take(count);
    124     }
    125     /// <summary>
    126     /// Same as <seealso cref="ChooseProportionalRandom<T>"/>, but selects each item exactly once.
    127     /// </summary>
    128     /// <remarks>
    129     /// In case both <paramref name="maximization"/> and <paramref name="windowing"/> are false values must be &gt; 0,
     130    public static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, IRandom random, int count, IEnumerable<double> weights, bool windowing = true, bool inverseProportional = false) {
     131      return source.SampleProportional(random, weights, windowing, inverseProportional).Take(count);
     132    }
     133    /// <summary>
     134    /// Same as <seealso cref="SampleProportional<T>"/>, but chooses an item exactly once.
     135    /// </summary>
     136    /// <remarks>
     137    /// In case both <paramref name="inverseProportional"/> and <paramref name="windowing"/> are false values must be &gt; 0,
    130138    /// otherwise an InvalidOperationException is thrown.
    131139    ///
    132     /// The method is online, but internally holds two arrays: One that is the sequence itself and another one for the values. The <paramref name="valueSelector"/>
    133     /// is only applied once for each item.
    134     /// </remarks>
    135     /// <typeparam name="T">The type of the items to be selected.</typeparam>
    136     /// <param name="source">The sequence of elements.</param>
    137     /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
    138     /// <param name="count">The number of items to be selected.</param>
    139     /// <param name="valueSelector">The function that calculates a proportional value for each item.</param>
     140    /// The method internally holds two arrays: One that is the sequence itself and another one for the values.
     141    /// </remarks>
     142    /// <typeparam name="T">The type of the items to be selected.</typeparam>
     143    /// <param name="source">The sequence of elements.</param>
     144    /// <param name="random">The random number generator to use, its NextDouble() method must produce values in the range [0;1)</param>
     145    /// <param name="count">The number of items to be selected.</param>
     146    /// <param name="weights">The weight values for the items.</param>
    140147    /// <param name="windowing">Whether to scale the proportional values or not.</param>
    141148    /// <param name="maximization">Determines whether to choose proportionally (true) or inverse-proportionally (false).</param>
    142149    /// <returns>A sequence of selected items.</returns>
    143     public static IEnumerable<T> ChooseProportionalRandomWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, int count, Func<T, double> valueSelector, bool windowing, bool maximization) {
    144       return source.ChooseProportionalRandomWithoutRepetition(random, valueSelector, windowing, maximization).Take(count);
     150    public static IEnumerable<T> SampleProportionalWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, int count, IEnumerable<double> weights, bool windowing = true, bool inverseProportional = false) {
     151      return source.SampleProportionalWithoutRepetition(random, weights, windowing, inverseProportional).Take(count);
    145152    }
    146153    #region Proportional Helpers
    147     private static IEnumerable<T> ChooseProportionalRandom<T>(this IEnumerable<T> source, IRandom random, Func<T, double> valueSelector, bool windowing, bool maximization) {
    148       if (source == null) throw new ArgumentNullException("source", "sequence is null");
    149       if (!source.Any()) throw new ArgumentException("sequence is empty.", "source");
    150 
     154    private static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    151155      var sourceArray = source.ToArray();
    152       var valueArray = PrepareProportional<T>(sourceArray, valueSelector, windowing, maximization);
     156      var valueArray = PrepareProportional<T>(sourceArray, weights, windowing, inverseProportional);
    153157      double total = valueArray.Sum();
    154158
     
    161165      }
    162166    }
    163     private static IEnumerable<T> ChooseProportionalRandomWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, Func<T, double> valueSelector, bool windowing, bool maximization) {
    164       if (source == null) throw new ArgumentNullException("source", "sequence is null");
    165       if (!source.Any()) throw new ArgumentException("sequence is empty.", "source");
    166 
     167    private static IEnumerable<T> SampleProportionalWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    167168      var sourceArray = source.ToArray();
    168       var valueArray = PrepareProportional<T>(sourceArray, valueSelector, windowing, maximization);
     169      var valueArray = PrepareProportional<T>(sourceArray, weights, windowing, inverseProportional);
    169170      double total = valueArray.Sum();
    170171
     
    183184      }
    184185    }
    185     private static double[] PrepareProportional<T>(IList<T> sourceArray, Func<T, double> valueSelector, bool windowing, bool maximization) {
     186    private static double[] PrepareProportional<T>(IList<T> sourceArray, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    186187      double maxValue = double.MinValue, minValue = double.MaxValue;
    187188      double[] valueArray = new double[sourceArray.Count];
    188189
    189       for (int i = 0; i < sourceArray.Count; i++) {
    190         valueArray[i] = valueSelector(sourceArray[i]);
     190      var weightsEnum = weights.GetEnumerator();
     191      for (int i = 0; i < sourceArray.Count && weightsEnum.MoveNext(); i++) {
     192        valueArray[i] = weightsEnum.Current;
    191193        if (valueArray[i] > maxValue) maxValue = valueArray[i];
    192194        if (valueArray[i] < minValue) minValue = valueArray[i];
     
    198200      } else {
    199201        if (windowing) {
    200           if (maximization) ProportionalScale(valueArray, minValue);
    201           else InverseProportionalScale(valueArray, maxValue);
     202          if (inverseProportional) InverseProportionalScale(valueArray, minValue);
     203          else ProportionalScale(valueArray, maxValue);
    202204        } else {
    203205          if (minValue < 0.0) throw new InvalidOperationException("Proportional selection without windowing does not work with values < 0.");
    204           if (!maximization) InverseProportionalScale(valueArray, 2 * maxValue);
     206          if (inverseProportional) InverseProportionalScale(valueArray, 2 * maxValue);
    205207        }
    206208      }
     
    220222
    221223    /// <summary>
    222     /// Selects the first element in the sequence that is maximal with respect to the given key.
     224    /// Selects all elements in the sequence that are maximal with respect to the given value.
    223225    /// </summary>
    224226    /// <remarks>
     
    226228    /// </remarks>
    227229    /// <typeparam name="T">The type of the elements.</typeparam>
    228     /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam>
    229     /// <param name="source">The enumeration in which the maximum item should be found.</param>
    230     /// <param name="keySelector">The function that selects the key.</param>
    231     /// <returns>The element in the enumeration where the selected key is the maximum.</returns>
    232     public static T ChooseMax<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable {
    233       if (source == null) throw new ArgumentNullException("source", "sequence is null");
     230    /// <param name="source">The enumeration in which the items with a maximal value should be found.</param>
     231    /// <param name="valueSelector">The function that selects the value to compare.</param>
     232    /// <returns>All elements in the enumeration where the selected value is the maximum.</returns>
     233    public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> source, Func<T, IComparable> valueSelector) {
    234234      IEnumerator<T> enumerator = source.GetEnumerator();
    235       if (!enumerator.MoveNext()) throw new ArgumentException("sequence is empty", "source");
    236       T result = enumerator.Current;
    237       TComparable max = keySelector(result);
     235      if (!enumerator.MoveNext()) return Enumerable.Empty<T>();
     236      IComparable max = valueSelector(enumerator.Current);
     237      var result = new List<T>();
     238      result.Add(enumerator.Current);
     239
    238240      while (enumerator.MoveNext()) {
    239241        T item = enumerator.Current;
    240         TComparable comparison = keySelector(item);
     242        IComparable comparison = valueSelector(item);
    241243        if (comparison.CompareTo(max) > 0) {
    242           result = item;
     244          result.Clear();
     245          result.Add(item);
    243246          max = comparison;
     247        } else if (comparison.CompareTo(max) == 0) {
     248          result.Add(item);
    244249        }
    245250      }
     
    248253
    249254    /// <summary>
    250     /// Selects the first element in the sequence where the selected key is the minimum.
     255    /// Selects all elements in the sequence that are minimal with respect to the given value.
    251256    /// </summary>
    252257    /// <remarks>
     
    254259    /// </remarks>
    255260    /// <typeparam name="T">The type of the elements.</typeparam>
    256     /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam>
    257     /// <param name="source">The enumeration in which the minimum item should be found.</param>
    258     /// <param name="keySelector">The function that selects the key.</param>
    259     /// <returns>The element in the enumeration where the selected key is the minimum.</returns>
    260     public static T ChooseMin<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable {
    261       if (source == null) throw new ArgumentNullException("source", "sequence is null");
     261    /// <param name="source">The enumeration in which items with a minimal value should be found.</param>
     262    /// <param name="valueSelector">The function that selects the value.</param>
     263    /// <returns>All elements in the enumeration where the selected value is the minimum.</returns>
     264    public static IEnumerable<T> MinItems<T>(this IEnumerable<T> source, Func<T, IComparable> valueSelector) {
    262265      IEnumerator<T> enumerator = source.GetEnumerator();
    263       if (!enumerator.MoveNext()) throw new ArgumentException("sequence is empty", "source");
    264       T result = enumerator.Current;
    265       TComparable min = keySelector(result);
     266      if (!enumerator.MoveNext()) return Enumerable.Empty<T>();
     267      IComparable min = valueSelector(enumerator.Current);
     268      var result = new List<T>();
     269      result.Add(enumerator.Current);
     270
    266271      while (enumerator.MoveNext()) {
    267272        T item = enumerator.Current;
    268         TComparable comparison = keySelector(item);
     273        IComparable comparison = valueSelector(item);
    269274        if (comparison.CompareTo(min) < 0) {
    270           result = item;
     275          result.Clear();
     276          result.Add(item);
    271277          min = comparison;
     278        } else if (comparison.CompareTo(min) == 0) {
     279          result.Add(item);
    272280        }
    273281      }
     
    281289    /// </summary>
    282290    /// <remarks>
    283     /// Note that this is an online shuffle, but the source enumerable is transformed into an array.
     291    /// Note that the source enumerable is transformed into an array.
    284292    ///
    285293    /// The implementation is described in http://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm.
     
    292300      T[] elements = source.ToArray();
    293301      for (int i = elements.Length - 1; i > 0; i--) {
    294         // Swap element "i" with a random earlier element it (or itself)
     302        // Swap element "i" with a random earlier element (including itself)
    295303        int swapIndex = random.Next(i + 1);
    296304        yield return elements[swapIndex];
     
    333341
    334342    #region Matrix operations
    335     public static double[,] Transpose(this double[,] matrix) {
    336       var result = new double[matrix.GetLength(1), matrix.GetLength(0)];
     343    /// <summary>
     344    /// Returns a transposed matrix of the given <paramref name="matrix"/>.
     345    /// </summary>
     346    /// <typeparam name="T">The type of the matrix</typeparam>
     347    /// <param name="matrix">The matrix that should be transposed.</param>
     348    /// <returns>The transposed matrix.</returns>
     349    public static T[,] Transpose<T>(this T[,] matrix) {
     350      var result = new T[matrix.GetLength(1), matrix.GetLength(0)];
    337351      for (int i = 0; i < matrix.GetLength(0); i++)
    338352        for (int j = 0; j < matrix.GetLength(1); j++)
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs

    r7470 r7807  
    157157      int bestIndex;
    158158      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
    159       if (maximization) bestIndex = tmp.ChooseMax(x => x.Value).Index;
    160       else bestIndex = tmp.ChooseMin(x => x.Value).Index;
     159      if (maximization) bestIndex = tmp.MaxItems(x => x.Value).First().Index;
     160      else bestIndex = tmp.MinItems(x => x.Value).First().Index;
    161161
    162162      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/GQAPPathRelinking.cs

    r7523 r7807  
    183183        if (B.Any()) {
    184184          GQAPSolution pi;
    185           if (maximization.Value) pi = B.ChooseProportionalRandom(random, 1, x => x.Quality.Value, false, true).First();
    186           else pi = B.ChooseProportionalRandom(random, 1, x => 1.0 / x.Quality.Value, false, true).First();
     185          if (maximization.Value) pi = B.SampleProportional(random, 1, B.Select(x => x.Quality.Value), false).First();
     186          else pi = B.SampleProportional(random, 1, B.Select(x => 1.0 / x.Quality.Value), false).First();
    187187          var diff = IntegerVectorEqualityComparer.GetDifferingIndices(pi.Assignment, target);
    188188          var I = phi.Except(diff);
    189           var i = I.ChooseUniformRandom(random);
     189          var i = I.SampleRandom(random);
    190190          fix.Add(i);
    191191          nonFix.Remove(i);
     
    226226          var T = nonFix.Where(x => x != equipment && assignment[x] == l && demands[x] <= maxSlack);
    227227          if (T.Any()) {
    228             int i = T.ChooseProportionalRandom(random, 1, x => demands[x], false, true).First();
    229             var j = Enumerable.Range(0, capacities.Length).Where(x => slack[x] >= demands[i]).ChooseUniformRandom(random);
     228            int i = T.SampleProportional(random, 1, T.Select(x => demands[x]), false).First();
     229            var j = Enumerable.Range(0, capacities.Length).Where(x => slack[x] >= demands[i]).SampleRandom(random);
    230230            result[i] = j;
    231231            slack[j] -= demands[i];
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/DemandEquivalentSwapEquipmentManipluator.cs

    r7523 r7807  
    8080          groupedLocations[location2].Remove(equipment2);
    8181          if (!groupedLocations[location2].Any()) break;
    82           equipment2 = groupedLocations[location2].ChooseUniformRandom(random);
     82          equipment2 = groupedLocations[location2].SampleRandom(random);
    8383        }
    8484      }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/RelocateEquipmentManipluator.cs

    r7523 r7807  
    7979      }
    8080
    81       var sourceLocation = overbookedLocations.ChooseUniformRandom(random);
    82       int equipmentToRelocate = groupedLocations[sourceLocation.Key].ChooseUniformRandom(random);
     81      var sourceLocation = overbookedLocations.SampleRandom(random);
     82      int equipmentToRelocate = groupedLocations[sourceLocation.Key].SampleRandom(random);
    8383      var bestFreeLocations = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipmentToRelocate]);
    8484
    8585      if (bestFreeLocations.Any()) { // a feasible solution will still be feasible, an infeasible solution might become feasible
    86         var selected = bestFreeLocations.ChooseUniformRandom(random);
     86        var selected = bestFreeLocations.SampleRandom(random);
    8787        assignment[equipmentToRelocate] = sourceLocation.Key;
    8888      } else { // the solution will become infeasible
    89         sourceLocation = freeLocations.ChooseUniformRandom(random);
     89        sourceLocation = freeLocations.SampleRandom(random);
    9090        assignment[equipmentToRelocate] = sourceLocation.Key;
    9191      }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs

    r7593 r7807  
    8484        do {
    8585          if (L.Any() && random.NextDouble() < threshold) {
    86             int l = L.ChooseUniformRandom(random);
     86            int l = L.SampleRandom(random);
    8787            L.Remove(l);
    8888            CL.Add(l);
     
    9090          }
    9191          if (T.Any()) {
    92             int f = T.ChooseUniformRandom(random);
     92            int f = T.SampleRandom(random);
    9393            T.Remove(f);
    9494            F.Remove(f);
    9595            CF.Add(f);
    96             int l = WithSlackGreaterOrEqual(CL, demands[f], slack).ChooseUniformRandom(random);
     96            int l = WithSlackGreaterOrEqual(CL, demands[f], slack).SampleRandom(random);
    9797            assignment.Add(f, l);
    9898            slack[l] -= demands[f];
     
    108108          // complete the solution and remember the one with least violation
    109109          while (F.Any()) {
    110             var f = F.ChooseMax(x => demands[x]);
    111             var l = L.ChooseMax(x => slack[x]);
     110            var f = F.MaxItems(x => demands[x]).SampleRandom(random);
     111            var l = L.MaxItems(x => slack[x]).SampleRandom(random);
    112112            F.Remove(f);
    113113            assignment.Add(f, l);
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/RandomButFeasibleSolutionCreator.cs

    r7593 r7807  
    7777        foreach (var equipment in Enumerable.Range(0, demands.Length).Shuffle(random)) {
    7878          var freeLocations = GetFreeLocations(equipment, demands, slack);
    79           assignment[equipment] = freeLocations.ChooseUniformRandom(random);
     79          assignment[equipment] = freeLocations.SampleRandom(random);
    8080          slack[assignment[equipment]] -= demands[equipment];
    8181        }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/SlackMinimizationSolutionCreator.cs

    r7679 r7807  
    111111          // complete the solution
    112112          while (remainingEquipment.Any()) {
    113             var f = remainingEquipment.ChooseMax(x => demands[x]);
    114             var l = Enumerable.Range(0, capacities.Length).ChooseMax(x => slack[x]);
     113            var f = remainingEquipment.MaxItems(x => demands[x]).SampleRandom(random);
     114            var l = Enumerable.Range(0, capacities.Length).MaxItems(x => slack[x]).SampleRandom(random);
    115115            remainingEquipment.Remove(f);
    116116            assignment.Add(f, l);
     
    133133      if (!feasibleEquipment.Any()) yield break;
    134134      if (depth == 0) {
    135         var e = feasibleEquipment.ChooseMax(x => demands[x]);
     135        var e = feasibleEquipment.MaxItems(x => demands[x]).First();
    136136        yield return e;
    137137        yield break;
     
    164164                && slack[assignment[e]] + demands[e] - demands[x] >= 0);
    165165          if (!partners.Any()) continue;
    166           var f = partners.ChooseUniformRandom(random);
     166          var f = partners.SampleRandom(random);
    167167          int h = assignment[e];
    168168          assignment[e] = assignment[f];
Note: See TracChangeset for help on using the changeset viewer.