Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15471 was 15471, checked in by rhanghof, 6 years ago

#2817

  • Added some unit tests
  • Enhanced the documentation
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Core;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Problems.BinPacking3D {
28  [Item("Packing Position (3d)", "Represents a packing-position associated with a three dimensional packing-problem.")]
29  [StorableClass]
30  // PackingPosition is immutable (and handled as value type concerning Equals and GetHashCode)
31  public class PackingPosition : BinPacking.PackingPosition, IComparable<PackingPosition> {
32    [Storable]
33    private readonly int x;
34    [Storable]
35    private readonly int y;
36    [Storable]
37    private readonly int z;
38
39    public int X { get { return x; } }
40    public int Y { get { return y; } }
41    public int Z { get { return z; } }
42
43    [StorableConstructor]
44    protected PackingPosition(bool deserializing) : base(deserializing) { }
45    protected PackingPosition(PackingPosition original, Cloner cloner)
46      : base(original, cloner) {
47      x = original.X;
48      y = original.Y;
49      z = original.Z;
50    }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new PackingPosition(this, cloner);
53    }
54
55    public PackingPosition(int assignedBin, int x, int y, int z, bool rotated = false)
56      : base(assignedBin, rotated) {
57      this.x = x;
58      this.y = y;
59      this.z = z;
60    }
61
62    public override string ToString() {
63      return string.Format("[AssignedBin: {0}; ({1},{2},{3})]", AssignedBin, X, Y, Z);
64    }
65
66
67    public override bool Equals(object obj) {
68      var tdp = obj as PackingPosition;
69      if (tdp != null)
70        return (tdp.X == this.X && tdp.Y == this.Y && tdp.Z == this.Z);
71      else return false;
72    }
73
74    public override int GetHashCode() {
75      return base.GetHashCode() + 13 * X + 17 * Y + 23 * Z;
76    }
77
78    [Obsolete]
79    public static PackingPosition MoveLeft(PackingPosition original) {
80      return new PackingPosition(original.AssignedBin, original.X - 1, original.Y, original.Z, original.Rotated);
81    }
82    [Obsolete]
83    public static PackingPosition MoveDown(PackingPosition original) {
84      return new PackingPosition(original.AssignedBin, original.X, original.Y - 1, original.Z, original.Rotated);
85    }
86    [Obsolete]
87    public static PackingPosition MoveBack(PackingPosition original) {
88      return new PackingPosition(original.AssignedBin, original.X, original.Y, original.Z - 1, original.Rotated);
89    }
90    [Obsolete]
91    public static PackingPosition MoveRight(PackingPosition original) {
92      return new PackingPosition(original.AssignedBin, original.X + 1, original.Y, original.Z, original.Rotated);
93    }
94    [Obsolete]
95    public static PackingPosition MoveUp(PackingPosition original) {
96      return new PackingPosition(original.AssignedBin, original.X, original.Y + 1, original.Z, original.Rotated);
97    }
98    [Obsolete]
99    public static PackingPosition MoveFront(PackingPosition original) {
100      return new PackingPosition(original.AssignedBin, original.X, original.Y, original.Z + 1, original.Rotated);
101    }
102
103    #region IComparable<PackingPosition> Members
104
105    /// <summary>
106    /// Compares two packing positions by their coordinates.
107    /// The order of comparing is y (height) -> z (depth) -> x (width).
108    /// </summary>
109    /// <param name="other"></param>
110    /// <returns></returns>
111    public int CompareTo(PackingPosition other) {
112      int result = Y.CompareTo(other.Y);
113      if (result == 0)
114        result = Z.CompareTo(other.Z);
115      if (result == 0)
116        result = X.CompareTo(other.X);
117      /*
118      int result = Z.CompareTo(other.Z);
119      if (result == 0)
120        result = X.CompareTo(other.X);
121      if (result == 0)
122        result = Y.CompareTo(other.Y);
123      */
124      return result;
125
126    }
127
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.