matlab語言 遺傳算法GA+BP神經(jīng)網(wǎng)絡(luò) 電路參數(shù)估計(jì)
一、整體設(shè)計(jì)概述:BP神經(jīng)網(wǎng)絡(luò)代理電路模型 GA尋找誤差最小參數(shù)
根據(jù)采樣數(shù)據(jù)訓(xùn)練BP神經(jīng)網(wǎng)絡(luò),用訓(xùn)練好的網(wǎng)絡(luò)來作為目標(biāo)函數(shù)輸出電路參數(shù)誤差,通過GA尋找誤差最小的電路參數(shù)值。
二、代碼實(shí)現(xiàn)(需要完整代碼的私聊)
BP.m 主函數(shù)
clc
close all
clear all
%% 訓(xùn)練BP模型
datapath='f=85000.xls';
f=85000;
% datapath='f=6.393341925217580e+04.xls';
myBP=BPMod(datapath);
%% 使用BP模型預(yù)測
Iin_cl=16.54;
Icf_cl=19.28;
[M_pre,RL_pre]=mypredictor(myBP,Iin_cl,Icf_cl);
[Iin_exp,Icf_exp]=get_expI(f,M_pre,RL_pre);
disp(['Iin測量值為:',num2str(Iin_cl)])
disp(['Icf測量值為:',num2str(Icf_cl)])
error=abs(Iin_cl-Iin_exp)+abs(Icf_cl-Icf_exp);
disp(['最小誤差為:',num2str(error)])
disp(['M預(yù)測值為:',num2str(M_pre)])
disp(['RL預(yù)測值為:',num2str(RL_pre)])
disp(['Iin計(jì)算值為:',num2str(Iin_exp)])
disp(['Icf計(jì)算值為:',num2str(Icf_exp)])
BPMod.m 數(shù)據(jù)處理+訓(xùn)練BP網(wǎng)絡(luò)
function myBP=BPMod(datapath)
%% 數(shù)據(jù)預(yù)處理
% datapath='f=85000.xls';
data=xlsread(datapath);
trainRatio=0.75;
valRatio=0;
testRatio=1-trainRatio;
[trainInd,valInd,testInd] = dividerand(max(size(data)),trainRatio,valRatio,testRatio);
input_train=data(trainInd,3:4);
output_train=data(trainInd,1:2);
input_test=data(testInd,3:4)';
output_test=data(testInd,1:2)';
% %% 數(shù)據(jù)歸一化
% for i=1:size(output_train,2)
% output_train(:,i)=mymaxminmap(output_train(:,i));
% end
%% 輸入輸出數(shù)據(jù)歸一化
[inputn,inputps]=mapminmax(input_train');
[outputn,outputps]=mapminmax(output_train');
% outputn=outputn';
%% BP網(wǎng)絡(luò)訓(xùn)練
node_in_num=2; % 輸入層節(jié)點(diǎn)數(shù)量
% node_hidden_num=2*node_in_num+1;% 隱含層節(jié)點(diǎn)數(shù)量
node_out_num=2;
node_hidden_num=(node_out_num+node_in_num)+randi([1,10]);% 隱含層節(jié)點(diǎn)數(shù)量
% %初始化網(wǎng)絡(luò)結(jié)構(gòu)
% net=newff(inputn,outputn,node_hidden_num);
net=feedforwardnet(11,'trainlm');
net.trainParam.epochs=10000;
net.trainParam.lr=0.01;
net.trainParam.goal=0.0002;
net.trainParam.showCommandLine=true;
net.trainParam.max_fail = 20;
% net.divideFcn='divideind';
% net.trainParam.showWindow=true;
%網(wǎng)絡(luò)訓(xùn)練
net=train(net,inputn,outputn);
%% BP網(wǎng)絡(luò)預(yù)測
%預(yù)測數(shù)據(jù)歸一化
inputn_test=mapminmax('apply',input_test,inputps);
%網(wǎng)絡(luò)預(yù)測輸出
an=sim(net,inputn_test);
%網(wǎng)絡(luò)輸出反歸一化
BPoutput=mapminmax('reverse',an,outputps);
%%
figure
subplot(2,1,1)
plot(BPoutput(1,:),'r*')
hold on
plot(output_test(1,:),'bo')
plot(abs(BPoutput(1,:)-output_test(1,:)),'c--.')
legend('BP預(yù)測值','實(shí)際值','誤差絕對(duì)值')
title('Iin')
grid on
subplot(2,1,2)
plot(BPoutput(2,:),'r*')
hold on
plot(output_test(2,:),'bo')
hold on
plot(abs(BPoutput(2,:)-output_test(2,:)),'c--.')
legend('BP預(yù)測值','實(shí)際值','誤差絕對(duì)值')
title('Icf')
grid on
[c,l]=size(BPoutput);
error=abs(BPoutput(2,:)-output_test(2,:))+...
abs(BPoutput(1,:)-output_test(1,:));
MAE1=sum(abs(error))/l;
MSE1=error*error'/l;
RMSE1=MSE1^(1/2);
disp(['-----------------------誤差計(jì)算--------------------------'])
disp(['隱含層節(jié)點(diǎn)數(shù)為',num2str(node_hidden_num),'時(shí)的誤差結(jié)果如下:'])
disp(['平均絕對(duì)誤差MAE為:',num2str(MAE1)])
disp(['均方誤差MSE為: ',num2str(MSE1)])
disp(['均方根誤差RMSE為: ',num2str(RMSE1)])
myBP.inputps=inputps;
myBP.net=net;
myBP.outputps=outputps;
% f=85000;
% RL=50;
% M=5.477e-5;
% % [Iin_exp,Icf_exp]=get_expI(f,M,RL);
% %
% fitness=getfitness(myBP,f,M,RL);
GA.m 遺傳算法主函數(shù)
clc
close all
clear all
%% 模型參數(shù)
f=85000;
RL_max=200;
RL_min=50;
M_max=1e-4;
M_min=1e-5;
Iin_cl=16.54;
Icf_cl=19.28;
%% GA參數(shù)設(shè)置
Maxgen=1000;
Popnum=50;
Pc=0.5;
Pm=0.5;
%% 初始化種群
for i=1:Popnum
chrom(i).M=(M_max-M_min)*rand()+M_min;
chrom(i).RL=(RL_max-RL_min)*rand()+RL_min;
chrom(i).fitness=getfitness(f,Iin_cl,Icf_cl,chrom(i).M,chrom(i).RL);
end
%% 種群進(jìn)化
gen=0;
while gen<Maxgen
gen=gen+1
Newchrom=chrom;
for i=1:Popnum
temppop=Newchrom(i);
pc=rand();
if pc<Pc
temppop=across(temppop,chrom,Popnum,f,Iin_cl,Icf_cl);
end
pm=0;
if pm<Pm
temppop=mutation(temppop,f,M_max,M_min,RL_max,RL_min,Iin_cl,Icf_cl);
end
Newchrom(i)=temppop;
end
Chrom_all=[chrom,Newchrom];
[V,ind]=sort([Chrom_all.fitness]);
chrom=Chrom_all(ind(1:Popnum));
bestV(gen)=V(1);
bestpop(gen)=Chrom_all(ind(1));
end
%% 顯示結(jié)果
figure
plot(bestV)
xlabel('迭代次數(shù)')
ylabel('目標(biāo)函數(shù)值')
title('進(jìn)化曲線')
legend('最優(yōu)解')
disp(['Iin測量值(BP預(yù)測)為:',num2str(Iin_cl)])
disp(['Icf測量值(BP預(yù)測)為:',num2str(Icf_cl)])
disp(['最小誤差為:',num2str(bestV(end))])
disp(['最小誤差對(duì)應(yīng)的M為:',num2str(bestpop(end).M)])
disp(['最小誤差對(duì)應(yīng)的RL為:',num2str(bestpop(end).RL)])
[Iin_exp,Icf_exp]=get_expI(f,bestpop(end).M,bestpop(end).RL);
disp(['Iin計(jì)算值為:',num2str(Iin_exp)])
disp(['Icf計(jì)算值為:',num2str(Icf_exp)])
三、結(jié)果展示
BP神經(jīng)網(wǎng)絡(luò)訓(xùn)練情況
本模型使用BP神經(jīng)網(wǎng)絡(luò)來代理實(shí)際電路的輸出,因此神經(jīng)網(wǎng)絡(luò)的回歸效果一定要好,預(yù)測結(jié)果貼近實(shí)際電路輸出,這樣才能確保后續(xù)參數(shù)尋優(yōu)的準(zhǔn)確性
遺傳算法優(yōu)化結(jié)果
以電路參數(shù)誤差為目標(biāo)函數(shù),通過GA+bp模型 找到最優(yōu)的參數(shù)。
一是通過進(jìn)化曲線,確認(rèn)GA算法已經(jīng)收斂,如果不收斂的話就調(diào)整GA的參數(shù),使得在迭代后期目標(biāo)函數(shù)值趨于穩(wěn)定。
可以看到BP模型的預(yù)測值和實(shí)際值很接近,說明BP神經(jīng)網(wǎng)絡(luò)訓(xùn)練效果較好,最終誤差為0.53179,對(duì)應(yīng)的電路參數(shù)M和RL也通過GA得到。文章來源:http://www.zghlxwxcb.cn/news/detail-433469.html
四、項(xiàng)目分享
源碼鏈接:C幣下載
需要免費(fèi)分享的關(guān)注+點(diǎn)贊+收藏的 私聊領(lǐng)取
完整的項(xiàng)目內(nèi)容
包含:
**模型文件和電路數(shù)據(jù)
BP模型代碼
GA模型代碼
均可分別運(yùn)行
內(nèi)含simulink封裝好的S函數(shù) 可以支持simulink使用
可以結(jié)合這個(gè)案例,學(xué)習(xí)遺傳算法和BP神經(jīng)網(wǎng)絡(luò)的單獨(dú)使用的方法,以及遺傳算法+BP代理模型聯(lián)合仿真的方法,如果你的問題是電路參數(shù)估計(jì)的模型,更改數(shù)據(jù)和公式之后可以直接使用文章來源地址http://www.zghlxwxcb.cn/news/detail-433469.html
到了這里,關(guān)于【MatLab】《遺傳算法GA+BP神經(jīng)網(wǎng)絡(luò)——電路參數(shù)估計(jì)》的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!