#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; namespace HeuristicLab.Problems.BinPacking.Decoders { [Item("Identical bin, three dimensional, direct permutation decoder", "")] [StorableClass] public class ThreeDimensionalBottomLeftPackingSequenceDecoder : IdenticalBinPackingSolutionDecoder< ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem, PackingPlan> { public ThreeDimensionalBottomLeftPackingSequenceDecoder() : base() { //EncodedSolutionParameter.ActualName = "EncodedSolution"; } [StorableConstructor] protected ThreeDimensionalBottomLeftPackingSequenceDecoder(bool deserializing) : base(deserializing) { } protected ThreeDimensionalBottomLeftPackingSequenceDecoder(ThreeDimensionalBottomLeftPackingSequenceDecoder original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ThreeDimensionalBottomLeftPackingSequenceDecoder(this, cloner); } protected override PackingPlan CreatePackingPlanFromEncoding(IPackingSolutionEncoding encodedSolution) { var solution = encodedSolution as PackingSequenceEncoding; if (solution == null) throw new InvalidOperationException("Encoding is not of type PackingSequence"); RegularSimpleRotationPackingPlan result = new RegularSimpleRotationPackingPlan(PackingBinMeasuresParameter.ActualValue, PackingItemMeasuresParameter.ActualValue); ObservableDictionary packingPositions = new ObservableDictionary(); int nrOfBins = 1; for (int i = 0; i < solution.PackingSequence.Length; i++) { var item = PackingItemMeasuresParameter.ActualValue[solution.PackingSequence[i]]; ThreeDimensionalPacking position = null; //Look for space in existing bins for (int binNr = 0; binNr < nrOfBins; binNr++) { position = PackingHeuristics.DeepestLeftBottomPosition(PackingBinMeasuresParameter.ActualValue, binNr, item, packingPositions, PackingItemMeasuresParameter.ActualValue); if (position != null) break; } //Did not find enough space in any of the existing bins => create new bin if (position == null) { nrOfBins++; position = PackingHeuristics.DeepestLeftBottomPosition(PackingBinMeasuresParameter.ActualValue, nrOfBins - 1, item, packingPositions, PackingItemMeasuresParameter.ActualValue); } if (position == null) position = new ThreeDimensionalPacking(-1, 0, 0, 0); packingPositions[solution.PackingSequence[i]] = position; } result.PackingItemPositions = packingPositions; return result; } } }