1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 |
|
---|
30 | namespace 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 |
|
---|
51 |
|
---|
52 | public double PackingDensity {
|
---|
53 | get {
|
---|
54 | double result = 0;
|
---|
55 | foreach (var entry in Items)
|
---|
56 | result += entry.Value.Volume;
|
---|
57 | result /= BinShape.Volume;
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public int FreeVolume {
|
---|
63 | get { return BinShape.Volume - Items.Sum(x => x.Value.Volume); }
|
---|
64 | }
|
---|
65 | #endregion Properties
|
---|
66 |
|
---|
67 | protected BinPacking(TBin binShape)
|
---|
68 | : base() {
|
---|
69 | Positions = new ObservableDictionary<int, TPos>();
|
---|
70 | Items = new ObservableDictionary<int, TItem>();
|
---|
71 | BinShape = (TBin)binShape.Clone();
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | protected BinPacking(bool deserializing) : base(deserializing) { }
|
---|
76 |
|
---|
77 | protected BinPacking(BinPacking<TPos, TBin, TItem> original, Cloner cloner)
|
---|
78 | : base(original, cloner) {
|
---|
79 | this.Positions = new ObservableDictionary<int, TPos>();
|
---|
80 | foreach (var kvp in original.Positions) {
|
---|
81 | Positions.Add(kvp.Key, cloner.Clone(kvp.Value));
|
---|
82 | }
|
---|
83 | this.Items = new ObservableDictionary<int, TItem>();
|
---|
84 | foreach (var kvp in original.Items) {
|
---|
85 | Items.Add(kvp.Key, cloner.Clone(kvp.Value));
|
---|
86 | }
|
---|
87 | this.BinShape = (TBin)original.BinShape.Clone(cloner);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// Generate new extreme points for a given item
|
---|
92 | /// </summary>
|
---|
93 | /// <param name="item"></param>
|
---|
94 | /// <param name="position"></param>
|
---|
95 | protected abstract void GenerateNewExtremePointsForNewItem(TItem item, TPos position);
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Packs an item into the bin packing
|
---|
99 | /// </summary>
|
---|
100 | /// <param name="itemID"></param>
|
---|
101 | /// <param name="item"></param>
|
---|
102 | /// <param name="position"></param>
|
---|
103 | public abstract void PackItem(int itemID, TItem item, TPos position);
|
---|
104 |
|
---|
105 | /// <summary>
|
---|
106 | /// Checks if the given position is feasible for the given item
|
---|
107 | /// </summary>
|
---|
108 | /// <param name="item"></param>
|
---|
109 | /// <param name="position"></param>
|
---|
110 | /// <param name="stackingConstraints"></param>
|
---|
111 | /// <returns>Returns true if the given position is feasible for the given item</returns>
|
---|
112 | public abstract bool IsPositionFeasible(TItem item, TPos position, bool stackingConstraints);
|
---|
113 |
|
---|
114 | /// <summary>
|
---|
115 | /// Checks if the given item is static stable on the given position
|
---|
116 | /// </summary>
|
---|
117 | /// <param name="measures">Item</param>
|
---|
118 | /// <param name="position">Position of the item</param>
|
---|
119 | /// <returns>Returns true if the given item is static stable on the given position</returns>
|
---|
120 | public abstract bool IsStaticStable(TItem measures, TPos position);
|
---|
121 |
|
---|
122 |
|
---|
123 | }
|
---|
124 | }
|
---|