Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/BinPacking2D.cs @ 14055

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

#1966: simplified parsers

File size: 9.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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
[14046]28using HeuristicLab.Encodings.PackingEncoding;
[9596]29
[14046]30namespace HeuristicLab.Problems.BinPacking2D {
[9596]31  [Item("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
32  [StorableClass]
[14049]33  public class BinPacking2D : BinPacking<PackingPosition, PackingShape, PackingItem> {
[9596]34
[14049]35    public BinPacking2D(PackingShape binMeasures)
[9599]36      : base(binMeasures) {
[14048]37      ExtremePoints = new SortedSet<PackingPosition>(new EPComparer2D());
[9599]38      ExtremePoints.Add(binMeasures.Origin);
[9596]39    }
40    [StorableConstructor]
41    protected BinPacking2D(bool deserializing) : base(deserializing) { }
42    protected BinPacking2D(BinPacking2D original, Cloner cloner)
43      : base(original, cloner) {
[14048]44      this.ExtremePoints = new SortedSet<PackingPosition>(original.ExtremePoints, new EPComparer2D());
[9596]45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new BinPacking2D(this, cloner);
48    }
[13612]49
[14049]50    protected override void GenerateNewExtremePointsForNewItem(PackingItem newItem, PackingPosition position) {
[9596]51
52      int newWidth = position.Rotated ? newItem.Height : newItem.Width;
53      int newHeight = position.Rotated ? newItem.Width : newItem.Height;
54
55      //Find ExtremePoints beginning from sourcepointX
[14048]56      var sourcePointX = new PackingPosition(0, position.X + newWidth, position.Y);
[9596]57      if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height) {
58        //Traversing down the y-axis       
[14048]59        var newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
[14038]60        while (sourcePointX.Y > 0 && !IsPointOccupied(newPoint)) {
61          sourcePointX = newPoint;
[14048]62          newPoint = new PackingPosition(0, sourcePointX.X, sourcePointX.Y - 1);
[9596]63        }
[14048]64        ExtremePoints.Add(new PackingPosition(0, sourcePointX.X, sourcePointX.Y));
[9596]65      }
66
67      //Find ExtremePoints beginning from sourcepointY
[14048]68      var sourcePointY = new PackingPosition(0, position.X, position.Y + newItem.Height);
[9596]69      if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height) {
70        //Traversing down the x-axis 
[14048]71        var newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
[14038]72        while (sourcePointY.X > 0 && !IsPointOccupied(newPoint)) {
73          sourcePointY = newPoint;
[14048]74          newPoint = new PackingPosition(0, sourcePointY.X - 1, sourcePointY.Y);
[9596]75        }
[14048]76        ExtremePoints.Add(new PackingPosition(0, sourcePointY.X, sourcePointY.Y));
[9596]77      }
78
[9599]79      //ExtremePoints.RemoveWhere(ep => IsPointOccupied(ep));
80
81      //ExtremePoints = new HashSet<TwoDimensionalPacking>(ExtremePoints.
82      //  OrderBy(ep => ep.X).
83      //  ThenBy(ep => ep.Y).
84      //  ThenBy(ep => ShortestPossibleSideFromPoint(ep)));
[9596]85    }
86
[14055]87    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
88      PackingItem rotatedItem = new PackingItem(
89        rotated ? item.Height : item.Width,
90        rotated ? item.Width : item.Height,
91        item.TargetBin);
[9596]92
93      int epIndex = 0;
[14055]94      while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
[9596]95
96      if (epIndex < ExtremePoints.Count) {
[14038]97        var currentPoint = ExtremePoints.ElementAt(epIndex);
98
[14048]99        var result = new PackingPosition(currentPoint.AssignedBin, currentPoint.X, currentPoint.Y, rotated);
[9596]100        return result;
101      }
102      return null;
103    }
[14055]104    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
[14048]105      PackingPosition currentPosition = new PackingPosition(0,
[14055]106        BinMeasures.Width - (rotated ? item.Height : item.Width),
107        BinMeasures.Height - (rotated ? item.Width : item.Height), rotated);
[9596]108      //Slide the item as far as possible to the left
[14055]109      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
110        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
[9596]111        //Slide the item as far as possible to the bottom
[14055]112        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
[14048]113          currentPosition = PackingPosition.MoveDown(currentPosition);
[9596]114        }
[14055]115        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
[14048]116          currentPosition = PackingPosition.MoveLeft(currentPosition);
[9596]117      }
118
[14055]119      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
[9596]120    }
[13612]121
[14055]122    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items) {
[9596]123      var temp = new List<int>(sequence);
124      for (int i = 0; i < temp.Count; i++) {
[14055]125        var item = items[temp[i]];
[9596]126        var position = FindPositionBySliding(item, false);
127        if (position != null) {
128          PackItem(temp[i], item, position);
129          sequence.Remove(temp[i]);
130        }
131      }
132    }
[14055]133    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, Dictionary<int, bool> rotationArray) {
[9596]134      var temp = new List<int>(sequence);
135      for (int i = 0; i < temp.Count; i++) {
[14055]136        var item = items[temp[i]];
[9596]137        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
138        if (position != null) {
139          PackItem(temp[i], item, position);
140          sequence.Remove(temp[i]);
141        }
142      }
143    }
[14055]144    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints) {
[9596]145      var temp = new List<int>(sequence);
146      foreach (int itemID in temp) {
[14055]147        var item = items[itemID];
[9596]148        var positionFound = FindExtremePointForItem(item, false, false);
149        if (positionFound != null) {
150          PackItem(itemID, item, positionFound);
151          sequence.Remove(itemID);
152        }
153      }
154    }
[14055]155    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
[9596]156      var temp = new List<int>(sequence);
157      foreach (int itemID in temp) {
[14055]158        var item = items[itemID];
[9596]159        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
160        if (positionFound != null) {
161          PackItem(itemID, item, positionFound);
162          sequence.Remove(itemID);
163        }
164      }
165    }
166
[14048]167    public override int ShortestPossibleSideFromPoint(PackingPosition position) {
[9596]168      int shortestSide = int.MaxValue;
169      int width = BinMeasures.Width;
170      int height = BinMeasures.Height;
171
172      if (position.X >= width || position.Y >= height)
173        return shortestSide;
174
[14048]175      PackingPosition current = new PackingPosition(0, position.X, position.Y);
176      while (current.X < width && IsPointOccupied(current)) { current = PackingPosition.MoveRight(current); }
[9596]177      if (current.X - position.X < shortestSide)
178        shortestSide = current.X - position.X;
179
180
[14048]181      current = new PackingPosition(0, position.X, position.Y);
182      while (current.Y < height && IsPointOccupied(current)) { current = PackingPosition.MoveUp(current); }
[9596]183      if (current.Y - position.Y < shortestSide)
184        shortestSide = current.Y - position.Y;
185
186      return shortestSide;
187    }
[14049]188    public override bool IsStaticStable(PackingItem item, PackingPosition position) {
[9596]189      throw new NotImplementedException();
190    }
[9599]191
192
193    protected override void InitializeOccupationLayers() {
[13612]194      for (int i = 0; i * 10 <= BinMeasures.Width; i += 1) {
[9599]195        OccupationLayers[i] = new List<int>();
196      }
197    }
[14049]198    protected override void AddNewItemToOccupationLayers(int itemID, PackingItem measures, PackingPosition position) {
[9599]199      int x1 = position.X / 10;
200      int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
201
202      for (int i = x1; i <= x2; i++)
203        OccupationLayers[i].Add(itemID);
204    }
[14048]205    protected override List<int> GetLayerItemIDs(PackingPosition position) {
[9599]206      return OccupationLayers[position.X / 10];
207    }
[14049]208    protected override List<int> GetLayerItemIDs(PackingItem measures, PackingPosition position) {
[13612]209      List<int> result = new List<int>();
[9599]210      int x1 = position.X / 10;
211      int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
212
213      for (int i = x1; i <= x2; i++)
214        result.AddRange(OccupationLayers[i]);
215
216      return result;
217    }
[9596]218  }
[14048]219  public class EPComparer2D : IComparer<PackingPosition> {
220    public int Compare(PackingPosition a, PackingPosition b) {
[9599]221      int result = a.X.CompareTo(b.X);
222      if (result == 0)
223        result = a.Y.CompareTo(b.Y);
224
225      return result;
226    }
227  }
[9596]228}
Note: See TracBrowser for help on using the repository browser.