Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: new implementation for 2d bin packing problem with permutation encoding

File size: 5.6 KB
RevLine 
[9596]1#region License Information
2/* HeuristicLab
[13032]3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9596]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;
[14046]28using HeuristicLab.Problems.BinPacking;
[9596]29
[14046]30namespace HeuristicLab.Encodings.PackingEncoding {
[9596]31  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
32  [StorableClass]
[14146]33  public abstract class BinPacking<D, B, I> : Item
[14048]34    where D : class, IPackingPosition
[14045]35    where B : PackingShape<D>
[14146]36    where I : PackingShape<D> {
[9596]37    #region Properties
38    [Storable]
[14146]39    public ObservableDictionary<int, D> ItemPositions { get; private set; }
[9596]40
41    [Storable]
[14128]42    public ObservableDictionary<int, I> ItemMeasures { get; private set; } // TODO: rename to items
[9596]43
44    [Storable]
[14146]45    public B BinMeasures { get; private set; }
[9596]46
47    [Storable]
[9599]48    public SortedSet<D> ExtremePoints { get; protected set; }
[9596]49
[9599]50    [Storable]
51    protected Dictionary<int, List<int>> OccupationLayers { get; set; }
52
[9596]53    #endregion Properties
54
[14146]55    protected BinPacking(B binMeasures)
56      : base() {
[9596]57      ItemPositions = new ObservableDictionary<int, D>();
58      ItemMeasures = new ObservableDictionary<int, I>();
59      BinMeasures = (B)binMeasures.Clone();
[9599]60      OccupationLayers = new Dictionary<int, List<int>>();
[13578]61      InitializeOccupationLayers(); // TODO
[9596]62    }
63
[9599]64
[9596]65    [StorableConstructor]
66    protected BinPacking(bool deserializing) : base(deserializing) { }
[14146]67    protected BinPacking(BinPacking<D, B, I> original, Cloner cloner)
[9596]68      : base(original, cloner) {
69      this.ItemPositions = new ObservableDictionary<int, D>(original.ItemPositions);
70      this.ItemMeasures = new ObservableDictionary<int, I>(original.ItemMeasures);
[14146]71      this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
72      this.OccupationLayers = new Dictionary<int, List<int>>(original.OccupationLayers);
[9596]73    }
[14146]74
[9596]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
[14146]80    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<I> itemMeasures);
81    public abstract void SlidingBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, Dictionary<int, bool> rotationArray);
82    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, bool stackingConstraints);
83    public abstract void ExtremePointBasedPacking(ref IList<int> sequence, IList<I> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray);
[9596]84
85    public void PackItem(int itemID, I measures, D position) {
86      ItemMeasures[itemID] = measures;
87      ItemPositions[itemID] = position;
88      ExtremePoints.Remove(position);
[9599]89      foreach (int id in ItemMeasures.Select(x => x.Key))
90        GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
91
92      AddNewItemToOccupationLayers(itemID, measures, position);
[9596]93    }
94
[14146]95    public double PackingDensity {
[9596]96      get {
97        double result = 0;
98        foreach (var entry in ItemMeasures)
[13605]99          result += entry.Value.Volume;
100        result /= BinMeasures.Volume;
[9596]101        return result;
102      }
103    }
104
[9598]105
106    public int PointOccupation(D position) {
[9599]107      foreach (var id in GetLayerItemIDs(position)) {
108        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
109          return id;
[9598]110      }
111      return -1;
112    }
[9599]113
[9596]114    public bool IsPointOccupied(D position) {
[9599]115      foreach (var id in GetLayerItemIDs(position)) {
116        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
[9596]117          return true;
118      }
119      return false;
120    }
[9599]121    public bool IsPositionFeasible(I measures, D position) {
[9598]122      //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
[9599]123      if (!BinMeasures.Encloses(position, measures))
[9596]124        return false;
125
[9599]126      foreach (var id in GetLayerItemIDs(measures, position)) {
127        if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
[9596]128          return false;
129      }
130
131      return true;
132    }
[14146]133    public abstract int ShortestPossibleSideFromPoint(D position);
134    public abstract bool IsStaticStable(I measures, D position);
[9599]135
136
137    protected abstract void InitializeOccupationLayers();
138    protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
139    protected abstract List<int> GetLayerItemIDs(D position);
140    protected abstract List<int> GetLayerItemIDs(I measures, D position);
[14146]141  }
[9596]142}
Note: See TracBrowser for help on using the repository browser.