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