• 大小: 4KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-19
  • 语言: Matlab
  • 标签: 算法  

资源简介

回溯搜索算法,回溯算法也叫试探法,它是一种系统地搜索问题的解的方法。回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试。

资源截图

代码片段和文件信息

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 回溯搜索(BSA)进行函数优化,
% 输入:popsize 种群个数,Dimsize 变量维数,pop=【pop1pop2...popi...popN】为种群集合,Maxgen 最大迭代次数,
% lowerboundary upperboundary分别为变量上下界, 
% 输出: globalminimizerglobalminimum
function BSA()
clc;
clear all;
format long;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%  给定初始化条件  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
popsize  = 10;                                                             % 种群数目
Dimsize  =  30;                                                            % 变量维数
Maxgen  =  1000;                                                           % 最大的迭代次数
mixrate = 1;                                                               % 最大交叉概率
constant_F = 3;
lowerboundary  =  -100.*ones(1Dimsize) ; 
upperboundary  =  100.*ones(1Dimsize) ;
globalminimum = Inf;
%%%%%%%%%%%%%%%%%%%%%%%%        种群初始化          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:popsize
    pop( i:) = lowerboundary + rand(1Dimsize) .* (upperboundary - lowerboundary);             % 初始化种群
    oldpop( i:) = lowerboundary + rand(1Dimsize) .* (upperboundary - lowerboundary);   
    Fitpop(i) = fitness( pop(i:)Dimsize );
end
%%%%%%%%%%%%%%%%%%%%%%%%%     选择第一阶段         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t = 1:Maxgen 
   for i =1:popsize
       rand_a = rand;
       rand_b = rand;
    if  rand_a < rand_b
        oldpop(i:) = pop(i:);
    else
        oldpop = oldpop(randperm(popsize):);
        oldpop = oldpop(:randperm(Dimsize));
    end
   end
  %%%%%%%%%%%%%%%%%%%%%         变异               %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  for i = 1:popsize  
      trialpop(i:) = pop(i:) + constant_F .* randn .* (oldpop(i:)-pop(i:));
  end
%%%%%%%%%%%%%%%%%%%%%         交叉              %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    integer_matrix = ones(popsizeDimsize);
    rand_c = rand;
    rand_d = rand;
    if rand_c < rand_d  
       number = ceil(mixrate * rand * Dimsize);
       sequence = randperm(Dimsize);
       for i = 1:popsize 
            for u = 1:number
                j = sequence(u);
                integer_matrix( ij ) = 0;
            end
       end
    else
        sequence = randperm(Dimsize);
        for i = 1:popsize
            j = sequence(1);
            integer_matrix( ij ) = 0;
        end
    end
    for i = 1:popsize
        for j = 1:Dimsize
            if  integer_matrix( ij ) == 1
                trialpop(ij) = pop(ij);
            end
        end
    end
    %%%%%%%%%%%%%%%%%%%%      边界检测          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i = 1:popsize
         for j = 1:Dimsize                                                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        4490  2020-03-26 10:56  回溯搜索算法    matlab  BSA.m.m

评论

共有 条评论