Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/PermutationEncoding/ExtremePointPermutationDecoderBase.cs @ 14976

Last change on this file since 14976 was 14976, checked in by dsouravl, 7 years ago

#2762: worked on best fit heuristics

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.BinPacking3D {
31  [Item("Extreme-point Permutation Decoder (3d) Base", "Base class for decoders")]
32  [StorableClass]
33  public abstract class ExtremePointPermutationDecoderBase : Item, IDecoder<Permutation> {
34
35    [StorableConstructor]
36    protected ExtremePointPermutationDecoderBase(bool deserializing) : base(deserializing) { }
37    protected ExtremePointPermutationDecoderBase(ExtremePointPermutationDecoderBase original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public ExtremePointPermutationDecoderBase() : base() { }
41
42    public abstract Solution Decode(Permutation permutation, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints);
43
44    public virtual bool FindExtremePointForItem(Dictionary<Tuple<BinPacking3D, PackingPosition>, int> PointsSpace, PackingItem item, bool rotated, bool stackingConstraints, out BinPacking3D bin, out PackingPosition pos) {
45
46      PackingItem newItem = new PackingItem(
47        rotated ? item.Depth : item.Width,
48        item.Height,
49        rotated ? item.Width : item.Depth,
50        item.TargetBin, item.Weight, item.Material);
51
52      var EPoints = PointsSpace.Keys.ToList();
53      BinPacking3D bp;
54      PackingPosition position;
55      int epIndex = 0;
56      while (epIndex < EPoints.Count && (
57        !(((bp = EPoints.ElementAt(epIndex).Item1)).IsPositionFeasible(newItem, (position = EPoints.ElementAt(epIndex).Item2)))
58        || !bp.IsSupportedByAtLeastOnePoint(newItem, position)
59        || (stackingConstraints && !bp.IsStaticStable(newItem, position))
60        || (stackingConstraints && !bp.IsWeightSupported(newItem, position))
61      )) { epIndex++; }
62
63      if (epIndex < EPoints.Count) {
64        bin = EPoints.ElementAt(epIndex).Item1;
65        var origPoint = EPoints.ElementAt(epIndex).Item2;
66        pos = new PackingPosition(origPoint.AssignedBin, origPoint.X, origPoint.Y, origPoint.Z, rotated);
67
68        return true;
69      }
70      bin = null;
71      pos = null;
72      return false;
73    }
74
75    public virtual Dictionary<Tuple<BinPacking3D, PackingPosition>, int> GetResidualSpaceAllPoints(Solution solution, PackingItem item) {
76      Dictionary<Tuple<BinPacking3D, PackingPosition>, int> result = new Dictionary<Tuple<BinPacking3D, PackingPosition>, int>();
77      foreach (BinPacking3D bp in solution.Bins) {
78        foreach (var ep in bp.ExtremePoints) {
79          result.Add(Tuple.Create(bp, ep), bp.GetResidualSpace(item, ep));
80        }
81      }
82      return result;
83    }
84
85    public virtual bool ExtremePointBasedPacking(Dictionary<Tuple<BinPacking3D, PackingPosition>, int> PointsSpace, int itemID, IList<PackingItem> items, bool stackingConstraints) {
86      BinPacking3D bp;
87      PackingPosition positionFound;
88      var item = items[itemID];
89      if (FindExtremePointForItem(PointsSpace, item, false, stackingConstraints, out bp, out positionFound)) {
90        bp.PackItem(itemID, item, positionFound);
91        return true;
92      }
93      return false;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.