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

Last change on this file since 15646 was 15646, checked in by rhanghof, 3 years ago

#2817:

  • Dealing with stackable items
  • Enhanced the Evaluator
  • Added parameters some paramters to the packing items
File size: 12.2 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 IFixedValueParameter<DoubleValue> SupportedWeightParameter {
63      get { return (IFixedValueParameter<DoubleValue>)Parameters["SupportedWeight"]; }
64    }
65    public double SupportedWeight {
66      get { return SupportedWeightParameter.Value.Value; }
67      set { SupportedWeightParameter.Value.Value = value; }
68    }
69
70    public IValueParameter<BoolValue> IsStackableParameter {
71      get { return (IValueParameter<BoolValue>)Parameters["IsStackable"]; }
72    }
73
74    /// <summary>
75    /// Indicates that another item can be stacked on the current one.
76    /// </summary>
77    public bool IsStackabel {
78      get { return IsStackableParameter.Value.Value; }
79      set { IsStackableParameter.Value.Value = value; }
80    }
81
82    public IValueParameter<BoolValue> RotateEnabledParameter {
83      get { return (IValueParameter<BoolValue>)Parameters["RotateEnabled"]; }
84    }
85
86    public IValueParameter<BoolValue> RotatedParameter {
87      get { return (IValueParameter<BoolValue>)Parameters["Rotated"]; }
88    }
89
90    public IValueParameter<BoolValue> TiltEnabledParameter {
91      get { return (IValueParameter<BoolValue>)Parameters["TiltEnabled"]; }
92    }
93
94    public IValueParameter<BoolValue> TiltedParameter {
95      get { return (IValueParameter<BoolValue>)Parameters["Tilted"]; }
96    }
97
98    /// <summary>
99    /// Enables that the current item can be rotated.
100    /// </summary>
101    public bool RotateEnabled {
102      get { return RotateEnabledParameter.Value.Value; }
103      set { RotateEnabledParameter.Value.Value = value; }
104    }
105
106    /// <summary>
107    /// Indicates that the current item is rotated.
108    /// If the item is also tilted it will be tilted first.
109    /// </summary>
110    public bool Rotated {
111      get { return RotatedParameter.Value.Value; }
112      set { RotatedParameter.Value.Value = value; }
113    }
114
115    /// <summary>
116    /// Enables that the current item can be tilted.
117    /// </summary>
118    public bool TiltEnabled {
119      get { return TiltEnabledParameter.Value.Value; }
120      set { TiltEnabledParameter.Value.Value = value; }
121    }
122
123    /// <summary>
124    /// Indicates that the current item is tilted.
125    /// Tilted means that the item is tilted sidewards.
126    /// If the item is also rotated it will be tilted first.
127    /// </summary>
128    public bool Tilted {
129      get { return TiltedParameter.Value.Value; }
130      set { TiltedParameter.Value.Value = value; }
131    }
132
133    public IFixedValueParameter<IntValue> LoadSecuringHeightParameter {
134      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringHeight"]; }
135    }
136
137    public IFixedValueParameter<IntValue> LoadSecuringWidthParameter {
138      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringWidth"]; }
139    }
140
141    public IFixedValueParameter<IntValue> LoadSecuringDepthParameter {
142      get { return (IFixedValueParameter<IntValue>)Parameters["LoadSecuringDepth"]; }
143    }
144
145    public int LoadSecuringHeight {
146      get { return LoadSecuringHeightParameter.Value.Value; }
147      set { LoadSecuringHeightParameter.Value.Value = value; }
148    }
149
150    public int LoadSecuringWidth {
151      get { return LoadSecuringWidthParameter.Value.Value; }
152      set { LoadSecuringWidthParameter.Value.Value = value; }
153    }
154
155    public int LoadSecuringDepth {
156      get { return LoadSecuringDepthParameter.Value.Value; }
157      set { LoadSecuringDepthParameter.Value.Value = value; }
158    }
159
160    /// <summary>
161    /// This property represents the height as needed in the bin packing.
162    /// </summary>
163    public new int Height {
164      get {
165        if (!Tilted) {
166          return HeightParameter.Value.Value + LoadSecuringHeightParameter.Value.Value;
167        } else {
168          return WidthParameter.Value.Value + LoadSecuringHeightParameter.Value.Value;
169        }
170      }
171    }
172
173    /// <summary>
174    /// This property represents the width as needed in the bin packing.
175    /// </summary>
176    public new int Width {
177      get {
178        if (Rotated) {
179          return DepthParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
180        } else {
181          if (!Tilted) {
182            return WidthParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
183          } else {
184            return HeightParameter.Value.Value + LoadSecuringWidthParameter.Value.Value;
185          }
186        }
187      }
188    }
189
190    /// <summary>
191    /// This property represents the depth as needed in the bin packing.
192    /// </summary>
193    public new int Depth {
194      get {
195        if (!Rotated) {
196          return DepthParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
197        } else {
198          if (!Tilted) {
199            return WidthParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
200          } else {
201            return HeightParameter.Value.Value + LoadSecuringDepthParameter.Value.Value;
202          }
203        }
204      }
205    }
206
207    /// <summary>
208    /// This property represents the height as it is seen in the view of the bin packing.
209    /// </summary>
210    public int HeightInView {
211      get {
212        if (!Tilted) {
213          return HeightParameter.Value.Value;
214        } else {
215          return WidthParameter.Value.Value;
216        }
217      }
218    }
219
220    /// <summary>
221    /// This property represents the width as it is seen in the view of the bin packing.
222    /// </summary>
223    public int WidthInView {
224      get {
225        if (Rotated) {
226          return DepthParameter.Value.Value;
227        } else {
228          if (!Tilted) {
229            return WidthParameter.Value.Value;
230          } else {
231            return HeightParameter.Value.Value;
232          }
233        }
234      }
235    }
236
237    /// <summary>
238    /// This property represents the depth as it is seen in the view of the bin packing.
239    /// </summary>
240    public int DepthInView {
241      get {
242        if (!Rotated) {
243          return DepthParameter.Value.Value;
244        } else {
245          if (!Tilted) {
246            return WidthParameter.Value.Value;
247          } else {
248            return HeightParameter.Value.Value;
249          }
250        }
251      }
252    }
253
254    /// <summary>
255    /// This property represents the original height.
256    /// </summary>
257    public int OriginalHeight {
258      get { return HeightParameter.Value.Value; }
259      set { HeightParameter.Value.Value = value; }
260    }
261
262    /// <summary>
263    /// This property represents the original width.
264    /// </summary>
265    public int OriginalWidth {
266      get { return WidthParameter.Value.Value; }
267      set { WidthParameter.Value.Value = value; }
268    }
269
270    /// <summary>
271    /// This property represents the original depth.
272    /// </summary>
273    public int OriginalDepth {
274      get { return DepthParameter.Value.Value; }
275      set { DepthParameter.Value.Value = value; }
276    }
277
278    public bool SupportsStacking(IPackingItem other) {
279      return ((other.Material < this.Material) || (other.Material.Equals(this.Material) && other.Weight <= this.Weight));
280    }
281    #endregion
282
283
284    [StorableConstructor]
285    protected PackingItem(bool deserializing) : base(deserializing) { }
286    protected PackingItem(PackingItem original, Cloner cloner)
287      : base(original, cloner) {
288      RegisterEvents();
289    }
290    public PackingItem()
291      : base() {
292      Parameters.Add(new ValueParameter<PackingShape>("TargetBin"));
293      Parameters.Add(new FixedValueParameter<DoubleValue>("Weight"));
294      Parameters.Add(new FixedValueParameter<IntValue>("Material"));
295
296      Parameters.Add(new FixedValueParameter<DoubleValue>("SupportedWeight"));     
297
298      Parameters.Add(new FixedValueParameter<BoolValue>("RotateEnabled"));
299      Parameters.Add(new FixedValueParameter<BoolValue>("Rotated"));
300      Parameters.Add(new FixedValueParameter<BoolValue>("TiltEnabled"));
301      Parameters.Add(new FixedValueParameter<BoolValue>("Tilted"));
302      Parameters.Add(new FixedValueParameter<BoolValue>("IsStackable"));
303
304      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringHeight"));
305      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringWidth"));
306      Parameters.Add(new FixedValueParameter<IntValue>("LoadSecuringDepth"));
307
308      IsStackabel = true;
309
310      RegisterEvents();
311    }
312
313    public PackingItem(int width, int height, int depth, PackingShape targetBin)
314      : this() {
315      this.OriginalWidth = width;
316      this.OriginalHeight = height;
317      this.OriginalDepth = depth;
318      this.TargetBin = (PackingShape)targetBin.Clone();
319    }
320
321    public PackingItem(int width, int height, int depth, PackingShape targetBin, double weight, int material)
322      : this(width, height, depth, targetBin) {
323      this.Weight = weight;
324      this.Material = material;
325    }
326
327
328
329    public PackingItem(PackingItem packingItem) : this() {
330      OriginalWidth = packingItem.OriginalWidth;
331      OriginalHeight = packingItem.OriginalHeight;
332      OriginalDepth = packingItem.OriginalDepth;
333      TargetBin = (PackingShape)packingItem.TargetBin.Clone();
334      Weight = packingItem.Weight;
335      Material = packingItem.Material;
336      Rotated = packingItem.Rotated;
337      Tilted = packingItem.Tilted;
338      IsStackabel = packingItem.IsStackabel;
339
340      LoadSecuringDepth = packingItem.LoadSecuringDepth;
341      LoadSecuringHeight = packingItem.LoadSecuringHeight;
342      LoadSecuringWidth = packingItem.LoadSecuringWidth;
343    }
344
345    [StorableHook(HookType.AfterDeserialization)]
346    private void AfterDeserialization() {
347      RegisterEvents();
348    }
349
350    public override IDeepCloneable Clone(Cloner cloner) {
351      return new PackingItem(this, cloner);
352    }
353
354    private void RegisterEvents() {
355      // NOTE: only because of ToString override
356      WeightParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
357      MaterialParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
358
359      // target bin does not occur in ToString()
360    }
361
362    public override string ToString() {
363      return string.Format("CuboidPackingItem ({0}, {1}, {2}; weight={3}, mat={4})", this.Width, this.Height, this.Depth, this.Weight, this.Material);
364    }
365
366  }
367}
Note: See TracBrowser for help on using the repository browser.