基于Matlab工具箱遗传算法和粒子群算法的直线阵列低副瓣方向图综合

本文利用matlab进行了直线阵列天线的低副瓣方向图综合仿真实验。主要利用到了Matlab工具箱里面的遗传算法以及粒子群算法。为了比较方向图结果,也用到了泰勒综合法以及切比雪夫综合法进行方向图综合


主要物理参数及变量说明
单元数量及排布: 30个全向单元(Nantenna值可以修改),沿着x轴方向分布,单元间距0.5倍波长

副瓣电平约束: SLL dB=-30dB(可以修改)

扫描角度: thetas可以取值-50°-50°(不起波束恶化)

算法参数: 种群规模,迭代次数,变异概率等等可以酌情修改(依托于Matlab自带工具箱)


 仿真结果

图1 阵元分布
图2 -50°波束扫描仿真结果(-30dB电平约束)
0°波束扫描仿真结果(-27dB电平约束)
+33°波束扫描仿真结果(-30dB电平约束)

 主程序代码: 

%程序功能:遗传算法和粒子群算法直线阵低副瓣方向图综合
%AUthor>> xtang225@foxmail.com
clc ;clear all;close all;
global SLL_dB SLL;
global AF;
global expphase0;
global widtheta theta side_flag scan_index;;
%%
lambda=1;
k=2*pi/lambda;
dx=0.5*lambda;
Np=180;%方向图采点数
theta=deg2rad(linspace(-90,90,Np));
thetas=deg2rad(0);%scannin angle
widtheta=deg2rad(6);%主瓣宽度,可以调整
SLL_dB=-27;%可以修改
SLL=10^(SLL_dB/20);
%% 生成阵列
Nantenna=30;
xx=linspace(-0.5*(Nantenna-1)*dx,0.5*(Nantenna-1)*dx,Nantenna);
zz=zeros(Nantenna,1);
yy=zeros(Nantenna,1);
plot3(xx/lambda,yy/lambda,zz/lambda,'.','MarkerSize',35,'Color','red');
xlabel('x/\lambda','FontSize',16);
ylabel('y/\lambda','FontSize',16);
zlabel('z/\lambda','FontSize',16);
title('阵列单元分布图','FontSize',22)
axis equal;
%% 计算阵列因子
AF=zeros(Np,Nantenna);%每一列存储一个天线的导向矢量
for n=1:Nantenna
    xn=xx(n);yn=yy(n);zn=zz(n);
    AF(:,n)=exp(1j*k*xn*sin(theta));
end
%% 确定主瓣副瓣区域
side_flag=zeros(Np,1);
[~,scan_index]=min(abs(theta-thetas));%扫描方向对应的索引值
for ind=1:Np
    if(abs(theta(ind)-thetas)>widtheta)
        side_flag(ind)=1;
    end
end
%% 计算初始的移相值,根据波束扫描公式计算初始移相值
expphase0=zeros(Nantenna,1);
for n=1:Nantenna
    xn=xx(n);yn=yy(n);zn=zz(n);
    expphase0(n)=exp(-1j*k*xn*sin(thetas));
end

%
%% 利用遗传算法来进行低副瓣
choice=1;%选择优化算法
% rng(2024);% 设置种子以便复现结果
Nvars=Nantenna;%粒子群优化的变量个数,
down= -1*ones(1,Nvars);%变量下限都为-1
up=ones(1,Nvars);%变量上限都为1
X=rand(1,Nvars);
%粒子群算法
options1= optimoptions('particleswarm', ...
    'PlotFcn','pswplotbestf', ...
    'SwarmSize',100, ...
    'HybridFcn', @fmincon, ...
    'MaxStallIterations',20);
x_pso=particleswarm(@Linearfitness,Nvars,down,up,options1) ;
%遗传算法
options2 = optimoptions('ga', ...
    'MaxGenerations', 600, ...         % 最大迭代次数
    'PopulationSize', 100, ...           % 种群大小
    'MutationFcn', {@mutationadaptfeasible, 0.05}, ...  % 变异函数
    'CrossoverFraction', 0.8, ...       % 交叉概率
    'SelectionFcn', @selectionroulette, ... % 选择函数
    'PlotFcn', {@gaplotbestf, @gaplotstopping}); % 绘图函数
x_ga=ga(@Linearfitness,Nantenna, [], [], [], [], down, up, [], options2);


