#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Problems.BinPacking.PackingItem;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Core;
using HeuristicLab.Common;
using HeuristicLab.Problems.BinPacking.Dimensions;
using HeuristicLab.Problems.BinPacking.PackingBin;
namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
[Item("BinPacking2D", "Represents a single-bin packing for a 2D bin-packing problem.")]
[StorableClass]
public class BinPacking2D : BinPacking {
public BinPacking2D(RectangularPackingBin binMeasures)
: base(binMeasures) {
ExtremePoints = new SortedSet(new EPComparer2D());
ExtremePoints.Add(binMeasures.Origin);
}
[StorableConstructor]
protected BinPacking2D(bool deserializing) : base(deserializing) { }
protected BinPacking2D(BinPacking2D original, Cloner cloner)
: base(original, cloner) {
this.ExtremePoints = new SortedSet(original.ExtremePoints, new EPComparer2D());
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BinPacking2D(this, cloner);
}
protected override void GenerateNewExtremePointsForNewItem(RectangularPackingItem newItem, TwoDimensionalPacking position) {
int newWidth = position.Rotated ? newItem.Height : newItem.Width;
int newHeight = position.Rotated ? newItem.Width : newItem.Height;
//Find ExtremePoints beginning from sourcepointX
var sourcePointX = new TwoDimensionalPacking(0, position.X + newWidth, position.Y);
if (sourcePointX.X < BinMeasures.Width && sourcePointX.Y < BinMeasures.Height) {
//Traversing down the y-axis
while (sourcePointX.Y > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y - 1))) {
sourcePointX.Y--;
}
ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointX.X, sourcePointX.Y));
}
//Find ExtremePoints beginning from sourcepointY
var sourcePointY = new TwoDimensionalPacking(0, position.X, position.Y + newItem.Height);
if (sourcePointY.X < BinMeasures.Width && sourcePointY.Y < BinMeasures.Height) {
//Traversing down the x-axis
while (sourcePointY.X > 0 && !IsPointOccupied(new TwoDimensionalPacking(0, sourcePointY.X - 1, sourcePointY.Y))) {
sourcePointY.X--;
}
ExtremePoints.Add(new TwoDimensionalPacking(0, sourcePointY.X, sourcePointY.Y));
}
//ExtremePoints.RemoveWhere(ep => IsPointOccupied(ep));
//ExtremePoints = new HashSet(ExtremePoints.
// OrderBy(ep => ep.X).
// ThenBy(ep => ep.Y).
// ThenBy(ep => ShortestPossibleSideFromPoint(ep)));
}
public override TwoDimensionalPacking FindExtremePointForItem(RectangularPackingItem measures, bool rotated, bool stackingConstraints) {
RectangularPackingItem item = new RectangularPackingItem(
rotated ? measures.Height : measures.Width,
rotated ? measures.Width : measures.Height,
measures.TargetBin);
int epIndex = 0;
while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(item, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
if (epIndex < ExtremePoints.Count) {
var result = ExtremePoints.ElementAt(epIndex);
result.Rotated = rotated;
return result;
}
return null;
}
public override TwoDimensionalPacking FindPositionBySliding(RectangularPackingItem measures, bool rotated) {
TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(0,
BinMeasures.Width - (rotated ? measures.Height : measures.Width),
BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);
//Slide the item as far as possible to the left
while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition))
|| IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
//Slide the item as far as possible to the bottom
while (IsPositionFeasible(measures, TwoDimensionalPacking.MoveDown(currentPosition))) {
currentPosition = TwoDimensionalPacking.MoveDown(currentPosition);
}
if (IsPositionFeasible(measures, TwoDimensionalPacking.MoveLeft(currentPosition)))
currentPosition = TwoDimensionalPacking.MoveLeft(currentPosition);
}
return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
}
public override void SlidingBasedPacking(ref List sequence, ItemList itemMeasures) {
var temp = new List(sequence);
for (int i = 0; i < temp.Count; i++) {
var item = itemMeasures[temp[i]];
var position = FindPositionBySliding(item, false);
if (position != null) {
PackItem(temp[i], item, position);
sequence.Remove(temp[i]);
}
}
}
public override void SlidingBasedPacking(ref List sequence, ItemList itemMeasures, Dictionary rotationArray) {
var temp = new List(sequence);
for (int i = 0; i < temp.Count; i++) {
var item = itemMeasures[temp[i]];
var position = FindPositionBySliding(item, rotationArray[temp[i]]);
if (position != null) {
PackItem(temp[i], item, position);
sequence.Remove(temp[i]);
}
}
}
public override void ExtremePointBasedPacking(ref List sequence, ItemList itemMeasures, bool stackingConstraints) {
var temp = new List(sequence);
foreach (int itemID in temp) {
var item = itemMeasures[itemID];
var positionFound = FindExtremePointForItem(item, false, false);
if (positionFound != null) {
PackItem(itemID, item, positionFound);
sequence.Remove(itemID);
}
}
}
public override void ExtremePointBasedPacking(ref List sequence, ItemList itemMeasures, bool stackingConstraints, Dictionary rotationArray) {
var temp = new List(sequence);
foreach (int itemID in temp) {
var item = itemMeasures[itemID];
var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
if (positionFound != null) {
PackItem(itemID, item, positionFound);
sequence.Remove(itemID);
}
}
}
public override int ShortestPossibleSideFromPoint(TwoDimensionalPacking position) {
int shortestSide = int.MaxValue;
int width = BinMeasures.Width;
int height = BinMeasures.Height;
if (position.X >= width || position.Y >= height)
return shortestSide;
TwoDimensionalPacking current = new TwoDimensionalPacking(0, position.X, position.Y);
while (current.X < width && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveRight(current); }
if (current.X - position.X < shortestSide)
shortestSide = current.X - position.X;
current = new TwoDimensionalPacking(0, position.X, position.Y);
while (current.Y < height && IsPointOccupied(current)) { current = TwoDimensionalPacking.MoveUp(current); }
if (current.Y - position.Y < shortestSide)
shortestSide = current.Y - position.Y;
return shortestSide;
}
public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking position) {
throw new NotImplementedException();
}
protected override void InitializeOccupationLayers() {
for (int i = 0; i * 10 <= BinMeasures.Width; i += 1) {
OccupationLayers[i] = new List();
}
}
protected override void AddNewItemToOccupationLayers(int itemID, RectangularPackingItem measures, TwoDimensionalPacking position) {
int x1 = position.X / 10;
int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
for (int i = x1; i <= x2; i++)
OccupationLayers[i].Add(itemID);
}
protected override List GetLayerItemIDs(TwoDimensionalPacking position) {
return OccupationLayers[position.X / 10];
}
protected override List GetLayerItemIDs(RectangularPackingItem measures, TwoDimensionalPacking position) {
List result = new List();
int x1 = position.X / 10;
int x2 = (position.X + (position.Rotated ? measures.Height : measures.Width)) / 10;
for (int i = x1; i <= x2; i++)
result.AddRange(OccupationLayers[i]);
return result;
}
}
public class EPComparer2D : IComparer {
public int Compare(TwoDimensionalPacking a, TwoDimensionalPacking b) {
int result = a.X.CompareTo(b.X);
if (result == 0)
result = a.Y.CompareTo(b.Y);
return result;
}
}
}