# Matlab simulation of transportation optimization algorithm based on PSO

2022-01-26 23:43:04

Suppose there is a collection track , It has 5 A collection heap , this 5 Each collection heap is regarded as a 4*20 Matrix （ There is only 4*10）, Each module （ such as ：A31 and A32 The element content is different ）, In order to meet the requirements of quantity and element content of collected articles （ such as ： Need to collect 5 Tons and the unit mass of an element are 65 And 62 Between ）, Find out in each 4*20 Which module in the matrix is taken out to meet the requirements and find the optimal track ？（ When taking out the module, we should consider only taking out the above , Before you can collect the following ）

Known data ：

1. The element content of each collection heap （ stay excel Tabular sheet 1）

2. Coordinates of modules in each acquisition stack , LWH （ In meters ）（ stay excel Tabular sheet 1）

3. Requirements for element content and quantity of collected articles （ stay excel Tabular sheet 2）, There are five maximum and minimum values of different contents , There are also requirements for the collection quantity , And error .

4. The two acquisition piles on the left side of the track are C Model and A Model number , The only distance between the two collection piles is 30m; The three acquisition piles on the right side of the track are... In order B model ,B Model and C model , Similarly, the distance between each collection heap is 20m.5. The shape of the acquisition stack is attached in Appendix 1

% Based on this assumption , Our design idea is when we move to a pile every time , First, collect on this pile of items , because
% The spacing between each stack of items is much greater than the spacing between the modules inside each stack , So in practice, it is impossible to be in two different
% Grab module switching back and forth between heaps , This is also consistent with our above assumptions .
% Based on the above assumption , The order we grab is B Pile up ,C Pile up ,A Pile up ,A Pile up ,B Pile up .
% here , The algorithm we use is local PSO Optimize , Then the whole PSO Optimized algorithm , That is, first through the collection of each pile
% In time PSO Optimize , And make the content of each element meet the constraints , Get the acquisition track with the shortest path , And then through
% Repeat the same optimization algorithm for the next three piles , At the end of the fifth pile , Under the premise of doing the same optimization , At the same time, check whether the total amount meets
% Conditions , If not, enter the next large iteration loop , Then repeat the above operation , Finally, the total acquisition trajectory satisfying the conditions is obtained .

clc;
clear;
close all;
warning off;
pack;

%**********************************************************************************
% Step one ： Call data
% Step one ： Call data

% Divide into ABC Three groups of
A_set = Dat( 1:40 ,:);
B_set = Dat(41:80 ,:);
C_set = Dat(81:120,:);

%A related data
% coordinate
A_POS = A_set(:,1:3);
% Element content
A_FAC = A_set(:,4:8);
% Volume length width height
A_VUM = A_set(:,9:11);

%B related data
% coordinate
B_POS = B_set(:,1:3);
% Element content
B_FAC = B_set(:,4:8);
% Volume length width height
B_VUM = B_set(:,9:11);

%C related data
% coordinate
C_POS = C_set(:,1:3);
% Element content
C_FAC = C_set(:,4:8);
% Volume length width height
C_VUM = C_set(:,9:11);

%**************************************************************************
%**************************************************************************

%**********************************************************************************
% Step two ： Parameter initialization
% Step two ： Parameter initialization
% Constraint parameters
%59999 ~ 60001
Mass_all = 60000;
Mass_err = 1;
% Elements 1
Mass1_max= 65;
Mass1_min= 62;
% Elements 2
Mass2_max= 6;
Mass2_min= 0;
% Elements 3
Mass3_max= 4;
Mass3_min= 0;
% Elements 4
Mass4_max= 0.077;
Mass4_min= 0;
% Elements 5
Mass5_max= 0.1;
Mass5_min= 0;

% Optimize algorithm parameters
% Optimize algorithm parameters
% The number of iterations
Iteration_all = 1;
Iteration_sub = 10000;
% Number of particles
Num_x     = 200;

% density
P         = 2.1;
% Calculate the mass of each module , Company t
% Be careful , There are four modules in a heap in this subject , It's a triangle , Three kinds of trapezoid , So we calculate the volume according to the length, width and height and the corresponding shape , So as to calculate the mass
A_Vulome = func_cal_volume(A_VUM);
B_Vulome = func_cal_volume(B_VUM);
C_Vulome = func_cal_volume(C_VUM);
% Calculate the mass of each module of each collection heap
A_mass   = P*A_Vulome;
B_mass   = P*B_Vulome;
C_mass   = P*C_Vulome;

