clear all;
close all;
clc;
xu = 5
xl = -5;
L = randint(1,1,[5 11]);
Ca = 5;
G = 200;
w = 1;
tabu = [];
x0 = rand(1,2)*(xu-xl)+xl;
bestsofar.key = xO;
xnow(1).key = xO;
%%%%%%%%%%%%%%最优解函数值,当前解函数值%%%%%%%%%%%%%%
bestsofar.value = func2(bestsofar.key);
xnow(1).value = func2(xnow(1).key);
g = 1;
while g < G
x_near = [];
w = w*0.998;
for i = l:Ca
%%%%%%%%%%%%%%产生邻域解%%%%%%%%%%%%%%
x_temp = xnow(g).key;
xl = x_temp(1);
x2 = x_temp(2);
x_near(i,l) = xl+(2*rand-l)*w*(xu-xl);
%%%%%%%%%%%%%%边界条件处理%%%%%%%%%%%%%%
if x_near(i,1) < xl
x_near(i,1> = xl;
end
if x_near(i,1) > xu
x_near(i,1) = xu;
end
x_near(i,2) = x2-f(2*rand-l)*w*(xu-xl);
%%%%%%%%%%%%%%边界吸收%%%%%%%%%%%%%%
if x_near(i,2) < xl
x_near(i,2) = xl;
end
if x_near(i,2) > xu
x_near(i,2) = xu;
end
%%%%%%%%%%%%%%计算邻域解点的函数值%%%%%%%%%%%%%%
fitvalue near(i) = func2(x_near(i,:));
end
%%%%%%%%%%%%%%最优邻域解为候选解%%%%%%%%%%%%%%
temp = find(fitvalue_near==max(fitvalue_near));
candidate(g).key = x_near(temp,:);
candidate(g).value = func2(candidate(g).key);
%%%%%%%%%%%%%%候选解和当前解的评价函数差%%%%%%%%%%%%%%
deltal = candidate(g).value-xnow(g).value;
%%%%%%%%%%%%%%候选解和目前最优解的评价函数差%%%%%%%%%%%%%%
delta2 = candidate(g).value-bestsofar.value;
%%%%%%%%%%%%%%候选解并没有改进解,把候选解赋给下一次迭代的当前解%%%%%%%%%%%%%%
if deltal <= 0
xnow(g+1).key = candidate(g).key;
xnow(g+1).value = func2(xnow(g).key);
%%%%%%%%%%%%%%%更新禁忌表%%%%%%%%%%%%%%%
tabu = [tabu;xnow(g+1).key];
if size(tabu,1) > L
tabu(1,:) = [];
end
g = g+i;
else
if delta2 > 0
xnow(g+1).key = candidate(g).key;
xnow(g+1).value = func2(xnow(g+1).key);
%%%%%%%%%%%%%%%更新禁忌表%%%%%%%%%%%%%%%
tabu = [tabu;xnow(g+1).key];
if size(tabu,1) > L
tabu(1,:) = [];
end
%%%%%%%%%%%%%%%把改进解赋给下一次迭代的目前最优解%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%包含藐视准则%%%%%%%%%%%%%%%
bestsofar.key = candidate(g).key;
bestsofar.value = func2(bestsofar.key);
g = g+1; %更新禁忌表后,迭代次数自增1
else
%%%%%%%%%%%%%%%判断改进解是否在禁忌表里%%%%%%%%%%%%%%%
[M,N] = size(tabu);
for m = 1:M
if candidate(g).key(1)==tabu(m,1)...
& candidate(g).key(2) == tabu(m,1)
r = 1;
end
end
if r==0
%改进解不在禁忌表里, 把改进解赋给下一次迭代的当前解
xnow(g+1).key = candidate(g).key;
xnow(g+1).value = func2(xnow(g+1).key);
%%%%%%%%%%%%%%%更新禁忌表%%%%%%%%%%%%%%%
tabu = [tabu;xnow(g).key];
if size(tabu,1) > L
tabu(1, : 〉 [ ];
end
g = g+1;
else
%%%%%%%%%%%%%%%如果改进解在禁忌表里,用当前解重新产生邻域解%%%%%%%%%%%%%%%
xnow(g).key = xnow(g).key;
xnow(g).value = func2(xnow(g).key)
end
end
end
trace(g) = func2(bestsofar.key);
end
bestsofar;
figure
plot(trace);
xlabel('迭代次数')
ylabel('目标函数值')
title('搜索过程最优值曲线')
function y = func2(x)
y = (cos(x(l)^2+x(2)^2)-0.1)/(1+0.3*(x(l)^2+x(2)^2)^2)+3;