#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.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Encodings.PackingEncoding.PackingSequence; using HeuristicLab.Problems.Instances; using HeuristicLab.Data; using HeuristicLab.Problems.BinPacking; namespace HeuristicLab.Problems.BinPacking2D { [Item("Bin Packing Problem (2D, identical rectangles) (BPP)", "Represents a two-dimensional bin-packing problem using only bins with identical measures and bins/items with rectangular shapes.")] [StorableClass] [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)] public class Problem : Problem, IProblemInstanceConsumer, IProblemInstanceExporter { #region Default Instance private static readonly BPPData DefaultInstance = new BPPData() { Name = "2D BPP Default Instance", Description = "The default instance for 2D Bin Packing.", BinShape = new PackingShape(20, 16), Items = new PackingItem[] { new PackingItem(3, 8, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(5, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(9, 3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}, new PackingItem(2, 7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0} }, }; #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 DecodingEvaluator()) { } #region Problem instance handling public void Load(BPPData data) { BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null; PackingBinMeasures = data.BinShape; PackingItemMeasures = new ItemList(data.Items); ApplyHorizontalOrientation(); SortItems(); PackingItemsParameter.Value.Value = PackingItemMeasures.Count; LowerBoundParameter.Value.Value = CalculateLowerBound(); } public BPPData Export() { return new BPPData { Name = Name, Description = Description, BinShape = PackingBinMeasures, Items = PackingItemMeasures.ToArray() }; } #endregion #region Helpers protected override void InitializeDecoder() { // Operators.RemoveAll(op => op is I3DOperator); 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 IEvaluator CreateDefaultEvaluator() { return new PackingRatioEvaluator(); } protected override void InitializeProblemData() { Load(DefaultInstance); } protected override void RemoveTooBigItems() { PackingItemMeasures.RemoveAll(pi => !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, false), pi) && !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, true), pi)); } #endregion } }