-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForce.java
More file actions
226 lines (199 loc) · 8.94 KB
/
Copy pathBruteForce.java
File metadata and controls
226 lines (199 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Comparator;
public class BruteForce extends StripPacking {
public int maxHeight;
public int bestHeight;
public ArrayList<Rect> bestSol;
public int n;
private int wideWidth;
public static Comparator<FloatingRect> decreasingWidth = (fr1,fr2) -> {
if (fr1.width < fr2.width) return 1;
if (fr1.width > fr2.width) return -1;
return 0;
};
public static Comparator<FloatingRect> decreasingWidthHeight = (fr1,fr2) -> {
if (fr1.width*fr1.height < fr2.width*fr2.height) return 1;
if (fr1.width*fr1.height > fr2.width*fr2.height) return -1;
return 0;
};
public BruteForce(FloatingRect[] floatingRects, int width) {
super(floatingRects, width);
processFrects();
wideWidth = width/2 + 1;
// maxHeight is sum of all rectangle heights plus one. nothing goes to this height.
maxHeight = Arrays.stream(floatingRects).map(floatingRect -> floatingRect.height).reduce(1, (a, b) -> a + b);
System.out.println("Add all heights: " + maxHeight + " - 1");
StripPacking algo = new FirstFitDecreasingHeight(floatingRects, width);
algo.execute();
System.out.println("FFDH: " + (algo.height + 1) + " - 1");
maxHeight = Math.min(maxHeight, algo.height + 1);
algo = new SplitFit(floatingRects, width);
algo.execute();
System.out.println("SF: " + (algo.height + 1) + " - 1");
maxHeight = Math.min(maxHeight, algo.height + 1);
}
// assign IDs and sort by DH
public void processFrects() {
for (int i = 0; i < floatingRects.length; i++) floatingRects[i].id = i;
//Arrays.sort(floatingRects, FirstFitDecreasingHeight.decreasingHeight);
//Arrays.sort(floatingRects, decreasingWidth);
Arrays.sort(floatingRects, decreasingWidthHeight);
n = floatingRects.length;
}
@Override
public void execute() {
this.bestHeight = maxHeight;
ArrayList<Rect> inPlace = new ArrayList<>();
ArrayList<MaxRect> boxes = new ArrayList<>();
boxes.add(createInitialBox());
boolean[] placed = new boolean[floatingRects.length];
for (int i = 0; i < floatingRects.length; i++) {
Rect firstRect = floatingRects[i].place(0, 0);
firstRect.id = i;
firstRect.pointerTrail = new boolean[n];
placeRect(firstRect, inPlace, boxes, placed, i, 0);
}
for (Rect solRect : bestSol) {
rects[solRect.id] = solRect;
}
this.height = bestHeight;
}
public MaxRect createInitialBox() {
ArrayList<Rect> leftSupport = new ArrayList<>();
ArrayList<Rect> rightSupport = new ArrayList<>();
ArrayList<Rect> upSupport = new ArrayList<>();
ArrayList<Rect> downSupport = new ArrayList<>();
leftSupport.add(new Rect(-1, 0, 0, maxHeight, -1));
rightSupport.add(new Rect(width, 0, width + 1, maxHeight, -1));
downSupport.add(new Rect(0, -1, width, 0, -1));
upSupport.add(new Rect(0, maxHeight, width, maxHeight + 1, -1));
return new MaxRect(0, 0, width, maxHeight, upSupport, downSupport, leftSupport, rightSupport);
}
public int placeRect(Rect placedRect, ArrayList<Rect> inPlace, ArrayList<MaxRect> boxes, boolean[] placed, int frIndex, int intermediateHeight) {
// System.out.println(placedRect);
// System.out.println(bestHeight);
intermediateHeight = placedRect.y2 > intermediateHeight ? placedRect.y2 : intermediateHeight;
if (intermediateHeight >= bestHeight) {
return 1; // pruned without exploring
}
// System.out.println(inPlace);
placed[frIndex] = true;
inPlace.add(placedRect);
snapshotFunction.run(null, inPlace, bestHeight);
boolean done = true;
for (int i = 0; i < placed.length; i++) {
if (!placed[i]) done = false;
}
if (done) {
calculateHeight(inPlace, intermediateHeight);
jumpOut(inPlace, placed, frIndex);
return 1;
}
ArrayList<MaxRect> newBoxes = new ArrayList<MaxRect>();
for (MaxRect box : boxes) {
if (!box.splitMaxRect(newBoxes, placedRect)) { // Copy box array
jumpOut(inPlace, placed, frIndex); // Prune if false returned
return 1;
}
}
// Commentoutable check
for (int i = 0; i < newBoxes.size(); i++) {
MaxRect currBox = newBoxes.get(i);
for (int j = i + 1; j < newBoxes.size(); j++) {
MaxRect checkedBox = newBoxes.get(j);
if (currBox.x1 == checkedBox.x1 && currBox.y1 == checkedBox.y1 && currBox.x2 == checkedBox.x2 && currBox.y2 == checkedBox.y2) System.out.println("FUCK curr = " + currBox + " other = " + checkedBox);
}
}
{
// S = Medium Rectangle Pruning
int sumWideRectangleHeights = 0;
int wideRectanglesMinWidth = Integer.MAX_VALUE;
for (int i = 0; i < floatingRects.length; i++) {
if (placed[i]) continue;
if (floatingRects[i].width >= wideWidth) {
sumWideRectangleHeights += floatingRects[i].height;
if (floatingRects[i].width < wideRectanglesMinWidth) {
wideRectanglesMinWidth = floatingRects[i].width;
}
}
}
// Find the lowest box we can fit into.
int minPlacementHeight = Integer.MAX_VALUE;
for (MaxRect newBox : newBoxes) {
if (wideRectanglesMinWidth <= newBox.width) {
if (newBox.y1 < minPlacementHeight) {
minPlacementHeight = newBox.y1;
}
}
}
if (wideRectanglesMinWidth != Integer.MAX_VALUE) {
// There is at least one wide rectangle.
if (minPlacementHeight + sumWideRectangleHeights >= bestHeight) {
//System.out.println("MEDIUM RECTANGLES PRUNE!");
jumpOut(inPlace, placed, frIndex);
return 1;
}
}
}
for (int i = 0; i < floatingRects.length; i++) {
if (placed[i]) continue;
boolean lbPrune = true;
for (MaxRect newBox : newBoxes) { // search for lowest box that the tallest can fit in without exceeding upper bound
Rect attemptTallest = floatingRects[i].place(newBox.x1, newBox.y1);
if (attemptTallest.y2 < bestHeight && newBox.fits(attemptTallest, placedRect, inPlace, n)) {
lbPrune = false;
break;
}
}
if (lbPrune) {
//System.out.println("LB prune");
jumpOut(inPlace, placed, frIndex);
return 1;
}
break;
}
int bestDepth = 1;
int k = 1;
for (int i = 0; i < floatingRects.length; i++) {
if (!placed[i]) {
if (k > bestDepth) {
//System.out.println("K-PRUNE: " + k + "/" + i + "/" + floatingRects.length);
jumpOut(inPlace, placed, frIndex);
return bestDepth;
}
for (MaxRect newBox : newBoxes) {
if (intermediateHeight >= bestHeight) {
jumpOut(inPlace, placed, frIndex);
return bestDepth;
}
Rect attemptPlace = floatingRects[i].place(newBox.x1, newBox.y1);
attemptPlace.id = i;
// pass both candidate and parent
// check if follows ordering
// update dependency array if feasible
if (newBox.fits(attemptPlace, placedRect, inPlace, n)) {
bestDepth = Math.max(bestDepth, 1 + placeRect(attemptPlace, inPlace, newBoxes, placed, i, intermediateHeight));
// System.out.println(bestDepth);
}
}
k++;
}
}
jumpOut(inPlace, placed, frIndex);
return bestDepth;
}
public void calculateHeight(ArrayList<Rect> inPlace, int solHeight) {
// int solHeight = inPlace.stream().map(inPlaceRect -> inPlaceRect.y2).reduce(0, (a, b) -> a > b ? a : b);
if (solHeight < bestHeight) {
bestHeight = solHeight;
bestSol = new ArrayList<>(inPlace);
System.out.println(bestHeight);
}
}
public void jumpOut(ArrayList<Rect> inPlace, boolean[] placed, int frIndex) {
inPlace.remove(inPlace.size() - 1); // remove the last one
placed[frIndex] = false;
}
}