Changeset 15488 for branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Algorithms
- Timestamp:
- 11/28/17 16:10:31 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Algorithms/ExtremePointAlgorithm.cs
r15473 r15488 42 42 public enum FittingMethod { All, FirstFit, ResidualSpaceBestFit, FreeVolumeBestFit } 43 43 44 public enum ExtremePointCreationMethod { All, PointProjection, LineProjection } 45 44 46 [Item("Extreme-point-based Bin Packing (3d)", "An implementation of the extreme-point based packing described in Crainic, T. G., Perboli, G., & Tadei, R. (2008). Extreme point-based heuristics for three-dimensional bin packing. Informs Journal on computing, 20(3), 368-384.")] 45 47 [StorableClass] … … 83 85 public IValueParameter<BoolValue> SortByMaterialParameter { 84 86 get { return sortByMaterialParameter; } 85 } 87 } 88 89 [Storable] 90 private readonly IValueParameter<EnumValue<ExtremePointCreationMethod>> extremePointCreationMethodParameter; 91 public IValueParameter<EnumValue<ExtremePointCreationMethod>> ExtremePointCreationMethodParameter { 92 get { return extremePointCreationMethodParameter; } 93 } 86 94 87 95 [StorableConstructor] … … 100 108 "FittingMethod", "Which method to fit should be used.", new EnumValue<FittingMethod>(FittingMethod.All))); 101 109 110 Parameters.Add(extremePointCreationMethodParameter = new ValueParameter<EnumValue<ExtremePointCreationMethod>>( 111 "ExtremePointCreationMethod", "Which method should be used for creatomg extreme points.", new EnumValue<ExtremePointCreationMethod>(ExtremePointCreationMethod.All))); 112 102 113 Parameters.Add(deltaParameter = new ValueParameter<PercentValue>( 103 114 "Delta", "[1;100]% Clustered sorting methods use a delta parameter to determine the clusters.", new PercentValue(.1))); … … 124 135 var items = Problem.Items; 125 136 var bin = Problem.BinShape; 137 126 138 var sorting = new[] { SortingMethodParameter.Value.Value }; 127 139 if (sorting[0] == SortingMethod.All) { 128 140 sorting = Enum.GetValues(typeof(SortingMethod)).Cast<SortingMethod>().Where(x => x != SortingMethod.All).ToArray(); 129 141 } 142 130 143 var fitting = new[] { fittingMethodParameter.Value.Value }; 131 144 if (fitting[0] == FittingMethod.All) { … … 133 146 } 134 147 148 var extremePointCreation = new[] { ExtremePointCreationMethodParameter.Value.Value }; 149 if (extremePointCreation[0] == ExtremePointCreationMethod.All) { 150 extremePointCreation = Enum.GetValues(typeof(ExtremePointCreationMethod)) 151 .Cast<ExtremePointCreationMethod>() 152 .Where(x => x != ExtremePointCreationMethod.All) 153 .ToArray(); 154 } 155 135 156 // 136 var result = GetBest(bin, items, sorting, fitting, token);157 var result = GetBest(bin, items, sorting, fitting, extremePointCreation, token); 137 158 if (result == null) { 138 159 throw new InvalidOperationException("No result obtained!"); … … 162 183 new EnumValue<FittingMethod>(result.Item4.Value))); 163 184 } 185 186 if (result.Item5.HasValue && extremePointCreation.Length > 1) { 187 Results.Add(new Result("Best extreme point creation method", 188 "The extreme point creation method that found the best solution", 189 new EnumValue<ExtremePointCreationMethod>(result.Item5.Value))); 190 } 164 191 } 165 192 … … 173 200 /// <param name="token"></param> 174 201 /// <returns></returns> 175 private Tuple<Solution, double, SortingMethod?, FittingMethod?> GetBest(PackingShape bin, IList<PackingItem> items, SortingMethod[] sortings, FittingMethod[] fittings, CancellationToken token) { 202 private Tuple<Solution, double, SortingMethod?, FittingMethod?, ExtremePointCreationMethod?> 203 GetBest(PackingShape bin, 204 IList<PackingItem> items, 205 SortingMethod[] sortings, 206 FittingMethod[] fittings, 207 ExtremePointCreationMethod[] ePGeneration, 208 CancellationToken token) { 176 209 SortingMethod? bestSorting = null; 177 210 FittingMethod? bestFitting = null; 211 ExtremePointCreationMethod? bestEPCreation = null; 178 212 var best = double.NaN; 179 213 Solution bestSolution = null; 180 214 foreach (var fit in fittings) { 181 215 foreach (var sort in sortings) { 182 IDecoder<Permutation> decoder = new ExtremePointPermutationDecoder(BinPackerFactory.CreateBinPacker(fit)); 183 Permutation sortedItems; 216 foreach (var epCreation in ePGeneration) { 217 IDecoder<Permutation> decoder = new ExtremePointPermutationDecoder() { 218 FittingMethod = fit, 219 ExtremePointCreationMethod = epCreation 220 }; 221 Permutation sortedItems; 184 222 185 if (SortByMaterialParameter.Value.Value) { 186 sortedItems = SortItemsByMaterialAndSortingMethod(bin, items, sort, DeltaParameter.Value.Value); 187 } else { 188 sortedItems = SortItemsBySortingMethod(bin, items, sort, DeltaParameter.Value.Value); 189 } 223 if (SortByMaterialParameter.Value.Value) { 224 sortedItems = SortItemsByMaterialAndSortingMethod(bin, items, sort, DeltaParameter.Value.Value); 225 } else { 226 sortedItems = SortItemsBySortingMethod(bin, items, sort, DeltaParameter.Value.Value); 227 } 228 229 var result = Optimize(new OptimaizationParamters() { 230 SortedItems = sortedItems, 231 Bin = bin, 232 Items = items, 233 StackingConstraints = Problem.UseStackingConstraints, 234 Decoder = decoder, 235 Evaluator = Problem.SolutionEvaluator, 236 ExtremePointGeneration = epCreation 237 }); 190 238 191 var result = Optimize(sortedItems, bin, items, Problem.UseStackingConstraints, decoder, Problem.SolutionEvaluator);192 193 if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2)) {194 continue; 195 }196 197 if (double.IsNaN(best) || Problem.Maximization && result.Item2 > best || !Problem.Maximization && result.Item2 < best) {198 bestSolution = result.Item1;199 best = result.Item2;200 bestSorting = sort;201 bestFitting = fit;202 }203 if (token.IsCancellationRequested) {204 return Tuple.Create(bestSolution, best, bestSorting, bestFitting);239 if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2)) { 240 continue; 241 } 242 243 if (double.IsNaN(best) || Problem.Maximization && result.Item2 > best || !Problem.Maximization && result.Item2 < best) { 244 bestSolution = result.Item1; 245 best = result.Item2; 246 bestSorting = sort; 247 bestFitting = fit; 248 bestEPCreation = epCreation; 249 } 250 if (token.IsCancellationRequested) { 251 return Tuple.Create(bestSolution, best, bestSorting, bestFitting, bestEPCreation); 252 } 205 253 } 206 254 } … … 209 257 return null; 210 258 } 211 return Tuple.Create(bestSolution, best, bestSorting, bestFitting );259 return Tuple.Create(bestSolution, best, bestSorting, bestFitting, bestEPCreation); 212 260 } 213 261 … … 215 263 /// Returns a tuple with the solution and the packing ratio depending on the given parameters 216 264 /// </summary> 217 /// <param name="sortedItems"></param> 218 /// <param name="bin"></param> 219 /// <param name="items"></param> 220 /// <param name="stackingConstraints"></param> 221 /// <param name="decoder"></param> 222 /// <param name="evaluator"></param> 265 /// <param name="parameters"></param> 223 266 /// <returns></returns> 224 private static Tuple<Solution, double> Optimize( Permutation sortedItems, PackingShape bin, IList<PackingItem> items, bool stackingConstraints, IDecoder<Permutation> decoder, IEvaluator evaluator) {267 private static Tuple<Solution, double> Optimize(OptimaizationParamters parameters) { 225 268 226 var sol = decoder.Decode(sortedItems, bin, items, stackingConstraints);227 var fit = evaluator.Evaluate(sol);269 var sol = parameters.Decoder.Decode(parameters.SortedItems, parameters.Bin, parameters.Items, parameters.StackingConstraints); 270 var fit = parameters.Evaluator.Evaluate(sol); 228 271 229 272 return Tuple.Create(sol, fit); 273 } 274 275 private class OptimaizationParamters { 276 public Permutation SortedItems { get; set; } 277 public PackingShape Bin { get; set; } 278 public IList<PackingItem> Items { get; set; } 279 public bool StackingConstraints { get; set; } 280 public IDecoder<Permutation> Decoder { get; set; } 281 public IEvaluator Evaluator { get; set; } 282 public ExtremePointCreationMethod ExtremePointGeneration { get; set; } 230 283 } 231 284
Note: See TracChangeset
for help on using the changeset viewer.