Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/BinPacking.cs @ 9598

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

#1966: Fixed a bug which caused the stacking-contraint to be wrongly enforced; Did some cleanup;

File size: 5.4 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.Problems.BinPacking.PackingItem;
28using HeuristicLab.Problems.BinPacking.Shapes;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Core;
31using HeuristicLab.Common;
32using HeuristicLab.Parameters;
33using HeuristicLab.Data;
34using HeuristicLab.Collections;
35using HeuristicLab.Problems.BinPacking.Dimensions;
36using HeuristicLab.Problems.BinPacking.PackingBin;
37
38namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
39  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
40  [StorableClass]
41  public abstract class BinPacking<D,B,I>  : Item
42    where D : class, IPackingDimensions
43    where B : PackingShape<D>, IPackingBin
44    where I : PackingShape<D>, IPackingItem {
45    #region Properties
46    [Storable]
47    public ObservableDictionary<int, D> ItemPositions { get; private set; }
48
49    [Storable]
50    public ObservableDictionary<int, I> ItemMeasures { get; private set; }
51
52    [Storable]
53    public B BinMeasures { get; private set; }
54
55    [Storable]
56    public HashSet<D> ExtremePoints { get; protected set; }
57
58    //[Storable]
59    //public OccupiedPoints<D, B, I> OccupiedPoints { get; protected set; }
60    #endregion Properties
61
62    public BinPacking(B binMeasures) : base() {   
63      ItemPositions = new ObservableDictionary<int, D>();
64      ItemMeasures = new ObservableDictionary<int, I>();
65      BinMeasures = (B)binMeasures.Clone();
66      ExtremePoints = new HashSet<D>();
67      ExtremePoints.Add(binMeasures.Origin);
68    }
69
70    [StorableConstructor]
71    protected BinPacking(bool deserializing) : base(deserializing) { }
72    protected BinPacking(BinPacking<D,B,I> original, Cloner cloner)
73      : base(original, cloner) {
74      this.ItemPositions = new ObservableDictionary<int, D>(original.ItemPositions);
75      this.ItemMeasures = new ObservableDictionary<int, I>(original.ItemMeasures);
76        this.BinMeasures = (B)original.BinMeasures.Clone(cloner);
77    }
78   
79    protected abstract void GenerateNewExtremePointsForNewItem(I measures, D position);
80
81    public abstract D FindExtremePointForItem(I measures, bool rotated, bool stackingConstraints);
82    public abstract D FindPositionBySliding(I measures, bool rotated);
83
84    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures);
85    public abstract void SlidingBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, Dictionary<int, bool> rotationArray);
86    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints);
87    public abstract void ExtremePointBasedPacking(ref List<int> sequence, ItemList<I> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray);
88
89    public void PackItem(int itemID, I measures, D position) {
90      ItemMeasures[itemID] = measures;
91      ItemPositions[itemID] = position;
92      ExtremePoints.Remove(position);
93      GenerateNewExtremePointsForNewItem(measures, position);
94      //OccupiedPoints.OccupyPoints(measures, position, itemID);
95    }
96
97    public double PackingDensity {
98      get {
99        double result = 0;
100        foreach (var entry in ItemMeasures)
101          result += entry.Value.MultipliedMeasures;
102        result /= BinMeasures.MultipliedMeasures;
103        return result;
104      }
105    }
106
107
108    public int PointOccupation(D position) {
109      foreach (var ipEntry in ItemPositions) {
110        if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
111          return ipEntry.Key;
112      }
113      return -1;
114    }
115    public bool IsPointOccupied(D position) {
116      foreach (var ipEntry in ItemPositions) {
117        if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
118          return true;
119      }
120      return false;
121    }
122    public bool IsPositionFeasible(I currentItem, D position) {
123      //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the point is supported by something; 3. the item does not collide with another already packed item
124      if (!BinMeasures.Encloses(position, currentItem))
125        return false;
126
127      foreach (var ipEntry in ItemPositions) {
128        if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, currentItem))
129          return false;
130      }
131
132      return true;
133    }
134    public abstract int ShortestPossibleSideFromPoint (D position);
135    public abstract bool IsStaticStable (I item, D position);
136  }   
137}
Note: See TracBrowser for help on using the repository browser.