#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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; using HeuristicLab.Common; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Encodings.PackingEncoding.PackingSequence; using HeuristicLab.Encodings.PackingEncoding.GroupingVector; using HeuristicLab.Problems.Instances; using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Data; using HeuristicLab.Problems.BinPacking; namespace HeuristicLab.Problems.BinPacking3D { [Item("Bin Packing Problem (3D, identical cuboids) (BPP)", "Represents a three-dimensional bin-packing problem using only bins with identical measures and bins/items with cuboidic shapes.")] [StorableClass] [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 310)] // TODO don't support generic BPPData but only 3d BPPData public class Problem : Problem, IProblemInstanceConsumer, IProblemInstanceExporter { #region Default Instance private static readonly BPPData DefaultInstance = new BPPData() { Name = "3D BPP Default Instance", Description = "The default instance for 3D Bin Packing.", BinMeasures = new int[] { 25, 25, 35 }, ItemMeasures = new int[][] { new int[] {12,5,10}, new int[] {10,18,20}, new int[] {9,7,7}, new int[] {21,12,4}, new int[] {8,8,12}, new int[] {3,6,14}, new int[] {20,4,9}, new int[] {5,9,8}, new int[] {7,17,3}, new int[] {13,20,15}, new int[] {9,11,9}, new int[] {10,18,20}, new int[] {9,7,7}, new int[] {21,12,4}, new int[] {8,8,12}, new int[] {3,6,14}, new int[] {20,4,9}, new int[] {5,9,8}, new int[] {7,17,3}, new int[] {13,20,15}, new int[] {9,11,9}, new int[] {10,18,20}, new int[] {9,7,7}, new int[] {21,12,4}, new int[] {8,8,12}, new int[] {3,6,14}, new int[] {20,4,9}, new int[] {5,9,8}, new int[] {7,17,3}, new int[] {13,20,15}, new int[] {9,11,9}, new int[] {10,18,20}, new int[] {9,7,7}, new int[] {21,12,4}, new int[] {8,8,12}, new int[] {3,6,14}, new int[] {20,4,9}, new int[] {5,9,8}, new int[] {7,17,3}, new int[] {13,20,15}, new int[] {9,11,9} }, Items = 30 }; #endregion [StorableConstructor] protected Problem(bool deserializing) : base(deserializing) { } protected Problem(Problem original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); } public Problem() : base( new PackingPlanEvaluationAlgorithm()) { } public void Load(BPPData data) { var realData = data as RealBPPData; var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]); var itemData = new ItemList(data.Items); for (int j = 0; j < data.Items; j++) { var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]); var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], data.ItemMeasures[j][2], bin); if (realData != null) { item.Weight = realData.ItemWeights[j]; item.Material = realData.ItemMaterials[j]; } itemData.Add(item); } BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null; PackingBinMeasures = binData; PackingItemMeasures = itemData; ApplyHorizontalOrientation(); SortItems(); PackingItemsParameter.Value.Value = PackingItemMeasures.Count; LowerBoundParameter.Value.Value = CalculateLowerBound(); } public BPPData Export() { var result = new BPPData { Name = Name, Description = Description, Items = PackingItemsParameter.Value.Value, BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height, PackingBinMeasures.Depth } }; var itemMeasures = new int[result.Items][]; int i = 0; foreach (var item in PackingItemMeasures) { itemMeasures[i] = new int[] { item.Width, item.Height, item.Depth }; i++; } result.ItemMeasures = itemMeasures; return result; } #region Helpers protected override void InitializeDecoder() { // Operators.RemoveAll(op => op is I2DOperator); TODO PackingSolutionDecoderParameter.ValidValues.Clear(); if (SolutionCreator is PackingSequenceRandomCreator) { PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances()); } else if (SolutionCreator is GroupingVectorRandomCreator) { PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances()); } else if (SolutionCreator is MultiComponentVectorRandomCreator) { PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances()); } else { string error = "The given problem does not support the selected solution-creator."; ErrorHandling.ShowErrorDialog(error, null); } } protected override IPackingPlanEvaluator CreateDefaultEvaluator() { return new PackingRatioCuboidIdenticalBinEvaluator(); } protected override void InitializeProblemData() { Load(DefaultInstance); } protected override void RemoveTooBigItems() { PackingItemMeasures.RemoveAll(pi => !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, 0, false), pi) && !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, 0, true), pi)); } #endregion } }