Free cookie consent management tool by TermsFeed Policy Generator

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