#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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 System.Text; using HeuristicLab.Problems.BinPacking.Interfaces; using HeuristicLab.Problems.BinPacking.PackingItem; using HeuristicLab.Problems.BinPacking.Shapes; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; using HeuristicLab.Common; using HeuristicLab.Parameters; using HeuristicLab.Data; using HeuristicLab.Collections; using HeuristicLab.Problems.BinPacking.Dimensions; using HeuristicLab.Problems.BinPacking.PackingBin; 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; } [Storable] public B BinMeasures { get; private set; } [Storable] public HashSet ExtremePoints { get; protected set; } //[Storable] //public OccupiedPoints OccupiedPoints { get; protected set; } #endregion Properties public BinPacking(B binMeasures) : base() { ItemPositions = new ObservableDictionary(); ItemMeasures = new ObservableDictionary(); BinMeasures = (B)binMeasures.Clone(); ExtremePoints = new HashSet(); ExtremePoints.Add(binMeasures.Origin); } [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); } 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); GenerateNewExtremePointsForNewItem(measures, position); //OccupiedPoints.OccupyPoints(measures, position, itemID); } public double PackingDensity { get { double result = 0; foreach (var entry in ItemMeasures) result += entry.Value.MultipliedMeasures; result /= BinMeasures.MultipliedMeasures; return result; } } public int PointOccupation(D position) { foreach (var ipEntry in ItemPositions) { if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position)) return ipEntry.Key; } return -1; } public bool IsPointOccupied(D position) { foreach (var ipEntry in ItemPositions) { if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position)) return true; } return false; } public bool IsPositionFeasible(I currentItem, 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, currentItem)) return false; foreach (var ipEntry in ItemPositions) { if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, currentItem)) return false; } return true; } public abstract int ShortestPossibleSideFromPoint (D position); public abstract bool IsStaticStable (I item, D position); } }