Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/07/17 08:24:24 (6 years ago)
Author:
rhanghof
Message:

#2817

  • Extreme point bin packing does not need the occupation layer anymore
  • Changes at the fitting algorithm. Now they are packing the items as in the paper of S. Martello, D. Pisinger, D. Vigo described
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/Algorithms/3D/ExtremePointAlgorithm.cs

    r15229 r15454  
    3232using HeuristicLab.Parameters;
    3333using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     34using HeuristicLab.Problems.BinPacking3D.Packer;
    3435
    3536namespace HeuristicLab.Problems.BinPacking3D {
    36  
     37
    3738  public enum SortingMethod { All, Given, VolumeHeight, HeightVolume, AreaHeight, HeightArea, ClusteredAreaHeight, ClusteredHeightArea }
    3839  public enum FittingMethod { All, FirstFit, ResidualSpaceBestFit, FreeVolumeBestFit }
     
    8687      Parameters.Add(fittingMethodParameter = new ValueParameter<EnumValue<FittingMethod>>("FittingMethod", "Which method to fit should be used.", new EnumValue<FittingMethod>(FittingMethod.All)));
    8788      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
    8990      Problem = new PermutationProblem();
    9091    }
     
    9394      return new ExtremePointAlgorithm(this, cloner);
    9495    }
    95    
     96
    9697    [StorableHook(HookType.AfterDeserialization)]
    9798    private void AfterDeserialization() {
    9899    }
    99100
     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>
    100105    protected override void Run(CancellationToken token) {
    101106      var items = Problem.Items;
     
    109114        fitting = Enum.GetValues(typeof(FittingMethod)).Cast<FittingMethod>().Where(x => x != FittingMethod.All).ToArray();
    110115      }
     116
     117      //
    111118      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)));
    120125
    121126      var binUtil = new BinUtilizationEvaluator();
     
    128133        new PercentValue(Math.Round(binUtil.Evaluate(result.Item1), 3))));
    129134
    130       if (result.Item3.HasValue && sorting.Length > 1)
     135      if (result.Item3.HasValue && sorting.Length > 1) {
    131136        Results.Add(new Result("Best Sorting Method",
    132137          "The sorting method that found the best solution",
    133138          new EnumValue<SortingMethod>(result.Item3.Value)));
    134       if (result.Item4.HasValue && fitting.Length > 1)
     139      }
     140
     141      if (result.Item4.HasValue && fitting.Length > 1) {
    135142        Results.Add(new Result("Best Fitting Method",
    136143          "The fitting method that found the best solution",
    137144          new EnumValue<FittingMethod>(result.Item4.Value)));
     145      }
    138146    }
    139147
     
    146154        foreach (var sort in sortings) {
    147155          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) {
    152161            bestSolution = result.Item1;
    153162            best = result.Item2;
     
    155164            bestFitting = fit;
    156165          }
    157           if (token.IsCancellationRequested) return Tuple.Create(bestSolution, best, bestSorting, bestFitting);
     166          if (token.IsCancellationRequested) {
     167            return Tuple.Create(bestSolution, best, bestSorting, bestFitting);
     168          }
    158169        }
    159170      }
    160       if (double.IsNaN(best)) return null;
     171      if (double.IsNaN(best)) {
     172        return null;
     173      }
    161174      return Tuple.Create(bestSolution, best, bestSorting, bestFitting);
    162175    }
    163176
    164177    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) {
    165228      Permutation sorted = null;
    166       switch (sorting) {
     229      switch (sortingMethod) {
    167230        case SortingMethod.Given:
    168231          sorted = new Permutation(PermutationTypes.Absolute, Enumerable.Range(0, items.Count).ToArray());
     
    216279                        .Select(x => x.Index).ToArray());
    217280          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) {
    221293      ExtremePointPermutationDecoderBase decoder = null;
    222       switch (fitting) {
     294      switch (fittingMethod) {
    223295        case FittingMethod.FirstFit:
    224296          decoder = new ExtremePointPermutationDecoder();
     
    230302          decoder = new ResidualSpaceBestFitExtremePointPermutationDecoder();
    231303          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;
    239308    }
    240309  }
Note: See TracChangeset for help on using the changeset viewer.