-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestingBWF.m
164 lines (129 loc) · 5.33 KB
/
testingBWF.m
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
firstRow = 1;
firstCol = 0;
presentsTemp = csvread('presents.csv', firstRow, firstCol);
[rows, columns] = size(presentsTemp);
numPresents = rows
idsCol = presentsTemp(:,1:1);
dimensions = presentsTemp(:,2:4);
dimensions = sort(dimensions,2);
presents = horzcat(idsCol,dimensions);
presentIDs = presents(:,1);
presentWidth = presents(:,2);
presentLength = presents(:,3);
presentHeight = presents(:,4);
presentVol = [presentIDs, presentWidth(:).*presentLength(:).*presentHeight(:)];
minVol = min(presentVol(:,2))
maxVol = max(presentVol(:,2))
width = 1000;
length = 1000;
xs = 1; ys = 1; zs = -1; % Initial coordinates for placing boxes
lastRowIdxs = zeros(100,1); % Buffer for storing row indices
lastLayerIdxs = zeros(500,1); % Buffer for storing layer indices
numInRow = 0;
numInLayer = 0;
presentCoords = zeros(numPresents,25); % PresentID and 8 sets of coordinates per present
numRowInLayer = 1;
for i = 1:numPresents
flag = 0;
if numRowInLayer > 1
bestRow = BWF(presentWidth(i),presentLength(i),lastPresentInRowDetails,presentCoords);
if bestRow ~=0
newXs = presentCoords(lastPresentInRowDetails(bestRow,1),5)+1;
newYs = presentCoords(lastPresentInRowDetails(bestRow,1),6);
newZs = presentCoords(lastPresentInRowDetails(bestRow,1),7);
presentCoords(i,1) = presentIDs(i);
presentCoords(i,[2 8 14 20]) = newXs;
presentCoords(i,[5 11 17 23]) = newXs + presentWidth(i) - 1;
presentCoords(i,[3 6 15 18]) = newYs;
presentCoords(i,[9 12 21 24]) = newYs + presentLength(i) - 1;
presentCoords(i,[4 7 10 13]) = newZs;
presentCoords(i,[16 19 22 25]) = newZs - presentHeight(i) + 1;
lastPresentInRowDetails(bestRow,1) = i;
lastPresentInRowDetails(bestRow,3) = 0;
numInLayer = numInLayer +1;
lastLayerIdxs(numInLayer) = presentIDs(i);
flag=1;
% presentCoords(i,:) = presentCoords(i,:)
end
end
if flag==0
% Move to the next row if there isn't room
if xs + presentWidth(i) > width + 1 % exceeded allowable width
ys = ys + max(presentLength(lastRowIdxs(1:numInRow))); % increment y to ensure no overlap
lastPresentInRowDetails(numRowInLayer,:) = [i-1,max(presentLength(lastRowIdxs(1:numInRow))),1];
numRowInLayer = numRowInLayer+1;
xs = 1;
numInRow = 0;
clear lastRowIdxs;
lastRowIdxs = zeros(100,1);
end
% Move to the next layer if there isn't room
if ys + presentLength(i) > length + 1 % exceeded allowable length
zs = zs - max(presentHeight(lastLayerIdxs(1:numInLayer))); % increment z to ensure no overlap
xs = 1;
ys = 1;
numInLayer = 0;
clear lastLayerIdxs;
lastLayerIdxs = zeros(500,1);
numInRow = 0;
clear lastRowIdxs;
lastRowIdxs = zeros(100,1);
clear lastPresentInRowDetails;
numRowInLayer = 1;
end
% Fill present coordinate matrix
presentCoords(i,1) = presentIDs(i);
presentCoords(i,[2 8 14 20]) = xs;
presentCoords(i,[5 11 17 23]) = xs + presentWidth(i) - 1;
presentCoords(i,[3 6 15 18]) = ys;
presentCoords(i,[9 12 21 24]) = ys + presentLength(i) - 1;
presentCoords(i,[4 7 10 13]) = zs;
presentCoords(i,[16 19 22 25]) = zs - presentHeight(i) + 1;
% Update location info
xs = xs + presentWidth(i);
numInRow = numInRow+1;
numInLayer = numInLayer+1;
lastRowIdxs(numInRow) = presentIDs(i);
lastLayerIdxs(numInLayer) = presentIDs(i);
end
end
zCoords = presentCoords(:,4:3:end);
minZ = min(zCoords(:));
presentCoords(:,4:3:end) = zCoords - minZ + 1;
idealOrder = presentIDs;
maxZ = max(max(presentCoords(:,4:3:end)));
maxZCoord = zeros(numPresents,2);
for i = 1:numPresents
maxZCoord(i,1) = presentCoords(i);
maxZCoord(i,2) = max(presentCoords(i,4:3:end));
end
maxzCoordSorted = sortrows(maxZCoord,[-2 1]); %sort max z-coord for each present
reOrder = maxzCoordSorted(:,1);
% Compare the new order to the ideal order
order = sum(abs(idealOrder - reOrder));
% Compute metric
metric = 2*maxZ + order;
subfile = 'submissionfile_BWF.csv';
fileID = fopen(subfile, 'w');
headers = {'PresentId','x1','y1','z1','x2','y2','z2','x3','y3','z3','x4','y4','z4','x5','y5','z5','x6','y6','z6','x7','y7','z7','x8','y8','z8'};
fprintf(fileID,'%s,',headers{1,1:end-1});
fprintf(fileID,'%s\n',headers{1,end});
fprintf(fileID,'%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n',presentCoords');
fclose(fileID);
function rowId = BWF(newPwidth, newPlength, lastPresentInRowDetails,presentCoords)
noOfRows = size(lastPresentInRowDetails,1);
minValue = 9999999999;
rowId = 0;
for j = 1:noOfRows
if lastPresentInRowDetails(j,3)==1
remainingWidth = 1000 - presentCoords(lastPresentInRowDetails(j,1),5);
maxLength = lastPresentInRowDetails(j,2);
if newPwidth < remainingWidth && newPlength < maxLength
if minValue > remainingWidth - newPwidth
minValue = remainingWidth - newPwidth;
rowId = j;
end
end
end
end
end