source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Problem.cs @ 14049

Last change on this file since 14049 was 14049, checked in by gkronber, 5 years ago

#1966: simplified class names

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