Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13605 was 13605, checked in by gkronber, 8 years ago

#1966 refactoring

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