#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.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Core;
using HeuristicLab.Problems.BinPacking.Dimensions;
using HeuristicLab.Problems.BinPacking.PackingBin;
using HeuristicLab.Problems.BinPacking.PackingItem;
using HeuristicLab.Problems.BinPacking.PackingPlans;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Common;
using HeuristicLab.Data;
using HeuristicLab.Collections;
using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
using HeuristicLab.Problems.BinPacking.Interfaces;
using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
namespace HeuristicLab.Problems.BinPacking.Decoders {
[Item("Identical bin two dimensional grouping vector decoder", "")]
[StorableClass]
public class TwoDimensionalBottomLeftGroupingVectorDecoder : IdenticalBinPackingSolutionDecoder<
TwoDimensionalPacking,
RectangularPackingBin,
RectangularPackingItem,
PackingPlan> {
public TwoDimensionalBottomLeftGroupingVectorDecoder()
: base() {
//EncodedSolutionParameter.ActualName = "EncodedSolution";
}
[StorableConstructor]
protected TwoDimensionalBottomLeftGroupingVectorDecoder(bool deserializing) : base(deserializing) { }
protected TwoDimensionalBottomLeftGroupingVectorDecoder(TwoDimensionalBottomLeftGroupingVectorDecoder original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new TwoDimensionalBottomLeftGroupingVectorDecoder(this, cloner);
}
protected override PackingPlan CreatePackingPlanFromEncoding(IPackingSolutionEncoding encodedSolution) {
var solution = encodedSolution as GroupingVectorEncoding;
if (solution == null) throw new InvalidOperationException("Encoding is not of type GroupingVector");
RegularSimpleRotationPackingPlan result =
new RegularSimpleRotationPackingPlan(PackingBinMeasuresParameter.ActualValue, PackingItemMeasuresParameter.ActualValue);
int nrOfBins = solution.GroupingVector.Max() + 1;
//Get all indexes of items for every bin according to grouping-vector
//It is assumed, that at this point the item-indexes are sorted according to their item-size!
Dictionary> unpackedItemIndexesPerBin = new Dictionary>();
for (int i = 0; i < nrOfBins; i++) {
unpackedItemIndexesPerBin[i] = solution.GroupingVector
.Select((Value, Index) => new { Value, Index })
.Where(temp => temp.Value == i)
.Select(temp => temp.Index).ToList();
}
ObservableDictionary packingPositions = new ObservableDictionary();
var remainingItems = new List();
//Iterate over all bin-lists
for (int binNr = 0; binNr < nrOfBins; binNr++ ) {
//Iterate over the current bin-item-list and find feasible positions in the current bin for each item
var unpackedItems = unpackedItemIndexesPerBin [binNr];
for (int i = 0; i < unpackedItems.Count; i++ ) {
var itemIndex = unpackedItems[i];
var item = PackingItemMeasuresParameter.ActualValue[itemIndex];
TwoDimensionalPacking position = null;
//Look for space in current bin
position = PackingHeuristics.BottomLeftPosition(PackingBinMeasuresParameter.ActualValue, binNr, item, packingPositions, PackingItemMeasuresParameter.ActualValue);
//Did not find enough space in current bin
if (position == null) {
remainingItems.Add(itemIndex);
//if (binNr + 1 >= nrOfBins) {
// nrOfBins++;
// unpackedItemIndexesPerBin[binNr + 1] = new List();
// unpackedItemIndexesPerBin[binNr + 1].Add(itemIndex);
// unpackedItemIndexesPerBin[binNr].Remove(itemIndex);
//} else {
// unpackedItemIndexesPerBin[binNr + 1].Add(itemIndex);
// unpackedItemIndexesPerBin[binNr + 1].Sort();
//}
//solution.GroupingVector[itemIndex] = binNr + 1;
} else
packingPositions[itemIndex] = position;
}
}
//Packing of remaining items
//Iterate over all bin-lists
for (int binNr = 0; binNr < nrOfBins && remainingItems.Count > 0; binNr++) {
var unpackedItems = new List(remainingItems);
for (int i = 0; i < unpackedItems.Count; i++) {
var itemIndex = unpackedItems[i];
var item = PackingItemMeasuresParameter.ActualValue[itemIndex];
TwoDimensionalPacking position = null;
//Look for space in current bin
position = PackingHeuristics.BottomLeftPosition(PackingBinMeasuresParameter.ActualValue, binNr, item, packingPositions, PackingItemMeasuresParameter.ActualValue);
if (position != null) {
packingPositions[itemIndex] = position;
remainingItems.Remove(itemIndex);
solution.GroupingVector[itemIndex] = binNr;
}
}
if (remainingItems.Count > 0 && binNr + 1 >= nrOfBins) {
nrOfBins++;
}
}
result.PackingItemPositions = packingPositions;
return result;
}
}
}