Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/BinPacking.cs @ 15471

Last change on this file since 15471 was 15454, checked in by rhanghof, 6 years ago

#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 size: 6.6 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.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
28using HeuristicLab.Collections;
29
30namespace HeuristicLab.Problems.BinPacking {
31  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
32  [StorableClass]
33  public abstract class BinPacking<TPos, TBin, TItem> : Item
34    where TPos : class, IPackingPosition
35    where TBin : PackingShape<TPos>
36    where TItem : PackingShape<TPos> {
37    #region Properties
38    [Storable]
39
40    //key = item id
41    public ObservableDictionary<int, TPos> Positions { get; private set; }
42
43    [Storable]
44    //key = item id
45    public ObservableDictionary<int, TItem> Items { get; private set; }
46
47    [Storable]
48    public TBin BinShape { get; private set; }
49
50    [Storable]
51    public SortedSet<TPos> ExtremePoints { get; protected set; }
52
53    [Storable]
54    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
55   
56    #endregion Properties
57
58    public int FreeVolume {
59      get { return BinShape.Volume - Items.Sum(x => x.Value.Volume); }
60    }
61
62    protected BinPacking(TBin binShape)
63      : base() {
64      Positions = new ObservableDictionary<int, TPos>();
65      Items = new ObservableDictionary<int, TItem>();
66      BinShape = (TBin)binShape.Clone();
67      ExtremePoints = new SortedSet<TPos>();
68      OccupationLayers = new Dictionary<int, List<int>>();
69    }
70
71    [StorableConstructor]
72    protected BinPacking(bool deserializing) : base(deserializing) { }
73    protected BinPacking(BinPacking<TPos, TBin, TItem> original, Cloner cloner)
74      : base(original, cloner) {
75      this.Positions = new ObservableDictionary<int, TPos>();
76      foreach (var kvp in original.Positions) {
77        Positions.Add(kvp.Key, cloner.Clone(kvp.Value));
78      }
79      this.Items = new ObservableDictionary<int, TItem>();
80      foreach (var kvp in original.Items) {
81        Items.Add(kvp.Key, cloner.Clone(kvp.Value));
82      }
83      this.BinShape = (TBin)original.BinShape.Clone(cloner);
84      this.ExtremePoints = new SortedSet<TPos>(original.ExtremePoints.Select(p => cloner.Clone(p)));
85      this.OccupationLayers = new Dictionary<int, List<int>>();
86      foreach (var kvp in original.OccupationLayers) {
87        OccupationLayers.Add(kvp.Key, new List<int>(kvp.Value));
88      }
89    }
90
91    protected abstract void GenerateNewExtremePointsForNewItem(TItem item, TPos position);
92
93    public abstract TPos FindExtremePointForItem(TItem item, bool rotated, bool stackingConstraints);
94    public abstract TPos FindPositionBySliding(TItem item, bool rotated, bool stackingConstraints);
95
96    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints);
97    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints);
98    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints);
99    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray);
100
101    public virtual void PackItem(int itemID, TItem item, TPos position) {
102      Items[itemID] = item;
103      Positions[itemID] = position;
104      ExtremePoints.Remove(position);
105      GenerateNewExtremePointsForNewItem(item, position);
106     
107      AddNewItemToOccupationLayers(itemID, item, position);
108    }
109    public virtual bool PackItemIfFeasible(int itemID, TItem item, TPos position, bool stackingConstraints) {
110      if (IsPositionFeasible(item, position, stackingConstraints)) {
111        PackItem(itemID, item, position);
112        return true;
113      }
114      return false;
115    }
116
117    public double PackingDensity {
118      get {
119        double result = 0;
120        foreach (var entry in Items)
121          result += entry.Value.Volume;
122        result /= BinShape.Volume;
123        return result;
124      }
125    }
126
127
128    public int PointOccupation(TPos position) {
129      foreach (var id in GetLayerItemIDs(position)) {
130        if (Items[id].EnclosesPoint(Positions[id], position))
131          return id;
132      }
133      return -1;
134    }
135
136    public bool IsPointOccupied(TPos position) {
137      foreach (var id in GetLayerItemIDs(position)) {
138        if (Items[id].EnclosesPoint(Positions[id], position))
139          return true;
140      }
141      return false;
142    }
143
144    /// <summary>
145    ///
146    /// </summary>
147    /// <param name="item"></param>
148    /// <param name="position"></param>
149    /// <param name="stackingConstraints"></param>
150    /// <returns></returns>
151    public virtual bool IsPositionFeasible(TItem item, TPos position, bool stackingConstraints) {
152      //In this case feasability is defined as following:
153      //1. the item fits into the bin-borders;
154      //2. the point is supported by something;
155      //3. the item does not collide with another already packed item
156      if (!BinShape.Encloses(position, item))
157        return false;
158
159      foreach (var id in GetLayerItemIDs(item, position)) {
160        if (Items[id].Overlaps(Positions[id], position, item))
161          return false;
162      }
163
164      return true;
165    }
166   
167    public abstract int ShortestPossibleSideFromPoint(TPos position);
168    public abstract bool IsStaticStable(TItem measures, TPos position);
169
170    protected abstract void InitializeOccupationLayers();
171    protected abstract void AddNewItemToOccupationLayers(int itemID, TItem item, TPos position);
172    protected abstract List<int> GetLayerItemIDs(TPos position);
173    protected abstract List<int> GetLayerItemIDs(TItem item, TPos position);
174  }
175}
Note: See TracBrowser for help on using the repository browser.