Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/CuboidIdenticalBinPackingProblem.cs @ 14040

Last change on this file since 14040 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.4 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Core;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.BinPacking.Evaluators;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
32using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
33using HeuristicLab.Problems.Instances;
34using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
35using HeuristicLab.PluginInfrastructure;
36using System;
37using HeuristicLab.Data;
38
39namespace HeuristicLab.Problems.BinPacking.Problem {
40  [Item("Bin Packing Problem (3D, identical cuboids) (BPP)", "Represents a three-dimensional bin-packing problem using only bins with identical measures and bins/items with cuboidic shapes.")]
41  [StorableClass]
42  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 310)]
43  // TODO don't support generic BPPData but only 3d BPPData
44  public class CuboidIdenticalBinPackingProblem : BinPackingProblem<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
45
46
47    #region Default Instance
48    private static readonly BPPData DefaultInstance = new BPPData() {
49      Name = "3D BPP Default Instance",
50      Description = "The default instance for 3D Bin Packing.",
51      BinMeasures = new int[] { 25, 25, 35 },
52      ItemMeasures = new int[][] {
53        new int[] {12,5,10},
54        new int[] {10,18,20},
55        new int[] {9,7,7},
56        new int[] {21,12,4},
57        new int[] {8,8,12},
58        new int[] {3,6,14},
59        new int[] {20,4,9},
60        new int[] {5,9,8},
61        new int[] {7,17,3},
62        new int[] {13,20,15},
63        new int[] {9,11,9},
64        new int[] {10,18,20},
65        new int[] {9,7,7},
66        new int[] {21,12,4},
67        new int[] {8,8,12},
68        new int[] {3,6,14},
69        new int[] {20,4,9},
70        new int[] {5,9,8},
71        new int[] {7,17,3},
72        new int[] {13,20,15},
73        new int[] {9,11,9},
74        new int[] {10,18,20},
75        new int[] {9,7,7},
76        new int[] {21,12,4},
77        new int[] {8,8,12},
78        new int[] {3,6,14},
79        new int[] {20,4,9},
80        new int[] {5,9,8},
81        new int[] {7,17,3},
82        new int[] {13,20,15},
83        new int[] {9,11,9},
84        new int[] {10,18,20},
85        new int[] {9,7,7},
86        new int[] {21,12,4},
87        new int[] {8,8,12},
88        new int[] {3,6,14},
89        new int[] {20,4,9},
90        new int[] {5,9,8},
91        new int[] {7,17,3},
92        new int[] {13,20,15},
93        new int[] {9,11,9}
94      },
95      Items = 30
96    };
97    #endregion
98
99    [StorableConstructor]
100    protected CuboidIdenticalBinPackingProblem(bool deserializing) : base(deserializing) { }
101    protected CuboidIdenticalBinPackingProblem(CuboidIdenticalBinPackingProblem original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new CuboidIdenticalBinPackingProblem(this, cloner);
106    }
107    public CuboidIdenticalBinPackingProblem() : base(
108      new PackingPlanEvaluationAlgorithm<Permutation, ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem>()) {
109    }
110
111
112
113    public void Load(BPPData data) {
114      var realData = data as RealBPPData;
115      var binData = new CuboidPackingBin(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);
116
117      var itemData = new ItemList<CuboidPackingItem>(data.Items);
118      for (int j = 0; j < data.Items; j++) {
119        var bin = new CuboidPackingBin(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);
120        var item = new CuboidPackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], data.ItemMeasures[j][2], bin);
121        if (realData != null) {
122          item.Weight = realData.ItemWeights[j];
123          item.Material = realData.ItemMaterials[j];
124        }
125        itemData.Add(item);
126      }
127
128      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
129
130      PackingBinMeasures = binData;
131      PackingItemMeasures = itemData;
132
133      ApplyHorizontalOrientation();
134      SortItems();
135      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
136      LowerBoundParameter.Value.Value = CalculateLowerBound();
137    }
138
139    public BPPData Export() {
140      var result = new BPPData {
141        Name = Name,
142        Description = Description,
143        Items = PackingItemsParameter.Value.Value,
144        BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height, PackingBinMeasures.Depth }
145      };
146
147      var itemMeasures = new int[result.Items][];
148      int i = 0;
149      foreach (var item in PackingItemMeasures) {
150        itemMeasures[i] = new int[] { item.Width, item.Height, item.Depth };
151        i++;
152      }
153      result.ItemMeasures = itemMeasures;
154      return result;
155    }
156
157    #region Helpers
158    protected override void InitializeDecoder() {
159      // Operators.RemoveAll(op => op is I2DOperator); TODO
160
161      PackingSolutionDecoderParameter.ValidValues.Clear();
162      if (SolutionCreator is PackingSequenceRandomCreator) {
163        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I3DPSDecoder>());
164      } else if (SolutionCreator is GroupingVectorRandomCreator) {
165        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I3DGVDecoder>());
166      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
167        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I3DMCVDecoder>());
168      } else {
169        string error = "The given problem does not support the selected solution-creator.";
170        ErrorHandling.ShowErrorDialog(error, null);
171      }
172    }
173
174    protected override IPackingPlanEvaluator CreateDefaultEvaluator() {
175      return new PackingRatioCuboidIdenticalBinEvaluator();
176    }
177
178    protected override void InitializeProblemData() {
179      Load(DefaultInstance);
180    }
181
182    protected override void RemoveTooBigItems() {
183      PackingItemMeasures.RemoveAll(pi =>
184        !PackingBinMeasures.Encloses(new ThreeDimensionalPacking(0, 0, 0, 0, false), pi) &&
185        !PackingBinMeasures.Encloses(new ThreeDimensionalPacking(0, 0, 0, 0, true), pi));
186    }
187    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.