Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/BinPacking.cs @ 15183

Last change on this file since 15183 was 14976, checked in by dsouravl, 7 years ago

#2762: worked on best fit heuristics

File size: 6.1 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    public ObservableDictionary<int, TPos> Positions { get; private set; }
40
41    [Storable]
42    public ObservableDictionary<int, TItem> Items { get; private set; }
43
44    [Storable]
45    public TBin BinShape { get; private set; }
46
47    [Storable]
48    public SortedSet<TPos> ExtremePoints { get; protected set; }
49
50    [Storable]
51    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
52
53    [Storable]
54    public Dictionary<TPos, Tuple<int,int,int>> ResidualSpace { get; protected 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      OccupationLayers = new Dictionary<int, List<int>>();
68    }
69
70    [StorableConstructor]
71    protected BinPacking(bool deserializing) : base(deserializing) { }
72    protected BinPacking(BinPacking<TPos, TBin, TItem> original, Cloner cloner)
73      : base(original, cloner) {
74      this.Positions = new ObservableDictionary<int, TPos>();
75      foreach (var kvp in original.Positions) {
76        Positions.Add(kvp.Key, cloner.Clone(kvp.Value));
77      }
78      this.Items = new ObservableDictionary<int, TItem>();
79      foreach (var kvp in original.Items) {
80        Items.Add(kvp.Key, cloner.Clone(kvp.Value));
81      }
82      this.BinShape = (TBin)original.BinShape.Clone(cloner);
83      this.OccupationLayers = new Dictionary<int, List<int>>();
84      foreach (var kvp in original.OccupationLayers) {
85        OccupationLayers.Add(kvp.Key, new List<int>(kvp.Value));
86      }
87    }
88
89    protected abstract void GenerateNewExtremePointsForNewItem(TItem item, TPos position);
90
91    public abstract TPos FindExtremePointForItem(TItem item, bool rotated, bool stackingConstraints);
92    public abstract TPos FindPositionBySliding(TItem item, bool rotated);
93
94    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items);
95    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<TItem> items, Dictionary<int, bool> rotationArray);
96    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints);
97    public abstract bool ExtremePointBasedPacking(int ID, IList<TItem> items, bool stackingConstraints);
98    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<TItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray);
99
100    public void PackItem(int itemID, TItem item, TPos position) {
101      Items[itemID] = item;
102      Positions[itemID] = position;
103      ExtremePoints.Remove(position);
104      if (ResidualSpace != null) ResidualSpace.Remove(position);
105      foreach (int id in Items.Select(x => x.Key))
106        GenerateNewExtremePointsForNewItem(Items[id], Positions[id]);
107     
108      AddNewItemToOccupationLayers(itemID, item, position);
109    }
110
111    public double PackingDensity {
112      get {
113        double result = 0;
114        foreach (var entry in Items)
115          result += entry.Value.Volume;
116        result /= BinShape.Volume;
117        return result;
118      }
119    }
120
121
122    public int PointOccupation(TPos position) {
123      foreach (var id in GetLayerItemIDs(position)) {
124        if (Items[id].EnclosesPoint(Positions[id], position))
125          return id;
126      }
127      return -1;
128    }
129
130    public bool IsPointOccupied(TPos position) {
131      foreach (var id in GetLayerItemIDs(position)) {
132        if (Items[id].EnclosesPoint(Positions[id], position))
133          return true;
134      }
135      return false;
136    }
137    public bool IsPositionFeasible(TItem item, TPos position) {
138      //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
139      if (!BinShape.Encloses(position, item))
140        return false;
141
142      foreach (var id in GetLayerItemIDs(item, position)) {
143        if (Items[id].Overlaps(Positions[id], position, item))
144          return false;
145      }
146
147      return true;
148    }
149    public abstract int ShortestPossibleSideFromPoint(TPos position);
150    public abstract bool IsStaticStable(TItem measures, TPos position);
151
152    protected abstract void InitializeOccupationLayers();
153    protected abstract void AddNewItemToOccupationLayers(int itemID, TItem item, TPos position);
154    protected abstract List<int> GetLayerItemIDs(TPos position);
155    protected abstract List<int> GetLayerItemIDs(TItem item, TPos position);
156  }
157}
Note: See TracBrowser for help on using the repository browser.