function [sys,x0,str,ts] = BFOA_PSO_pwm(t,x,u,flag)
%
%u为输入变量,u(1)为电流,u(2)为电压
%
persistent first;
persistent stop;
persistent i;
persistent mg;
persistent count;
persistent Uin;
%Initialize the parameters
NP = 6; %细菌种群数量
Ped = 0.005; %迁移概率,按照(NU-NL)/(NP*40)计算
%CR = 0.5; %可以控制收敛速度
XL = -1; %修改为pwm基准源最大最小值,[-1,1]
XU = 1;
maxgen = 100;
Nc = 4;
Nre = 3;
MaxStep = 0.1; %迁移的最大速度
persistent w ;
persistent Gbest;
%persistent Gmaxval;
persistent D_out; %输出给sys
persistent D_out_current;
%persistent U; %不再使用
%persistent V;
persistent fitval_current;
persistent fitval_new;
persistent stable_flag; %电路进入稳定状态标记
persistent stable_count; %电路未稳定时,进行计数
persistent fit_order;
persistent Nc_count;
persistent Nre_count;
persistent MoveStep;
persistent MoveStep_PSO;
persistent Pbest;
%persistent Pbestval;
persistent best_index;
persistent c1;
persistent c2;
persistent r1;
persistent r2;
switch flag,
case 0,
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0=[];
str=[];
ts=[0.004 ,0.001];
% initialize the static variables
first = 1;
stop = 0;
i = 0;
mg = 0;
count = 3;
Nc_count = 0;
Nre_count = 0;
D_out = zeros(1, NP); %初始化D_out、U、fitval_current、fitval_new为全0
D_out_current = zeros(1,NP);
best_index = 1;
%U = zeros(1, NP);
%V = zeros(1, NP);
fitval_current = zeros(1, NP);
fitval_new = zeros(1, NP);
fit_order = zeros(1, NP);
Pbest = zeros(1, NP);
Gbest = 0;
%Gmaxval = 0;
%X(:) = unifrnd(XL, XU, 1,NP)
D_out_current(:) = linspace(XL+0.005,XU-0.005,NP); %在[-1,1]间均匀取值
D_out(:) = D_out_current(:);
MoveStep = unifrnd(-MaxStep, MaxStep, 1,NP); %初始化单个细菌的移动速度
MoveStep_PSO = zeros(1, NP);
stable_flag = 1; %初始化,电路未稳定
stable_count = 0;
c1 = 0.030;
c2 = 0.030;
r1 = rand();
r2 = rand();
case 3,
%if count == 3
%count = count + 1;
count = 1;
%迭代完成,输出最优值
if stop == 1;
[best, best_index]= max(fitval_current);
sys = D_out(best_index);
return;
end
%判断是否迭代完成
if mg > maxgen
stop = 1;
end
if mg == 0 % 第一代,只进行迭代,然后计算各自功率,作为初始比较功率fitval_current
if i == 0
i= i + 1;
sys = D_out_current(1);
elseif i > 1 && i < NP
fitval_current(i-1) = adaptfunc(D_out_current(i-1),Uin); %记录第一代的上一个个体的功率
Pbest(i-1) = D_out_current(i-1);
i= i + 1;
sys = D_out_current(i);
elseif i == NP
fitval_current(i-1) = adaptfunc(D_out_current(i-1),Uin);
Pbest(i-1) = D_out_current(i-1);
i= i + 1;
sys = D_out_current(NP);
elseif i == NP + 1
fitval_current(i-1) = adaptfunc(D_out_current(i-1),Uin);
Pbest(i-1) = D_out_current(i-1);
[best, best_index]= max(fitval_current);
Gbest = D_out_current(best_index);
i = 1;
Nc_count = Nc_count + 1;
Nre_count = Nre_count + 1;
mg = mg + 1;
for j = 1 : NP %第一代种群中个体进行游动
MoveStep_PSO = c1*r1*(Pbest(j)-D_out_current(j)) + c2*r2*(Gbest-D_out_current(j));
D_out(j) = D_out_current(j)+ MoveStep(j) + MoveStep_PSO;
if D_out(j) <= XL
D_out(j) = XL+0.001;
elseif D_out(j) >= XU
D_out(j) = XU-0.001;
end
end
sys = D_out(1);
else
i= i + 1;
sys = D_out_current(i);
end
return;
else %第一代之后
if i > 1 && i < NP
fitval_new(i-1) =adaptfunc(D_out(i-1), Uin); %计算前一次占空比下的功率
Pbest(i-1) = D_out(i-1);
i = i + 1;
sys = D_out(i);
elseif i == NP
fitval_new(i-1) =adaptfunc(D_out(i-1), Uin);
Pbest(i-1) = D_out(i-1);
i = i + 1;
sys = D_out(NP);
elseif i == 1
i = i + 1;
sys = D_out(i);
elseif i == NP + 1 %种群迭代一次结束,开始进行新的一代种群繁殖
%趋向性操作,判定运动方向
fitval_new(i-1) =adaptfunc(D_out(i-1), Uin);
Pbest(i-1) = D_out(i-1);
r1 = rand();
r2 = rand();
for j = 1 : NP
if (fitval_new(j) >= fitval_current(j))
D_out_current(j) = D_out(j);
fitval_current(j) = fitval_new(j);
else
MoveStep(j) = - MoveStep(j); %若该方向功率未改进,说明不适应生存,改变方向运动
end
end
c1 = c1/3;
c2 = c2/3;
D_out(:) %打印最新的占空比
[best, best_index]= max(fitval_current); %计算最大功率点,best为最大功率,best_index为最大功率点在种群中的位置
mg
D_out(best_index) %显示最大功率点的占空比
%Gbest = U(best_index) %显示最大功率点的电压
%Gmaxval = best %显示最大功率点的功率
best
fit_order = order(fitval_current,NP) %显示当前种群功率从大到小顺序
Gbest = D_out(best_index);
% mutation
if Nc_count == Nc %进行满Nc次趋向性操作
Nc_count = 0;
Nre_count = Nre_count + 1;
%复制操作
MaxStep = MaxStep/2;
MoveStep = unifrnd(-MaxStep,MaxStep,1,NP); %每Nc次繁殖生成一次新的随机步长
[fitval_current,D_out_current]=Reproduction(fitval_current,D_out_current,NP); %复制
D_out=D_out_current;
if Nre_count == Nre %进行满Nre次复制操作后迁移操作
Nre_count = 0;
for j=1:NP
if(rand(0,1)<Ped)
fitval_current = 0;
%MoveStep(i) = unifrnd(-MaxStep,MaxStep);
D_out(j) = unifrnd(XL,XU);
end
end
end
else
Nc_count = Nc_count + 1;
for j=1:NP
MoveStep_PSO = c1*r1*(Pbest(j)-D_out(j)) + c2*r2*(Gbest-D_out(j));
D_out(j) = D_out(j) + MoveStep(j) + MoveStep_PSO;
if D_out(j) <= XL
D_
- 1
- 2
- 3
- 4
- 5
- 6
前往页