%% 优化之后的结果可视化
% 利用泰勒分布和切比雪夫分布
Amp_taylor=taylorwin(Nantenna,6,SLL_dB);
Pha_taylor=angle(expphase0);
Amp_cheb=chebwin(Nantenna,-SLL_dB);%转置?
Pha_cheb=angle(expphase0);
Amp_pso=(x_pso./max(abs(x_pso)))';%对优化之后的幅度进行归一化处理
Pha_pso=angle(expphase0);

S_pso_abs=abs(AF*(Amp_pso.*exp(1j*Pha_pso)));
S_max=max(S_pso_abs);
S_pso_norm=S_pso_abs/S_max;
S_pso_dB=20*log10(S_pso_norm);

Amp_ga=(x_ga./max(abs(x_ga)))';
Pha_ga=angle(expphase0);
S_ga_abs=abs(AF*(Amp_ga.*exp(1j*Pha_ga)));
S_max=max(S_ga_abs);
S_ga_norm=S_pso_abs/S_max;
S_ga_dB=20*log10(S_ga_norm);


S_taylor_abs=abs(AF*(Amp_taylor.*exp(1j*Pha_taylor)));
S_max=max(S_taylor_abs);
S_taylor_norm=S_taylor_abs/S_max;
S_taylor_dB=20*log10(S_taylor_norm);

S_cheb_abs=abs(AF*(Amp_cheb.*exp(1j*Pha_cheb)));
S_max=max(S_cheb_abs);
S_cheb_norm=S_cheb_abs/S_max;
S_cheb_dB=20*log10(S_cheb_norm);


figure()
plot(rad2deg(theta),S_pso_dB,'LineWidth',1.2,'color','red');hold on
plot(rad2deg(theta),S_ga_dB,'LineWidth',1.2,'color','blue');hold on
plot(rad2deg(theta),S_taylor_dB,'LineWidth',1.2);hold on
plot(rad2deg(theta),S_cheb_dB,'LineWidth',1.2);hold on

plot(rad2deg(theta),SLL_dB*ones(1,Np),'--','LineWidth',1.2,'Color','black');hold on
xlabel('\theta/°','FontSize',16);ylabel('Pattern/dB','FontSize',16);
title({['优化之后的方向图对比']},{['thetas=',num2str(rad2deg(thetas)),'°']},'FontSize',12);
legend('PSO','GA','TaylorWin','ChebexuvWin','Constraint SLL')
xlim([-91,91]);
ylim([-40,0])
%%
disp('程序运行结束!')

 适应度函数代码

选则副瓣电平的最大值与设定的电平值之间的误差值作为目标函数,整个优化过程对目标函数进行极小化,即让峰值副瓣电平值被压到和设定的电平值最接近

function value=Linearfitness(X)
%程序功能:计算线阵低副瓣的适应读函数
%% 
global SLL_dB SLL;
global AF;
global expphase0;
global widtheta theta side_flag scan_index;
%
w0=X';%转成列向量
w=w0.*expphase0;%复数权值
%% 
S_abs=abs(AF*w);
S_max=max(S_abs);
S_norm=S_abs./S_max;
S_dB=20*log10(S_norm);

temp1=1-S_norm(scan_index);%控制主瓣在扫描的方向
%找到副瓣的电平峰值
side_index=find(side_flag==1);%将副瓣的索引提取出来
side_pattern=S_norm(side_index,1);

%利用对数值来计算
temp1=0-S_dB(scan_index);%控制主瓣在扫描的方向
%找到副瓣的电平峰值
side_index=find(side_flag==1);%将副瓣的索引提取出来
side_pattern_dB=S_dB(side_index,1);
[mSLL_dB,~]=max(side_pattern_dB);%最大的副瓣电平
maxSLL=mSLL_dB(1);%防止有多个最大值
value=abs(mSLL_dB-SLL_dB);

%% 


end

 本文程序仅作为公众号的读者学习参考,不宜用于其他途径!

 

 

