📄 main_cell_li.m
字号:
function [best_fit,best_fitvalue,out_dai,pop_best,time]=main_cell_li(pop,n,popsize,chromlength,fun_num,up_range,down_range,var_num)
%函数说明
%入口参数:pop 当前处理的种群,
% n 绝对上限,
% popsize 种群大小,
% chromlength 基因位长度,
% fun_num 函数序列号,
% up_range 自变量的相对上限,
% down_range 自变量的相对下限
%出口参数:best_fit 各代最有适应度值的集合
%功能说明:初始种群pop通过遗传交叉变异在给定定义域中寻优,在遗传最大代数dai之前得到最优值就自动退出遗传,
% 判定最优值的标准:前后两代间最优值之间差值连续30次小于 阈值0.00001
% 如果达到遗传最大代数也没有达到退出标准,则强制结束遗传。
% clear all;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pop = [];
%
% var_num = 2;
% % fun_num=6;
% % fun_num = 11;
% fun_num = 1;
%
% switch fun_num
% case 1
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
% fun_max=20000;
%
% case 2
% n=2000;
% chromlength=11; %字符串长度(个体长度),染色体长度
% fun_max=121;
%
% case 3 %worse
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
% fun_max=50000;
%
% case 4
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
% fun_max=20000;
%
% case 5 %use short time
% n=60000;
% chromlength=16; %字符串长度(个体长度),染色体长度
% fun_max=87000000;
%
% case 6 % only a little
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
%
%
% case 7 % rand
% n=25600;
% chromlength=15; %字符串长度(个体长度),染色体长度
% fun_max=30;
%
% case 8 %much better
% n=100000;
% chromlength=17; %字符串长度(个体长度),染色体长度
% fun_max=1500;
%
% case 9 % only a little
% n=102400;
% chromlength=17; %字符串长度(个体长度),染色体长度
% fun_max=120;
%
% case 10 %only a little
% n=64000;
% chromlength=16; %字符串长度(个体长度),染色体长度
% fun_max=30;
%
% case 11 % good
% n=120000;
% chromlength=17; %字符串长度(个体长度),染色体长度
% fun_max=200;
%
% case 12 %use more time
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
%
% case 13
% n=20000;
% chromlength=15; %字符串长度(个体长度),染色体长度
%
% otherwise
% break
% end
% up_range=n;
% down_range=0;
% popsize=62; %设置初始参数,群体大小
%
%
% pop=my_sga_initpop(popsize,n,var_num); %运行初始化函数,随机产生初始群体
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic; %用于记录运行时间
% popsize=66; %设置初始参数,群体大小
% L = 6; % 设置子群体规模
popsize=64; %设置初始参数,群体大小
L = 4; % 设置子群体规模
S = 1; % 设置子种群交叠个体个数
% M = 16; % 设置 子种群个数
M = 21; % 设置 子种群个数
cont=zeros(1,M); % 子群体计数器
contor = 0; % 群体计数器
% pc=0.95; %设置交叉概率
% pm=0.05; %设置变异概率
pc=0.1; %设置交叉概率
pm=0.1; %设置变异概率
%参数设置结束
best_fit = [];
% 局部公告板
% local_board_dna = zeros(var_num,M);
% local_board_fitvalue = zeros(1,M);
% % 全局公告板
% global_board_dna = zeros(var_num,1);
% global_board_fitvalue = -999999;
dai=1000;
%给最佳染色体及其适应度值赋初值%
pop_best=zeros(var_num,1);
value_best=-999999;
dai_best=1;
%结束
% i=1;
% max_fitvalue=0;
%计算群体中每个个体的适应度
fitvalue=my_sga_calfitvalue(pop,fun_num,var_num);
[bestindividual,bestfit]=my_sga_best(popsize,pop,fitvalue);
best_fit(1)=bestfit;
pop_best = bestindividual;
value_best = bestfit;
old_max_fitvalue=max(fitvalue);
old_max_dna = bestindividual;
for i=1:dai %1000为迭代次数
% fitvalue=my_sga_calfitvalue(pop,fun_num,var_num);
% [bestindividual,bestfit]=my_sga_best(popsize,pop,fitvalue);
% old_max_fitvalue=max(fitvalue);
% old_max_dna = bestindividual;
start_number = 1;
end_number = L;
for j = 1:M
% if subpop_flg(j) ~= 1
% 子种群初始化
subpop = pop(:,start_number:end_number);
% 计算子种群的适应值
subpop_fitvalue = my_sga_calfitvalue(subpop,fun_num,var_num);
%左右邻域竞争
[sub_newpop]=improve_compete3(L,subpop,subpop_fitvalue,fun_num,up_range,down_range,var_num);
% 计算适应值
subpop_fitvalue = my_sga_calfitvalue(sub_newpop,fun_num,var_num);
%交叉
[sub_newpop]=complete3_o_crossover(L,sub_newpop,pc,subpop_fitvalue,fun_num,up_range,down_range,var_num);
%变异
[sub_newpop]=complete_mutation_cell_new(L,sub_newpop,pm,i,fun_num,var_num);
% 更新大种群
pop(:,start_number:end_number)=sub_newpop;
% %计算群体中每个个体的适应度
% subpop_fitvalue=my_sga_calfitvalue(subpop,fun_num,var_num);
% % 求出进化后子种群的最大适应值
% [new_subpop_fitvalue_max,max_indax] = max(subpop_fitvalue);
% new_subpop_dna_max = subpop(:,max_indax);
% % 更新全局最优值
% if new_subpop_fitvalue_max > value_best
% pop_best = new_subpop_dna_max;
% value_best = new_subpop_fitvalue_max;
% dai_best=i;
% end
% end
% 更新起始终止位
start_number = end_number - S+1;
end_number = start_number + L -1;
% sub_best_fit
end
fitvalue_tem=my_sga_calfitvalue(pop,fun_num,var_num);
[bestindividual,bestfit]=my_sga_best(popsize,pop,fitvalue_tem);
new_max_dna = bestindividual;
new_max_fitvalue = bestfit;
if new_max_fitvalue > old_max_fitvalue
pop_best = new_max_dna;
value_best = new_max_fitvalue;
best_fitvalue = value_best;
best_fit(i+1) = new_max_fitvalue;
dai_best=i;
old_max_fitvalue=new_max_fitvalue;
old_max_dna = new_max_dna;
else
new_max_fitvalue = old_max_fitvalue;
pop_best = old_max_dna;
value_best = old_max_fitvalue;
best_fitvalue = value_best;
best_fit(i+1) = old_max_fitvalue;
% 精英保留
[max_value,indax] = max(fitvalue_tem);
pop(:,indax) = old_max_dna;
old_max_fitvalue=old_max_fitvalue;
old_max_dna = old_max_dna;
end
% fitvalue_aaaa=my_sga_calfitvalue(pop,fun_num,var_num);
% aaaa = max(fitvalue_aaaa)
% best_fit
if best_fit(i+1) == best_fit(i)
% if new_max_fitvalue == old_max_fitvalue
contor = contor + 1;
else
contor = 0;
end
if contor == 10
out_dai = i;
break;
end
if i == dai
out_dai = i;
best_fitvalue = best_fit(i);
end
% if min(subpop_flg) ~= 0
% out_dai = i;
% break;
%
% end
% new_max_value = max(max(sub_best_fit));
% [temp,subpop_num] = max(sub_best_fit);
% [new_max_value,best_dai] = max(temp);
% best_fitvalue = new_max_value;
% temp_dna = sub_best_dna(:,subpop_num);
% bestindividual = temp_dna(:,best_dai);
% best_fit = temp;
% new_max_value = max(local_board_fitvalue);
% if new_max_value > old_max_value
% best_fit(i+1)=new_max_value;
% else
% best_fit(i+1)=old_max_value;
% end
% best_fitvalue = max(best_fit);
% if new_max_value == old_max_value
% contor = contor + 1;
% else
% old_max_value = new_max_value;
% contor = 0;
% end
% if contor == 10
% out_dai = i;
% break;
% end
end
time = toc;
%
%
% plot(-sub_best_fit,'r--')
%
% sub_best_fit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -