基于matlab的模拟退火法.doc
文本预览下载声明
基于matlab的模拟退火法
已关闭 30 [ 标签:matlab ] 匿名 2010-03-21 18:11
编写一个matlab的程序用模拟退火法求函数最优解,函数不用太复杂
推荐答案
function [xo,fo] = Opt_Simu(f,x0,l,u,kmax,q,TolFun)% 模拟退火算法求函数 f(x)的最小值点, 且 l = x = u% f为待求函数,x0为初值点,l,u分别为搜索区间的上下限,kmax为最大迭代次数% q为退火因子,TolFun为函数容许误差%%%%算法第一步根据输入变量数,将某些量设为缺省值if nargin 7 TolFun = 1e-8;endif nargin 6 q = 1;endif nargin 5 kmax = 100;end%%%%算法第二步,求解一些基本变量N = length(x0); %自变量维数x = x0;fx = feval(f,x); %函数在初始点x0处的函数值xo = x;fo = fx;%%%%%算法第三步,进行迭代计算,找出近似全局最小点for k =0:kmax Ti = (k/kmax)^q; mu = 10^(Ti*100); % 计算mu dx = Mu_Inv(2*rand(size(x))-1,mu).*(u - l);%步长dx x1 = x + dx; %下一个估计点 x1 = (x1 l).*l +(l = x1).*(x1 = u).*x1 +(u x1).*u; %将x1限定在区间[l,u]上 fx1 = feval(f,x1); df = fx1- fx; if df 0||rand exp(-Ti*df/(abs(fx) + eps)/TolFun) %如果fx1fx或者概率大于随机数z x = x1; fx = fx1; end if fx fo xo = x; fo = fx1; endendfunction x = Mu_Inv(y,mu) x = (((1+mu).^abs(y)- 1)/mu).*sign(y);
SA的算法很简单!最好自己编!有助于理解!给你一个!网上找的!很纯的SA!可以用!主程序:%J. C. Spall, March 1999%Written in support of text, Introduction to Stochastic Search and Optimization, 2003%Simulated annealing code. Uses geometric decay of temperature.%Provides two ways of dealing with noisy%loss measurements: one is by using the tau coefficient to alter the %decision criterion and the other is by simple averaging of the loss%measurements.%p=10;theta_0=2*3.1338*ones(p,1);sigma=0; %standard dev. of meas. noiseSTexamp6(theta_0)n=2401; %total no. of loss measurements(iterations/lossavg) niter=100; %no. of iters. per temp. setting bk=1; %Boltzmanns constantlambda=.90; %cooling rate (=1)scalpert=1; %scale factor on pertubation to %to current theta valuetau=0; %adjustment for noise in key decision statement for accept/rej. updaterandn(seed,1111113)rand(seedcases=1;lossavg=1; %number of loss functions averaged if noisy loss meas. (choose s.t. %(n-loss
显示全部