資源簡介
回溯搜索算法,回溯算法也叫試探法,它是一種系統(tǒng)地搜索問題的解的方法。回溯算法的基本思想是:從一條路往前走,能進(jìn)則進(jìn),不能進(jìn)則退回來,換一條路再試。

代碼片段和文件信息
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%?回溯搜索(BSA)進(jìn)行函數(shù)優(yōu)化,
%?輸入:popsize?種群個數(shù),Dimsize?變量維數(shù),pop=【pop1pop2...popi...popN】為種群集合,Maxgen?最大迭代次數(shù),
%?lowerboundary?upperboundary分別為變量上下界,?
%?輸出:?globalminimizerglobalminimum
function?BSA()
clc;
clear?all;
format?long;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%??給定初始化條件??%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
popsize??=?10;?????????????????????????????????????????????????????????????%?種群數(shù)目
Dimsize??=??30;????????????????????????????????????????????????????????????%?變量維數(shù)
Maxgen??=??1000;???????????????????????????????????????????????????????????%?最大的迭代次數(shù)
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?????????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????????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
評論
共有 條評論