Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: renamed *PackingDimension -> PackingPosition

File size: 6.9 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.Core;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Common;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.PluginInfrastructure;
27using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
28using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
29using HeuristicLab.Problems.Instances;
30using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
31using HeuristicLab.Data;
32using System;
33using HeuristicLab.Problems.BinPacking;
34
35namespace HeuristicLab.Problems.BinPacking2D {
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.")]
37  [StorableClass]
38  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
39  // TODO: only support 2d BPPData
40  public class RectangularIdenticalBinPackingProblem : BinPackingProblem<PackingPosition, RectangularPackingShape, RectangularPackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
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]
91    protected RectangularIdenticalBinPackingProblem(bool deserializing) : base(deserializing) { }
92    protected RectangularIdenticalBinPackingProblem(RectangularIdenticalBinPackingProblem original, Cloner cloner)
93      : base(original, cloner) {
94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new RectangularIdenticalBinPackingProblem(this, cloner);
97    }
98    public RectangularIdenticalBinPackingProblem() : base(
99      new PackingPlanEvaluationAlgorithm<Permutation, PackingPosition, RectangularPackingShape, RectangularPackingItem>()) {
100    }
101
102    #region Problem instance handling
103    public void Load(BPPData data) {
104      var realData = data as RealBPPData;
105      var binData = new RectangularPackingShape(data.BinMeasures[0], data.BinMeasures[1]);
106
107      var itemData = new ItemList<RectangularPackingItem>(data.Items);
108      for (int j = 0; j < data.Items; j++) {
109        var bin = new RectangularPackingShape(data.BinMeasures[0], data.BinMeasures[1]);
110        var item = new RectangularPackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);
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
130    public BPPData Export() {
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
149    #region Helpers
150    protected override void InitializeDecoder() {
151      // Operators.RemoveAll(op => op is I3DOperator); TODO
152
153      PackingSolutionDecoderParameter.ValidValues.Clear();
154      if (SolutionCreator is PackingSequenceRandomCreator) {
155        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
156      } else if (SolutionCreator is GroupingVectorRandomCreator) {
157        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
158      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
159        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
160      } else {
161        string error = "The given problem does not support the selected solution-creator.";
162        ErrorHandling.ShowErrorDialog(error, null);
163      }
164    }
165
166    protected override IPackingPlanEvaluator CreateDefaultEvaluator() {
167      return new PackingRatioRectangularIdenticalBinEvaluator();
168    }
169
170    protected override void InitializeProblemData() {
171      Load(DefaultInstance);
172    }
173
174    protected override void RemoveTooBigItems() {
175      PackingItemMeasures.RemoveAll(pi =>
176        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, false), pi) &&
177        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, true), pi));
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.