1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Collections;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
8 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
9 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.BinPacking.Decoders {
|
---|
12 | public static class PackingHeuristics {
|
---|
13 | public static TwoDimensionalPacking BottomLeftPosition(RectangularPackingBin binMeasures, int binNr, RectangularPackingItem currentItem, ObservableDictionary<int, TwoDimensionalPacking> itemPositions, ItemList<RectangularPackingItem> itemMeasures) {
|
---|
14 | //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
|
---|
15 | TwoDimensionalPacking currentPosition = new TwoDimensionalPacking(binNr, binMeasures.Width - currentItem.Width, binMeasures.Height - currentItem.Height);
|
---|
16 | //Slide the item as far as possible to the left
|
---|
17 | while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)
|
---|
18 | || IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures)) {
|
---|
19 | //Slide the item as far as possible to the bottom
|
---|
20 | while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures)) {
|
---|
21 | currentPosition = MoveDown(currentPosition);
|
---|
22 | }
|
---|
23 | if (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures))
|
---|
24 | currentPosition = MoveLeft(currentPosition);
|
---|
25 | }
|
---|
26 |
|
---|
27 | return IsPositionFeasible(binMeasures, binNr, currentItem, currentPosition, itemPositions, itemMeasures) ? currentPosition : null;
|
---|
28 | }
|
---|
29 |
|
---|
30 | private static bool IsPositionFeasible(RectangularPackingBin binMeasures, int binNr, RectangularPackingItem currentItem,
|
---|
31 | TwoDimensionalPacking currentPosition, ObservableDictionary<int, TwoDimensionalPacking> itemPositions, ItemList<RectangularPackingItem> itemMeasures) {
|
---|
32 | //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
|
---|
33 | if (!binMeasures.Encloses(currentPosition, currentItem))
|
---|
34 | return false;
|
---|
35 | var itemsWithPositions = itemMeasures.
|
---|
36 | Select((Value, Index) => new { Value, Index }).
|
---|
37 | Where(s => itemPositions.ContainsKey(s.Index)).
|
---|
38 | Select((s) => new { Measures = itemMeasures[s.Index], Position = itemPositions[s.Index] });
|
---|
39 |
|
---|
40 | foreach (var itemWithPosition in itemsWithPositions) {
|
---|
41 | if (itemWithPosition.Position.AssignedBin == binNr && itemWithPosition.Measures.Overlaps(itemWithPosition.Position, currentPosition, currentItem))
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private static TwoDimensionalPacking MoveLeft(TwoDimensionalPacking original) {
|
---|
49 | return new TwoDimensionalPacking(original.AssignedBin, original.X - 1, original.Y);
|
---|
50 | }
|
---|
51 | private static TwoDimensionalPacking MoveDown(TwoDimensionalPacking original) {
|
---|
52 | return new TwoDimensionalPacking(original.AssignedBin, original.X, original.Y - 1);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | public static ThreeDimensionalPacking DeepestLeftBottomPosition(CuboidPackingBin binMeasures, int binNr, CuboidPackingItem currentItem, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, ItemList<CuboidPackingItem> itemMeasures) {
|
---|
61 | //Starting-position at upper right corner (=left bottom point of item-rectangle is at position item.width,item.height)
|
---|
62 | ThreeDimensionalPacking currentPosition = new ThreeDimensionalPacking(binNr, binMeasures.Width - currentItem.Width, binMeasures.Height - currentItem.Height, binMeasures.Depth - currentItem.Depth);
|
---|
63 |
|
---|
64 | //Slide the item as far as possible to the bottom
|
---|
65 | while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures)
|
---|
66 | || IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)
|
---|
67 | || IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
|
---|
68 | //Slide the item as far as possible to the left
|
---|
69 | while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures)
|
---|
70 | || IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
|
---|
71 | //Slide the item as far as possible to the back
|
---|
72 | while (IsPositionFeasible(binMeasures, binNr, currentItem, MoveBack(currentPosition), itemPositions, itemMeasures)) {
|
---|
73 | currentPosition = MoveBack(currentPosition);
|
---|
74 | }
|
---|
75 | if (IsPositionFeasible(binMeasures, binNr, currentItem, MoveLeft(currentPosition), itemPositions, itemMeasures))
|
---|
76 | currentPosition = MoveLeft(currentPosition);
|
---|
77 | }
|
---|
78 | if (IsPositionFeasible(binMeasures, binNr, currentItem, MoveDown(currentPosition), itemPositions, itemMeasures))
|
---|
79 | currentPosition = MoveDown(currentPosition);
|
---|
80 | }
|
---|
81 |
|
---|
82 | return IsPositionFeasible(binMeasures, binNr, currentItem, currentPosition, itemPositions, itemMeasures) ? currentPosition : null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private static bool IsPositionFeasible(CuboidPackingBin binMeasures, int binNr, CuboidPackingItem currentItem,
|
---|
86 | ThreeDimensionalPacking currentPosition, ObservableDictionary<int, ThreeDimensionalPacking> itemPositions, ItemList<CuboidPackingItem> itemMeasures) {
|
---|
87 | //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
|
---|
88 | if (!binMeasures.Encloses(currentPosition, currentItem))
|
---|
89 | return false;
|
---|
90 |
|
---|
91 | var itemsWithPositions = itemMeasures.
|
---|
92 | Select((Value, Index) => new { Value, Index }).
|
---|
93 | Where(s => itemPositions.ContainsKey(s.Index)).
|
---|
94 | Select((s) => new { Measures = itemMeasures[s.Index], Position = itemPositions[s.Index] });
|
---|
95 |
|
---|
96 | foreach (var itemWithPosition in itemsWithPositions) {
|
---|
97 | if (itemWithPosition.Position.AssignedBin == binNr && itemWithPosition.Measures.Overlaps(itemWithPosition.Position, currentPosition, currentItem))
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static ThreeDimensionalPacking MoveLeft(ThreeDimensionalPacking original) {
|
---|
105 | return new ThreeDimensionalPacking(original.AssignedBin, original.X - 1, original.Y, original.Z);
|
---|
106 | }
|
---|
107 | private static ThreeDimensionalPacking MoveDown(ThreeDimensionalPacking original) {
|
---|
108 | return new ThreeDimensionalPacking(original.AssignedBin, original.X, original.Y - 1, original.Z);
|
---|
109 | }
|
---|
110 | private static ThreeDimensionalPacking MoveBack(ThreeDimensionalPacking original) {
|
---|
111 | return new ThreeDimensionalPacking(original.AssignedBin, original.X, original.Y, original.Z - 1);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|