clear all
clc
%%%%%%%%%%%
SearchAgents_no=200; % 設定個數
Ts1 = [ 3.0448 1];%Ts1低通濾波器初始引數
Ts2 = [ 13.94501];%Ts2低通濾波器初始引數
Usc =48*6; %超級電容單體個數
Rsc = 7*6.3e-3;%超級電容內阻
F = 165/(Usc/48);%超級電容容量
Ahbat=30;%電池容量
Max_iteration=50; % 智能尋優演算法
lb = [1,10,300,40];%優化引數上界
ub = [5,15,500,60];%優化引數下界
dim = 4;%優化引數個數
fobj = @costfunction
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
figure('Position',[269 240 660 290])
semilogy(WOA_cg_curve,'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');
plot(WOA_cg_curve,'LineWidth',2,'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid on
box on
function [Elite_antlion_fitness,Elite_antlion_position,Convergence_curve]=ALO(SearchAgents_no,Max_iter,lb,ub,dim,fobj)
% Initialize the positions of antlions and ants
antlion_position=initialization(SearchAgents_no,dim,ub,lb);
ant_position=initialization(SearchAgents_no,dim,ub,lb);
% Initialize variables to save the position of elite, sorted antlions,
% convergence curve, antlions fitness, and ants fitness
Sorted_antlions=zeros(SearchAgents_no,dim);
Elite_antlion_position=zeros(1,dim);
Elite_antlion_fitness=inf;
Convergence_curve=zeros(1,Max_iter);
antlions_fitness=zeros(1,SearchAgents_no);
ants_fitness=zeros(1,SearchAgents_no);
% Calculate the fitness of initial antlions and sort them
for i=1:size(antlion_position,1)
antlions_fitness(1,i)=fobj(antlion_position(i,:));
end
[sorted_antlion_fitness,sorted_indexes]=sort(antlions_fitness);
for newindex=1:SearchAgents_no
Sorted_antlions(newindex,:)=antlion_position(sorted_indexes(newindex),:);
end
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=sorted_antlion_fitness(1);
% Main loop start from the second iteration since the first iteration
% was dedicated to calculating the fitness of antlions
Current_iter=2;
while Current_iter<Max_iter+1
% This for loop simulate random walks
for i=1:size(ant_position,1)
% Select ant lions based on their fitness (the better anlion the higher chance of catching ant)
Rolette_index=RouletteWheelSelection(1./sorted_antlion_fitness);
if Rolette_index==-1
Rolette_index=1;
end
% RA is the random walk around the selected antlion by rolette wheel
RA=Random_walk_around_antlion(dim,Max_iter,lb,ub, Sorted_antlions(Rolette_index,:),Current_iter);
% RA is the random walk around the elite (best antlion so far)
[RE]=Random_walk_around_antlion(dim,Max_iter,lb,ub, Elite_antlion_position(1,:),Current_iter);
ant_position(i,:)= (RA(Current_iter,:)+RE(Current_iter,:))/2; % Equation (2.13) in the paper
end
for i=1:size(ant_position,1)
% Boundar checking (bring back the antlions of ants inside search
% space if they go beyoud the boundaries
Flag4ub=ant_position(i,:)>ub;
Flag4lb=ant_position(i,:)<lb;
ant_position(i,:)=(ant_position(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
ants_fitness(1,i)=fobj(ant_position(i,:));
All_fitness(1,i)=ants_fitness(1,i);
end
% Update antlion positions and fitnesses based of the ants (if an ant
% becomes fitter than an antlion we assume it was cought by the antlion
% and the antlion update goes to its position to build the trap)
double_population=[Sorted_antlions;ant_position];
double_fitness=[sorted_antlion_fitness ants_fitness];
[double_fitness_sorted I]=sort(double_fitness);
double_sorted_population=double_population(I,:);
antlions_fitness=double_fitness_sorted(1:SearchAgents_no);
Sorted_antlions=double_sorted_population(1:SearchAgents_no,:);
% Calculate objective function for each search agent
a1=0.5;%電池損傷所占權重
a2=0.2;%電網波動所占權重
a3=0.3;%經濟性所占權重
X = ant_position(i,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rsc = (X(3)/48)*6e-3; %計算超級電容電阻
Rbat = 0.4/(X(4)/10);%計算電池電阻
Ts1=X(1);
Ts2=X(2);
F=165/(X(3)/48);
Usc=X(3);
Ahbat=X(4);
%%%%%%%%%%%%%%%%設定模型引數%%%%%%%%%%%%%%%%%%%%
set_param('energy_soure_designV2/Battery1','NomQ',num2str(Ahbat))
set_param('energy_soure_designV2/Battery1','R',num2str(Rbat))
set_param('energy_soure_designV2/Saturation1','UpperLimit',num2str(Ahbat*560))
set_param('energy_soure_designV2/Saturation1','LowerLimit',num2str(-Ahbat*560))
set_param('energy_soure_designV2/Constant','Value',num2str(Usc*0.85))
set_param('energy_soure_designV2/Supercapacitor1','Voltage',num2str(Usc))
set_param('energy_soure_designV2/Supercapacitor1','Vinit',num2str(Usc*0.85))
set_param('energy_soure_designV2/Supercapacitor1','C',num2str(F))
set_param('energy_soure_designV2/Supercapacitor1','Rdc',num2str(Rsc))
set_param('energy_soure_designV2/Constant6','Value',num2str(Ts1))
set_param('energy_soure_designV2/Constant8','Value',num2str(Ts2))
%set_param('energy_soure_designV2/ts1','Denominator',num2str([Ts1 1]))
%set_param('energy_soure_designV2/ts2','Denominator',num2str([Ts2 1]))
%運行模型
sim('energy_soure_designV2');
%資料處理——船舶機動航行模型處理
Ibat(1,:)=[];
I= Ibat(:,2);
Vbat(1,:)=[];
Vsc_d=Vsc;
I1=mapminmax(Ibat'); %已分類資料的標準化
Ibat=I1';
V=mapminmax(Vbat'); %待分類資料的標準化
Vbat=V';
%超級電容電壓過低懲罰函式
Phess=0;
Vuc_min=X(3)*0.6;
Vuc_max=X(3)*1.05;
for a=1:1800
if (Vsc_d(a,2)< Vuc_min||Vsc_d(a,2)>Vuc_max)
Phess =Phess+1;
end
end
%計算電池電流波動
Ibat1=Ibat;
Ibat(end,:)=[];
Ibat1(1,:)=[];
Cibat=(13.9/X(4))*sum(abs(Ibat(:,2)-Ibat1(:,2)));
%計算電池偏移波動
CVbus=(0.000005)*sum(abs(Vbat(:,2)-610));
%計算電池電壓波動
Vbat1=Vbat;
Vbat(end,:)=[];
Vbat1(1,:)=[];
Cvbat=(13.9/X(4))*sum(abs(Vbat(:,2)-Vbat1(:,2)));
%計算經濟性
Csc=sum(0.2*Rsc*Isc(:,2).^2)/1000; %超級電容內阻消耗的能量
Cbat=sum(0.1*Rbat*I.^2)/1000; %電池組內阻消耗的能量
CE=(Csc+Cbat)/36;
cost = 0.5*(X(3)/48) + 0.25*X(4); %(超級電容) (電池/DC變換器) 價格
%計算綜合費用
C=(a1*Cibat+a2*(Cvbat+CVbus)+a3*CE+0.3*cost+Phess);
fitness = C;
% Update the position of elite if any antlinons becomes fitter than it
if antlions_fitness(1)<Elite_antlion_fitness
Elite_antlion_position=Sorted_antlions(1,:);
Elite_antlion_fitness=antlions_fitness(1);
end
% Keep the elite in the population
Sorted_antlions(1,:)=Elite_antlion_position;
antlions_fitness(1)=Elite_antlion_fitness;
% Update the convergence curve
Convergence_curve(Current_iter)=Elite_antlion_fitness;
if Current_iter>2
line([Current_iter-1 Current_iter], [Convergence_curve(Current_iter-1) Convergence_curve(Current_iter)],'Color',[0 0.4470 0.7410])
xlabel('Iteration');
ylabel('Best score obtained so far');
drawnow
end
results = get(handles.uitable1,'data');
results{2,1}=Current_iter;
results{2,2}=Elite_antlion_fitness;
set(handles.uitable1,'data',results);
Current_iter=Current_iter+1;
end
end
出現以下問題:
fobj =
包含以下值的 function_handle:
@costfunction
錯誤使用 costfunction
輸入引數太多。
出錯 ALO_new>ALO (line 52)
antlions_fitness(1,i)=fobj(antlion_position(i,:));
出錯 ALO_new (line 19)
[Best_score,Best_pos,cg_curve]=ALO(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
請大佬幫忙看看,我應該去怎么解決呢?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/138724.html
標籤:其他
下一篇:ansys安裝問題
