3D Visualization of Items Arrangement

6 views (last 30 days)
Thulasy Chandran
Thulasy Chandran on 22 Apr 2024
Edited: Thulasy Chandran on 7 May 2024
How do I get the 3D visualization of items in the bin from this algorithm?
%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPAP105
% Project Title: Solving Bin Packing Problem using PSO, FA and IWO
% Publisher: Yarpiz (www.yarpiz.com)
%
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
%
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%
clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create Bin Packing Model
CostFunction = @(x) BinPackingCost(x, model); % Objective Function
nVar = 2*model.n-1; % Number of Decision Variables
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = 0; % Lower Bound of Decision Variables
VarMax = 1; % Upper Bound of Decision Variables
%% PSO Parameters
MaxIt=50; % Maximum Number of Iterations
nPop=20; % Population Size (Swarm Size)
% PSO Parameters
w=1; % Inertia Weight
wdamp=0.99; % Inertia Weight Damping Ratio
c1=1.5; % Personal Learning Coefficient
c2=2.0; % Global Learning Coefficient
% If you would like to use Constriction Coefficients for PSO,
% uncomment the following block and comment the above set of parameters.
% % Constriction Coefficients
% phi1=2.05;
% phi2=2.05;
% phi=phi1+phi2;
% chi=2/(phi-2+sqrt(phi^2-4*phi));
% w=chi; % Inertia Weight
% wdamp=1; % Inertia Weight Damping Ratio
% c1=chi*phi1; % Personal Learning Coefficient
% c2=chi*phi2; % Global Learning Coefficient
% Velocity Limits
VelMax=0.1*(VarMax-VarMin);
VelMin=-VelMax;
nParticleMutation = 2; % Number of Mutations Performed on Each Particle
nGlobalBestMutation = 5; % Number of Mutations Performed on Global Best
%% Initialization
empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Sol=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
empty_particle.Best.Sol=[];
particle=repmat(empty_particle,nPop,1);
GlobalBest.Cost=inf;
for i=1:nPop
% Initialize Position
particle(i).Position=unifrnd(VarMin,VarMax,VarSize);
% Initialize Velocity
particle(i).Velocity=zeros(VarSize);
% Evaluation
[particle(i).Cost, particle(i).Sol]=CostFunction(particle(i).Position);
% Update Personal Best
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
particle(i).Best.Sol=particle(i).Sol;
% Update Global Best
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
% Update Velocity
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);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity,VelMin);
particle(i).Velocity = min(particle(i).Velocity,VelMax);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Velocity Mirror Effect
IsOutside=(particle(i).Position<VarMin | particle(i).Position>VarMax);
particle(i).Velocity(IsOutside)=-particle(i).Velocity(IsOutside);
% Apply Position Limits
particle(i).Position = max(particle(i).Position,VarMin);
particle(i).Position = min(particle(i).Position,VarMax);
% Evaluation
[particle(i).Cost, particle(i).Sol] = CostFunction(particle(i).Position);
% Perform Mutation
for j=1:nParticleMutation
NewParticle = particle(i);
NewParticle.Position = Mutate(particle(i).Position);
[NewParticle.Cost, NewParticle.Sol] = CostFunction(NewParticle.Position);
if NewParticle.Cost <= particle(i).Cost
particle(i) = NewParticle;
end
end
% Update Personal Best
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;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
end
% Perform Mutation on Global Best
for i=1:nGlobalBestMutation
NewParticle = GlobalBest;
NewParticle.Position = Mutate(GlobalBest.Position);
[NewParticle.Cost, NewParticle.Sol] = CostFunction(NewParticle.Position);
if NewParticle.Cost <= GlobalBest.Cost
GlobalBest = NewParticle;
end
end
BestCost(it)=GlobalBest.Cost;
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
w=w*wdamp;
end
BestSol = GlobalBest;
%% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;

Answers (1)

