Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.BinPacking/3.3/3D/PackingItem.cs @ 16226

Last change on this file since 16226 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.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    public IValueParameter<PackingShape> TargetBinParameter {
34      get { return (IValueParameter<PackingShape>)Parameters["TargetBin"]; }
35    }
36    public IFixedValueParameter<DoubleValue> WeightParameter {
37      get { return (IFixedValueParameter<DoubleValue>)Parameters["Weight"]; }
38    }
39    public IFixedValueParameter<IntValue> MaterialParameter {
40      get { return (IFixedValueParameter<IntValue>)Parameters["Material"]; }
41    }
42
43    public PackingShape TargetBin {
44      get { return TargetBinParameter.Value; }
45      set { TargetBinParameter.Value = value; }
46    }
47
48    public double Weight {
49      get { return WeightParameter.Value.Value; }
50      set { WeightParameter.Value.Value = value; }
51    }
52
53    public int Material {
54      get { return MaterialParameter.Value.Value; }
55      set { MaterialParameter.Value.Value = value; }
56    }
57
58    public bool SupportsStacking(IPackingItem other) {
59      return ((other.Material < this.Material) || (other.Material.Equals(this.Material) && other.Weight <= this.Weight));
60    }
61
62    [StorableConstructor]
63    protected PackingItem(bool deserializing) : base(deserializing) { }
64    protected PackingItem(PackingItem original, Cloner cloner)
65      : base(original, cloner) {
66      RegisterEvents();
67    }
68    public PackingItem()
69      : base() {
70      Parameters.Add(new ValueParameter<PackingShape>("TargetBin"));
71      Parameters.Add(new FixedValueParameter<DoubleValue>("Weight"));
72      Parameters.Add(new FixedValueParameter<IntValue>("Material"));
73
74      RegisterEvents();
75    }
76
77    public PackingItem(int width, int height, int depth, PackingShape targetBin, double weight, int material)
78      : this() {
79      this.Width = width;
80      this.Height = height;
81      this.Depth = depth;
82      this.Weight = weight;
83      this.Material = material;
84      this.TargetBin = (PackingShape)targetBin.Clone();
85    }
86
87    public PackingItem(int width, int height, int depth, PackingShape targetBin)
88      : this() {
89      this.Width = width;
90      this.Height = height;
91      this.Depth = depth;
92      this.TargetBin = (PackingShape)targetBin.Clone();
93    }
94
95    [StorableHook(HookType.AfterDeserialization)]
96    private void AfterDeserialization() {
97      RegisterEvents();
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new PackingItem(this, cloner);
102    }
103
104    private void RegisterEvents() {
105      // NOTE: only because of ToString override
106      WeightParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
107      MaterialParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
108
109      // target bin does not occur in ToString()
110    }
111
112
113    public override string ToString() {
114      return string.Format("CuboidPackingItem ({0}, {1}, {2}; weight={3}, mat={4})", this.Width, this.Height, this.Depth, this.Weight, this.Material);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.