#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.Collections.Generic;
using System.Linq;
using HeuristicLab.Problems.BinPacking.Interfaces;
using HeuristicLab.Problems.BinPacking.Shapes;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Core;
using HeuristicLab.Common;
using HeuristicLab.Collections;
namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
[Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
[StorableClass]
public abstract class BinPacking : Item
where D : class, IPackingDimensions
where B : PackingShape, IPackingBin
where I : PackingShape, IPackingItem {
#region Properties
[Storable]
public ObservableDictionary ItemPositions { get; private set; }
[Storable]
public ObservableDictionary ItemMeasures { get; private set; } // TODO: renamed to item
[Storable]
public B BinMeasures { get; private set; }
[Storable]
public SortedSet ExtremePoints { get; protected set; }
[Storable]
protected Dictionary> OccupationLayers { get; set; }
#endregion Properties
protected BinPacking(B binMeasures) : base() {
ItemPositions = new ObservableDictionary();
ItemMeasures = new ObservableDictionary();
BinMeasures = (B)binMeasures.Clone();
OccupationLayers = new Dictionary>();
InitializeOccupationLayers(); // TODO
}
[StorableConstructor]
protected BinPacking(bool deserializing) : base(deserializing) { }
protected BinPacking(BinPacking original, Cloner cloner)
: base(original, cloner) {
this.ItemPositions = new ObservableDictionary(original.ItemPositions);
this.ItemMeasures = new ObservableDictionary(original.ItemMeasures);
this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
this.OccupationLayers = new Dictionary>(original.OccupationLayers);
}
protected abstract void GenerateNewExtremePointsForNewItem(I measures, D position);
public abstract D FindExtremePointForItem(I measures, bool rotated, bool stackingConstraints);
public abstract D FindPositionBySliding(I measures, bool rotated);
public abstract void SlidingBasedPacking(ref List sequence, ItemList itemMeasures);
public abstract void SlidingBasedPacking(ref List sequence, ItemList itemMeasures, Dictionary rotationArray);
public abstract void ExtremePointBasedPacking(ref List sequence, ItemList itemMeasures, bool stackingConstraints);
public abstract void ExtremePointBasedPacking(ref List sequence, ItemList itemMeasures, bool stackingConstraints, Dictionary rotationArray);
public void PackItem(int itemID, I measures, D position) {
ItemMeasures[itemID] = measures;
ItemPositions[itemID] = position;
ExtremePoints.Remove(position);
foreach (int id in ItemMeasures.Select(x => x.Key))
GenerateNewExtremePointsForNewItem(ItemMeasures[id], ItemPositions[id]);
//GenerateNewExtremePointsForNewItem(measures, position);
//OccupiedPoints.OccupyPoints(measures, position, itemID);
AddNewItemToOccupationLayers(itemID, measures, position);
}
public double PackingDensity {
get {
double result = 0;
foreach (var entry in ItemMeasures)
result += entry.Value.Volume;
result /= BinMeasures.Volume;
return result;
}
}
public int PointOccupation(D position) {
//foreach (var ipEntry in ItemPositions) {
// if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
// return ipEntry.Key;
//}
foreach (var id in GetLayerItemIDs(position)) {
if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
return id;
}
return -1;
}
public bool IsPointOccupied(D position) {
//foreach (var ipEntry in ItemPositions) {
// if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
// return true;
//}
foreach (var id in GetLayerItemIDs(position)) {
if (ItemMeasures[id].EnclosesPoint(ItemPositions[id], position))
return true;
}
return false;
}
public bool IsPositionFeasible(I measures, D position) {
//In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the point is supported by something; 3. the item does not collide with another already packed item
if (!BinMeasures.Encloses(position, measures))
return false;
//foreach (var ipEntry in ItemPositions) {
// if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, measures))
// return false;
//}
foreach (var id in GetLayerItemIDs(measures, position)) {
if (ItemMeasures[id].Overlaps(ItemPositions[id], position, measures))
return false;
}
return true;
}
public abstract int ShortestPossibleSideFromPoint (D position);
public abstract bool IsStaticStable (I measures, D position);
protected abstract void InitializeOccupationLayers();
protected abstract void AddNewItemToOccupationLayers(int itemID, I measures, D position);
protected abstract List GetLayerItemIDs(D position);
protected abstract List GetLayerItemIDs(I measures, D position);
}
}