遗传算法matlab程序(2009-04-14 18:25:19)转载标签: 遗传算法二进制编码if杂谈 遗传算法程序: 说明: fga.m 为遗传算法的主程序; 采用二进制Gray编码,采用基于轮盘赌法的非线性排名选择, 均匀交叉,变异操作,而且还引入了倒位操作! function [BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options) % [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation) % Finds a maximum of a function of several variables. % fmaxga solves problems of the form: % max F(X) subject to: LB <= X <= UB % BestPop - 最优的群体即为最优的染色体群 % Trace - 最佳染色体所对应的目标函数值 % FUN - 目标函数 % LB - 自变量下限 % UB - 自变量上限 % eranum - 种群的代数,取100--1000(默认200) % popsize - 每一代种群的规模;此可取50--200(默认100) % pcross - 交叉概率,一般取0.5--0.85之间较好(默认0.8) % pmutation - 初始变异概率,一般取0.05-0.2之间较好(默认0.1) % pInversion - 倒位概率,一般取0.05-0.3之间较好(默认0.2) % options - 1*2矩,options(1)=0二进制编码(默认0),option(1)~=0十进制编 %码,option(2)设定求解精度(默认1e-4) % % ------------------------------------------------------------------------ T1=clock; if nargin<3, error('FMAXGA requires at least three input arguments'); end if nargin==3, eranum=200;popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end if nargin==4, popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end if nargin==5, pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end if nargin==6, pMutation=0.1;pInversion=0.15;options=[0 1e-4];end if nargin==7, pInversion=0.15;options=[0 1e-4];end if find((LB-UB)>0) error('数据输入错误,请重新输入(LB<UB):'); end s=sprintf('程序运行需要约%.4f 秒钟时间,请稍等......',(eranum*popsize/1000)); disp(s); global m n NewPop children1 children2 VarNum bounds=[LB;UB]';bits=[];VarNum=size(bounds,1); precision=options(2);%由求解精度确定二进制编码长度 bits=ceil(log2((bounds(:,2)-bounds(:,1))' ./ precision));%由设定精度划分区间 [Pop]=InitPopGray(popsize,bits);%初始化种群 [m,n]=size(Pop); NewPop=zeros(m,n); children1=zeros(1,n); children2=zeros(1,n); pm0=pMutation; BestPop=zeros(eranum,n);%分配初始解空间BestPop,Trace Trace=zeros(eranum,length(bits)+1); i=1; while i<=eranum for j=1:m value(j)=feval_r(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%计算适应度 end [MaxValue,Index]=max(value); BestPop(i,:)=Pop(Index,:); Trace(i,1)=MaxValue; Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits); [selectpop]=NonlinearRankSelect(FUN,Pop,bounds,bits);%非线性排名选择 [CrossOverPop]=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum)); %采用多点交叉均匀交叉,且逐步增大均匀交叉的概率 %round(unidrnd(eranum-i)/eranum) [MutationPop]=Mutation(CrossOverPop,pMutation,VarNum);%变异 [InversionPop]=Inversion(MutationPop,pInversion);%倒位 Pop=InversionPop;%更新 pMutation=pm0+(i^4)*(pCross/3-pm0)/(eranum^4); %随着种群向前进化,逐步增大变异率至1/2交叉率 p(i)=pMutation; i=i+1; end t=1:eranum; plot(t,Trace(:,1)'); title('函数优化遗传算法');xlabel('进化世代数(eranum)');ylabel('每一代最优适应度(maxfitness)'); [MaxFval,I]=max(Trace(:,1)); X=Trace(I,(2:length(bits)+1)); hold on; plot(I,MaxFval,'*'); text(I+5,MaxFval,['FMAX=' num2str(MaxFval)]); str1=sprintf('进化到 %d 代 ,自变量为 %s 时,得本次求解的最优值 %f\n对应染色体是:%s',I,num2str(X),MaxFval,num2str(BestPop(I,:))); disp(str1); %figure(2);plot(t,p);%绘制变异值增大过程 T2=clock; elapsed_time=T2-T1; if elapsed_time(6)<0 elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1; end if elapsed_time(5)<0 elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1; end %像这种程序当然不考虑运行上小时啦 str2=sprintf('程序运行耗时 %d 小时 %d 分钟 %.4f 秒',elapsed_time(4),elapsed_time(5),elapsed_time(6)); disp(str2); %初始化种群 %采用二进制Gray编码,其目的是为了克服二进制编码的Hamming悬崖缺点 function [initpop]=InitPopGray(popsize,bits) len=sum(bits); initpop=zeros(popsize,len);%The whole zero encoding individual for i=2:popsize-1 pop=round(rand(1,len)); pop=mod(([0 pop]+[pop 0]),2); %i=1时,b(1)=a(1);i>1时,b(i)=mod(a(i-1)+a(i),2) %其中原二进制串:a(1)a(2)...a(n),Gray串:b(1)b(2)...b(n) initpop(i,:)=pop(1:end-1); end initpop(popsize,:)=ones(1,len);%The whole one encoding individual %解码 function [fval] = b2f(bval,bounds,bits) % fval - 表征各变量的十进制数 % bval - 表征各变量的二进制编码串 % bounds - 各变量的取值范围 % bits - 各变量的二进制编码长度 scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables numV=size(bounds,1); cs=[0 cumsum(bits)]; for i=1:numV a=bval((cs(i)+1):cs(i+1)); fval(i)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1); end %选择操作 %采用基于轮盘赌法的非线性排名选择 %各个体成员按适应值从大到小分配选择概率: %P(i)=(q/1-(1-q)^n)*(1-q)^i, 其中 P(0)>P(1)>...>P(n), sum(P(i))=1 function [selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits) global m n selectpop=zeros(m,n); fit=zeros(m,1); for i=1:m fit(i)=feval_r(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函数值为适应值做排名依据 end selectprob=fit/sum(fit);%计算各个体相对适应度(0,1) q=max(selectprob);%选择最优的概率 x=zeros(m,2); x(:,1)=[m:-1:1]'; [y x(:,2)]=sort(selectprob); r=q/(1-(1-q)^m);%标准分布基值 newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成选择概率 newfit=cumsum(newfit);%计算各选择概率之 rNums=sort(rand(m,1)); fitIn=1;newIn=1; while newIn<=m if rNums(newIn)<newfit(fitIn) selectpop(newIn,:)=pop(fitIn,:); newIn=newIn+1; else fitIn=fitIn+1; end end %交叉操作 function [NewPop]=CrossOver(OldPop,pCross,opts) %OldPop为父代种群,pcross为交叉概率 global m n NewPop r=rand(1,m); y1=find(r<pCross); y2=find(r>=pCross); len=length(y1); if len>2&mod(len,2)==1%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数 y2(length(y2)+1)=y1(len); y1(len)=[]; end if length(y1)>=2 for i=0:2:length(y1)-2 if opts==0 [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:)); else [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:)); end end end NewPop(y2,:)=OldPop(y2,:); %采用均匀交叉 function [children1,children2]=EqualCrossOver(parent1,parent2) global n children1 children2 hidecode=round(rand(1,n));%随机生成掩码 crossposition=find(hidecode==1); holdposition=find(hidecode==0); children1(crossposition)=parent1(crossposition);%掩码为1,父1为子1提供基因 children1(holdposition)=parent2(holdposition);%掩码为0,父2为子1提供基因 children2(crossposition)=parent2(crossposition);%掩码为1,父2为子2提供基因 children2(holdposition)=parent1(holdposition);%掩码为0,父1为子2提供基因 %采用多点交叉,交叉点数由变量数决定 function [Children1,Children2]=MultiPointCross(Parent1,Parent2) global n Children1 Children2 VarNum Children1=Parent1; Children2=Parent2; Points=sort(unidrnd(n,1,2*VarNum)); for i=1:VarNum Children1(Points(2*i-1):Points(2*i))=Parent2(Points(2*i-1):Points(2*i)); Children2(Points(2*i-1):Points(2*i))=Parent1(Points(2*i-1):Points(2*i)); end %变异操作 function [NewPop]=Mutation(OldPop,pMutation,VarNum) global m n NewPop r=rand(1,m); position=find(r<=pMutation); len=length(position); if len>=1 for i=1:len k=unidrnd(n,1,VarNum); %设置变异点数,一般设置1点 for j=1:length(k) if OldPop(position(i),k(j))==1 OldPop(position(i),k(j))=0; else OldPop(position(i),k(j))=1; end end end end NewPop=OldPop; %倒位操作 function [NewPop]=Inversion(OldPop,pInversion) global m n NewPop NewPop=OldPop; r=rand(1,m); PopIn=find(r<=pInversion); len=length(PopIn); if len>=1 for i=1:len d=sort(unidrnd(n,1,2)); if d(1)~=1&d(2)~=n NewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1); NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1)); NewPop(PopIn(i),d(2)+1:n)=OldPop(PopIn(i),d(2)+1:n); end end end
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值