[9348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13032] | 3 | * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9348] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 | using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
|
---|
| 28 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
| 29 | using HeuristicLab.Problems.Instances;
|
---|
[9440] | 30 | using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
|
---|
[14039] | 31 | using HeuristicLab.Data;
|
---|
| 32 | using System;
|
---|
[14046] | 33 | using HeuristicLab.Problems.BinPacking;
|
---|
[9348] | 34 |
|
---|
[14046] | 35 | namespace HeuristicLab.Problems.BinPacking2D {
|
---|
[13460] | 36 | [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.")]
|
---|
[9348] | 37 | [StorableClass]
|
---|
[13460] | 38 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
|
---|
[14040] | 39 | // TODO: only support 2d BPPData
|
---|
[14049] | 40 | public class Problem : Problem<PackingPosition, PackingShape, PackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
|
---|
[9348] | 41 |
|
---|
| 42 | #region Default Instance
|
---|
| 43 | private static readonly BPPData DefaultInstance = new BPPData() {
|
---|
| 44 | Name = "2D BPP Default Instance",
|
---|
| 45 | Description = "The default instance for 2D Bin Packing.",
|
---|
| 46 | BinMeasures = new int[] { 20, 16 },
|
---|
| 47 | ItemMeasures = new int[][] {
|
---|
| 48 | new int[] {3,8},
|
---|
| 49 | new int[] {5,3},
|
---|
| 50 | new int[] {9,3},
|
---|
| 51 | new int[] {2,7},
|
---|
| 52 | new int[] {5,3},
|
---|
| 53 | new int[] {9,3},
|
---|
| 54 | new int[] {2,7},
|
---|
| 55 | new int[] {5,3},
|
---|
| 56 | new int[] {9,3},
|
---|
| 57 | new int[] {2,7},
|
---|
| 58 | new int[] {5,3},
|
---|
| 59 | new int[] {9,3},
|
---|
| 60 | new int[] {2,7},
|
---|
| 61 | new int[] {5,3},
|
---|
| 62 | new int[] {9,3},
|
---|
| 63 | new int[] {2,7},
|
---|
| 64 | new int[] {5,3},
|
---|
| 65 | new int[] {9,3},
|
---|
| 66 | new int[] {2,7},
|
---|
| 67 | new int[] {5,3},
|
---|
| 68 | new int[] {9,3},
|
---|
| 69 | new int[] {2,7},
|
---|
| 70 | new int[] {5,3},
|
---|
| 71 | new int[] {9,3},
|
---|
| 72 | new int[] {2,7},
|
---|
| 73 | new int[] {5,3},
|
---|
| 74 | new int[] {9,3},
|
---|
| 75 | new int[] {2,7},
|
---|
| 76 | new int[] {5,3},
|
---|
| 77 | new int[] {9,3},
|
---|
| 78 | new int[] {2,7},
|
---|
| 79 | new int[] {5,3},
|
---|
| 80 | new int[] {9,3},
|
---|
| 81 | new int[] {2,7},
|
---|
| 82 | new int[] {5,3},
|
---|
| 83 | new int[] {9,3},
|
---|
| 84 | new int[] {2,7}
|
---|
| 85 | },
|
---|
| 86 | Items = 30
|
---|
| 87 | };
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
| 90 | [StorableConstructor]
|
---|
[14049] | 91 | protected Problem(bool deserializing) : base(deserializing) { }
|
---|
| 92 | protected Problem(Problem original, Cloner cloner)
|
---|
[9348] | 93 | : base(original, cloner) {
|
---|
| 94 | }
|
---|
| 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[14049] | 96 | return new Problem(this, cloner);
|
---|
[9348] | 97 | }
|
---|
[14049] | 98 | public Problem() : base(
|
---|
| 99 | new PackingPlanEvaluationAlgorithm<Permutation, PackingPosition, PackingShape, PackingItem>()) {
|
---|
[9348] | 100 | }
|
---|
| 101 |
|
---|
[14039] | 102 | #region Problem instance handling
|
---|
[14040] | 103 | public void Load(BPPData data) {
|
---|
[14039] | 104 | var realData = data as RealBPPData;
|
---|
[14049] | 105 | var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);
|
---|
[9348] | 106 |
|
---|
[14049] | 107 | var itemData = new ItemList<PackingItem>(data.Items);
|
---|
[14039] | 108 | for (int j = 0; j < data.Items; j++) {
|
---|
[14049] | 109 | var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);
|
---|
| 110 | var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);
|
---|
[14039] | 111 | if (realData != null) {
|
---|
| 112 | item.Weight = realData.ItemWeights[j];
|
---|
| 113 | item.Material = realData.ItemMaterials[j];
|
---|
| 114 | }
|
---|
| 115 | itemData.Add(item);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
|
---|
| 119 |
|
---|
| 120 | PackingBinMeasures = binData;
|
---|
| 121 | PackingItemMeasures = itemData;
|
---|
| 122 |
|
---|
| 123 | ApplyHorizontalOrientation();
|
---|
| 124 | SortItems();
|
---|
| 125 | PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
|
---|
| 126 | LowerBoundParameter.Value.Value = CalculateLowerBound();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
[14040] | 130 | public BPPData Export() {
|
---|
[14039] | 131 | var result = new BPPData {
|
---|
| 132 | Name = Name,
|
---|
| 133 | Description = Description,
|
---|
| 134 | Items = PackingItemsParameter.Value.Value,
|
---|
| 135 | BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height }
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | var itemMeasures = new int[result.Items][];
|
---|
| 139 | int i = 0;
|
---|
| 140 | foreach (var item in PackingItemMeasures) {
|
---|
| 141 | itemMeasures[i] = new int[] { item.Width, item.Height };
|
---|
| 142 | i++;
|
---|
| 143 | }
|
---|
| 144 | result.ItemMeasures = itemMeasures;
|
---|
| 145 | return result;
|
---|
| 146 | }
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
[9348] | 149 | #region Helpers
|
---|
| 150 | protected override void InitializeDecoder() {
|
---|
[13611] | 151 | // Operators.RemoveAll(op => op is I3DOperator); TODO
|
---|
[9495] | 152 |
|
---|
[9563] | 153 | PackingSolutionDecoderParameter.ValidValues.Clear();
|
---|
[14039] | 154 | if (SolutionCreator is PackingSequenceRandomCreator) {
|
---|
[9563] | 155 | PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
|
---|
[14039] | 156 | } else if (SolutionCreator is GroupingVectorRandomCreator) {
|
---|
[9563] | 157 | PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
|
---|
[14039] | 158 | } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
|
---|
[9563] | 159 | PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
|
---|
[9348] | 160 | } else {
|
---|
| 161 | string error = "The given problem does not support the selected solution-creator.";
|
---|
[13460] | 162 | ErrorHandling.ShowErrorDialog(error, null);
|
---|
[9348] | 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | protected override IPackingPlanEvaluator CreateDefaultEvaluator() {
|
---|
[14039] | 167 | return new PackingRatioRectangularIdenticalBinEvaluator();
|
---|
[9348] | 168 | }
|
---|
| 169 |
|
---|
| 170 | protected override void InitializeProblemData() {
|
---|
| 171 | Load(DefaultInstance);
|
---|
| 172 | }
|
---|
[9598] | 173 |
|
---|
| 174 | protected override void RemoveTooBigItems() {
|
---|
| 175 | PackingItemMeasures.RemoveAll(pi =>
|
---|
[14048] | 176 | !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, false), pi) &&
|
---|
| 177 | !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, true), pi));
|
---|
[9598] | 178 | }
|
---|
[9348] | 179 | #endregion
|
---|
| 180 | }
|
---|
| 181 | }
|
---|