Changeset 7807
- Timestamp:
- 05/14/12 15:59:39 (13 years ago)
- Location:
- branches/GeneralizedQAP
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3/ExtensionMethods.cs
r7593 r7807 30 30 #region Choosing from a sequence 31 31 /// <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}"/> and32 /// 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 36 36 /// O(N) for all other. 37 37 /// </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> 38 57 /// <typeparam name="T">The type of the items to be selected.</typeparam> 39 58 /// <param name="source">The sequence of elements.</param> … … 41 60 /// <param name="count">The number of items to be selected.</param> 42 61 /// <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) { 64 63 var listSource = source as IList<T>; 65 if (listSource != null) 64 if (listSource != null) { 66 65 while (count > 0) { 67 66 yield return listSource[random.Next(listSource.Count)]; 68 67 count--; 69 68 } 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. 93 94 /// </remarks> 94 95 /// <typeparam name="T">The type of the items to be selected.</typeparam> … … 97 98 /// <param name="count">The number of items to be selected.</param> 98 99 /// <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 > 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 > 0, 109 118 /// otherwise an InvalidOperationException is thrown. 110 119 /// 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> 119 127 /// <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> 121 129 /// <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 eachitem exactly once.127 /// </summary> 128 /// <remarks> 129 /// In case both <paramref name=" maximization"/> and <paramref name="windowing"/> are false values must be > 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 > 0, 130 138 /// otherwise an InvalidOperationException is thrown. 131 139 /// 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> 140 147 /// <param name="windowing">Whether to scale the proportional values or not.</param> 141 148 /// <param name="maximization">Determines whether to choose proportionally (true) or inverse-proportionally (false).</param> 142 149 /// <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); 145 152 } 146 153 #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) { 151 155 var sourceArray = source.ToArray(); 152 var valueArray = PrepareProportional<T>(sourceArray, valueSelector, windowing, maximization);156 var valueArray = PrepareProportional<T>(sourceArray, weights, windowing, inverseProportional); 153 157 double total = valueArray.Sum(); 154 158 … … 161 165 } 162 166 } 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) { 167 168 var sourceArray = source.ToArray(); 168 var valueArray = PrepareProportional<T>(sourceArray, valueSelector, windowing, maximization);169 var valueArray = PrepareProportional<T>(sourceArray, weights, windowing, inverseProportional); 169 170 double total = valueArray.Sum(); 170 171 … … 183 184 } 184 185 } 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) { 186 187 double maxValue = double.MinValue, minValue = double.MaxValue; 187 188 double[] valueArray = new double[sourceArray.Count]; 188 189 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; 191 193 if (valueArray[i] > maxValue) maxValue = valueArray[i]; 192 194 if (valueArray[i] < minValue) minValue = valueArray[i]; … … 198 200 } else { 199 201 if (windowing) { 200 if ( maximization)ProportionalScale(valueArray, minValue);201 else InverseProportionalScale(valueArray, maxValue);202 if (inverseProportional) InverseProportionalScale(valueArray, minValue); 203 else ProportionalScale(valueArray, maxValue); 202 204 } else { 203 205 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); 205 207 } 206 208 } … … 220 222 221 223 /// <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. 223 225 /// </summary> 224 226 /// <remarks> … … 226 228 /// </remarks> 227 229 /// <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) { 234 234 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 238 240 while (enumerator.MoveNext()) { 239 241 T item = enumerator.Current; 240 TComparable comparison = keySelector(item);242 IComparable comparison = valueSelector(item); 241 243 if (comparison.CompareTo(max) > 0) { 242 result = item; 244 result.Clear(); 245 result.Add(item); 243 246 max = comparison; 247 } else if (comparison.CompareTo(max) == 0) { 248 result.Add(item); 244 249 } 245 250 } … … 248 253 249 254 /// <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. 251 256 /// </summary> 252 257 /// <remarks> … … 254 259 /// </remarks> 255 260 /// <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) { 262 265 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 266 271 while (enumerator.MoveNext()) { 267 272 T item = enumerator.Current; 268 TComparable comparison = keySelector(item);273 IComparable comparison = valueSelector(item); 269 274 if (comparison.CompareTo(min) < 0) { 270 result = item; 275 result.Clear(); 276 result.Add(item); 271 277 min = comparison; 278 } else if (comparison.CompareTo(min) == 0) { 279 result.Add(item); 272 280 } 273 281 } … … 281 289 /// </summary> 282 290 /// <remarks> 283 /// Note that th is is an online shuffle, but the source enumerable is transformed into an array.291 /// Note that the source enumerable is transformed into an array. 284 292 /// 285 293 /// The implementation is described in http://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm. … … 292 300 T[] elements = source.ToArray(); 293 301 for (int i = elements.Length - 1; i > 0; i--) { 294 // Swap element "i" with a random earlier element it (oritself)302 // Swap element "i" with a random earlier element (including itself) 295 303 int swapIndex = random.Next(i + 1); 296 304 yield return elements[swapIndex]; … … 333 341 334 342 #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)]; 337 351 for (int i = 0; i < matrix.GetLength(0); i++) 338 352 for (int j = 0; j < matrix.GetLength(1); j++) -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs
r7470 r7807 157 157 int bestIndex; 158 158 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; 161 161 162 162 if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) { -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/GQAPPathRelinking.cs
r7523 r7807 183 183 if (B.Any()) { 184 184 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(); 187 187 var diff = IntegerVectorEqualityComparer.GetDifferingIndices(pi.Assignment, target); 188 188 var I = phi.Except(diff); 189 var i = I. ChooseUniformRandom(random);189 var i = I.SampleRandom(random); 190 190 fix.Add(i); 191 191 nonFix.Remove(i); … … 226 226 var T = nonFix.Where(x => x != equipment && assignment[x] == l && demands[x] <= maxSlack); 227 227 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); 230 230 result[i] = j; 231 231 slack[j] -= demands[i]; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/DemandEquivalentSwapEquipmentManipluator.cs
r7523 r7807 80 80 groupedLocations[location2].Remove(equipment2); 81 81 if (!groupedLocations[location2].Any()) break; 82 equipment2 = groupedLocations[location2]. ChooseUniformRandom(random);82 equipment2 = groupedLocations[location2].SampleRandom(random); 83 83 } 84 84 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/RelocateEquipmentManipluator.cs
r7523 r7807 79 79 } 80 80 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); 83 83 var bestFreeLocations = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipmentToRelocate]); 84 84 85 85 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); 87 87 assignment[equipmentToRelocate] = sourceLocation.Key; 88 88 } else { // the solution will become infeasible 89 sourceLocation = freeLocations. ChooseUniformRandom(random);89 sourceLocation = freeLocations.SampleRandom(random); 90 90 assignment[equipmentToRelocate] = sourceLocation.Key; 91 91 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs
r7593 r7807 84 84 do { 85 85 if (L.Any() && random.NextDouble() < threshold) { 86 int l = L. ChooseUniformRandom(random);86 int l = L.SampleRandom(random); 87 87 L.Remove(l); 88 88 CL.Add(l); … … 90 90 } 91 91 if (T.Any()) { 92 int f = T. ChooseUniformRandom(random);92 int f = T.SampleRandom(random); 93 93 T.Remove(f); 94 94 F.Remove(f); 95 95 CF.Add(f); 96 int l = WithSlackGreaterOrEqual(CL, demands[f], slack). ChooseUniformRandom(random);96 int l = WithSlackGreaterOrEqual(CL, demands[f], slack).SampleRandom(random); 97 97 assignment.Add(f, l); 98 98 slack[l] -= demands[f]; … … 108 108 // complete the solution and remember the one with least violation 109 109 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); 112 112 F.Remove(f); 113 113 assignment.Add(f, l); -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/RandomButFeasibleSolutionCreator.cs
r7593 r7807 77 77 foreach (var equipment in Enumerable.Range(0, demands.Length).Shuffle(random)) { 78 78 var freeLocations = GetFreeLocations(equipment, demands, slack); 79 assignment[equipment] = freeLocations. ChooseUniformRandom(random);79 assignment[equipment] = freeLocations.SampleRandom(random); 80 80 slack[assignment[equipment]] -= demands[equipment]; 81 81 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/SlackMinimizationSolutionCreator.cs
r7679 r7807 111 111 // complete the solution 112 112 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); 115 115 remainingEquipment.Remove(f); 116 116 assignment.Add(f, l); … … 133 133 if (!feasibleEquipment.Any()) yield break; 134 134 if (depth == 0) { 135 var e = feasibleEquipment. ChooseMax(x => demands[x]);135 var e = feasibleEquipment.MaxItems(x => demands[x]).First(); 136 136 yield return e; 137 137 yield break; … … 164 164 && slack[assignment[e]] + demands[e] - demands[x] >= 0); 165 165 if (!partners.Any()) continue; 166 var f = partners. ChooseUniformRandom(random);166 var f = partners.SampleRandom(random); 167 167 int h = assignment[e]; 168 168 assignment[e] = assignment[f];
Note: See TracChangeset
for help on using the changeset viewer.