编程语言
首页 > 编程语言> > 模拟退火算法经典图的代码

模拟退火算法经典图的代码

作者:互联网

        模拟退火算法(Simulated Annealing,SA)有一张特别经典的图,用于说明SA算法为何能跳出局部最优解,找到全局最优解。在写论文是必须要有原图和可编辑的原始文件,网上找了好久都没找到代码,在此记录一下。

        假定初始解为左边蓝色点A,模拟退火算法会快速搜索到局部最优解B,但在搜索到局部最优解后,不是就此结束,而是会以一定的概率接受到左边的移动。经过几次这样的不是局部最优的移动后有可能会到达全局最优点D,于是就跳出了局部最小值。
在这里插入图片描述
MATLAB版实现如下:

x=-8.2:0.05:8.5;
y =(x-2).*(x+2).*(x+5).*(x-4).*(x+7).*(x-8.5);
plot(x,y,'r','LineWidth',2);
axis([-10,10,-60000,60000]);
hold on
//(x1,y1),(x2,y2),(x3,y3)分别是三个极小值点,其中(x3,y3)是最小值点
x1=-6.25;
x2=-0.1;
x3=7.2;
y1=-4970;
y2=-4757;
y3=-34480;
sz=200;
scatter(x1,y1,sz,'p','MarkerEdgeColor','g','MarkerFaceColor','y')
scatter(x2,y2,sz,'p','MarkerEdgeColor','g','MarkerFaceColor','y')
scatter(x3,y3,sz,'p','MarkerEdgeColor','k','MarkerFaceColor','k')
hold on
sz1=80;
x0=-8:0.4:-6.7;
y0=(x0-2).*(x0+2).*(x0+5).*(x0-4).*(x0+7).*(x0-8.5);
scatter(x0,y0,sz1,'filled','b');
hold on
x0=-5.5:0.9:-0.5;
y0=(x0-2).*(x0+2).*(x0+5).*(x0-4).*(x0+7).*(x0-8.5);
scatter(x0,y0,sz1,'filled','b');
hold on
x0=0.8:0.8:6.8;
y0=(x0-2).*(x0+2).*(x0+5).*(x0-4).*(x0+7).*(x0-8.5);
scatter(x0,y0,sz1,'filled','b');

'MarkerEdgeColor' - 标记轮廓颜色,使用方法:'MarkerEdgeColor','某个颜色代号',

'MarkerFaceColor' - 标记填充颜色,使用方法:'MarkerFaceColor','某个颜色代号',

'filled' - 用于填充标记内部的选项,使用方法:'filled','某个颜色代号',

        注意:'MarkerEdgeColor' 和 'MarkerFaceColor' 可以结合使用,两者都不可以与 ‘filled’ 结合使用。

        原因是: 'filled' 选项将 Scatter 对象的 MarkerFaceColor 属性设置为 'flat',并将 MarkerEdgeColor 属性设置为 'none',这样便可只填充标记的面,而不绘制边。

        更多Scatter 知识请参见这篇博文 MATLAB中scatter函数的用法(绘制散点图)

想直接拿无水印图的点击 csdn上传图片无水印

标签:MarkerFaceColor,代码,算法,模拟退火,y0,MarkerEdgeColor,x0,scatter,filled
来源: https://www.cnblogs.com/2944014083-zhiyu/p/14873825.html