Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: unified namespaces

File size: 6.3 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]
33  public abstract class BinPacking<D,B,I>  : Item
34    where D : class, IPackingDimensions
[14045]35    where B : PackingShape<D>
[9596]36    where I : PackingShape<D>, IPackingItem {
37    #region Properties
38    [Storable]
39    public ObservableDictionary<int, D> ItemPositions { get; private set; }
40
41    [Storable]
[13578]42    public ObservableDictionary<int, I> ItemMeasures { get; private set; } // TODO: renamed to item
[9596]43
44    [Storable]
45    public B BinMeasures { get; private set; }
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
[13578]55    protected BinPacking(B binMeasures) : base() {   
[9596]56      ItemPositions = new ObservableDictionary<int, D>();
57      ItemMeasures = new ObservableDictionary<int, I>();
58      BinMeasures = (B)binMeasures.Clone();
[9599]59      OccupationLayers = new Dictionary<int, List<int>>();
[13578]60      InitializeOccupationLayers(); // TODO
[9596]61    }
62
[9599]63
[9596]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);
[9599]71        this.OccupationLayers = new Dictionary<int, List<int>>(original.OccupationLayers);
[9596]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 List<int> sequence, ItemList<I> itemMeasures);
80    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, Dictionary<int, bool> rotationArray);
81    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints);
82    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<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);
[9599]88      foreach (int id in ItemMeasures.Select(x => x.Key))
89        GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
90     
91       
92      //GenerateNewExtremePointsForNewItem(measures, position);
[9596]93      //OccupiedPoints.OccupyPoints(measures, position, itemID);
[9599]94
95      AddNewItemToOccupationLayers(itemID, measures, position);
[9596]96    }
97
98    public double PackingDensity {
99      get {
100        double result = 0;
101        foreach (var entry in ItemMeasures)
[13605]102          result += entry.Value.Volume;
103        result /= BinMeasures.Volume;
[9596]104        return result;
105      }
106    }
107
[9598]108
109    public int PointOccupation(D position) {
[9599]110      //foreach (var ipEntry in ItemPositions) {
111      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
112      //    return ipEntry.Key;
113      //}
114      foreach (var id in GetLayerItemIDs(position)) {
115        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
116          return id;
[9598]117      }
118      return -1;
119    }
[9599]120
[9596]121    public bool IsPointOccupied(D position) {
[9599]122      //foreach (var ipEntry in ItemPositions) {
123      //  if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
124      //    return true;
125      //}     
126      foreach (var id in GetLayerItemIDs(position)) {
127        if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
[9596]128          return true;
129      }
130      return false;
131    }
[9599]132    public bool IsPositionFeasible(I measures, D position) {
[9598]133      //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]134      if (!BinMeasures.Encloses(position, measures))
[9596]135        return false;
136
[9599]137      //foreach (var ipEntry in ItemPositions) {
138      //  if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, measures))
139      //    return false;
140      //}
141      foreach (var id in GetLayerItemIDs(measures, position)) {
142        if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
[9596]143          return false;
144      }
145
146      return true;
147    }
148    public abstract int ShortestPossibleSideFromPoint (D position);
[9599]149    public abstract bool IsStaticStable (I measures, D position);
150
151
152    protected abstract void InitializeOccupationLayers();
153    protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
154    protected abstract List<int> GetLayerItemIDs(D position);
155    protected abstract List<int> GetLayerItemIDs(I measures, D position);
[9596]156  }   
157}
Note: See TracBrowser for help on using the repository browser.