Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966 restored functionality after splitting into 2d and 3d problems

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  public class RectangularIdenticalBinPackingProblem : RegularIdenticalBinPackingProblem<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> {
44
45    #region Default Instance
46    private static readonly BPPData DefaultInstance = new BPPData() {
47      Name = "2D BPP Default Instance",
48      Description = "The default instance for 2D Bin Packing.",
49      BinMeasures = new int[] { 20, 16 },
50      ItemMeasures = new int[][] {
51        new int[] {3,8},
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        new int[] {5,3},
86        new int[] {9,3},
87        new int[] {2,7}
88      },
89      Items = 30
90    };
91    #endregion
92
93    [StorableConstructor]
94    protected RectangularIdenticalBinPackingProblem(bool deserializing) : base(deserializing) { }
95    protected RectangularIdenticalBinPackingProblem(RectangularIdenticalBinPackingProblem original, Cloner cloner)
96      : base(original, cloner) {
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new RectangularIdenticalBinPackingProblem(this, cloner);
100    }
101    public RectangularIdenticalBinPackingProblem() : base(
102      new PackingPlanEvaluationAlgorithm<Permutation, TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>()) {
103    }
104
105    #region Problem instance handling
106    public override void Load(BPPData data) {
107      var realData = data as RealBPPData;
108      var binData = new RectangularPackingBin(data.BinMeasures[0], data.BinMeasures[1]);
109
110      var itemData = new ItemList<RectangularPackingItem>(data.Items);
111      for (int j = 0; j < data.Items; j++) {
112        var bin = new RectangularPackingBin(data.BinMeasures[0], data.BinMeasures[1]);
113        var item = new RectangularPackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);
114        if (realData != null) {
115          item.Weight = realData.ItemWeights[j];
116          item.Material = realData.ItemMaterials[j];
117        }
118        itemData.Add(item);
119      }
120
121      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
122
123      PackingBinMeasures = binData;
124      PackingItemMeasures = itemData;
125
126      ApplyHorizontalOrientation();
127      SortItems();
128      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
129      LowerBoundParameter.Value.Value = CalculateLowerBound();
130    }
131
132
133    public override BPPData Export() {
134      var result = new BPPData {
135        Name = Name,
136        Description = Description,
137        Items = PackingItemsParameter.Value.Value,
138        BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height }
139      };
140
141      var itemMeasures = new int[result.Items][];
142      int i = 0;
143      foreach (var item in PackingItemMeasures) {
144        itemMeasures[i] = new int[] { item.Width, item.Height };
145        i++;
146      }
147      result.ItemMeasures = itemMeasures;
148      return result;
149    }
150    #endregion
151
152    #region Helpers
153    protected override void InitializeDecoder() {
154      // Operators.RemoveAll(op => op is I3DOperator); TODO
155
156      PackingSolutionDecoderParameter.ValidValues.Clear();
157      if (SolutionCreator is PackingSequenceRandomCreator) {
158        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
159      } else if (SolutionCreator is GroupingVectorRandomCreator) {
160        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
161      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
162        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
163      } else {
164        string error = "The given problem does not support the selected solution-creator.";
165        ErrorHandling.ShowErrorDialog(error, null);
166      }
167    }
168
169    protected override IPackingPlanEvaluator CreateDefaultEvaluator() {
170      return new PackingRatioRectangularIdenticalBinEvaluator();
171    }
172
173    protected override void InitializeProblemData() {
174      Load(DefaultInstance);
175    }
176
177    protected override void RemoveTooBigItems() {
178      PackingItemMeasures.RemoveAll(pi =>
179        !PackingBinMeasures.Encloses(new TwoDimensionalPacking(0, 0, 0, false), pi) &&
180        !PackingBinMeasures.Encloses(new TwoDimensionalPacking(0, 0, 0, true), pi));
181    }
182    #endregion
183  }
184}
Note: See TracBrowser for help on using the repository browser.