Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinPacking/3.3/2D/BinPacking2D.cs @ 15448

Last change on this file since 15448 was 15241, checked in by abeham, 7 years ago

#2762:

  • Moved initialization / cloning of extreme points list to base class
  • Removed residual space from base class and put it into derived class for 3d bin packing
  • Updated sample
File size: 9.0 KB
RevLine 
[14162]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;
28
29namespace HeuristicLab.Problems.BinPacking2D {
30  [Item("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
31  [StorableClass]
32  public class BinPacking2D : BinPacking.BinPacking<PackingPosition, PackingShape, PackingItem> {
33
34    public BinPacking2D(PackingShape binShape)
35      : base(binShape) {
36      ExtremePoints.Add(binShape.Origin);
37      InitializeOccupationLayers();
38    }
39
40    [StorableConstructor]
41    protected BinPacking2D(bool deserializing) : base(deserializing) { }
[15241]42    protected BinPacking2D(BinPacking2D original, Cloner cloner) : base(original, cloner) { }
[14162]43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new BinPacking2D(this, cloner);
45    }
46
47    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
48
49      int newWidth = position.Rotated ? newItem.Height : newItem.Width;
50      int newHeight = position.Rotated ? newItem.Width : newItem.Height;
51
52      //Find ExtremePoints beginning from sourcepointX
53      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y);
54      if (sourcePointX.X < BinShape.Width && sourcePointX.Y < BinShape.Height) {
55        //Traversing down the y-axis       
56        var newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
57        while (sourcePointX.Y > 0 && !IsPointOccupied(newPoint)) {
58          sourcePointX = newPoint;
59          newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
60        }
61        ExtremePoints.Add(new PackingPosition(0, sourcePointX.X, sourcePointX.Y));
62      }
63
64      //Find ExtremePoints beginning from sourcepointY
65      var sourcePointY = new PackingPosition(0, position.X, position.Y + newHeight);
66      if (sourcePointY.X < BinShape.Width && sourcePointY.Y < BinShape.Height) {
67        //Traversing down the x-axis 
68        var newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
69        while (sourcePointY.X > 0 && !IsPointOccupied(newPoint)) {
70          sourcePointY = newPoint;
71          newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
72        }
73        ExtremePoints.Add(new PackingPosition(0, sourcePointY.X, sourcePointY.Y));
74      }
75    }
76
77    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
78      PackingItem rotatedItem = new PackingItem(
79        rotated ? item.Height : item.Width,
80        rotated ? item.Width : item.Height,
[14916]81        item.TargetBin) {
82        Material = item.Material,
83        Weight = item.Weight
84      };
[14162]85
[15230]86      var ep = ExtremePoints.Where(x => IsPositionFeasible(rotatedItem, x, stackingConstraints)).FirstOrDefault();
87      if (ep != null) {
88        var result = new PackingPosition(ep.AssignedBin, ep.X, ep.Y, rotated);
[14162]89        return result;
90      }
91      return null;
92    }
[15230]93    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated, bool stackingConstraints) {
[14162]94      PackingPosition currentPosition = new PackingPosition(0,
95        BinShape.Width - (rotated ? item.Height : item.Width),
96        BinShape.Height - (rotated ? item.Width : item.Height), rotated);
97      //Slide the item as far as possible to the left
[15230]98      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints)
99        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) {
[14162]100        //Slide the item as far as possible to the bottom
[15230]101        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition), stackingConstraints)) {
[14162]102          currentPosition = PackingPosition.MoveDown(currentPosition);
103        }
[15230]104        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition), stackingConstraints))
[14162]105          currentPosition = PackingPosition.MoveLeft(currentPosition);
106      }
107
[15230]108      return IsPositionFeasible(item, currentPosition, stackingConstraints) ? currentPosition : null;
[14162]109    }
110
[15230]111    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
[14162]112      var temp = new List<int>(sequence);
113      for (int i = 0; i < temp.Count; i++) {
114        var item = items[temp[i]];
[15230]115        var position = FindPositionBySliding(item, false, stackingConstraints);
[14162]116        if (position != null) {
117          PackItem(temp[i], item, position);
118          sequence.Remove(temp[i]);
119        }
120      }
121    }
[15230]122    public override void SlidingBasedPacking(ref IList<int> sequence, IList<PackingItem> items, Dictionary<int, bool> rotationArray, bool stackingConstraints) {
[14162]123      var temp = new List<int>(sequence);
124      for (int i = 0; i < temp.Count; i++) {
125        var item = items[temp[i]];
[15230]126        var position = FindPositionBySliding(item, rotationArray[temp[i]], stackingConstraints);
[14162]127        if (position != null) {
128          PackItem(temp[i], item, position);
129          sequence.Remove(temp[i]);
130        }
131      }
132    }
133    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints) {
134      var temp = new List<int>(sequence);
135      foreach (int itemID in temp) {
136        var item = items[itemID];
137        var positionFound = FindExtremePointForItem(item, false, false);
138        if (positionFound != null) {
139          PackItem(itemID, item, positionFound);
140          sequence.Remove(itemID);
141        }
142      }
143    }
[15230]144   
[14162]145    public override void ExtremePointBasedPacking(ref IList<int> sequence, IList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
146      var temp = new List<int>(sequence);
147      foreach (int itemID in temp) {
148        var item = items[itemID];
149        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
150        if (positionFound != null) {
151          PackItem(itemID, item, positionFound);
152          sequence.Remove(itemID);
153        }
154      }
155    }
156
157    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
158      int shortestSide = int.MaxValue;
159      int width = BinShape.Width;
160      int height = BinShape.Height;
161
162      if (position.X >= width || position.Y >= height)
163        return shortestSide;
164
165      PackingPosition current = new PackingPosition(0, position.X, position.Y);
166      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
167      if (current.X - position.X < shortestSide)
168        shortestSide = current.X - position.X;
169
170
171      current = new PackingPosition(0, position.X, position.Y);
172      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
173      if (current.Y - position.Y < shortestSide)
174        shortestSide = current.Y - position.Y;
175
176      return shortestSide;
177    }
178    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
179      throw new NotSupportedException();
180    }
181    protected override void InitializeOccupationLayers() {
182      for (int i = 0; i * 10 <= BinShape.Width; i += 1) {
183        OccupationLayers[i] = new List<int>();
184      }
185    }
186
187    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem item, PackingPosition position) {
188      int x1 = position.X / 10;
189      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
190
191      for (int i = x1; i <= x2; i++)
192        OccupationLayers[i].Add(itemID);
193    }
194    protected override List<int> GetLayerItemIDs(PackingPosition position) {
195      return OccupationLayers[position.X / 10];
196    }
197    protected override List<int> GetLayerItemIDs(PackingItem item, PackingPosition position) {
198      List<int> result = new List<int>();
199      int x1 = position.X / 10;
200      int x2 = (position.X + (position.Rotated ? item.Height : item.Width)) / 10;
201
202      for (int i = x1; i <= x2; i++)
203        result.AddRange(OccupationLayers[i]);
204
205      return result;
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.