资源简介
通过编写杂草算法来寻找到一个二元函数的最小值
代码片段和文件信息
clear;
close all;
N0=30; %%%%初始种群个数%%
itmax=500; %%%迭代次数%%%%
dim=2; %%%%维数%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1 定义参数 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pmax=50; %%%%种群最大范围%%%
smax=5; %%%最大种子数%%%%
smin=0; %%%最小种子数%%%%
n=3; %%%%调和指数,计算方差使用,为固定好的值%%%
delta_initial=3; %%%方差最大值%%%
delta_final=0.001; %%%方差最小值%%%
xini=-100+200*rand(N02); %%%随机产生初始种群,在[01]之间%
%rand函数返回一个30*2的随机矩阵,并且在1-200之间
%本身函数关系为X=xmin+(xmax-xmin)*rand(N0dim)
%这里定义xmin=-100xmax=100
plot(xini(:1)xini(:2)‘b*‘) %%%%画出产生初始杂草种群图像(蓝色)
title(‘产生初始杂草种群‘); %%%%定义标题%%
xlabel(‘z1‘); %%%定义横坐标%%%
ylabel(‘z2‘); %%%%定义纵坐标%%%%
hold on; %%%%保持%%%
FB=zeros(itmax1); %%%生成itmax=500行*1列的0矩阵%%%
iter=1; %%%%定义初始迭代次数为1 %%%
while (iter %%%%循环迭代%%
%%%%%%%%%%%%%%%%%%%%5%%%%%%%% 2 生产繁殖,产生种子 %%%%%%%%%%%%%%%%%%%%%5%%%%%%%%%%%%%%%%%%%%%%%%%%
Niter=N0; %%%%定义此时的种群范围Niter=N0=30 刚刚开始等于初始种群个数%%%
fitness=xini(:1).^2+xini(:2).^2; %%%计算种群适应度函数%%%%
Nseed=round(-5/(max(fitness)-min(fitness))*(fitness-min(fitness))+5);
%%%产生种子 round函数输入任何一个数组,对这个数组
%%%里每个元素进行四舍五入取整
%%%原公式 Nseed=round(-(smax-smin)/max(fitness)-min(fitness))*(fitness-min(min(fitness)))+smax)
%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 空间扩散,以正态随机分布 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta_iter=(itmax-iter)^n/(itmax)^n*(delta_initial-delta_final)+delta_final;
%%%%%%%%%%% 计算每一代的标准差 %%%%%%%%%%%
%%原公式为 delta_iter=(delta_initial-delta_final)*((itmax-iter)^n)/((itmax)^n)+delta_final
M=size(Nseed); %%%% size函数返回Nseed矩阵的行数 %%%
m1=[];
m2=[];
a=[];
b=[]; %%%%% 定义m1 m2 a b四个空矩阵 %%%%
for i=1:M(11) %%% 对于每个个体 M=[501] 即i从1到50循环 %%%%
x1_iter=no
- 上一篇:用MATLAB产生各类信号
- 下一篇:三相逆变器simuli
nk仿真模型
评论
共有 条评论