Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/CuboidPackingShape.cs @ 13599

Last change on this file since 13599 was 13574, checked in by gkronber, 9 years ago

#1966: changed PackingShapes to ParameterizedItems

File size: 7.2 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 System;
23using System.Diagnostics;
24using HeuristicLab.Problems.BinPacking.Interfaces;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.BinPacking.Dimensions;
31
32namespace HeuristicLab.Problems.BinPacking.Shapes {
33  [Item("CuboidPackingShape", "Represents the cuboid measures (width, height, depth) of a three-dimensional cuboidic bin-packing object.")]
34  [StorableClass]
35  public abstract class CuboidPackingShape : PackingShape<ThreeDimensionalPacking>, IRegularPackingShape, IComparable<CuboidPackingShape> {
36    #region Properties
37    public int Height {
38      get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
39      set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
40    }
41
42    public int Width {
43      get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
44      set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
45    }
46
47    public int Depth {
48      get { return ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value; }
49      set { ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value = value; }
50    }
51
52    #endregion
53
54    #region Helpers
55    public override ThreeDimensionalPacking Origin { get { return new ThreeDimensionalPacking(0, 0, 0, 0); } }
56    public override int MultipliedMeasures { get { return Width * Height * Depth; } }
57
58    public override bool EnclosesPoint(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPoint) {
59      return (myPosition.X <= checkedPoint.X &&
60                (myPosition.X + (myPosition.Rotated ? Depth : Width) - 1) >= checkedPoint.X &&
61                myPosition.Y <= checkedPoint.Y &&
62                (myPosition.Y + Height - 1) >= checkedPoint.Y &&
63                myPosition.Z <= checkedPoint.Z &&
64                (myPosition.Z + (myPosition.Rotated ? Width : Depth) - 1) >= checkedPoint.Z);
65    }
66    public override bool Encloses(ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
67      return Encloses(checkedPosition, (CuboidPackingShape)checkedShape);
68    }
69    private bool Encloses(ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
70      return Encloses(new CuboidDiagonal(this), new CuboidDiagonal(checkedPosition, checkedShape));
71    }
72    private bool Encloses(CuboidDiagonal c1, CuboidDiagonal c2) {
73      return (c1.x1 <= c2.x1 &&
74                c1.x2 >= c2.x2 &&
75                c1.y1 <= c2.y1 &&
76                c1.y2 >= c2.y2 &&
77                c1.z1 <= c2.z1 &&
78                c1.z2 >= c2.z2);
79    }
80
81    public override bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
82      return Overlaps(myPosition, checkedPosition, (CuboidPackingShape)checkedShape);
83    }
84    private bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
85      return Overlaps(new CuboidDiagonal(myPosition, this), new CuboidDiagonal(checkedPosition, checkedShape));
86    }
87    private bool Overlaps(CuboidDiagonal c1, CuboidDiagonal c2) {
88      return !(c1.x1 > c2.x2 ||
89               c1.y1 > c2.y2 ||
90               c1.z1 > c2.z2 ||
91               c1.x2 < c2.x1 ||
92               c1.y2 < c2.y1 ||
93               c1.z2 < c2.z1);
94    }
95
96    public void ApplyHorizontalOrientation() {
97      if (Width > Depth) {
98        var aux = Width;
99        Width = Depth;
100        Depth = aux;
101      }
102    }
103    #endregion
104
105
106
107    public override void InitializeFromMeasures(int[] measures) {
108      if (measures.Length != 3)
109        throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
110      this.Width = measures[0];
111      this.Height = measures[1];
112      this.Depth = measures[2];
113    }
114    public override int[] ToArray() {
115      return new int[] { Width, Height, Depth };
116    }
117
118
119    protected CuboidPackingShape()
120      : base() {
121      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
122      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
123      Parameters.Add(new FixedValueParameter<IntValue>("Depth"));
124    }
125
126    protected CuboidPackingShape(int width, int height, int depth)
127      : this() {
128      this.Width = width;
129      this.Height = height;
130      this.Depth = depth;
131    }
132
133    [StorableConstructor]
134    protected CuboidPackingShape(bool deserializing) : base(deserializing) { }
135    protected CuboidPackingShape(CuboidPackingShape original, Cloner cloner)
136      : base(original, cloner) {
137    }
138
139    public override string ToString() {
140      return String.Format("CuboidPackingShape ({0}, {1}, {2})", this.Width, this.Height, this.Depth);
141    }
142
143    #region IComparable Members
144
145    public int CompareTo(CuboidPackingShape other) {
146      //Using "Clustered-Area-Height"-comparison as descr
147
148      int result = (this.Width * this.Depth).CompareTo(other.Width * other.Depth);
149
150      if (result == 0)
151        result = this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
152      if (result == 0)
153        result = this.Height.CompareTo(other.Height);
154      return result;
155    }
156
157    public int CompareTo(object obj) {
158      var other = (CuboidPackingShape)obj;
159      if (other != null) return CompareTo(other);
160      else throw new ArgumentException(string.Format("Cannot compare with object {0}", obj), "obj");
161    }
162
163    #endregion
164
165    private struct CuboidDiagonal {
166      public int x1;
167      public int y1;
168      public int z1;
169      public int x2;
170      public int y2;
171      public int z2;
172      public CuboidDiagonal(CuboidPackingShape myShape) : this(new ThreeDimensionalPacking(0, 0, 0, 0), myShape) { }
173      public CuboidDiagonal(ThreeDimensionalPacking myPosition, CuboidPackingShape myShape) {
174        x1 = myPosition.X;
175        y1 = myPosition.Y;
176        z1 = myPosition.Z;
177        x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
178        y2 = myPosition.Y + myShape.Height - 1;
179        z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
180      }
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.