source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/BinPacking.cs @ 15473

Last change on this file since 15473 was 15473, checked in by rhanghof, 23 months ago

#2817

  • Added unit tests
  • Refactoring of bp 3D
File size: 4.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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
28using HeuristicLab.Collections;
29
30namespace HeuristicLab.Problems.BinPacking {
31  [Item("BinPacking", "Represents a single-bin packing for a bin-packing problem.")]
32  [StorableClass]
33  public abstract class BinPacking<TPos, TBin, TItem> : Item
34    where TPos : class, IPackingPosition
35    where TBin : PackingShape<TPos>
36    where TItem : PackingShape<TPos> {
37    #region Properties
38    [Storable]
39
40    //key = item id
41    public ObservableDictionary<int, TPos> Positions { get; private set; }
42
43    [Storable]
44    //key = item id
45    public ObservableDictionary<int, TItem> Items { get; private set; }
46
47    [Storable]
48    public TBin BinShape { get; private set; }
49
50    [Storable]
51    public SortedSet<TPos> ExtremePoints { get; protected set; }
52
53    public double PackingDensity {
54      get {
55        double result = 0;
56        foreach (var entry in Items)
57          result += entry.Value.Volume;
58        result /= BinShape.Volume;
59        return result;
60      }
61    }
62
63    public int FreeVolume {
64      get { return BinShape.Volume - Items.Sum(x => x.Value.Volume); }
65    }
66    #endregion Properties
67   
68    protected BinPacking(TBin binShape)
69      : base() {
70      Positions = new ObservableDictionary<int, TPos>();
71      Items = new ObservableDictionary<int, TItem>();
72      BinShape = (TBin)binShape.Clone();
73      ExtremePoints = new SortedSet<TPos>();
74    }
75
76    [StorableConstructor]
77    protected BinPacking(bool deserializing) : base(deserializing) { }
78
79    protected BinPacking(BinPacking<TPos, TBin, TItem> original, Cloner cloner)
80      : base(original, cloner) {
81      this.Positions = new ObservableDictionary<int, TPos>();
82      foreach (var kvp in original.Positions) {
83        Positions.Add(kvp.Key, cloner.Clone(kvp.Value));
84      }
85      this.Items = new ObservableDictionary<int, TItem>();
86      foreach (var kvp in original.Items) {
87        Items.Add(kvp.Key, cloner.Clone(kvp.Value));
88      }
89      this.BinShape = (TBin)original.BinShape.Clone(cloner);
90      this.ExtremePoints = new SortedSet<TPos>(original.ExtremePoints.Select(p => cloner.Clone(p)));
91    }
92   
93    /// <summary>
94    /// Generate new extreme points for a given item
95    /// </summary>
96    /// <param name="item"></param>
97    /// <param name="position"></param>
98    protected abstract void GenerateNewExtremePointsForNewItem(TItem item, TPos position);
99   
100    /// <summary>
101    /// Packs an item into the bin packing
102    /// </summary>
103    /// <param name="itemID"></param>
104    /// <param name="item"></param>
105    /// <param name="position"></param>
106    public abstract void PackItem(int itemID, TItem item, TPos position);
107   
108    /// <summary>
109    /// Checks if the given position is feasible for the given item
110    /// </summary>
111    /// <param name="item"></param>
112    /// <param name="position"></param>
113    /// <param name="stackingConstraints"></param>
114    /// <returns>Returns true if the given position is feasible for the given item</returns>
115    public abstract bool IsPositionFeasible(TItem item, TPos position, bool stackingConstraints);
116
117    /// <summary>
118    /// Checks if the given item is static stable on the given position
119    /// </summary>
120    /// <param name="measures">Item</param>
121    /// <param name="position">Position of the item</param>
122    /// <returns>Returns true if the given item is static stable on the given position</returns>
123    public abstract bool IsStaticStable(TItem measures, TPos position);
124   
125   
126  }
127}
Note: See TracBrowser for help on using the repository browser.