Changeset 15454 for branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/Algorithms/3D/ExtremePointAlgorithm.cs
- Timestamp:
- 11/07/17 08:24:24 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/Algorithms/3D/ExtremePointAlgorithm.cs
r15229 r15454 32 32 using HeuristicLab.Parameters; 33 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.Problems.BinPacking3D.Packer; 34 35 35 36 namespace HeuristicLab.Problems.BinPacking3D { 36 37 37 38 public enum SortingMethod { All, Given, VolumeHeight, HeightVolume, AreaHeight, HeightArea, ClusteredAreaHeight, ClusteredHeightArea } 38 39 public enum FittingMethod { All, FirstFit, ResidualSpaceBestFit, FreeVolumeBestFit } … … 86 87 Parameters.Add(fittingMethodParameter = new ValueParameter<EnumValue<FittingMethod>>("FittingMethod", "Which method to fit should be used.", new EnumValue<FittingMethod>(FittingMethod.All))); 87 88 Parameters.Add(deltaParameter = new ValueParameter<PercentValue>("Delta", "[1;100]% Clustered sorting methods use a delta parameter to determine the clusters.", new PercentValue(.1))); 88 89 89 90 Problem = new PermutationProblem(); 90 91 } … … 93 94 return new ExtremePointAlgorithm(this, cloner); 94 95 } 95 96 96 97 [StorableHook(HookType.AfterDeserialization)] 97 98 private void AfterDeserialization() { 98 99 } 99 100 101 /// <summary> 102 /// Runs the extreme point algorithm and adds the results to the property <see cref="Result"/> 103 /// </summary> 104 /// <param name="token"></param> 100 105 protected override void Run(CancellationToken token) { 101 106 var items = Problem.Items; … … 109 114 fitting = Enum.GetValues(typeof(FittingMethod)).Cast<FittingMethod>().Where(x => x != FittingMethod.All).ToArray(); 110 115 } 116 117 // 111 118 var result = GetBest(bin, items, sorting, fitting, token); 112 if (result == null) throw new InvalidOperationException("No result obtained!"); 113 114 Results.Add(new Result("Best Solution", 115 "The best found solution", 116 result.Item1)); 117 Results.Add(new Result("Best Solution Quality", 118 "The quality of the best found solution according to the evaluator", 119 new DoubleValue(result.Item2))); 119 if (result == null) { 120 throw new InvalidOperationException("No result obtained!"); 121 } 122 123 Results.Add(new Result("Best Solution", "The best found solution", result.Item1)); 124 Results.Add(new Result("Best Solution Quality", "The quality of the best found solution according to the evaluator", new DoubleValue(result.Item2))); 120 125 121 126 var binUtil = new BinUtilizationEvaluator(); … … 128 133 new PercentValue(Math.Round(binUtil.Evaluate(result.Item1), 3)))); 129 134 130 if (result.Item3.HasValue && sorting.Length > 1) 135 if (result.Item3.HasValue && sorting.Length > 1) { 131 136 Results.Add(new Result("Best Sorting Method", 132 137 "The sorting method that found the best solution", 133 138 new EnumValue<SortingMethod>(result.Item3.Value))); 134 if (result.Item4.HasValue && fitting.Length > 1) 139 } 140 141 if (result.Item4.HasValue && fitting.Length > 1) { 135 142 Results.Add(new Result("Best Fitting Method", 136 143 "The fitting method that found the best solution", 137 144 new EnumValue<FittingMethod>(result.Item4.Value))); 145 } 138 146 } 139 147 … … 146 154 foreach (var sort in sortings) { 147 155 var result = Optimize(bin, items, sort, fit, DeltaParameter.Value.Value, Problem.UseStackingConstraints, Problem.SolutionEvaluator, token); 148 if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2)) continue; 149 if (double.IsNaN(best) 150 || Problem.Maximization && result.Item2 > best 151 || !Problem.Maximization && result.Item2 < best) { 156 if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2)) { 157 continue; 158 } 159 160 if (double.IsNaN(best) || Problem.Maximization && result.Item2 > best || !Problem.Maximization && result.Item2 < best) { 152 161 bestSolution = result.Item1; 153 162 best = result.Item2; … … 155 164 bestFitting = fit; 156 165 } 157 if (token.IsCancellationRequested) return Tuple.Create(bestSolution, best, bestSorting, bestFitting); 166 if (token.IsCancellationRequested) { 167 return Tuple.Create(bestSolution, best, bestSorting, bestFitting); 168 } 158 169 } 159 170 } 160 if (double.IsNaN(best)) return null; 171 if (double.IsNaN(best)) { 172 return null; 173 } 161 174 return Tuple.Create(bestSolution, best, bestSorting, bestFitting); 162 175 } 163 176 164 177 private static Tuple<Solution, double> Optimize(PackingShape bin, IList<PackingItem> items, SortingMethod sorting, FittingMethod fitting, double delta, bool stackingConstraints, IEvaluator evaluator, CancellationToken token) { 178 Permutation sortedItems = SortItemsBySortingMethod(bin, items, sorting, delta); 179 180 if (false) {// alt 181 ExtremePointPermutationDecoderBase decoder = GetDecoderByFittingMethod(fitting); 182 var sol = decoder.Decode(sortedItems, bin, items, stackingConstraints); 183 var fit = evaluator.Evaluate(sol); 184 185 return Tuple.Create(sol, fit); 186 } else { 187 Decoder.ExtremePointPermutationDecoder decoder = new Decoder.ExtremePointPermutationDecoder(GetBinPackerByFittingMethod(fitting, sortedItems, bin, items, stackingConstraints)); 188 189 var sol = decoder.Decode(sortedItems, bin, items, stackingConstraints); 190 var fit = evaluator.Evaluate(sol); 191 192 return Tuple.Create(sol, fit); 193 } 194 195 196 197 } 198 199 200 201 private static BinPacker GetBinPackerByFittingMethod(FittingMethod fittingMethod, Permutation sortedItems, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) { 202 BinPacker binPacker = null; 203 switch (fittingMethod) { 204 case FittingMethod.FirstFit: 205 binPacker = new BinPackerFirstFit(sortedItems, binShape, items, useStackingConstraints); 206 break; 207 case FittingMethod.FreeVolumeBestFit: 208 binPacker = new BinPackerFreeVolumeBestFit(sortedItems, binShape, items, useStackingConstraints); 209 break; 210 case FittingMethod.ResidualSpaceBestFit: 211 binPacker = new BinPackerResidualSpaceBestFit(sortedItems, binShape, items, useStackingConstraints); 212 break; 213 default: 214 throw new ArgumentException("Unknown fitting method: " + fittingMethod); 215 } 216 return binPacker; 217 } 218 219 /// <summary> 220 /// Returns a new permutation of the given items depending on the sorting method 221 /// </summary> 222 /// <param name="bin"></param> 223 /// <param name="items"></param> 224 /// <param name="sortingMethod"></param> 225 /// <param name="delta"></param> 226 /// <returns></returns> 227 private static Permutation SortItemsBySortingMethod(PackingShape bin, IList<PackingItem> items, SortingMethod sortingMethod, double delta) { 165 228 Permutation sorted = null; 166 switch (sorting ) {229 switch (sortingMethod) { 167 230 case SortingMethod.Given: 168 231 sorted = new Permutation(PermutationTypes.Absolute, Enumerable.Range(0, items.Count).ToArray()); … … 216 279 .Select(x => x.Index).ToArray()); 217 280 break; 218 default: throw new ArgumentException("Unknown sorting method: " + sorting); 219 } 220 281 default: 282 throw new ArgumentException("Unknown sorting method: " + sortingMethod); 283 } 284 return sorted; 285 } 286 287 /// <summary> 288 /// Returns a decoder depending on the given fitting method 289 /// </summary> 290 /// <param name="fittingMethod"></param> 291 /// <returns></returns> 292 private static ExtremePointPermutationDecoderBase GetDecoderByFittingMethod(FittingMethod fittingMethod) { 221 293 ExtremePointPermutationDecoderBase decoder = null; 222 switch (fitting ) {294 switch (fittingMethod) { 223 295 case FittingMethod.FirstFit: 224 296 decoder = new ExtremePointPermutationDecoder(); … … 230 302 decoder = new ResidualSpaceBestFitExtremePointPermutationDecoder(); 231 303 break; 232 default: throw new ArgumentException("Unknown fitting method: " + fitting); 233 } 234 235 var sol = decoder.Decode(sorted, bin, items, stackingConstraints); 236 var fit = evaluator.Evaluate(sol); 237 238 return Tuple.Create(sol, fit); 304 default: 305 throw new ArgumentException("Unknown fitting method: " + fittingMethod); 306 } 307 return decoder; 239 308 } 240 309 }
Note: See TracChangeset
for help on using the changeset viewer.