3D plotting of the bounding box around few boxes
hi,
im trying to plot a bounding box around the final arrangement of boxes but im not getting the ideal output.
this is how the final arrangement of the boxes looks:
expected output:
the output im getting:
this is the flow of my code:
i get the vertices of the 5 boxes in the final arrangement and parse them to a spreadsheet.
clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create Bin Packing Model
CostFunction = @(x) BinPackingCost(x, model); % Objective Function
nVar = model.n * 4; % Each item now has a dimension setting (x, y, z, orientation)
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = 0; % Lower Bound of Decision Variables
VarMax = 1; % Upper Bound of Decision Variables
%% PSO Parameters
MaxIt = 1000; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
w = 1; % Inertia Weight
wdamp = 0.99; % Inertia Weight Damping Ratio
c1 = 1.5; % Personal Learning Coefficient
c2 = 2.0; % Global Learning Coefficient
VelMax = 0.1 * (VarMax – VarMin); % Velocity Limits
VelMin = -VelMax;
%% Initialization
particle = repmat(struct(‘Position’, [], ‘Cost’, [], ‘Sol’, [], ‘Velocity’, [], ‘Best’, struct()), nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize position with additional orientation indices
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Ensure orientation indices are integers between 1 and 6
particle(i).Position(4:4:end) = randi(6, 1, model.n);
% Velocity and other initialization as before
particle(i).Velocity = zeros(VarSize);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
BestCost = zeros(MaxIt, 1);
%% PSO Main Loop
for it = 1:MaxIt
for i = 1:nPop
particle(i).Velocity = w * particle(i).Velocity …
+ c1 * rand(VarSize) .* (particle(i).Best.Position – particle(i).Position) …
+ c2 * rand(VarSize) .* (GlobalBest.Position – particle(i).Position);
particle(i).Velocity = max(particle(i).Velocity, VelMin);
particle(i).Velocity = min(particle(i).Velocity, VelMax);
particle(i).Position = particle(i).Position + particle(i).Velocity;
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
BestCost(it) = GlobalBest.Cost;
disp([‘Iteration ‘ num2str(it) ‘: Best Cost = ‘ num2str(BestCost(it))]);
w = w * wdamp;
end
%% Results
figure;
plot(BestCost, ‘LineWidth’, 2);
xlabel(‘Iteration’);
ylabel(‘Best Cost’);
title(‘Convergence of PSO’);
grid on;
%% Bin Configuration Visualization and Vertex Calculation
plotBins(GlobalBest.Sol, model);
vertices = cell(model.n, 1);
for i = GlobalBest.Sol.B{1}
item_dims = model.items(i, :);
orientation = GlobalBest.Position(4*i);
item_pos = GlobalBest.Position(4*i-3:4*i-1);
% Rotate dimensions based on orientation
switch orientation
case 1 % (L, W, H)
rotated_dims = item_dims;
case 2 % (L, H, W)
rotated_dims = [item_dims(1), item_dims(3), item_dims(2)];
case 3 % (W, L, H)
rotated_dims = [item_dims(2), item_dims(1), item_dims(3)];
case 4 % (W, H, L)
rotated_dims = [item_dims(2), item_dims(3), item_dims(1)];
case 5 % (H, L, W)
rotated_dims = [item_dims(3), item_dims(1), item_dims(2)];
case 6 % (H, W, L)
rotated_dims = [item_dims(3), item_dims(2), item_dims(1)];
end
[X, Y, Z] = getBoxVertices(rotated_dims, item_pos);
vertices{i} = [X; Y; Z];
end
%% Exporting Vertex Data to CSV
% Create a cell array to hold the data for the CSV file (This part remains the same)
csv_data = cell(model.n + 1, 1);
csv_data{1} = {‘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’};
% Fill the cell array with vertex data for each item (This part remains the same)
for i = 1:model.n
item_vertices = vertices{i}’; % Transpose to get rows as vertices
csv_data{i+1} = [i, item_vertices(:)’]; % Add item ID and flatten vertex coordinates
end
% Open the file for writing
fileID = fopen(‘vertex_data.csv’,’w’);
% Write header row (remains the same)
fprintf(fileID,’%s,’,csv_data{1}{:});
fprintf(fileID,’n’);
% Write data rows (corrected loop)
for i = 2:size(csv_data,1)
row_data = csv_data{i}; % Get the cell array for the current row
for j = 1:length(row_data)
fprintf(fileID,’%d,’,round(row_data(j))); % Round values to nearest integer
end
fprintf(fileID,’n’);
end
% Close the file (remains the same)
fclose(fileID);
2. i use another function to plot the bounding bos from the data from spreadsheet.
%get the coordinates
solutionFile = ‘vertex_data.csv’;
solutionData = readmatrix(solutionFile);
% get the two bounding box corners (min(x),min(y),min(z)) and (max(x),max(y),max(z))
p1 = min(reshape(min(solutionData(:,2:end),[],1),3,[]).’,[],1);
p8 = max(reshape(max(solutionData(:,2:end),[],1),3,[]).’,[],1);
coordinates = solutionData(:, 2:end);
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p8,p1,p8,p8,p8];
p_bb([5 9 13]) = p8([2 3 1]);
p_bb([10 18 20]) = p1([1 3 2]);
coordinates(end+1,:) = p_bb;
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
% make the bounding box a special color:
colors(N,:) = [0.8 0.8 0.8];
figure
F = [1 2 4 3; 5 6 8 7; 1 2 6 5; 4 3 7 8; 2 6 8 4; 1 5 7 3];
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).’;
patch( …
‘Vertices’,C, …
‘Faces’,F, …
‘FaceColor’,’flat’, …
‘FaceVertexCData’,colors(ii,:), …
‘FaceAlpha’,0.5);
end
view(3)
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title(‘Visualization of Boxes’);
grid on;
i have attached all my .m files for reference. i need help on how to plot bounding box according to the boxes arrangement.hi,
im trying to plot a bounding box around the final arrangement of boxes but im not getting the ideal output.
this is how the final arrangement of the boxes looks:
expected output:
the output im getting:
this is the flow of my code:
i get the vertices of the 5 boxes in the final arrangement and parse them to a spreadsheet.
clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create Bin Packing Model
CostFunction = @(x) BinPackingCost(x, model); % Objective Function
nVar = model.n * 4; % Each item now has a dimension setting (x, y, z, orientation)
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = 0; % Lower Bound of Decision Variables
VarMax = 1; % Upper Bound of Decision Variables
%% PSO Parameters
MaxIt = 1000; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
w = 1; % Inertia Weight
wdamp = 0.99; % Inertia Weight Damping Ratio
c1 = 1.5; % Personal Learning Coefficient
c2 = 2.0; % Global Learning Coefficient
VelMax = 0.1 * (VarMax – VarMin); % Velocity Limits
VelMin = -VelMax;
%% Initialization
particle = repmat(struct(‘Position’, [], ‘Cost’, [], ‘Sol’, [], ‘Velocity’, [], ‘Best’, struct()), nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize position with additional orientation indices
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Ensure orientation indices are integers between 1 and 6
particle(i).Position(4:4:end) = randi(6, 1, model.n);
% Velocity and other initialization as before
particle(i).Velocity = zeros(VarSize);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
BestCost = zeros(MaxIt, 1);
%% PSO Main Loop
for it = 1:MaxIt
for i = 1:nPop
particle(i).Velocity = w * particle(i).Velocity …
+ c1 * rand(VarSize) .* (particle(i).Best.Position – particle(i).Position) …
+ c2 * rand(VarSize) .* (GlobalBest.Position – particle(i).Position);
particle(i).Velocity = max(particle(i).Velocity, VelMin);
particle(i).Velocity = min(particle(i).Velocity, VelMax);
particle(i).Position = particle(i).Position + particle(i).Velocity;
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
BestCost(it) = GlobalBest.Cost;
disp([‘Iteration ‘ num2str(it) ‘: Best Cost = ‘ num2str(BestCost(it))]);
w = w * wdamp;
end
%% Results
figure;
plot(BestCost, ‘LineWidth’, 2);
xlabel(‘Iteration’);
ylabel(‘Best Cost’);
title(‘Convergence of PSO’);
grid on;
%% Bin Configuration Visualization and Vertex Calculation
plotBins(GlobalBest.Sol, model);
vertices = cell(model.n, 1);
for i = GlobalBest.Sol.B{1}
item_dims = model.items(i, :);
orientation = GlobalBest.Position(4*i);
item_pos = GlobalBest.Position(4*i-3:4*i-1);
% Rotate dimensions based on orientation
switch orientation
case 1 % (L, W, H)
rotated_dims = item_dims;
case 2 % (L, H, W)
rotated_dims = [item_dims(1), item_dims(3), item_dims(2)];
case 3 % (W, L, H)
rotated_dims = [item_dims(2), item_dims(1), item_dims(3)];
case 4 % (W, H, L)
rotated_dims = [item_dims(2), item_dims(3), item_dims(1)];
case 5 % (H, L, W)
rotated_dims = [item_dims(3), item_dims(1), item_dims(2)];
case 6 % (H, W, L)
rotated_dims = [item_dims(3), item_dims(2), item_dims(1)];
end
[X, Y, Z] = getBoxVertices(rotated_dims, item_pos);
vertices{i} = [X; Y; Z];
end
%% Exporting Vertex Data to CSV
% Create a cell array to hold the data for the CSV file (This part remains the same)
csv_data = cell(model.n + 1, 1);
csv_data{1} = {‘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’};
% Fill the cell array with vertex data for each item (This part remains the same)
for i = 1:model.n
item_vertices = vertices{i}’; % Transpose to get rows as vertices
csv_data{i+1} = [i, item_vertices(:)’]; % Add item ID and flatten vertex coordinates
end
% Open the file for writing
fileID = fopen(‘vertex_data.csv’,’w’);
% Write header row (remains the same)
fprintf(fileID,’%s,’,csv_data{1}{:});
fprintf(fileID,’n’);
% Write data rows (corrected loop)
for i = 2:size(csv_data,1)
row_data = csv_data{i}; % Get the cell array for the current row
for j = 1:length(row_data)
fprintf(fileID,’%d,’,round(row_data(j))); % Round values to nearest integer
end
fprintf(fileID,’n’);
end
% Close the file (remains the same)
fclose(fileID);
2. i use another function to plot the bounding bos from the data from spreadsheet.
%get the coordinates
solutionFile = ‘vertex_data.csv’;
solutionData = readmatrix(solutionFile);
% get the two bounding box corners (min(x),min(y),min(z)) and (max(x),max(y),max(z))
p1 = min(reshape(min(solutionData(:,2:end),[],1),3,[]).’,[],1);
p8 = max(reshape(max(solutionData(:,2:end),[],1),3,[]).’,[],1);
coordinates = solutionData(:, 2:end);
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p8,p1,p8,p8,p8];
p_bb([5 9 13]) = p8([2 3 1]);
p_bb([10 18 20]) = p1([1 3 2]);
coordinates(end+1,:) = p_bb;
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
% make the bounding box a special color:
colors(N,:) = [0.8 0.8 0.8];
figure
F = [1 2 4 3; 5 6 8 7; 1 2 6 5; 4 3 7 8; 2 6 8 4; 1 5 7 3];
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).’;
patch( …
‘Vertices’,C, …
‘Faces’,F, …
‘FaceColor’,’flat’, …
‘FaceVertexCData’,colors(ii,:), …
‘FaceAlpha’,0.5);
end
view(3)
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title(‘Visualization of Boxes’);
grid on;
i have attached all my .m files for reference. i need help on how to plot bounding box according to the boxes arrangement. hi,
im trying to plot a bounding box around the final arrangement of boxes but im not getting the ideal output.
this is how the final arrangement of the boxes looks:
expected output:
the output im getting:
this is the flow of my code:
i get the vertices of the 5 boxes in the final arrangement and parse them to a spreadsheet.
clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create Bin Packing Model
CostFunction = @(x) BinPackingCost(x, model); % Objective Function
nVar = model.n * 4; % Each item now has a dimension setting (x, y, z, orientation)
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = 0; % Lower Bound of Decision Variables
VarMax = 1; % Upper Bound of Decision Variables
%% PSO Parameters
MaxIt = 1000; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
w = 1; % Inertia Weight
wdamp = 0.99; % Inertia Weight Damping Ratio
c1 = 1.5; % Personal Learning Coefficient
c2 = 2.0; % Global Learning Coefficient
VelMax = 0.1 * (VarMax – VarMin); % Velocity Limits
VelMin = -VelMax;
%% Initialization
particle = repmat(struct(‘Position’, [], ‘Cost’, [], ‘Sol’, [], ‘Velocity’, [], ‘Best’, struct()), nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize position with additional orientation indices
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Ensure orientation indices are integers between 1 and 6
particle(i).Position(4:4:end) = randi(6, 1, model.n);
% Velocity and other initialization as before
particle(i).Velocity = zeros(VarSize);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
BestCost = zeros(MaxIt, 1);
%% PSO Main Loop
for it = 1:MaxIt
for i = 1:nPop
particle(i).Velocity = w * particle(i).Velocity …
+ c1 * rand(VarSize) .* (particle(i).Best.Position – particle(i).Position) …
+ c2 * rand(VarSize) .* (GlobalBest.Position – particle(i).Position);
particle(i).Velocity = max(particle(i).Velocity, VelMin);
particle(i).Velocity = min(particle(i).Velocity, VelMax);
particle(i).Position = particle(i).Position + particle(i).Velocity;
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
particle(i).Best.Sol = particle(i).Sol;
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
BestCost(it) = GlobalBest.Cost;
disp([‘Iteration ‘ num2str(it) ‘: Best Cost = ‘ num2str(BestCost(it))]);
w = w * wdamp;
end
%% Results
figure;
plot(BestCost, ‘LineWidth’, 2);
xlabel(‘Iteration’);
ylabel(‘Best Cost’);
title(‘Convergence of PSO’);
grid on;
%% Bin Configuration Visualization and Vertex Calculation
plotBins(GlobalBest.Sol, model);
vertices = cell(model.n, 1);
for i = GlobalBest.Sol.B{1}
item_dims = model.items(i, :);
orientation = GlobalBest.Position(4*i);
item_pos = GlobalBest.Position(4*i-3:4*i-1);
% Rotate dimensions based on orientation
switch orientation
case 1 % (L, W, H)
rotated_dims = item_dims;
case 2 % (L, H, W)
rotated_dims = [item_dims(1), item_dims(3), item_dims(2)];
case 3 % (W, L, H)
rotated_dims = [item_dims(2), item_dims(1), item_dims(3)];
case 4 % (W, H, L)
rotated_dims = [item_dims(2), item_dims(3), item_dims(1)];
case 5 % (H, L, W)
rotated_dims = [item_dims(3), item_dims(1), item_dims(2)];
case 6 % (H, W, L)
rotated_dims = [item_dims(3), item_dims(2), item_dims(1)];
end
[X, Y, Z] = getBoxVertices(rotated_dims, item_pos);
vertices{i} = [X; Y; Z];
end
%% Exporting Vertex Data to CSV
% Create a cell array to hold the data for the CSV file (This part remains the same)
csv_data = cell(model.n + 1, 1);
csv_data{1} = {‘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’};
% Fill the cell array with vertex data for each item (This part remains the same)
for i = 1:model.n
item_vertices = vertices{i}’; % Transpose to get rows as vertices
csv_data{i+1} = [i, item_vertices(:)’]; % Add item ID and flatten vertex coordinates
end
% Open the file for writing
fileID = fopen(‘vertex_data.csv’,’w’);
% Write header row (remains the same)
fprintf(fileID,’%s,’,csv_data{1}{:});
fprintf(fileID,’n’);
% Write data rows (corrected loop)
for i = 2:size(csv_data,1)
row_data = csv_data{i}; % Get the cell array for the current row
for j = 1:length(row_data)
fprintf(fileID,’%d,’,round(row_data(j))); % Round values to nearest integer
end
fprintf(fileID,’n’);
end
% Close the file (remains the same)
fclose(fileID);
2. i use another function to plot the bounding bos from the data from spreadsheet.
%get the coordinates
solutionFile = ‘vertex_data.csv’;
solutionData = readmatrix(solutionFile);
% get the two bounding box corners (min(x),min(y),min(z)) and (max(x),max(y),max(z))
p1 = min(reshape(min(solutionData(:,2:end),[],1),3,[]).’,[],1);
p8 = max(reshape(max(solutionData(:,2:end),[],1),3,[]).’,[],1);
coordinates = solutionData(:, 2:end);
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p8,p1,p8,p8,p8];
p_bb([5 9 13]) = p8([2 3 1]);
p_bb([10 18 20]) = p1([1 3 2]);
coordinates(end+1,:) = p_bb;
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
% make the bounding box a special color:
colors(N,:) = [0.8 0.8 0.8];
figure
F = [1 2 4 3; 5 6 8 7; 1 2 6 5; 4 3 7 8; 2 6 8 4; 1 5 7 3];
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).’;
patch( …
‘Vertices’,C, …
‘Faces’,F, …
‘FaceColor’,’flat’, …
‘FaceVertexCData’,colors(ii,:), …
‘FaceAlpha’,0.5);
end
view(3)
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title(‘Visualization of Boxes’);
grid on;
i have attached all my .m files for reference. i need help on how to plot bounding box according to the boxes arrangement. 3d plots, matlab, plots, toolbox MATLAB Answers — New Questions