加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab 程序实现 模拟退火算法程序 函数求极值(引用后修改)

(2012-12-01 07:07:55)
标签:

matlab

模拟退火算法

函数极值

杂谈

分类: 仿生学算法

%matlab 程序实现 模拟退火算法程序 函数求极值(引用后修改,感谢 ARMYLAU)

%使用模拟退火法求函数f(x,y) = 3*cos(xy) + x + y2的最小值
%解:根据题意,我们设计冷却表进度表为:
%即初始温度为30
%衰减参数为0.95
%马可夫链长度为10000
%Metropolis的步长为0.02
%结束条件为根据上一个最优解与最新的一个最优解的之差小于某个容差。
%使用METROPOLIS接受准则进行模拟, 程序如下

%* 日期:2012-11-29
%* 作者:steven
%* EMAIL:hxs2004@126.com
%* 结束条件为两次最优解之差小于某小量

function [BestX,BestY]=SimulateAnnealing1
clear;
clc;
%// 要求最优值的目标函数,搜索的最大区间
  XMAX= 4;
  YMAX = 4;
%冷却表参数
 MarkovLength = 10000; %// 马可夫链长度
 DecayScale = 0.95; %// 衰减参数
 StepFactor = 0.02; %// 步长因子
 Temperature=30; %// 初始温度
 Tolerance = 1e-8; %// 容差
AcceptPoints = 0.0; %// Metropolis过程中总接受点
rnd =rand;

% 随机选点 初值设定
PreX = -XMAX * rand ;
PreY = -YMAX * rand;
PreBestX  = PreX;
PreBestY = PreY;

PreX = -XMAX * rand ;
PreY = -YMAX * rand;
BestX = PreX;
 BestY = PreY;
% 每迭代一次退火一次(降温), 直到满足迭代条件为止
mm=abs( ObjectFunction( BestX,BestY)-ObjectFunction (PreBestX, PreBestY));
while mm > Tolerance

Temperature=DecayScale*Temperature;
AcceptPoints = 0.0;
% 在当前温度T下迭代loop(即MARKOV链长度)次
for i=0:MarkovLength:1
% 1) 在此点附近随机选下一点
p=0;
while p==0
     NextX = PreX + StepFactor*XMAX*(rand-0.5);
    NextY = PreY + StepFactor*YMAX*(rand-0.5);
    if p== (~(NextX >= -XMAX && NextX <= XMAX && NextY >= -YMAX && NextY <= YMAX))
        p=1;
    end
end

% 2) 是否全局最优解
if (ObjectFunction(BestX,BestY) > ObjectFunction(NextX,NextY))
% 保留上一个最优解
PreBestX =BestX;
PreBestY = BestY;
% 此为新的最优解
BestX=NextX;
BestY=NextY;
end
% 3) Metropolis过程

if( ObjectFunction(PreX,PreY) - ObjectFunction(NextX,NextY) > 0 )
  %// 接受, 此处lastPoint即下一个迭代的点以新接受的点开始
PreX=NextX;
PreY=NextY;
AcceptPoints=AcceptPoints+1;
else
 changer = -1 * ( ObjectFunction(NextX,NextY) - ObjectFunction(PreX,PreY) ) / Temperature ;
rnd=rand;
p1=exp(changer);
double (p1);

if p1 > rand             %// 不接受, 保存原解
    PreX=NextX;
    PreY=NextY;
    AcceptPoints=AcceptPoints+1;
end
end
end
mm=abs( ObjectFunction( BestX,BestY)-ObjectFunction (PreBestX, PreBestY));
end

disp('最小值在点:');
BestX
BestY
disp( '最小值为:{0}');
ObjectFunction(BestX, BestY)
end

 

 

****************************************************

子函数,目标函数值计算

 

function  value=ObjectFunction(x,y)
          value=3*cos(x*y)+x+y*y;
end

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有