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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
27 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
28 | using HeuristicLab.Problems.BinPacking.Shapes;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Common;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 | using HeuristicLab.Collections;
|
---|
35 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
36 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
37 |
|
---|
38 | namespace 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 | public bool IsPointOccupied(D position) {
|
---|
108 | foreach (var ipEntry in ItemPositions) {
|
---|
109 | if (ItemMeasures[ipEntry.Key].EnclosesPoint(ipEntry.Value, position))
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | public bool IsPositionFeasible(I currentItem, D position) {
|
---|
116 | //In this case feasability is defined as following: 1. the item fits into the bin-borders; 2. the item does not collide with another already packed item
|
---|
117 | if (!BinMeasures.Encloses(position, currentItem))
|
---|
118 | return false;
|
---|
119 |
|
---|
120 | foreach (var ipEntry in ItemPositions) {
|
---|
121 | if (ItemMeasures[ipEntry.Key].Overlaps(ipEntry.Value, position, currentItem))
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 | public abstract int ShortestPossibleSideFromPoint (D position);
|
---|
128 | public abstract bool IsStaticStable (I item, D position);
|
---|
129 |
|
---|
130 | //public bool IsPositionFeasible1(I currentItem, D position) {
|
---|
131 | // if (!BinMeasures.Encloses(position, currentItem))
|
---|
132 | // return false;
|
---|
133 |
|
---|
134 | // return OccupiedPoints.IsPositionFeasible (currentItem, position);
|
---|
135 | //}
|
---|
136 | }
|
---|
137 | }
|
---|