source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/BinPacking.cs @ 14128

Last change on this file since 14128 was 14128, checked in by gkronber, 3 years ago

#1966: refactoring of bin packing implementation

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
27using HeuristicLab.Collections;
28using HeuristicLab.Problems.BinPacking;
29
30namespace HeuristicLab.Encodings.PackingEncoding {
31  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
32  [StorableClass]
33  public abstract class BinPacking<D,B,I>  : Item
34    where D : class, IPackingPosition
35    where B : PackingShape<D>
36    where I : PackingShape<D>, IPackingItem {
37    #region Properties
38    [Storable]
39    public ObservableDictionary<int, D> ItemPositions { get; private set; } 
40
41    [Storable]
42    public ObservableDictionary<int, I> ItemMeasures { get; private set; } // TODO: rename to items
43
44    [Storable]
45    public B BinMeasures { get; private set; } 
46
47    [Storable]
48    public SortedSet<D> ExtremePoints { get; protected set; }
49
50    [Storable]
51    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
52
53    #endregion Properties
54
55    protected BinPacking(B binMeasures) : base() {   
56      ItemPositions = new ObservableDictionary<int, D>();
57      ItemMeasures = new ObservableDictionary<int, I>();
58      BinMeasures = (B)binMeasures.Clone();
59      OccupationLayers = new Dictionary<int, List<int>>();
60      InitializeOccupationLayers(); // TODO
61    }
62
63
64    [StorableConstructor]
65    protected BinPacking(bool deserializing) : base(deserializing) { }
66    protected BinPacking(BinPacking<D,B,I> original, Cloner cloner)
67      : base(original, cloner) {
68      this.ItemPositions = new ObservableDictionary<int, D>(original.ItemPositions);
69      this.ItemMeasures = new ObservableDictionary<int, I>(original.ItemMeasures);
70        this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
71        this.OccupationLayers = new Dictionary<int, List<int>>(original.OccupationLayers);
72    }
73   
74    protected abstract void GenerateNewExtremePointsForNewItem(I measures, D position);
75
76    public abstract D FindExtremePointForItem(I measures, bool rotated, bool stackingConstraints);
77    public abstract D FindPositionBySliding(I measures, bool rotated);
78
79    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures);
80    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, Dictionary<int, bool> rotationArray);
81    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints);
82    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray);
83
84    public void PackItem(int itemID, I measures, D position) {
85      ItemMeasures[itemID] = measures;
86      ItemPositions[itemID] = position;
87      ExtremePoints.Remove(position);
88      foreach (int id in ItemMeasures.Select(x => x.Key))
89        GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
90     
91       
92      //GenerateNewExtremePointsForNewItem(measures, position);
93      //OccupiedPoints.OccupyPoints(measures, position, itemID);
94
95      AddNewItemToOccupationLayers(itemID, measures, position);
96    }
97
98    public double PackingDensity { 
99      get {
100        double result = 0;
101        foreach (var entry in ItemMeasures)
102          result += entry.Value.Volume;
103        result /= BinMeasures.Volume;
104        return result;
105      }
106    }
107
108
109    public int PointOccupation(D position) {
110      //foreach (var ipEntry in ItemPositions) {
111      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
112      //    return ipEntry.Key;
113      //}
114      foreach (var id in GetLayerItemIDs(position)) {
115        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
116          return id;
117      }
118      return -1;
119    }
120
121    public bool IsPointOccupied(D position) {
122      //foreach (var ipEntry in ItemPositions) {
123      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
124      //    return true;
125      //}     
126      foreach (var id in GetLayerItemIDs(position)) {
127        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
128          return true;
129      }
130      return false;
131    }
132    public bool IsPositionFeasible(I measures, D position) {
133      //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the point is supported by something; 3. the item does not collide with another already packed item
134      if (!BinMeasures.Encloses(position, measures))
135        return false;
136
137      //foreach (var ipEntry in ItemPositions) {
138      //  if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, measures))
139      //    return false;
140      //}
141      foreach (var id in GetLayerItemIDs(measures, position)) {
142        if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
143          return false;
144      }
145
146      return true;
147    }
148    public abstract int ShortestPossibleSideFromPoint (D position);
149    public abstract bool IsStaticStable (I measures, D position);
150
151
152    protected abstract void InitializeOccupationLayers();
153    protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
154    protected abstract List<int> GetLayerItemIDs(D position);
155    protected abstract List<int> GetLayerItemIDs(I measures, D position);
156  }   
157}
Note: See TracBrowser for help on using the repository browser.