clc
clear all;
global N
%———- initialization of variable———%
N = 60;%等分区间的数量
N_ants=50 ;%蚂蚁的数目
N_vars = 30;%变量数量
Times=250;%迭代的次数
Q = 10;%信息量强度
Rou=0.05;%信息量残留因子
% det = ceil(N/4);%最优解为中心 上下浮动的程度范围一般为[1,6]
det = 4;
index = 2;% 概率计算公式中分子的指数
%———————————————-%
% ———–F Domain———%
% Lower(1) = 0;
% Upper(1) = 2;
% Lower(2) = 1;
% Upper(2) = 3;
% ——————————-%
% ———–F1 F3 F4 F5 F Domain———%
for i = 1:N_vars
Lower(i) = -100;
Upper(i) = 100;
end
% ——————————-%
%———–F3 Domain———–%
% for i = 1:N_vars
% Lower(i) = -10;
% Upper(i) = 10;
% end
%——————————-%
%———–F6 Domain———–%
% for i = 1:N_vars
% Lower(i) = -1.28;
% Upper(i) = 1.28;
% end
%——————————-%
%———–F7 Domain———–%
% for i = 1:N_vars
% Lower(i) = -500;
% Upper(i) = 500;
% end
%——————————-%
%———–F8 F16 Domain———–%
% for i = 1:N_vars
% Lower(i) = -5.12;
% Upper(i) = 5.12;
% end
%——————————-%
%———–F9 F17 Domain———–%
% for i = 1:N_vars
% Lower(i) = -32;
% Upper(i) = 32;
% end
%——————————-%
%———–F10 F11 Domain———–%
% for i = 1:N_vars
% Lower(i) = -50;
% Upper(i) = 50;
% end
%——————————-%
%———–F12 Domain———–%
% for i = 1:N_vars
% Lower(i) = -5;
% Upper(i) = 5;
% end
%——————————-%
%———–F13 F14 F15 Domain———–%
% for i = 1:N_vars
% Lower(i) = 0;
% Upper(i) = 10;
% end
%——————————-%
tao(1:N+1,1:N_vars) = 1/(N+1);
% tao(N+1,:) = 0.01
x_best = Lower;
% fx_best = feval(f,x_best);
fx_best = F1(x_best);%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
x(1,N_vars) = 0;
tao_sum(N_vars) = 0;
P(N+1,N_vars) = 0;
pos = ceil((N+1)*rand(1,N_vars)) %随机放置蚂蚁
%% 算法开始
for iter=1:Times %迭代次数
for i = 1:N_ants%遍历每只蚂蚁
% pos = ceil((N+1)*rand(1,N_vars))%随机放置蚂蚁
%% 将变量区间进行划分网格
for j = 1:N_vars
h(j) = (Upper(j)-Lower(j))/N;%划分后区间的大小
% x(j) = Lower(j)+(pos(j)-1)*h(j);%计算蚂蚁当前位置(区间)的 左区间的值
end%j
% f_value = feval(f,x);%计算蚂蚁当前位置的左区间对应的值
f_value = F1(x);%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
%%
if f_value < fx_best
% 更新节点信息量
for j= 1:N_vars
tao(pos(j),j) = (1-Rou)*tao(pos(j),j)+Q/f_value;
end %j
% 计算信息量总和
for j = 1:N_vars
tao_sum(j) = sum(tao(:,N_vars));
% tao_sum(j) = sum(tao(:,N_vars).^index);
end%j
%% 更新概率
for j = 1:N+1
for k = 1:N_vars
P(j,k) = tao(j,k) /tao_sum(k);
% P(j,k) = tao(j,k)^index /tao_sum(k);
end%k
end %j
end % end if
end
%% 通过概率的大小选择最优的变量和最优的函数值
for j = 1:N_vars
P_vars = P(:,j);%第j个变量的所有区间概率
[P_x_best,ind] = sort(P_vars);%当前变量各个区间概率从小到大排序 ind 是对应概率的节点序号
maxP_ind (j) = ind(N+1);%概率最大的节点序号=ind最后一个数
x_best(j) = Lower(j)+(Upper(j)-Lower(j))*(ind(N+1))/N;%计算最大概率变量的值
end %j
% f_value = feval(f,x_best);%计算函数值
f_value = F1(x_best);%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if f_value < fx_best %更新最佳函数值
fx_best = f_value;
x_best = x;
end
optimal_f(iter) = fx_best
optimal_x(iter,:) = x_best
%% 缩小变量范围 优化变量范围
for j = 1:N_vars
Lower_new(j) = Lower(j)+(maxP_ind(j)-det)*((Upper(j)-Lower(j))/N);
Upper_new(j) = Lower(j)+(maxP_ind(j)+det)*((Upper(j)-Lower(j))/N);
% 越界处理
if Lower_new(j)<Lower(j)
Lower_new(j)=Lower(j);
end
if Upper_new(j) > Upper(j)
Upper_new(j) = Upper(j);
end
Lower(j) = Lower_new(j);
Upper(j) = Upper_new(j);
end %j
end %i
fx_best
x_best
figure(1)%变量的变化趋势
for k = 1:N_vars
subplot(N_vars,1,k);
plot([1:Times],optimal_x(:,k),’b’)
end
figure (2) %函数值的变化趋势
plot ([1:Times],optimal_f,’b-‘)
虽然是小小的程序,解决一些简单的函数求解 但是也是我科研的突破 嘿嘿,很开心