% The following is set according to the distribution of the heap on the actual track
maxs_sets = [B_mass;C_mass;A_mass;A_mass;B_mass];
FAC_sets  = [B_FAC;C_FAC;A_FAC;A_FAC;B_FAC];

%**************************************************************************
%**************************************************************************

%**********************************************************************************
% Step three ： Start optimization operation
% Step three ： Start optimization operation
X_pos{1} = B_POS(:,1);
Y_pos{1} = B_POS(:,2);
Z_pos{1} = B_POS(:,3);

X_pos{2} = C_POS(:,1);
Y_pos{2} = C_POS(:,2);
Z_pos{2} = C_POS(:,3);

X_pos{3} = A_POS(:,1);
Y_pos{3} = A_POS(:,2);
Z_pos{3} = A_POS(:,3);

X_pos{4} = A_POS(:,1);
Y_pos{4} = A_POS(:,2);
Z_pos{4} = A_POS(:,3);

X_pos{5} = B_POS(:,1);
Y_pos{5} = B_POS(:,2);
Z_pos{5} = B_POS(:,3);

% Through the first PSO Optimize the demand model
for Num_pso = 4:40% There is no need to set it too large , If the setting is large, the demand will certainly exceed 60000, therefore , The value is determined according to the demand , The approximate range
Num_pso
x = zeros(Num_x,Num_pso);

i = 0;

% Generate random particle data that can meet the acquisition rules
for jj = 1:Num_x
% When generating random numbers , The first layer must be collected first , Then collect the second layer , By analogy
% The first 1 layer
index1 = [1:10,41:50,81:90,121:130,161:170];
% The first 2 layer
index2 = [1:10,41:50,81:90,121:130,161:170]+10;
% The first 3 layer
index3 = [1:10,41:50,81:90,121:130,161:170]+20;
% The first 4 layer
index4 = [1:10,41:50,81:90,121:130,161:170]+30;

% Generate random numbers according to the collection rules
% Generate random numbers according to the collection rules
% Generate random numbers according to the collection rules
index  = [index1;index2;index3;index4];

i = 0;
while i < Num_pso
i = i + 1;
if i> 1
for j = 1:50;
index(IS(j),ind(1)) = 9999;
end
end

for j = 1:50;
[VS,IS(j)] = min(index(:,j));
tmps(1,j)  = index(IS(j),j);
end
ind  = randperm(40);
a(i) = tmps(ind(1));
if a(i) == 9999
i = i-1;
end
end

x(jj,:) = a;

end

n             = Num_pso;
F             = fitness_mass(x,maxs_sets,Mass_all);
Fitness_tmps1 = F(1);
Fitness_tmps2 = 1;

for i=1:Num_x
if Fitness_tmps1 >= F(i)
Fitness_tmps1 =  F(i);
Fitness_tmps2 =  i;
end
end
xuhao      = Fitness_tmps2;
Tour_pbest = x;                        % The current individual is optimal
Tour_gbest = x(xuhao,:) ;              % Current global optimal path
Pb         = inf*ones(1,Num_x);        % Individual optimal record
Gb         = F(Fitness_tmps2);         % Group optimal record
xnew1      = x;
N          = 1;

while N <= Iteration_sub
% Calculate Fitness
F = fitness_mass(x,maxs_sets,Mass_all);
for i=1:Num_x
if F(i)<Pb(i)
% Assign the current value to the new best value
Pb(i)=F(i);
Tour_pbest(i,:)=x(i,:);
end

if F(i)<Gb
Gb=F(i);
Tour_gbest=x(i,:);
end
end

Fitness_tmps1 = Pb(1);
Fitness_tmps2 = 1;

for i=1:Num_x
if Fitness_tmps1>=Pb(i)
Fitness_tmps1=Pb(i);
Fitness_tmps2=i;
end
end

nummin = Fitness_tmps2;
% The optimal demand of the current group is poor
Gb(N)  = Pb(nummin);

