多输入多输出 | MATLAB实现GA-BP遗传算法优化BP神经网络多输入多输出

2023-09-17 23:08:05

多输入多输出 | MATLAB实现GA-BP遗传算法优化BP神经网络多输入多输出

预测效果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

基本介绍

多输入多输出 | MATLAB实现GA-BP遗传算法优化BP神经网络多输入多输出
1.data为数据集,10个输入特征,3个输出变量。
2.main.m为主程序文件。
3.命令窗口输出MBE、MAE和R2,可在下载区获取数据和程序内容。

程序设计

  • 完整程序和数据下载方式:私信博主回复MATLAB实现GA-BP遗传算法优化BP神经网络多输入多输出
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function [x, endPop, bPop, traceInfo] = ga(bounds, evalFN, evalOps, startPop, opts, ...
termFN, termOps, selectFN, selectOps, xOverFNs, xOverOps, mutFNs, mutOps)
                              
% Output Arguments:
%   x            - the best solution found during the course of the run
%   endPop       - the final population 
%   bPop         - a trace of the best population
%   traceInfo    - a matrix of best and means of the ga for each generation
%
% Input Arguments:
%   bounds       - a matrix of upper and lower bounds on the variables
%   evalFN       - the name of the evaluation .m function
%   evalOps      - options to pass to the evaluation function ([NULL])
%   startPop     - a matrix of solutions that can be initialized
%                  from initialize.m
%   opts         - [epsilon prob_ops display] change required to consider two 
%                  solutions different, prob_ops 0 if you want to apply the
%                  genetic operators probabilisticly to each solution, 1 if
%                  you are supplying a deterministic number of operator
%                  applications and display is 1 to output progress 0 for
%                  quiet. ([1e-6 1 0])
%   termFN       - name of the .m termination function (['maxGenTerm'])
%   termOps      - options string to be passed to the termination function
%                  ([100]).
%   selectFN     - name of the .m selection function (['normGeomSelect'])
%   selectOpts   - options string to be passed to select after
%                  select(pop,#,opts) ([0.08])
%   xOverFNS     - a string containing blank seperated names of Xover.m
%                  files (['arithXover heuristicXover simpleXover']) 
%   xOverOps     - A matrix of options to pass to Xover.m files with the
%                  first column being the number of that xOver to perform
%                  similiarly for mutation ([2 0;2 3;2 0])
%   mutFNs       - a string containing blank seperated names of mutation.m 
%                  files (['boundaryMutation multiNonUnifMutation ...
%                           nonUnifMutation unifMutation'])
%   mutOps       - A matrix of options to pass to Xover.m files with the
%                  first column being the number of that xOver to perform
%                  similiarly for mutation ([4 0 0;6 100 3;4 100 3;4 0 0])

%%  初始化参数
n = nargin;
if n < 2 || n == 6 || n == 10 || n == 12
  disp('Insufficient arguements') 
end

% 默认评估选项
if n < 3 
  evalOps = [];
end

% 默认参数
if n < 5
  opts = [1e-6, 1, 0];
end

% 默认参数
if isempty(opts)
  opts = [1e-6, 1, 0];
end

%%  判断是否为m文件
if any(evalFN < 48)
  % 浮点数编码 
  if opts(2) == 1
    e1str = ['x=c1; c1(xZomeLength)=', evalFN ';'];  
    e2str = ['x=c2; c2(xZomeLength)=', evalFN ';']; 
  % 二进制编码
  else
    e1str = ['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=', evalFN ';'];
  end
else
  % 浮点数编码
  if opts(2) == 1
    e1str = ['[c1 c1(xZomeLength)]=' evalFN '(c1,[gen evalOps]);'];  
    e2str = ['[c2 c2(xZomeLength)]=' evalFN '(c2,[gen evalOps]);'];
  % 二进制编码
  else
    e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' evalFN ...
	'(x,[gen evalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];  
  end
end

%%  默认终止信息
if n < 6
  termOps = 100;
  termFN = 'maxGenTerm';
end

%%  默认变异信息
if n < 12
  % 浮点数编码
  if opts(2) == 1
  mutFNs = 'boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation';
    mutOps = [4, 0, 0; 6, termOps(1), 3; 4, termOps(1), 3;4, 0, 0];
  % 二进制编码
  else
    mutFNs = 'binaryMutation';
    mutOps = 0.05;
  end
end

%%  默认交叉信息
if n < 10
  % 浮点数编码
  if opts(2) == 1
    xOverFNs = 'arithXover heuristicXover simpleXover';
    xOverOps = [2, 0; 2, 3; 2, 0];
  % 二进制编码
  else
    xOverFNs = 'simpleXover';
    xOverOps = 0.6;
  end
end

%%  仅默认选择选项,即轮盘赌。
if n < 9
  selectOps = [];
end

%%  默认选择信息
if n < 8
  selectFN = 'normGeomSelect';
  selectOps = 0.08;
end

%%  默认终止信息
if n < 6
  termOps = 100;
  termFN = 'maxGenTerm';
end

%%  没有定的初始种群
if n < 4
  startPop = [];
end

%%  随机生成种群
if isempty(startPop)
  startPop = initializega(80, bounds, evalFN, evalOps, opts(1: 2));
end

%%  二进制编码
if opts(2) == 0
  bits = calcbits(bounds, opts(1));
end

%%  参数设置
xOverFNs     = parse(xOverFNs);
mutFNs       = parse(mutFNs);
xZomeLength  = size(startPop, 2); 	          % xzome 的长度
numVar       = xZomeLength - 1; 	          % 变量数
popSize      = size(startPop,1); 	          % 种群人口个数
endPop       = zeros(popSize, xZomeLength);   % 第二种群矩阵
numXOvers    = size(xOverFNs, 1);             % Number of Crossover operators
numMuts      = size(mutFNs, 1); 		      % Number of Mutation operators
epsilon      = opts(1);                       % Threshold for two fittness to differ
oval         = max(startPop(:, xZomeLength)); % Best value in start pop
bFoundIn     = 1; 			                  % Number of times best has changed
done         = 0;                             % Done with simulated evolution
gen          = 1; 			                  % Current Generation Number
collectTrace = (nargout > 3); 		          % Should we collect info every gen
floatGA      = opts(2) == 1;                  % Probabilistic application of ops
display      = opts(3);                       % Display progress 

%%  精英模型
while(~done)
  [bval, bindx] = max(startPop(:, xZomeLength));            % Best of current pop
  best =  startPop(bindx, :);
  if collectTrace
    traceInfo(gen, 1) = gen; 		                        % current generation
    traceInfo(gen, 2) = startPop(bindx,  xZomeLength);      % Best fittness
    traceInfo(gen, 3) = mean(startPop(:, xZomeLength));     % Avg fittness
    traceInfo(gen, 4) = std(startPop(:,  xZomeLength)); 
  end
  
  %%  最佳解
  if ( (abs(bval - oval) > epsilon) || (gen==1))
    
    % 更新显示
    if display
      fprintf(1, '\n%d %f\n', gen, bval);          
    end

    % 更新种群矩阵
    if floatGA
      bPop(bFoundIn, :) = [gen, startPop(bindx, :)]; 
    else
      bPop(bFoundIn, :) = [gen, b2f(startPop(bindx, 1 : numVar), bounds, bits)...
	  startPop(bindx, xZomeLength)];
    end

    bFoundIn = bFoundIn + 1;                      % Update number of changes
    oval = bval;                                  % Update the best val
  else
    if display
      fprintf(1,'%d ',gen);	                      % Otherwise just update num gen
    end
  end
%%  选择种群
  endPop = feval(selectFN, startPop, [gen, selectOps]);
  
  % 以参数为操作数的模型运行
  if floatGA
    for i = 1 : numXOvers
      for j = 1 : xOverOps(i, 1)
          a = round(rand * (popSize - 1) + 1); 	     % Pick a parent
	      b = round(rand * (popSize - 1) + 1); 	     % Pick another parent
	      xN = deblank(xOverFNs(i, :)); 	         % Get the name of crossover function
	      [c1, c2] = feval(xN, endPop(a, :), endPop(b, :), bounds, [gen, xOverOps(i, :)]);

          % Make sure we created a new 
          if c1(1 : numVar) == endPop(a, (1 : numVar)) 
	         c1(xZomeLength) = endPop(a, xZomeLength);
	      elseif c1(1:numVar) == endPop(b, (1 : numVar))
	         c1(xZomeLength) = endPop(b, xZomeLength);
          else
             eval(e1str);
          end

          if c2(1 : numVar) == endPop(a, (1 : numVar))
	          c2(xZomeLength) = endPop(a, xZomeLength);
	      elseif c2(1 : numVar) == endPop(b, (1 : numVar))
	          c2(xZomeLength) = endPop(b, xZomeLength);
          else
	          eval(e2str);
          end

          endPop(a, :) = c1;
          endPop(b, :) = c2;
      end
    end

    for i = 1 : numMuts
      for j = 1 : mutOps(i, 1)
          a = round(rand * (popSize - 1) + 1);
          c1 = feval(deblank(mutFNs(i, :)), endPop(a, :), bounds, [gen, mutOps(i, :)]);
          if c1(1 : numVar) == endPop(a, (1 : numVar)) 
              c1(xZomeLength) = endPop(a, xZomeLength);
          else
              eval(e1str);
          end
          endPop(a, :) = c1;
      end
    end

%%  运行遗传算子的概率模型
  else 
    for i = 1 : numXOvers
        xN = deblank(xOverFNs(i, :));
        cp = find((rand(popSize, 1) < xOverOps(i, 1)) == 1);

        if rem(size(cp, 1), 2) 
            cp = cp(1 : (size(cp, 1) - 1)); 
        end
        cp = reshape(cp, size(cp, 1) / 2, 2);

        for j = 1 : size(cp, 1)
            a = cp(j, 1); 
            b = cp(j, 2); 
            [endPop(a, :), endPop(b, :)] = feval(xN, endPop(a, :), endPop(b, :), ...
                bounds, [gen, xOverOps(i, :)]);
        end
    end

    for i = 1 : numMuts
        mN = deblank(mutFNs(i, :));
        for j = 1 : popSize
            endPop(j, :) = feval(mN, endPop(j, :), bounds, [gen, mutOps(i, :)]);
            eval(e1str);
        end
    end

  end
  
  %  更新记录
  gen = gen + 1;
  done = feval(termFN, [gen, termOps], bPop, endPop); % See if the ga is done
  startPop = endPop; 			                      % Swap the populations
  [~, bindx] = min(startPop(:, xZomeLength));         % Keep the best solution
  startPop(bindx, :) = best; 		                  % replace it with the worst
  
end
[bval, bindx] = max(startPop(:, xZomeLength));

%%  显示结果
if display 
  fprintf(1, '\n%d %f\n', gen, bval);	  
end

%%  二进制编码
x = startPop(bindx, :);
if opts(2) == 0
  x = b2f(x, bounds,bits);
  bPop(bFoundIn, :) = [gen, b2f(startPop(bindx, 1 : numVar), bounds, bits)...
      startPop(bindx, xZomeLength)];
else
  bPop(bFoundIn, :) = [gen, startPop(bindx, :)];
end

%%  赋值
if collectTrace
  traceInfo(gen, 1) = gen; 		                      % 当前迭代次数
  traceInfo(gen, 2) = startPop(bindx, xZomeLength);   % 最佳适应度
  traceInfo(gen, 3) = mean(startPop(:, xZomeLength)); % 平均适应度
end

往期精彩

MATLAB实现RBF径向基神经网络多输入多输出预测
MATLAB实现BP神经网络多输入多输出预测
MATLAB实现DNN神经网络多输入多输出预测

参考资料

[1] https://blog.csdn.net/kjm13182345320/article/details/116377961
[2] https://blog.csdn.net/kjm13182345320/article/details/127931217
[3] https://blog.csdn.net/kjm13182345320/article/details/127894261

更多推荐

Pycharm中画图警告:MatplotlibDeprecationWarning

前言:\textcolor{Green}{前言:}前言:💞这是由于在python中画图出现的问题,一般不会有错。因为它只是个警告,但是我们也可以知道解决这个问题的方法,防止后面出问题的时候知道怎么解决。前因后果问题:原因解决方法一解决方法二问题:在使用Matplotlib的show方法时,控制台出现以下警告信息:Ma

【Python】Python 包 ③ ( Python 第三方包简介 | 安装第三方包 | 命令行安装 | PyCharm 安装 | 设置代理 | 使用第三方包开发 )

文章目录一、Python第三方包简介二、安装第三方包1、使用pip命令安装第三方包2、指定第三方包安装版本3、设置代理4、PyCharm中安装第三方包三、使用第三方包开发一、Python第三方包简介Python包中包含了很多Python模块,每个Python模块对应一个Python源码,其中包含了若干功能(函数);Py

Nginx配置文件详解

一、nginx的配置文件1、常见的配置文件及其作用nginx常见配置文件位置:安装路径/conf目录中。[root@centos7nginx]#tree.├──conf│├──fastcgi.conf│├──fastcgi.conf.default│├──fastcgi_params│├──fastcgi_params

pycharm中恢复原始界面布局_常用快捷键_常用设置

文章目录1恢复默认布局1.1直接点击file→ManageIDESettings→RestoreDefaultSettings(如下图所示):1.2直接点击RestoreandRestart,然后Pycharm就会自动重启,重启之后的界面就是最原始的界面了2改变主题2.1在PyCharm的右上角,点击“齿轮,然后点击:

ThreadPoolExecutor的使用

1.在SpringBoot项目中使用ThreadPoolExecutor:SpringBoot中可以通过创建一个配置类来定义ThreadPoolExecutor,然后在需要使用的地方直接注入即可。@ConfigurationpublicclassThreadPoolConfig{@BeanpublicExecutora

【OpenSSL】VC编译OpenSSL

VC编译OpenSSL编译工具准备编译OpenSSL建立`HelloWorld`工程创建VS工程编译工具准备安装好VisualStudio。安装Perl,主要是用来生成nmake的。准备好汇编语言编译工具nasm,并添加到path路径。下载好OpenSSL源代码。编译OpenSSL安装Perl,并加入到path路径,检

Sentinel控制台配置 持久化到nacos

sentinel控制台,使用方便,功能强大。使用官方的jar包,配置不会持久化,sentinel重启后会导致,之前的规则全部丢失,下面一起改造源码实现规则数据的持久化sentinel源码地址(github访问太慢,直接上镜像版)Sentinel:Sentinel是什么随着微服务的流行,服务和服务之间的稳定性变得越来越重

深入了解代理服务器:Socks5、IP代理与网络安全

在当今数字化时代,网络安全和数据采集对于网络工程师和爬虫开发者来说至关重要。代理服务器是一项关键技术,为保护隐私、绕过访问限制和提高网络安全提供了有力工具。本文将深入探讨几种不同类型的代理服务器,包括Socks5代理、IP代理,以及它们在网络安全和爬虫开发中的作用。1.Socks5代理:全能的代理协议Socks5代理协

什么是网络安全?网络安全包括哪几个方面?

提及网络安全,很多人都是既熟悉又陌生,所谓的熟悉就是知道网络安全可以保障网络服务不中断。那么到底什么是网络安全?网络安全包括哪几个方面?通过下文为大家介绍一下。什么是网络安全?网络安全是指网络系统的硬件、软件及系统中的数据受到保护,不因偶然的或者恶意的原因而遭受到破坏、更改、泄露,系统连续可靠正常地运行,网络服务不中断

由于数字化转型对集成和扩展性的要求,定制化需求难以满足,百数低代码服务商该如何破局?

当政策、技术环境的日益成熟,数字化转型逐步成为企业发展的必选项,企业数字化转型不再是一道选择题,而是决定其生存发展的必由之路。通过数字化转型升级生产方式、管理模式和组织形式,激发内生动力,成为企业顺应时代变化,实现高质量发展的必然选择。一般来说,实现数字化转型的方式有3种:采购已有的标准系统、定制外包或者选购低代码平台

iOS17适配指南-新版

文章目录一、iOS17适配点二、具体代码一、iOS17适配点UIView与UIViewController。可以设置数据为空时的占位视图,增加SymbolAnimations,通过addSymbolEffect()与removeSymbolEffect()方法,可以实现SFSymbols图标的添加与移除动画。UIPag

热文推荐