Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: More refactoring; Added more sophisticated structures for packing-plan and bin-packing representation; Transferred parts of the decoding-algorithms to these structures; Did some more refactoring and cleanup;

File size: 8.5 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("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
40  [StorableClass]
41  public class BinPacking2D : BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> {
42
43    public BinPacking2D(RectangularPackingBin binMeasures) : base(binMeasures) {
44      //OccupiedPoints = new OccupiedPoints2D(binMeasures);
45    }
46    [StorableConstructor]
47    protected BinPacking2D(bool deserializing) : base(deserializing) { }
48    protected BinPacking2D(BinPacking2D original, Cloner cloner)
49      : base(original, cloner) {
50    }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new BinPacking2D(this, cloner);
53    }
54                     
55    protected override void GenerateNewExtremePointsForNewItem(RectangularPackingItem newItem, TwoDimensionalPacking position) {
56
57      int newWidth = position.Rotated ? newItem.Height : newItem.Width;
58      int newHeight = position.Rotated ? newItem.Width : newItem.Height;
59
60      //Find ExtremePoints beginning from sourcepointX
61      var sourcePointX = new TwoDimensionalPacking(0, position.X + newWidth, position.Y);
62      if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height) {
63        //Traversing down the y-axis       
64        while (sourcePointX.Y > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y - 1))) {
65          sourcePointX.Y--;
66        }
67        ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y));
68      }
69
70
71
72
73      //Find ExtremePoints beginning from sourcepointY
74      var sourcePointY = new TwoDimensionalPacking(0, position.X, position.Y + newItem.Height);
75      if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height) {
76        //Traversing down the x-axis 
77        while (sourcePointY.X > 0 && !IsPointOccupied(new TwoDimensionalPacking (0,sourcePointY.X - 1, sourcePointY.Y))) {
78          sourcePointY.X--;
79        }
80        ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointY.X, sourcePointY.Y));
81      }
82
83      ExtremePoints = new HashSet<TwoDimensionalPacking>(ExtremePoints.
84        OrderBy(ep => ep.X).
85        ThenBy(ep => ep.Y).
86        ThenBy(ep => ShortestPossibleSideFromPoint(ep)));
87    }
88
89    public override TwoDimensionalPacking FindExtremePointForItem(RectangularPackingItem measures, bool rotated, bool stackingConstraints) {
90      RectangularPackingItem item = new RectangularPackingItem(
91        rotated ? measures.Height : measures.Width,
92        rotated ? measures.Width : measures.Height,
93        measures.TargetBin);
94
95      int epIndex = 0;
96      while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(item, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
97
98      if (epIndex < ExtremePoints.Count) {
99        var result = ExtremePoints.ElementAt(epIndex);
100        result.Rotated = rotated;
101        return result;
102      }
103      return null;
104    }
105    public override TwoDimensionalPacking FindPositionBySliding(RectangularPackingItem measures, bool rotated) {
106      TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(0,
107        BinMeasures.Width - (rotated ? measures.Height : measures.Width),
108        BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);
109      //Slide the item as far as possible to the left
110      while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition))
111        || IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
112        //Slide the item as far as possible to the bottom
113          while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
114            currentPosition = TwoDimensionalPacking.MoveDown(currentPosition);
115        }
116          if (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition)))
117          currentPosition = TwoDimensionalPacking.MoveLeft(currentPosition);
118      }
119
120      return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
121    }
122   
123    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures) {
124      var temp = new List<int>(sequence);
125      for (int i = 0; i < temp.Count; i++) {
126        var item = itemMeasures[temp[i]];
127        var position = FindPositionBySliding(item, false);
128        if (position != null) {
129          PackItem(temp[i], item, position);
130          sequence.Remove(temp[i]);
131        }
132      }
133    }
134    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, Dictionary<int, bool> rotationArray) {
135      var temp = new List<int>(sequence);
136      for (int i = 0; i < temp.Count; i++) {
137        var item = itemMeasures[temp[i]];
138        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
139        if (position != null) {
140          PackItem(temp[i], item, position);
141          sequence.Remove(temp[i]);
142        }
143      }
144    }
145    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints) {
146      var temp = new List<int>(sequence);
147      foreach (int itemID in temp) {
148        var item = itemMeasures[itemID];
149        var positionFound = FindExtremePointForItem(item, false, false);
150        if (positionFound != null) {
151          PackItem(itemID, item, positionFound);
152          sequence.Remove(itemID);
153        }
154      }
155    }
156    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<RectangularPackingItem> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
157      var temp = new List<int>(sequence);
158      foreach (int itemID in temp) {
159        var item = itemMeasures[itemID];
160        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
161        if (positionFound != null) {
162          PackItem(itemID, item, positionFound);
163          sequence.Remove(itemID);
164        }
165      }
166    }
167
168
169    public override int ShortestPossibleSideFromPoint(TwoDimensionalPacking position) {
170      int shortestSide = int.MaxValue;
171      int width = BinMeasures.Width;
172      int height = BinMeasures.Height;
173
174      if (position.X >= width || position.Y >= height)
175        return shortestSide;
176
177      TwoDimensionalPacking current = new TwoDimensionalPacking(0, position.X, position.Y);
178      while (current.X < width && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveRight(current); }
179      if (current.X - position.X < shortestSide)
180        shortestSide = current.X - position.X;
181
182
183      current = new TwoDimensionalPacking(0, position.X, position.Y);
184      while (current.Y < height && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveUp(current); }
185      if (current.Y - position.Y < shortestSide)
186        shortestSide = current.Y - position.Y;
187
188      return shortestSide;
189    }
190    public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking position) {
191      throw new NotImplementedException();
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.