Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/RectangularIdenticalBinPackingProblem.cs @ 14042

Last change on this file since 14042 was 14040, checked in by gkronber, 8 years ago

#1966:

  • removed separation of general bin packing problems and 'regular' (=rectangular or cuboid) bin packing problems (=> all our bin packing problems are regular)
  • removed ISOContainer BinPacking problem (seems to be just a minor variant for generic 3d bin packing)
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Problems.BinPacking.Dimensions;
23using HeuristicLab.Problems.BinPacking.Interfaces;
24using HeuristicLab.Problems.BinPacking.PackingBin;
25using HeuristicLab.Problems.BinPacking.PackingItem;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.BinPacking.Evaluators;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
33using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
34using HeuristicLab.Problems.Instances;
35using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
36using HeuristicLab.Data;
37using System;
38
39namespace HeuristicLab.Problems.BinPacking.Problem {
40  [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.")]
41  [StorableClass]
42  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
43  // TODO: only support 2d BPPData
44  public class RectangularIdenticalBinPackingProblem : BinPackingProblem<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
45
46    #region Default Instance
47    private static readonly BPPData DefaultInstance = new BPPData() {
48      Name = "2D BPP Default Instance",
49      Description = "The default instance for 2D Bin Packing.",
50      BinMeasures = new int[] { 20, 16 },
51      ItemMeasures = new int[][] {
52        new int[] {3,8},
53        new int[] {5,3},
54        new int[] {9,3},
55        new int[] {2,7},
56        new int[] {5,3},
57        new int[] {9,3},
58        new int[] {2,7},
59        new int[] {5,3},
60        new int[] {9,3},
61        new int[] {2,7},
62        new int[] {5,3},
63        new int[] {9,3},
64        new int[] {2,7},
65        new int[] {5,3},
66        new int[] {9,3},
67        new int[] {2,7},
68        new int[] {5,3},
69        new int[] {9,3},
70        new int[] {2,7},
71        new int[] {5,3},
72        new int[] {9,3},
73        new int[] {2,7},
74        new int[] {5,3},
75        new int[] {9,3},
76        new int[] {2,7},
77        new int[] {5,3},
78        new int[] {9,3},
79        new int[] {2,7},
80        new int[] {5,3},
81        new int[] {9,3},
82        new int[] {2,7},
83        new int[] {5,3},
84        new int[] {9,3},
85        new int[] {2,7},
86        new int[] {5,3},
87        new int[] {9,3},
88        new int[] {2,7}
89      },
90      Items = 30
91    };
92    #endregion
93
94    [StorableConstructor]
95    protected RectangularIdenticalBinPackingProblem(bool deserializing) : base(deserializing) { }
96    protected RectangularIdenticalBinPackingProblem(RectangularIdenticalBinPackingProblem original, Cloner cloner)
97      : base(original, cloner) {
98    }
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new RectangularIdenticalBinPackingProblem(this, cloner);
101    }
102    public RectangularIdenticalBinPackingProblem() : base(
103      new PackingPlanEvaluationAlgorithm<Permutation, TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>()) {
104    }
105
106    #region Problem instance handling
107    public void Load(BPPData data) {
108      var realData = data as RealBPPData;
109      var binData = new RectangularPackingBin(data.BinMeasures[0], data.BinMeasures[1]);
110
111      var itemData = new ItemList<RectangularPackingItem>(data.Items);
112      for (int j = 0; j < data.Items; j++) {
113        var bin = new RectangularPackingBin(data.BinMeasures[0], data.BinMeasures[1]);
114        var item = new RectangularPackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);
115        if (realData != null) {
116          item.Weight = realData.ItemWeights[j];
117          item.Material = realData.ItemMaterials[j];
118        }
119        itemData.Add(item);
120      }
121
122      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
123
124      PackingBinMeasures = binData;
125      PackingItemMeasures = itemData;
126
127      ApplyHorizontalOrientation();
128      SortItems();
129      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
130      LowerBoundParameter.Value.Value = CalculateLowerBound();
131    }
132
133
134    public BPPData Export() {
135      var result = new BPPData {
136        Name = Name,
137        Description = Description,
138        Items = PackingItemsParameter.Value.Value,
139        BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height }
140      };
141
142      var itemMeasures = new int[result.Items][];
143      int i = 0;
144      foreach (var item in PackingItemMeasures) {
145        itemMeasures[i] = new int[] { item.Width, item.Height };
146        i++;
147      }
148      result.ItemMeasures = itemMeasures;
149      return result;
150    }
151    #endregion
152
153    #region Helpers
154    protected override void InitializeDecoder() {
155      // Operators.RemoveAll(op => op is I3DOperator); TODO
156
157      PackingSolutionDecoderParameter.ValidValues.Clear();
158      if (SolutionCreator is PackingSequenceRandomCreator) {
159        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
160      } else if (SolutionCreator is GroupingVectorRandomCreator) {
161        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
162      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
163        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
164      } else {
165        string error = "The given problem does not support the selected solution-creator.";
166        ErrorHandling.ShowErrorDialog(error, null);
167      }
168    }
169
170    protected override IPackingPlanEvaluator CreateDefaultEvaluator() {
171      return new PackingRatioRectangularIdenticalBinEvaluator();
172    }
173
174    protected override void InitializeProblemData() {
175      Load(DefaultInstance);
176    }
177
178    protected override void RemoveTooBigItems() {
179      PackingItemMeasures.RemoveAll(pi =>
180        !PackingBinMeasures.Encloses(new TwoDimensionalPacking(0, 0, 0, false), pi) &&
181        !PackingBinMeasures.Encloses(new TwoDimensionalPacking(0, 0, 0, true), pi));
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.