source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/PackingItem.cs @ 15617

Last change on this file since 15617 was 15617, checked in by rhanghof, 20 months ago

#2817:

  • The items can be rotated and tilted now.
  • Added pruning of extreme points in packed bins.
  • Added new packer which packs items by positioning them on the point with the minimum of wasted space. He uses rotating and tilting of items.
  • Added classes for sorting given items.
File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Core;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Problems.BinPacking;
28
29namespace HeuristicLab.Problems.BinPacking3D {
30  [Item("PackingItem (3d)", "Represents a cuboidic packing-item for bin-packing problems.")]
31  [StorableClass]
32  public class PackingItem : PackingShape, IPackingItem {
33    #region Properties
34
35
36    public IValueParameter<PackingShape> TargetBinParameter {
37      get { return (IValueParameter<PackingShape>)Parameters["TargetBin"]; }
38    }
39    public IFixedValueParameter<DoubleValue> WeightParameter {
40      get { return (IFixedValueParameter<DoubleValue>)Parameters["Weight"]; }
41    }
42    public IFixedValueParameter<IntValue> MaterialParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters["Material"]; }
44    }
45   
46    public PackingShape TargetBin {
47      get { return TargetBinParameter.Value; }
48      set { TargetBinParameter.Value = value; }
49    }
50
51    public double Weight {
52      get { return WeightParameter.Value.Value; }
53      set { WeightParameter.Value.Value = value; }
54    }
55
56    public int Material {
57      get { return MaterialParameter.Value.Value; }
58      set { MaterialParameter.Value.Value = value; }
59    }
60
61
62    public IValueParameter<BoolValue> RotateEnabledParameter {
63      get { return (IValueParameter<BoolValue>)Parameters["RotateEnabled"]; }
64    }
65
66    public IValueParameter<BoolValue> RotatedParameter {
67      get { return (IValueParameter<BoolValue>)Parameters["Rotated"]; }
68    }
69
70    public IValueParameter<BoolValue> TiltEnabledParameter {
71      get { return (IValueParameter<BoolValue>)Parameters["TiltEnabled"]; }
72    }
73
74    public IValueParameter<BoolValue> TiltedParameter {
75      get { return (IValueParameter<BoolValue>)Parameters["Tilted"]; }
76    }
77   
78    /// <summary>
79    /// Enables that the current item can be rotated.
80    /// </summary>
81    public bool RotateEnabled {
82      get { return RotateEnabledParameter.Value.Value; }
83      set { RotateEnabledParameter.Value.Value = value; }
84    }
85
86    /// <summary>
87    /// Indicates that the current item is rotated.
88    /// If the item is also tilted it will be tilted first.
89    /// </summary>
90    public bool Rotated {
91      get { return RotatedParameter.Value.Value; }
92      set { RotatedParameter.Value.Value = value; }
93    }
94
95    /// <summary>
96    /// Enables that the current item can be tilted.
97    /// </summary>
98    public bool TiltEnabled {
99      get { return TiltEnabledParameter.Value.Value; }
100      set { TiltEnabledParameter.Value.Value = value; }
101    }
102
103    /// <summary>
104    /// Indicates that the current item is tilted.
105    /// Tilted means that the item is tilted sidewards.
106    /// If the item is also rotated it will be tilted first.
107    /// </summary>
108    public bool Tilted {
109      get { return TiltedParameter.Value.Value; }
110      set { TiltedParameter.Value.Value = value; }
111    }
112   
113    public IFixedValueParameter<IntValue> LoadSecuringHeightParameter {
114      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringHeight"]; }
115    }
116   
117    public IFixedValueParameter<IntValue> LoadSecuringWidthParameter {
118      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringWidth"]; }
119    }
120
121    public IFixedValueParameter<IntValue> LoadSecuringDepthParameter {
122      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringDepth"]; }
123    }
124
125    public int LoadSecuringHeight {
126      get { return LoadSecuringHeightParameter.Value.Value; }
127      set { LoadSecuringHeightParameter.Value.Value = value; }
128    }
129
130    public int LoadSecuringWidth {
131      get { return LoadSecuringWidthParameter.Value.Value; }
132      set { LoadSecuringWidthParameter.Value.Value = value; }
133    }
134
135    public int LoadSecuringDepth {
136      get { return LoadSecuringDepthParameter.Value.Value; }
137      set { LoadSecuringDepthParameter.Value.Value = value; }
138    }
139
140    /// <summary>
141    /// This property represents the height as needed in the bin packing.
142    /// </summary>
143    public new int Height {
144      get {
145        if (!Tilted) {
146          return HeightParameter.Value.Value + LoadSecuringHeightParameter.Value.Value;
147        } else {
148          return WidthParameter.Value.Value + LoadSecuringHeightParameter.Value.Value;
149        }
150      }
151    }
152
153    /// <summary>
154    /// This property represents the width as needed in the bin packing.
155    /// </summary>
156    public new int Width {
157      get {
158        if (Rotated) {
159          return DepthParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
160        } else {
161          if (!Tilted) {
162            return WidthParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
163          } else {
164            return HeightParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
165          }
166        }
167      }
168    }
169
170    /// <summary>
171    /// This property represents the depth as needed in the bin packing.
172    /// </summary>
173    public new int Depth {
174      get {
175        if (!Rotated) {
176          return DepthParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
177        } else {
178          if (!Tilted) {
179            return WidthParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
180          } else {
181            return HeightParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
182          }
183        }
184      }
185    }
186
187    /// <summary>
188    /// This property represents the height as it is seen in the view of the bin packing.
189    /// </summary>
190    public int HeightInView {
191      get {
192        if (!Tilted) {
193          return HeightParameter.Value.Value;
194        } else {
195          return WidthParameter.Value.Value;
196        }
197      } 
198    }
199
200    /// <summary>
201    /// This property represents the width as it is seen in the view of the bin packing.
202    /// </summary>
203    public int WidthInView {
204      get {
205        if (Rotated) {
206          return DepthParameter.Value.Value;
207        } else {
208          if (!Tilted) {
209            return WidthParameter.Value.Value;
210          } else {
211            return HeightParameter.Value.Value;
212          }
213        }
214      }
215    }
216
217    /// <summary>
218    /// This property represents the depth as it is seen in the view of the bin packing.
219    /// </summary>
220    public int DepthInView {
221      get {
222        if (!Rotated) {
223          return DepthParameter.Value.Value;
224        } else {
225          if (!Tilted) {
226            return WidthParameter.Value.Value;
227          } else {
228            return HeightParameter.Value.Value;
229          }
230        }
231      }
232    }
233
234    /// <summary>
235    /// This property represents the original height.
236    /// </summary>
237    public int OriginalHeight {
238      get { return HeightParameter.Value.Value; }
239      set { HeightParameter.Value.Value = value; }
240    }
241
242    /// <summary>
243    /// This property represents the original width.
244    /// </summary>
245    public int OriginalWidth {
246      get { return WidthParameter.Value.Value; }
247      set { WidthParameter.Value.Value = value; }
248    }
249
250    /// <summary>
251    /// This property represents the original depth.
252    /// </summary>
253    public int OriginalDepth {
254      get { return DepthParameter.Value.Value; }
255      set { DepthParameter.Value.Value = value; }
256    }
257   
258    public bool SupportsStacking(IPackingItem other) {
259      return ((other.Material < this.Material) || (other.Material.Equals(this.Material) && other.Weight <= this.Weight));
260    }
261    #endregion
262
263
264    [StorableConstructor]
265    protected PackingItem(bool deserializing) : base(deserializing) { }
266    protected PackingItem(PackingItem original, Cloner cloner)
267      : base(original, cloner) {
268      RegisterEvents();
269    }
270    public PackingItem()
271      : base() {
272      Parameters.Add(new ValueParameter<PackingShape>("TargetBin"));
273      Parameters.Add(new FixedValueParameter<DoubleValue>("Weight"));
274      Parameters.Add(new FixedValueParameter<IntValue>("Material"));
275      Parameters.Add(new FixedValueParameter<BoolValue>("RotateEnabled"));
276      Parameters.Add(new FixedValueParameter<BoolValue>("Rotated"));
277      Parameters.Add(new FixedValueParameter<BoolValue>("TiltEnabled"));
278      Parameters.Add(new FixedValueParameter<BoolValue>("Tilted"));
279     
280      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringHeight"));
281      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringWidth"));
282      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringDepth"));
283
284      RegisterEvents();
285    }
286
287    public PackingItem(int width, int height, int depth, PackingShape targetBin)
288      : this() {
289      this.OriginalWidth = width;
290      this.OriginalHeight = height;
291      this.OriginalDepth = depth;
292      this.TargetBin = (PackingShape)targetBin.Clone();
293    }
294
295    public PackingItem(int width, int height, int depth, PackingShape targetBin, double weight, int material)
296      : this(width, height, depth, targetBin) {   
297      this.Weight = weight;
298      this.Material = material;
299    }
300
301   
302
303    public PackingItem(PackingItem packingItem) 
304      : this() {
305      OriginalWidth = packingItem.OriginalWidth;
306      OriginalHeight = packingItem.OriginalHeight;
307      OriginalDepth =  packingItem.OriginalDepth;
308      TargetBin = (PackingShape)packingItem.TargetBin.Clone();
309      Weight = packingItem.Weight;
310      Material = packingItem.Material;
311      Rotated = packingItem.Rotated;
312      Tilted = packingItem.Tilted;
313
314      LoadSecuringDepth = packingItem.LoadSecuringDepth;
315      LoadSecuringHeight = packingItem.LoadSecuringHeight;
316      LoadSecuringWidth = packingItem.LoadSecuringWidth;
317    }
318
319    [StorableHook(HookType.AfterDeserialization)]
320    private void AfterDeserialization() {
321      RegisterEvents();
322    }
323
324    public override IDeepCloneable Clone(Cloner cloner) {
325      return new PackingItem(this, cloner);
326    }
327
328    private void RegisterEvents() {
329      // NOTE: only because of ToString override
330      WeightParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
331      MaterialParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
332
333      // target bin does not occur in ToString()
334    }
335
336    public override string ToString() {
337      return string.Format("CuboidPackingItem ({0}, {1}, {2}; weight={3}, mat={4})", this.Width, this.Height, this.Depth, this.Weight, this.Material);
338    }
339   
340  }
341}
Note: See TracBrowser for help on using the repository browser.