Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13032 was 13032, checked in by gkronber, 9 years ago

#1966:

  • removed unused using
  • added/updated license headers
File size: 6.4 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; }
44
45    [Storable]
46    public B BinMeasures { get; private set; }
47
48    [Storable]
49    //public HashSet<D> ExtremePoints { get; protected set; }
50    public SortedSet<D> ExtremePoints { get; protected set; }
51
52    [Storable]
53    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
54
55    #endregion Properties
56
57    public BinPacking(B binMeasures) : base() {   
58      ItemPositions = new ObservableDictionary<int, D>();
59      ItemMeasures = new ObservableDictionary<int, I>();
60      BinMeasures = (B)binMeasures.Clone();
61      OccupationLayers = new Dictionary<int, List<int>>();
62      InitializeOccupationLayers();
63    }
64
65
66    [StorableConstructor]
67    protected BinPacking(bool deserializing) : base(deserializing) { }
68    protected BinPacking(BinPacking<D,B,I> original, Cloner cloner)
69      : base(original, cloner) {
70      this.ItemPositions = new ObservableDictionary<int, D>(original.ItemPositions);
71      this.ItemMeasures = new ObservableDictionary<int, I>(original.ItemMeasures);
72        this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
73        this.OccupationLayers = new Dictionary<int, List<int>>(original.OccupationLayers);
74    }
75   
76    protected abstract void GenerateNewExtremePointsForNewItem(I measures, D position);
77
78    public abstract D FindExtremePointForItem(I measures, bool rotated, bool stackingConstraints);
79    public abstract D FindPositionBySliding(I measures, bool rotated);
80
81    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures);
82    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, Dictionary<int, bool> rotationArray);
83    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints);
84    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray);
85
86    public void PackItem(int itemID, I measures, D position) {
87      ItemMeasures[itemID] = measures;
88      ItemPositions[itemID] = position;
89      ExtremePoints.Remove(position);
90      foreach (int id in ItemMeasures.Select(x => x.Key))
91        GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
92     
93       
94      //GenerateNewExtremePointsForNewItem(measures, position);
95      //OccupiedPoints.OccupyPoints(measures, position, itemID);
96
97      AddNewItemToOccupationLayers(itemID, measures, position);
98    }
99
100    public double PackingDensity {
101      get {
102        double result = 0;
103        foreach (var entry in ItemMeasures)
104          result += entry.Value.MultipliedMeasures;
105        result /= BinMeasures.MultipliedMeasures;
106        return result;
107      }
108    }
109
110
111    public int PointOccupation(D position) {
112      //foreach (var ipEntry in ItemPositions) {
113      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
114      //    return ipEntry.Key;
115      //}
116      foreach (var id in GetLayerItemIDs(position)) {
117        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
118          return id;
119      }
120      return -1;
121    }
122
123    public bool IsPointOccupied(D position) {
124      //foreach (var ipEntry in ItemPositions) {
125      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
126      //    return true;
127      //}     
128      foreach (var id in GetLayerItemIDs(position)) {
129        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
130          return true;
131      }
132      return false;
133    }
134    public bool IsPositionFeasible(I measures, D position) {
135      //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
136      if (!BinMeasures.Encloses(position, measures))
137        return false;
138
139      //foreach (var ipEntry in ItemPositions) {
140      //  if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, measures))
141      //    return false;
142      //}
143      foreach (var id in GetLayerItemIDs(measures, position)) {
144        if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
145          return false;
146      }
147
148      return true;
149    }
150    public abstract int ShortestPossibleSideFromPoint (D position);
151    public abstract bool IsStaticStable (I measures, D position);
152
153
154    protected abstract void InitializeOccupationLayers();
155    protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
156    protected abstract List<int> GetLayerItemIDs(D position);
157    protected abstract List<int> GetLayerItemIDs(I measures, D position);
158  }   
159}
Note: See TracBrowser for help on using the repository browser.