模拟退火工具箱(求解一元,二元)
开启工具箱:optimtool
求解一元
-
%fitness.m文件 function fitnessVal = fitness(x) fitnessVal = - sin(10*pi*x)/x; end
求解二元
function fitnessVal = fitness(x)
%fitnessVal = - sin(10*pi*x)/x;
fitnessVal = -1 * (x(1).^2+x(2).^2-10*cos(2*pi*x(1))-10*cos(2*pi*x(2))+20);
end
C++版本:https://blog.csdn.net/Haipai1998/article/details/81570902
MATLAB版本:
%%main.m
T=1000; %初始化温度值
T_min=1e-12; %设置温度下界
alpha=0.98; %温度的下降率
k=1000; %迭代次数(解空间的大小)
x=getX; %随机得到初始解
while(T>T_min)
for I=1:100
fx=Fx(x);
x_new=getX;
if(x_new>=-2 && x_new<=2)
fx_new=Fx(x_new);
delta=fx_new-fx;
if (delta<0)
x=x_new+(2*rand-1);
else
P=getP(delta,T);
if(P>rand)
x=x_new;
end
end
end
end
T=T*alpha;
end
disp('最优解为:')
disp(x)
%%getX.m
function x=getX
x=4*rand-2;
end
%%Fx.m
function fx=Fx(x)
fx=(x-2)^2+4;
end
%%getP.m
function p=getP(c,t)
p=exp(-c/t);
end
Lingo:https://blog.csdn.net/zyl_wjl_1413/article/details/89074864
画图:https://blog.csdn.net/sunyueqinghit/article/details/81905509