Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.Problems.BinPacking/3.3/2D/PackingPosition.cs @ 15852

Last change on this file since 15852 was 14162, checked in by gkronber, 8 years ago

#2641: first import of bin packing problem (TODO: persistence and sample)

File size: 3.5 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.BinPacking2D {
28  [Item("Packing Position (2d)", "Represents a packing-position associated with a two dimensional packing-problem.")]
29  [StorableClass]
30  // PackingPosition is an immutable class (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    public int X { get { return x; } }
35
36    [Storable]
37    private readonly int y;
38    public int Y { get { return y; } }
39
40    [StorableConstructor]
41    protected PackingPosition(bool deserializing) : base(deserializing) { }
42    protected PackingPosition(PackingPosition original, Cloner cloner)
43      : base(original, cloner) {
44      x = original.X;
45      y = original.Y;
46    }
47
48    public PackingPosition(int assignedBin, int x, int y, bool rotated = false)
49      : base(assignedBin, rotated) {
50      this.x = x;
51      this.y = y;
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new PackingPosition(this, cloner);
56    }
57
58    public override string ToString() {
59      return string.Format("[AssignedBin: {0}; ({1},{2})]", AssignedBin, x, y);
60    }
61
62    public override bool Equals(object obj) {
63      var other = obj as PackingPosition;
64      if (other != null)
65        return (other.X == this.X && other.Y == this.Y && base.Equals(other));
66      else return false;
67    }
68
69    public override int GetHashCode() {
70      return base.GetHashCode() + 13 * X + 17 * Y;
71    }
72
73    public static PackingPosition MoveLeft(PackingPosition original) {
74      return new PackingPosition(original.AssignedBin, original.X - 1, original.Y, original.Rotated);
75    }
76    public static PackingPosition MoveDown(PackingPosition original) {
77      return new PackingPosition(original.AssignedBin, original.X, original.Y - 1, original.Rotated);
78    }
79
80    public static PackingPosition MoveRight(PackingPosition original) {
81      return new PackingPosition(original.AssignedBin, original.X + 1, original.Y, original.Rotated);
82    }
83    public static PackingPosition MoveUp(PackingPosition original) {
84      return new PackingPosition(original.AssignedBin, original.X, original.Y + 1, original.Rotated);
85    }
86
87
88    #region IComparable<PackingPosition> Members
89
90    public int CompareTo(PackingPosition other) {
91      int result = X.CompareTo(other.X);
92      if (result == 0)
93        result = Y.CompareTo(other.Y);
94
95      return result;
96    }
97
98    #endregion
99  }
100}
Note: See TracBrowser for help on using the repository browser.