佛山网站建设哪里有,珠海网站制作计划,杭州外贸网站多少钱,输入网站域名往期博客#x1f449; 【Matlab】BP神经网络遗传算法(BP-GA)函数极值寻优——非线性函数求极值 【Matlab】GRNN神经网络遗传算法(GRNN-GA)函数极值寻优——非线性函数求极值 【Matlab】RBF神经网络遗传算法(RBF-GA)函数极值寻优——非线性函数求极值 本篇博客将主要介绍Elman神… 往期博客 【Matlab】BP神经网络遗传算法(BP-GA)函数极值寻优——非线性函数求极值 【Matlab】GRNN神经网络遗传算法(GRNN-GA)函数极值寻优——非线性函数求极值 【Matlab】RBF神经网络遗传算法(RBF-GA)函数极值寻优——非线性函数求极值 本篇博客将主要介绍Elman神经网络希望能帮助大家快速入门Elman网络。 1.背景条件
要求对于未知模型函数表达式未知求解极值。 条件已知模型的一些输入输出数据。
程序的示例是根据用神经网络遗传算法寻优非线性函数 y x 1 2 x 2 2 y x_1^2x_2^2 yx12x22 的极值输入参数有2个输出参数有1个易知函数有极小值0极小值点为(0, 0)。已知的只有一些输入输出数据用rand函数生成输入然后代入表达式生成输出
for i1:4000input(i,:)10*rand(1,2)-5;output(i)input(i,1)^2input(i,2)^2;
end2.Elman神经网络函数说明
elmannet
Elman神经网络参数设置函数 函数形式
net elmannet(layerdelays,hiddenSizes,trainFcn)layerdelays: 网络层延迟的行向量可取的值为0或整数默认值为12 hiddenSizes: 隐含层的大小是一个行向量默认值为10 trainFcn: 训练函数的字符串默认值为‘trainlm’。
例如
netelmannet(1:2,10)newelm() 也是创建 Elman 神经网络的函数不过适用于较低版本的 matlab 我的 matlab 版本为 R2022b识别不了这个函数。
3.完整代码
data.m
用于生成神经网络拟合的原始数据。
for i1:4000input(i,:)10*rand(1,2)-5;output(i)input(i,1)^2input(i,2)^2;
end
outputoutput;save data input outputElman.m
用函数输入输出数据训练Elman神经网络使训练后的网络能够拟合非线性函数输出保存训练好的网络用于计算个体适应度值。根据非线性函数方程随机得到该函数的4000组输入输出数据存储于data中其中input为函数输入数据output为函数对应输出数据从中随机抽取3900组训练数据训练网络100组测试数据测试网络拟合性能。最后保存训练好的网络。
%% 清空环境变量
clctic
%% 训练数据预测数据提取及归一化
%从1到4000间随机排序
krand(1,4000);
[m,n]sort(k);%划分训练数据和预测数据
input_traininput(n(1:3900),:);
output_trainoutput(n(1:3900),:);
input_testinput(n(3901:4000),:);
output_testoutput(n(3901:4000),:);[inputn,inputps]mapminmax(input_train);
[outputn,outputps]mapminmax(output_train);%% Elman网络训练
% 初始化网络结构
netelmannet(1:2,10); % Elman网络
% elmannet(layerdelays,hiddenSizes,trainFcn)
% layerdelays表示网络层延迟的行向量可取的值为0或整数默认值为12
% hiddenSizes为隐含层的大小是一个行向量默认值为10
% trainFcn表示训练函数的字符串默认值为‘trainlm’。% 设置网络参数迭代次数、学习率和目标
net.trainParam.epochs1000; % 最大迭代次数
net.trainParam.lr0.0001; % 学习率
net.trainParam.goal1e-5; % 误差容限达到此误差就可以停止训练
net.trainParam.max_fail5; % 最多验证失败次数
view(net)%网络训练
nettrain(net,inputn,outputn);%% Elman网络预测
%预测数据归一化
inputn_testmapminmax(apply,input_test,inputps);%网络预测输出
ansim(net,inputn_test);%网络输出反归一化
Eloutputmapminmax(reverse,an,outputps);%% 结果分析
erroroutput_test-Eloutput;
errorsumsum(abs(error))figure(1);
plot(Eloutput,:og);
hold on
plot(output_test,-*);
legend(Predictive output,Expected output,fontsize,10);
title(Elman network predictive output,fontsize,12);
xlabel(samples,fontsize,12);figure(2);
plot(error,-*);
title(Elman Neural network prediction error);
xlabel(samples,fontsize,12);figure(3);
plot(100*(output_test-Eloutput)./output_test,-*);
title(Elman Neural network prediction error percentage (%));
xlabel(samples,fontsize,12);tocsave data net inputps outputpsCode.m
编码成染色体。
function retCode(lenchrom,bound)
%本函数将变量编码成染色体用于随机初始化一个种群
% lenchrom input : 染色体长度
% bound input : 变量的取值范围
% ret output: 染色体的编码值
flag0;
while flag0pickrand(1,length(lenchrom));retbound(:,1)(bound(:,2)-bound(:,1)).*pick; %线性插值编码结果以实数向量存入ret中flagtest(lenchrom,bound,ret); %检验染色体的可行性
endfun.m
把训练好的Elman神经网络预测输出作为个体适应度值。
function fitness fun(x)
% 函数功能计算该个体对应适应度值
% x input 个体
% fitness output 个体适应度值%
load data net inputps outputps%数据归一化
xx;
inputn_testmapminmax(apply,x,inputps);%网络预测输出
ansim(net,inputn_test);%网络输出反归一化
fitnessmapminmax(reverse,an,outputps);对于求极小值的函数适应度可以设为Elman网络预测结果如果需要求极大值可以对适应度取反。
Select.m
选择操作采用轮盘赌法从种群中选择适应度好的个体组成新种群。
function retselect(individuals,sizepop)
% 本函数对每一代种群中的染色体进行选择以进行后面的交叉和变异
% individuals input : 种群信息
% sizepop input : 种群规模
% ret output : 经过选择后的种群fitness11./individuals.fitness;
sumfitnesssum(fitness1);
sumffitness1./sumfitness;
index[];
for i1:sizepop %转sizepop次轮盘pickrand;while pick0 pickrand; endfor i1:sizepop pickpick-sumf(i); if pick0 index[index i]; break; %寻找落入的区间此次转轮盘选中了染色体i注意在转sizepop次轮盘的过程中有可能会重复选择某些染色体endend
end
individuals.chromindividuals.chrom(index,:);
individuals.fitnessindividuals.fitness(index);
retindividuals;Cross.m
交叉操作从种群中选择两个个体按一定概率交叉得到新个体。
function retCross(pcross,lenchrom,chrom,sizepop,bound)
%本函数完成交叉操作
% pcorss input : 交叉概率
% lenchrom input : 染色体的长度
% chrom input : 染色体群
% sizepop input : 种群规模
% ret output : 交叉后的染色体for i1:sizepop %每一轮for循环中可能会进行一次交叉操作染色体是随机选择的交叉位置也是随机选择的%但该轮for循环中是否进行交叉操作则由交叉概率决定continue控制% 随机选择两个染色体进行交叉pickrand(1,2);while prod(pick)0pickrand(1,2);endindexceil(pick.*sizepop);% 交叉概率决定是否进行交叉pickrand;while pick0pickrand;endif pickpcrosscontinue;endflag0;while flag0% 随机选择交叉位pickrand;while pick0pickrand;endposceil(pick.*sum(lenchrom)); %随机选择进行交叉的位置即选择第几个变量进行交叉注意两个染色体交叉的位置相同pickrand; %交叉开始v1chrom(index(1),pos);v2chrom(index(2),pos);chrom(index(1),pos)pick*v2(1-pick)*v1;chrom(index(2),pos)pick*v1(1-pick)*v2; %交叉结束flag1test(lenchrom,bound,chrom(index(1),:)); %检验染色体1的可行性flag2test(lenchrom,bound,chrom(index(2),:)); %检验染色体2的可行性if flag1*flag20flag0;else flag1;end %如果两个染色体不是都可行则重新交叉endend
retchrom;test.m
检验染色体的可行性。
function flagtest(lenchrom,bound,code)
% lenchrom input : 染色体长度
% bound input : 变量的取值范围
% code output: 染色体的编码值xcode; %先解码
flag1;
if (x(1)bound(1,1))(x(2)bound(2,1))(x(1)bound(1,2))(x(2)bound(2,2))flag0;
endMutation.m
变异操作从种群中随机选择一个个体按一定概率变异得到新个体。
function retMutation(pmutation,lenchrom,chrom,sizepop,pop,bound)
% 本函数完成变异操作
% pcorss input : 变异概率
% lenchrom input : 染色体长度
% chrom input : 染色体群
% sizepop input : 种群规模
% opts input : 变异方法的选择
% pop input : 当前种群的进化代数和最大的进化代数信息
% ret output : 变异后的染色体
for i1:sizepop %每一轮for循环中可能会进行一次变异操作染色体是随机选择的变异位置也是随机选择的%但该轮for循环中是否进行变异操作则由变异概率决定continue控制% 随机选择一个染色体进行变异pickrand;while pick0pickrand;endindexceil(pick*sizepop);% 变异概率决定该轮循环是否进行变异pickrand;if pickpmutationcontinue;endflag0;while flag0% 变异位置pickrand;while pick0 pickrand;endposceil(pick*sum(lenchrom)); %随机选择了染色体变异的位置即选择了第pos个变量进行变异vchrom(i,pos); v1v-bound(pos,1); v2bound(pos,2)-v; pickrand; %变异开始 if pick0.5deltav2*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)vdelta;elsedeltav1*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)v-delta;end %变异结束flagtest(lenchrom,bound,chrom(i,:)); %检验染色体的可行性end
end
retchrom;Genetic.m
%% 清空环境变量
clc
% clear%% 初始化遗传算法参数
%初始化参数
maxgen100; %进化代数即迭代次数
sizepop20; %种群规模
pcross[0.4]; %交叉概率选择0和1之间
pmutation[0.2]; %变异概率选择0和1之间lenchrom[1 1]; %每个变量的字串长度如果是浮点变量则长度都为1
bound[-5 5;-5 5]; %数据范围individualsstruct(fitness,zeros(1,sizepop), chrom,[]); %将种群信息定义为一个结构体
avgfitness[]; %每一代种群的平均适应度
bestfitness[]; %每一代种群的最佳适应度
bestchrom[]; %适应度最好的染色体%% 初始化种群计算适应度值
% 初始化种群
for i1:sizepop%随机产生一个种群individuals.chrom(i,:)Code(lenchrom,bound); xindividuals.chrom(i,:);%计算适应度individuals.fitness(i)fun(x); %染色体的适应度
end
%找最好的染色体
[bestfitness bestindex]min(individuals.fitness);
bestchromindividuals.chrom(bestindex,:); %最好的染色体
avgfitnesssum(individuals.fitness)/sizepop; %染色体的平均适应度
% 记录每一代进化中最好的适应度和平均适应度
trace[avgfitness bestfitness]; %% 迭代寻优
% 进化开始
for i1:maxgeni% 选择individualsSelect(individuals,sizepop); avgfitnesssum(individuals.fitness)/sizepop;% 交叉individuals.chromCross(pcross,lenchrom,individuals.chrom,sizepop,bound);% 变异individuals.chromMutation(pmutation,lenchrom,individuals.chrom,sizepop,[i maxgen],bound);% 计算适应度 for j1:sizepopxindividuals.chrom(j,:); %解码individuals.fitness(j)fun(x); end%找到最小和最大适应度的染色体及它们在种群中的位置[newbestfitness,newbestindex]min(individuals.fitness);[worestfitness,worestindex]max(individuals.fitness);% 代替上一次进化中最好的染色体if bestfitnessnewbestfitnessbestfitnessnewbestfitness;bestchromindividuals.chrom(newbestindex,:);endindividuals.chrom(worestindex,:)bestchrom;individuals.fitness(worestindex)bestfitness;avgfitnesssum(individuals.fitness)/sizepop;trace[trace;avgfitness bestfitness]; %记录每一代进化中最好的适应度和平均适应度
end
%进化结束%% 结果分析
[r c]size(trace);
plot([1:r],trace(:,2),r-);
title(适应度曲线,fontsize,12);
xlabel(进化代数,fontsize,12);ylabel(适应度,fontsize,12);
disp(适应度 变量);
xbestchrom;
% 窗口显示
disp([bestfitness x]);4.代码使用说明
上述代码运行顺序
data.m 生成数据如果已有 input output 数据可跳过 Elman.m 进行Elman神经网络训练及函数拟合 Genetic.m主函数利用遗传算法求极值。
求最大值的方法
上述代码用于求解最小值对于求解最大值的需求可以在适应度函数里面对适应度计算结果求反把求解最大值的问题转化为求解最小值的问题。
例如对于非线性函数 y − ( x 1 2 x 2 2 ) 4 y -(x_1^2x_2^2)4 y−(x12x22)4
for i1:4000input(i,:)10*rand(1,2)-5;output(i)-(input(i,1)^2input(i,2)^2)4;
end求最大值时需要在 fun.m 里面修改最后一行代码
fitness-mapminmax(reverse,an,outputps);注意每次运行结果不尽相同。
5.代码运行结果
对 y x 1 2 x 2 2 y x_1^2x_2^2 yx12x22 求极小值
Elman神经网络拟合
运行Elman.m之后 输出
errorsum 64.6588历时 4.034772 秒。注意每次运行结果不尽相同。
遗传算法寻优
运行主函数 Genetic.m之后 输出
...
i 100适应度 变量-0.8407 0.6137 -0.0228历时 20.067215 秒。最终结果最优个体为0.6137-0.0228适应度为 -0.8407。
注意每次运行结果不尽相同。
参考
《MATLAB神经网络30个案例分析》