Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: fixed compile errors and reverted some changes from the last commit which prepared for refactoring to use new Encoding framework

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