Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9599 was 9599, checked in by jhelm, 11 years ago

#1966: Bugfixing; Refactoring; Performancetuning;

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