for i=1:Num_x
% Cross with the individual optimum
c1 = round(rand*(n-2))+1;
c2 = round(rand*(n-2))+1;
while c1==c2
c1 = round(rand*(n-2))+1;
c2 = round(rand*(n-2))+1;
end
chb1 = min(c1,c2);
chb2 = max(c1,c2);
cros = Tour_pbest(i,chb1:chb2);
% Number of cross region elements
ncros= size(cros,2);
% Delete the same element as the crossing area
for j=1:ncros
for k=1:n
if xnew1(i,k)==cros(j)
xnew1(i,k)=0;
for t=1:n-k
temp=xnew1(i,k+t-1);
xnew1(i,k+t-1)=xnew1(i,k+t);
xnew1(i,k+t)=temp;
end
end
end
end
xnew = xnew1;
% Insert cross area
for j=1:ncros
xnew1(i,n-ncros+j) = cros(j);
end
% Judge whether the demand difference decreases
masses=0;
masses = sum(maxs_sets(xnew1(i,:)));
if F(i)>masses
x(i,:) = xnew1(i,:);
end

% Cross with all the best
c1 = round(rand*(n-2))+1;
c2 = round(rand*(n-2))+1;
while c1==c2
c1=round(rand*(n-2))+1;
c2=round(rand*(n-2))+1;
end
chb1 = min(c1,c2);
chb2 = max(c1,c2);
% Cross region matrix
cros = Tour_gbest(chb1:chb2);
% Number of cross region elements
ncros= size(cros,2);
% Delete the same element as the crossing area
for j=1:ncros
for k=1:n
if xnew1(i,k)==cros(j)
xnew1(i,k)=0;
for t=1:n-k
temp=xnew1(i,k+t-1);
xnew1(i,k+t-1)=xnew1(i,k+t);
xnew1(i,k+t)=temp;
end
end
end
end
xnew = xnew1;
% Insert cross area
for j=1:ncros
xnew1(i,n-ncros+j) = cros(j);
end
% Judge whether the demand difference decreases
masses=0;
masses = sum(maxs_sets(xnew1(i,:)));
if F(i)>masses
x(i,:)=xnew1(i,:);
end
% Perform mutation operation
c1          = round(rand*(n-1))+1;
c2          = round(rand*(n-1))+1;
temp        = xnew1(i,c1);
xnew1(i,c1) = xnew1(i,c2);
xnew1(i,c2) = temp;
% Judge whether the demand difference decreases
masses=0;
masses = sum(maxs_sets(xnew1(i,:)));

if F(i)>masses
x(i,:)=xnew1(i,:);
end
end

Fitness_tmps1=F(1);
Fitness_tmps2=1;
for i=1:Num_x
if Fitness_tmps1>=F(i)
Fitness_tmps1=F(i);
Fitness_tmps2=i;
end
end
xuhao      = Fitness_tmps2;
L_best(N)  = min(F);
% The current global optimal demand
Tour_gbest = x(xuhao,:);
N          = N + 1;

end
% Judge whether the content meets the requirements
for ii = 1:5
Fac_tmps(ii) = sum(FAC_sets(Tour_gbest,ii)'.*maxs_sets(Tour_gbest))/sum(maxs_sets(Tour_gbest));
end

if (Fac_tmps(1) >= Mass1_min & Fac_tmps(1) <= Mass1_max) &...
(Fac_tmps(2) >= Mass2_min & Fac_tmps(2) <= Mass2_max) &...
(Fac_tmps(3) >= Mass3_min & Fac_tmps(3) <= Mass3_max) &...
(Fac_tmps(4) >= Mass4_min & Fac_tmps(4) <= Mass4_max) &...
(Fac_tmps(5) >= Mass5_min & Fac_tmps(5) <= Mass5_max)
flag(Num_pso-3) = 1;
else
flag(Num_pso-3) = 0;
end
Mass_fig(Num_pso-3)  = min(L_best);
Mass_Index{Num_pso-3}= Tour_gbest ;
end

figure;
plot(Mass_fig,'b-o');
xlabel(' Number of acquisition modules ');
ylabel(' Difference between calculated demand and standard demand ');

save temp\result1.mat Mass_fig Mass_Index flag

The final optimization demerit , That is, the shortest track length under the condition of satisfaction

A-06-10