From f2a15388eb0229706adb077fc6fb57f8690f06c9 Mon Sep 17 00:00:00 2001 From: Oleksiy Penkov Date: Fri, 10 Feb 2023 10:59:13 +0800 Subject: [PATCH] + LFPSO files --- Matlab_LFPSO/Calc_refl.m | 14 +++ Matlab_LFPSO/Tent.m | 14 +++ Matlab_LFPSO/extract_H.m | 26 +++++ Matlab_LFPSO/initializationNew.m | 37 ++++++++ Matlab_LFPSO/input_stucture.m | 27 ++++++ Matlab_LFPSO/levy.m | 35 +++++++ Matlab_LFPSO/main_CPSO_v2_vp1.m | 157 +++++++++++++++++++++++++++++++ Matlab_LFPSO/main_CPSO_v2_vp2.m | 131 ++++++++++++++++++++++++++ Matlab_LFPSO/main_LFPSO.m | 101 ++++++++++++++++++++ Matlab_LFPSO/main_function.m | 44 +++++++++ Matlab_LFPSO/optimal_solution.m | 30 ++++++ 11 files changed, 616 insertions(+) create mode 100644 Matlab_LFPSO/Calc_refl.m create mode 100644 Matlab_LFPSO/Tent.m create mode 100644 Matlab_LFPSO/extract_H.m create mode 100644 Matlab_LFPSO/initializationNew.m create mode 100644 Matlab_LFPSO/input_stucture.m create mode 100644 Matlab_LFPSO/levy.m create mode 100644 Matlab_LFPSO/main_CPSO_v2_vp1.m create mode 100644 Matlab_LFPSO/main_CPSO_v2_vp2.m create mode 100644 Matlab_LFPSO/main_LFPSO.m create mode 100644 Matlab_LFPSO/main_function.m create mode 100644 Matlab_LFPSO/optimal_solution.m diff --git a/Matlab_LFPSO/Calc_refl.m b/Matlab_LFPSO/Calc_refl.m new file mode 100644 index 0000000..e1b4f49 --- /dev/null +++ b/Matlab_LFPSO/Calc_refl.m @@ -0,0 +1,14 @@ +function Chi_Square=Calc_refl(T,Size) + + for i=1:Size + layers(:,:)=T(:,:,i);%A three-dimensional array turns into a two-dimensional array + Layers=array2table(layers); + filename= ['.\Input\input_structure_' num2str(i) '.txt']; + writetable(Layers, filename, 'Delimiter',';','WriteVariableNames',false); + end +system("xrccmd.exe -f input_structure_#.txt -i exp_data.dat -n "+num2str(Size)); +fileID = fopen("ChiSquare.dat","r"); +formatSpec = '%f'; +A = fscanf(fileID,formatSpec); +fclose(fileID); +Chi_Square = A; \ No newline at end of file diff --git a/Matlab_LFPSO/Tent.m b/Matlab_LFPSO/Tent.m new file mode 100644 index 0000000..2396017 --- /dev/null +++ b/Matlab_LFPSO/Tent.m @@ -0,0 +1,14 @@ + %Tent映射 + function [x] = Tent(Max_iter,a) + x(1)=rand; %初始点 +% a = 0.5;%参数a的值 + for i=1:Max_iter-1 + if x(i)=a + x(i+1)=2*(1-x(i)) ; + end + end + end + \ No newline at end of file diff --git a/Matlab_LFPSO/extract_H.m b/Matlab_LFPSO/extract_H.m new file mode 100644 index 0000000..cc020f9 --- /dev/null +++ b/Matlab_LFPSO/extract_H.m @@ -0,0 +1,26 @@ +function [x,y1,y2,y3]=extract_H(~) +filename = 'current_best_structure.txt'; +fileID = fopen(filename); +A = textscan(fileID,'%*q %f %*f %*f','Delimiter',';'); +B=cell2mat(A);% whole thickness + +%% extract Boron thickness +B1=B(3:end-2); +n = length(B1); +y1 = B1(1:4:n); +x=linspace(1,length(y1),length(y1)); % number of layer + +%% extract MoB2 thickness +B2=B(4:end-2); +y2= B2(1:4:n); + +%% extract Mo thickness +B3=B(5:end-2); +y3= B3(1:4:n); + +%% plot +% plot(x,y1);hold on +% plot(x,y2);hold on +% plot(x,y3); +% ylim([0 B1(1)+10]) +% xlabel('Layer number'); ylabel('Thickness(A)'); \ No newline at end of file diff --git a/Matlab_LFPSO/initializationNew.m b/Matlab_LFPSO/initializationNew.m new file mode 100644 index 0000000..cbc46aa --- /dev/null +++ b/Matlab_LFPSO/initializationNew.m @@ -0,0 +1,37 @@ +%% 基于Tent映射的种群初始化 +function Positions=initializationNew(SearchAgents_no,dim,ub,lb,a) +Boundary_no= size(ub,2); % numnber of boundaries +Positions=zeros(SearchAgents_no,Boundary_no); +% If the boundaries of all variables are equal and user enter a signle +% number for both ub and lb +if Boundary_no==1 + for i = 1:SearchAgents_no + Temp = Tent(dim,a).*(ub-lb)+lb; + Temp(Temp>ub) = ub; + Temp(Temp1 + for j = 1:SearchAgents_no + TentValue = Tent(dim,a); + for i=1:dim + ub_i=ub(i); + lb_i=lb(i); + Positions(j,i)=TentValue(i).*(ub_i-lb_i)+lb_i; + if(Positions(j,i)>ub_i) + Positions(j,i) = ub_i; + end + if(Positions(j,i) Number of steps +% m -> Number of Dimensions +% beta -> Power law index % Note: 1 < beta < 2 + +% Output +% z -> 'n' levy steps in 'm' dimension + + num = gamma(1+beta)*sin(pi*beta/2); % used for Numerator + + den = gamma((1+beta)/2)*beta*2^((beta-1)/2); % used for Denominator + + sigma_u = (num/den)^(1/beta);% Standard deviation +% +% u = random('Normal',0,sigma_u^2,n,m); +% +% v = random('Normal',0,1,n,m); + u = normrnd(0,sigma_u,n,m); + + v = normrnd(0,1,n,m); + + z = u./(abs(v).^(1/beta)); + + +end + + + diff --git a/Matlab_LFPSO/main_CPSO_v2_vp1.m b/Matlab_LFPSO/main_CPSO_v2_vp1.m new file mode 100644 index 0000000..443820e --- /dev/null +++ b/Matlab_LFPSO/main_CPSO_v2_vp1.m @@ -0,0 +1,157 @@ +function [best_x , f_gg]=main_CPSO_v2_vp1(Stucture,Xmax,Xmin,Size,Tmax) +%% PSO parameters +dimension=length(Xmax);%dimension +% Size=4;% Size +% Tmax=3;%%iteration Tmax +c1=1.49445; +c2=1.49445;%study factor + w_max = 0.8; + w_min = 0.1; +k=0.2;%Proportional relationship between position and velocity +Vmax= Xmax*k; +Vmin=-Vmax; +u=3.999; +MaxC=10; +a=0.5; +% X=zeros(Size,dimension); +% V=zeros(Size,dimension); +% f_x=zeros(1,Size); +% f_xbest=zeros(1,Size); +%% initialization population +X = rand(Size,dimension).*(Xmax-Xmin)+Xmin; +V = rand(Size,dimension).*(Vmax-Vmin)+Vmin; +Pi=X; +for j=1:Size + structure_3D(:,:,j)=Stucture(X(j,:));% used for generating Size structures +end +f_x=Calc_refl(structure_3D,Size);% used for generating Chi-square +f_xbest=f_x; +[f_g,I]=min(f_xbest); +Pg=X(I,:); +time=zeros(1,Tmax); +f_gg=zeros(1,Tmax); +%% iteration +for t=1:Tmax + time(t)=t; + fprintf('Current evolution number:%d\n',t) + w = w_max-(w_max-w_min)*t/Tmax; %Adaptive inertia weights (linearly decreasing) + r1=rand(1);r2=rand(1); + for j=1:Size + V(j,:) = w*V(j,:) + c1*r1*(Pi(j,:)-X(j,:)) + c2*r2*(Pg-X(j,:)); %Speed Update + index1 = find(V(j,:)>Vmax); + V(j,index1) = rand*(Vmax(index1)-Vmin(index1))+Vmin(index1); + index1= find(V(j,:)Xmax); + X(j,index1) = rand*(Xmax(index1)-Xmin(index1))+Xmin(index1); + index1= find(X(j,:)Vmax); + V(j,index1) = Vmax(index1); + index1= find(V(j,:)Xmax); + X(j,index1) = Xmax(index1); + index1= find(X(j,:) Calc_refl(structure_3D1(:,:,i),1) + X(i,:)=childx1(i,:); %子代的位置替换父代位置 + V(i,:)=childv1(i,:); %子代的速度替换父代速度 + end + end + %%进行高斯变异 + mutationpool=round(pm*Size); + for i=1:mutationpool %选择要进行变异的粒子 + seed3=floor(rand()*mutationpool)+1; + mutationchild(i,:)=X(seed3,:)*(1+ randn); + structure_3D2(:,:,i)=Stucture( mutationchild(i,:)); + if Calc_refl(structure_3D(:,:,i),1)> Calc_refl(structure_3D2(:,:,i),1) + X(i,:)=mutationchild(i,:); %子代的位置替换父代位置 + end + end + for j=1:Size + structure_3D(:,:,j)=Stucture(X(j,:));% used for generating updated Size 3 dimension structures structure_3D + end + f_x=Calc_refl(structure_3D,Size); % used for generating updated Chi-square f_x + for j=1:Size + if f_x(j)Vmax); + V(j,index1) = rand*(Vmax(index1)-Vmin(index1))+Vmin(index1); + index1= find(V(j,:)Xmax); + X(j,index1) = rand*(Xmax(index1)-Xmin(index1))+Xmin(index1); + index1= find(X(j,:)Vmax); + V(j,index1) = rand*(Vmax(index1)-Vmin(index1))+Vmin(index1); + index1= find(V(j,:)Xmax); + X(j,index1) = rand*(Xmax(index1)-Xmin(index1))+Xmin(index1); + index1= find(X(j,:)