-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBacktracking.java
275 lines (260 loc) · 7.65 KB
/
Backtracking.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import java.util.ArrayList;
/**
* A class with a backtracking algorithm that tries to fill the total volume of
* a cargo
*
* @author Julián Marrades
* @version 0.1, 11-01-2018
*
* @author Julián Marrades
* @version 0.2, 12-01-2018
*
* @author Silvia Fallone
* @version 0.3, 14-01-2018
*/
public class Backtracking {
public static Cargo tmp;
public static boolean solved = false;
public static long iterations = 0;
/**
* Try to fill the cargo with certain types of items
* @param items the set of items that can be used
* @param shape the cargo
* @param optimized if optimization is wanted
* @param counter call counter
*/
public static void solveFor(Item[] items, Item[][][] shape, boolean optimized, int counter) {
// iterations++;
// if(iterations == 500_000) {
// System.out.println(iterations + " iterations");
// }
if (counter == 0) {
tmp = null;
solved = false;
}
if (solved) return;
if (isFull(shape)) {
// System.out.println("The cargo is full");
// print3DArray(shape);
tmp = new Cargo("WTF", shape);
solved = true;
// tmp.printSolution(items);
// System.exit(0);
return;
}
for (int j = 0; j < shape[0].length; j++) {
for (int k = 0; k < shape[0][0].length; k++) {
for (int i = 0; i < shape.length; i++) {
if (shape[i][j][k] == null) {
for (int t = 0; t < items.length; t++) {
for (Item item : Item.getAllShapes(items[t])) {
// System.out.println(canBePut(item, shape, i, j, k));
if (canBePut(item, shape, i, j, k)) {
Item[][][] newShape = insert(item, shape, i, j, k);
// System.out.println("Inserting " + item.getName());
if (optimized) {
if (shouldContinue(newShape)) {
solveFor(items, newShape, optimized, counter + 1);
}
}
else {
solveFor(items, newShape, optimized, counter + 1);
}
}
}
}
// System.out.println("Going back!");
return;
}
}
}
}
}
/**
* Decide if continue the search
* @param shape the current state of the cargo
* @return if it should continue, false otherwise
*/
public static boolean shouldContinue(Item[][][] shape) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
for (int k = 0; k < shape[i][j].length; k++) {
if (shape[i][j][k] == null && isolated(shape, i, j, k)) {
// System.out.println("Isolated cell found!");
return false;
}
}
}
}
// System.out.println("Continuing...");
return true;
}
/**
* Check if a certain cell is isolated
* @param shape the cargo that contains the cell
* @param i the position along the x-axis
* @param j the position along the y-axis
* @param k the position along the z-axis
* @return true is the cell is isolated, false otherwise
*/
public static boolean isolated(Item[][][] shape, int i, int j, int k) {
boolean iso = false;
if (checkPosition(shape, i, j-1, k)) { // TOP
if (checkPosition(shape, i, j+1, k)) { // BOTTOM
if (checkPosition(shape, i, j, k+1)) { // FRONT
if (checkPosition(shape, i, j, k-1)) { // REAR
if (checkPosition(shape, i-1, j, k)) { // LEFT
if (checkPosition(shape, i+1, j, k)) { // RIGHT
iso = true;
}
}
}
}
}
}
return iso;
}
/**
* Check if a certain cell is full or out of bounds
* @param shape the cargo that contains the cell
* @param i the position along the x-axis
* @param j the position along the y-axis
* @param k the position along the z-axis
* @return true is the cell is out or full, false otherwise
*/
public static boolean checkPosition(Item[][][] shape, int i, int j, int k) {
boolean off = false;
if (i < 0 || i >= shape.length) {
off = true;
}
else if (j < 0 || j >= shape[0].length) {
off = true;
}
else if (k < 0 || k >= shape[0][0].length) {
off = true;
}
else if (shape[i][j][k] != null) {
off = true;
}
return off;
}
/**
* Determine if a 3D Item array is full or not
* @param shape the 3D array to check
* @return true if it is full, false otherwise
*/
public static boolean isFull(Item[][][] shape) {
boolean full = true;
int i = 0;
while (full && i < shape.length) {
int j = 0;
while (full && j < shape[i].length) {
int k = 0;
while (full && k < shape[i][j].length) {
if (shape[i][j][k] == null) {
full = false;
}
k++;
}
j++;
}
i++;
}
return full;
}
/**
* See if an item fits in a certain position of the cargo
* @param item the item to check
* @param shape the shape of the cargo
* @param i the position on the x axis
* @param j the position on the y axis
* @param k the position on the z axis
* @return true if the item fits, false otherwise
*/
public static boolean canBePut(Item item, Item[][][] shape, int i, int j, int k) {
boolean permission = true;
int w = 0;
while (permission && w < item.getWidth()) {
int h = 0;
while (permission && h < item.getHeight()) {
int d = 0;
while (permission && d < item.getDepth()) {
if (w + i >= shape.length || h + j >= shape[w + i].length || d + k >= shape[w + i][h + j].length) {
permission = false;
}
else if (shape[w+i][h+j][d+k] != null) {
permission = false;
}
d++;
}
h++;
}
w++;
}
return permission;
}
/**
* Insert an item in a given position of the cargo, creating a new version of it
* @param item the item to fit
* @param shape the cargo that recieves the item
* @param i the position along the x-axis
* @param j the position along the y-axis
* @param k the position along the z-axis
* @return new version of shape with the item inside
*/
public static Item[][][] insert(Item item, Item[][][] shape, int i, int j, int k) {
Item[][][] newShape = new Item[shape.length][shape[0].length][shape[0][0].length];
for (int x = 0; x < newShape.length; x++) {
for (int y = 0; y < newShape[x].length; y++) {
for (int z = 0; z < newShape[x][y].length; z++) {
newShape[x][y][z] = shape[x][y][z];
}
}
}
Item newItem = item.clone();
for (int w = 0; w < item.getWidth(); w++) {
for (int h = 0; h < item.getHeight(); h++) {
for (int d = 0; d < item.getDepth(); d++) {
newShape[w + i][h + j][d + k] = newItem;
}
}
}
return newShape;
}
/**
* Print the slices of a 3D array to the screen
* @param shape the 3D array to print
*/
public static void print3DArray(Item[][][] shape) {
for (Item[][] matrix : shape) {
printMatrix(matrix);
System.out.println();
}
}
/**
* Print a matrix to the screen
* @param matrix the matrix to print
*/
public static void printMatrix(Item[][] matrix) {
for (Item[] array : matrix) {
printArray(array);
}
}
/**
* Print an array to the screen
* @param array the array to print
*/
public static void printArray(Item[] array) {
System.out.print("[ ");
for (Item item : array) {
if (item != null) {
System.out.print(item.getName());
}
else {
System.out.print("N");
}
System.out.print(" ");
}
System.out.println("]");
}
}