Yatharth
Yatharth on 2 May 2024
Hi Thulasy,
You can use the "patch" function to draw each item as a 3D box. You'll need to calculate the vertices of each box based on it's position and dimensions.
Initial step to extract item positions and dimensions : You need to have coordinates of bottom-left-corner and dimensions (length , width, height) of each item. This information you'll need to extract from the solution structure.
Here is a basic framework on how to implement the visualization.
Note: that you'll need to adapt this code to fit the structure of your solution.
Code for defining vertices and using patch function
items = [2, 2, 1, 0, 0, 0;
2, 1, 2, 0, 0, 0;
1, 2, 2, 0, 0, 0;
1, 1, 1, 0, 0, 0;
3, 2, 1, 0, 0, 0;
2, 2, 2, 0, 0, 0;
1, 3, 2, 0, 0, 0;
2, 1, 1, 0, 0, 0;
1, 2, 1, 0, 0, 0;
1, 1, 2, 0, 0, 0];
figure;
hold on;
for i = 1:size(items, 1)
% Extract position and dimensions here I am using items matrix but you
% will have to extract it from your solution structure.
l = items(i, 1);
w = items(i, 2);
h = items(i, 3);
x = items(i, 4);
y = items(i, 5);
z = items(i, 6);
% Define vertices of the 3D box
vertices = [x, y, z;
x+l, y, z;
x+l, y+w, z;
x, y+w, z;
x, y, z+h;
x+l, y, z+h;
x+l, y+w, z+h;
x, y+w, z+h];
% Define faces of the box
faces = [1, 2, 3, 4; % Bottom
5, 6, 7, 8; % Top
1, 2, 6, 5; % Side 1
2, 3, 7, 6; % Side 2
3, 4, 8, 7; % Side 3
4, 1, 5, 8];% Side 4
% Plot the box
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', rand(1,3), 'EdgeColor', 'k', 'FaceAlpha', 0.5);
end
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Visualization of Bin Packing');
axis equal;
grid on;
view(3); % Set the view to 3D
hold off;
  3 Comments
Yatharth
Yatharth on 7 May 2024
You can plot the bounding box using this custom function
function plotBoxes(boxPosition, boxSize)
% figure;
hold on;
axis equal;
xlabel('X');
ylabel('Y');
zlabel('Z');
view(3); % Set the view to 3D
numBoxes = size(boxPosition, 2);
for i = 1:numBoxes
pos = boxPosition(:, i);
sz = boxSize(:, i);
% Calculate the vertices of the box
vertices = [pos(1), pos(2), pos(3);
pos(1) + sz(1), pos(2), pos(3);
pos(1) + sz(1), pos(2) + sz(2), pos(3);
pos(1), pos(2) + sz(2), pos(3);
pos(1), pos(2), pos(3) + sz(3);
pos(1) + sz(1), pos(2), pos(3) + sz(3);
pos(1) + sz(1), pos(2) + sz(2), pos(3) + sz(3);
pos(1), pos(2) + sz(2), pos(3) + sz(3)];
% Define the faces of the box
faces = [1, 2, 3, 4; % bottom
5, 6, 7, 8; % top
1, 2, 6, 5; % front
2, 3, 7, 6; % right
3, 4, 8, 7; % back
4, 1, 5, 8]; % left
% Plot the box
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'none', 'EdgeColor', 'r');
end
hold off;
end
Here is how you can use this function at the end of your code.
clear;
items = [2, 2, 1, 0, 0, 0;
2, 1, 2, 0, 0, 0;
1, 2, 2, 0, 0, 0;
1, 1, 1, 0, 0, 0;
3, 2, 1, 0, 0, 0;
2, 2, 2, 0, 0, 0;
1, 3, 2, 0, 0, 0;
2, 1, 1, 0, 0, 0;
1, 2, 1, 0, 0, 0;
1, 1, 2, 0, 0, 0];
figure;
hold on;
for i = 1:size(items, 1)
% Extract position and dimensions here I am using items matrix but you
% will have to extract it from your solution structure.
l = items(i, 1);
w = items(i, 2);
h = items(i, 3);
x = items(i, 4);
y = items(i, 5);
z = items(i, 6);
% Define vertices of the 3D box
vertices = [x, y, z;
x+l, y, z;
x+l, y+w, z;
x, y+w, z;
x, y, z+h;
x+l, y, z+h;
x+l, y+w, z+h;
x, y+w, z+h];
% Define faces of the box
faces = [1, 2, 3, 4; % Bottom
5, 6, 7, 8; % Top
1, 2, 6, 5; % Side 1
2, 3, 7, 6; % Side 2
3, 4, 8, 7; % Side 3
4, 1, 5, 8];% Side 4
% Plot the box
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', rand(1,3), 'EdgeColor', 'k', 'FaceAlpha', 0.5);
end
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Visualization of Bin Packing');
axis equal;
grid on;
view(3); % Set the view to 3D
boxPosition = [0, 0, 0; 2, 0, 0; 0, 2, 0]; % 3 boxes positioned at the origin
boxSize = [1, 1, 3; 1, 1, 3; 1, 1, 3]; % All boxes are 1x1x1 cubes
% Plot the boxes
plotBoxes(boxPosition, boxSize); %calling the custom function here
hold off;
Thulasy Chandran
Thulasy Chandran on 7 May 2024
Edited: Thulasy Chandran on 7 May 2024
i want to plot the boundary box around the arrangment of items where we can calculate the minimum space left. this looks more like the the boundary box plotted at the maximum size of the bin.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!