Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Dimensions/OccupiedPoints2D.cs @ 9593

Last change on this file since 9593 was 9593, checked in by jhelm, 11 years ago

#1966: Applied some heavy refactoring on the decoder-classes and cleaned up the code a bit;

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.BinPacking.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Common;
30using HeuristicLab.Problems.BinPacking.Shapes;
31using HeuristicLab.Problems.BinPacking.PackingItem;
32using HeuristicLab.Problems.BinPacking.PackingBin;
33
34namespace HeuristicLab.Problems.BinPacking.Dimensions {
35  [Item("Occupated Points 2D", "A datastructure holding all currently occupied points of a 2D packing-bin.")]
36  [StorableClass]
37  public class OccupiedPoints2D : OccupiedPoints<TwoDimensionalPacking, RectangularPackingItem> {
38                   
39    [Storable]
40    private int[,] occupiedPoints { get; set; }
41
42    public OccupiedPoints2D(RectangularPackingBin binMeasures) : base() {
43      occupiedPoints = new int[binMeasures.Width, binMeasures.Height];
44      for (int w = 0; w < binMeasures.Width; w++) {
45        for (int h = 0; h < binMeasures.Height; h++) {
46          occupiedPoints[w, h] = -1;
47        }
48      }
49    }
50    [StorableConstructor]
51    protected OccupiedPoints2D(bool deserializing) : base(deserializing) { }
52    protected OccupiedPoints2D(OccupiedPoints2D original, Cloner cloner)
53      : base(original, cloner) {
54      this.occupiedPoints = (int[,])original.occupiedPoints.Clone();
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new OccupiedPoints2D(this, cloner);
58    }
59
60    public override bool IsPositionFeasible(RectangularPackingItem measures, TwoDimensionalPacking position) {
61      for (int w = position.X; w < position.X + measures.Width; w++) {
62        for (int h = position.Y; h < position.Y + measures.Height; h++) {
63          if (occupiedPoints[w, h] != -1)
64            return false;
65        }
66      }
67      return true;
68    }
69
70    public override void OccupyPoints(RectangularPackingItem measures, TwoDimensionalPacking position, int itemID) {
71      int width = position.Rotated ? measures.Height : measures.Width;
72      int height = position.Rotated ? measures.Width : measures.Height;
73      for (int w = position.X; w < position.X + width; w++) {
74        for (int h = position.Y; h < position.Y + height; h++) {
75          occupiedPoints[w, h] = itemID;
76        }
77      }
78    }
79
80    public override bool IsPointOccupied(TwoDimensionalPacking point) {
81      return occupiedPoints[point.X, point.Y] != -1;
82    }
83
84    public override int ShortestPossibleSideFromEP(TwoDimensionalPacking ep) {
85      int shortestSide = int.MaxValue;
86      int width = occupiedPoints.GetLength(0);
87      int height = occupiedPoints.GetLength(1);
88
89      if (ep.X >= width || ep.Y >= height)
90        return shortestSide;
91
92      int currentX = ep.X;
93      while (currentX < width && occupiedPoints[currentX, ep.Y] == -1) { currentX++; }
94      if (currentX - ep.X < shortestSide)
95        shortestSide = currentX - ep.X;
96
97      int currentY = ep.Y;
98      while (currentY < height && occupiedPoints[ep.X, currentY] == -1) { currentY++; }
99      if (currentY - ep.Y < shortestSide)
100        shortestSide = currentY - ep.Y;
101
102      return shortestSide;
103    }
104
105    public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking ep) {
106      //For the current implementation there was no need for stacking-constraints in the 2D-case.
107      //But it could be interesting for shelf-packing applications!
108      return true;
109    }
110    public override bool WeightIsSupported(RectangularPackingItem item, TwoDimensionalPacking ep, ItemList<RectangularPackingItem> items) {
111      return true;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.