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

Last change on this file since 14151 was 14151, checked in by gkronber, 6 years ago

#1966: added abstract problem and move evaluator classes and implemented 2d bin packing problem based on integer vector encoding

File size: 5.5 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;
28
29namespace HeuristicLab.Problems.BinPacking {
30  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
31  [StorableClass]
32  public abstract class BinPacking<D, B, I> : Item
33    where D : class, IPackingPosition
34    where B : PackingShape<D>
35    where I : PackingShape<D> {
36    #region Properties
37    [Storable]
38    public ObservableDictionary<int, D> ItemPositions { get; private set; }
39
40    [Storable]
41    public ObservableDictionary<int, I> ItemMeasures { get; private set; } // TODO: rename to items
42
43    [Storable]
44    public B BinMeasures { get; private set; }
45
46    [Storable]
47    public SortedSet<D> ExtremePoints { get; protected set; }
48
49    [Storable]
50    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
51
52    #endregion Properties
53
54    protected BinPacking(B binMeasures)
55      : 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 IList<int> sequence, IList<I> itemMeasures);
80    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, Dictionary<int, bool> rotationArray);
81    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, bool stackingConstraints);
82    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<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      AddNewItemToOccupationLayers(itemID, measures, position);
92    }
93
94    public double PackingDensity {
95      get {
96        double result = 0;
97        foreach (var entry in ItemMeasures)
98          result += entry.Value.Volume;
99        result /= BinMeasures.Volume;
100        return result;
101      }
102    }
103
104
105    public int PointOccupation(D position) {
106      foreach (var id in GetLayerItemIDs(position)) {
107        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
108          return id;
109      }
110      return -1;
111    }
112
113    public bool IsPointOccupied(D position) {
114      foreach (var id in GetLayerItemIDs(position)) {
115        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
116          return true;
117      }
118      return false;
119    }
120    public bool IsPositionFeasible(I measures, D position) {
121      //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
122      if (!BinMeasures.Encloses(position, measures))
123        return false;
124
125      foreach (var id in GetLayerItemIDs(measures, position)) {
126        if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
127          return false;
128      }
129
130      return true;
131    }
132    public abstract int ShortestPossibleSideFromPoint(D position);
133    public abstract bool IsStaticStable(I measures, D position);
134
135
136    protected abstract void InitializeOccupationLayers();
137    protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
138    protected abstract List<int> GetLayerItemIDs(D position);
139    protected abstract List<int> GetLayerItemIDs(I measures, D position);
140  }
141}
Note: See TracBrowser for help on using the repository browser.