Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: removed types for *PackingBin because PackingBins and PackingShapes have the same capabilities

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