【预测模型】基于蝙蝠算法优化BP神经网络实现数据预测matlab代码

141 阅读2分钟

1 简介

为降低某车型前保险杠注塑成型中产生的翘曲变形,基于数值模拟结果,将神经网络-遗传算法寻优模型与蝙蝠算法结合,确定了BP神经网络的初始权值和阈值.将BA-BP模型代入遗传算法中求解最佳工艺参数.由极差分析可知,影响翘曲变形最显著的因素为保压时间和模具温度.基于极差分析设计补充实验,训练BA-BP神经网络并作为遗传算法的适应度值进行迭代寻优.结果表明:BA-BP模型的相关系数R2可达0.99以上,平均绝对误差为1.05%,能较精准地预测翘曲量.

2 部分代码

clc,clear,close all

warning off

% BA算法参数

maxiter = 200;  % 迭代次数

sizepop = 10;  % 种群数量

% 频率范围

popmin1 = -1;  popmax1 = 1; % x1  频率

popmin2 = -1;  popmax2 = 1; % x2  频率

Qmin = 0.1;    % 最小频率

Qmax = 0.5;    % 最大频率

impluse = 0.4; % 脉冲率

Vmin = -1; % 最小速度

Vmax = 1;  % 最大速度

%% 初始化种群

for i=1:sizepop

    x1 = popmin1 + (popmax1-popmin1)*rand;

    x2 = popmin2 + (popmax2-popmin2)*rand;

    pop(i,1) = x1;

    pop(i,2) = x2;

    fitness(i) = fun([x1,x2]);

    V(i,1)=0;

    V(i,2)=0;

end

% 记录一组最优值

[bestfitness,bestindex]=min(fitness);

zbest=pop(bestindex,:);   % 全局最佳

gbest=pop;                % 个体最佳

fitnessgbest=fitness;     % 个体最佳适应度值

fitnesszbest=bestfitness; % 全局最佳适应度值

%% 迭代寻优

for i=1:maxiter

    for j=1:sizepop

        Q = Qmin + (Qmax-Qmin)*rand;

        V(j,:) = V(j,:) + (pop(j,:)-zbest)*Q;

        % V--x1

        if V(j,1)>Vmax

            V(j,1)=Vmax;

        end

        if V(j,1)<Vmin

            V(j,1)=Vmin;

        end

        % V--x2

        if V(j,2)>Vmax

            V(j,2)=Vmax;

        end

        if V(j,2)<Vmin

            V(j,2)=Vmin;

        end

        

        pop(j,:) = pop(j,:) + 0.5*V(j,:);

        

        % 脉冲率

        if rand>impluse

            x1 = popmin1 + (popmax1-popmin1)*rand;

            x2 = popmin2 + (popmax2-popmin2)*rand;

            pop(j,:) = [x1,x2];

        end

        

        % x1  越界限制

        if pop(j,1)>popmax1

            pop(j,1)=popmax1;

        end

        if pop(j,1)<popmin1

            pop(j,1)=popmin1;

        end

        % x2  越界限制

        if pop(j,2)>popmax2

            pop(j,2)=popmax2;

        end

        if pop(j,2)<popmin2

            pop(j,2)=popmin2;

        end

        

        % 适应度更新

        fitness(j) = fun(pop(j,:));

        

        % 比较  个体间比较

        if fitness(j)<fitnessgbest(j)

            fitnessgbest(j) = fitness(j);

            gbest(j,:) = pop(j,:);

        end

        if fitness(j)<bestfitness

            bestfitness = fitness(j);

            zbest =  pop(j,:);

        end

        

    end

    fitness_iter(i) = bestfitness;

end

disp('最优解')

disp(zbest)

fprintf('\n')

figure('color',[1,1,1])

plot(fitness_iter,'ro-','linewidth',2)

figure('color',[1,1,1])

loglog(fitness_iter,'ro-','linewidth',2)

axis tight

3 仿真结果

4 参考文献

[1]姚珊. 基于蝙蝠算法的神经网络预测模型的研究及应用. Diss. 辽宁科技大学, 2019.

5 MATLAB代码与数据下载地址

见博客主页