模拟退火算法Matlab源程序

模拟退火算法Matlab源程序
模拟退火算法Matlab源程序

MCM战备历程3(模拟退火算法Matlab源程序)For glory

2007-02-03 11:20:04| 分类:数学建模 | 标签:学习|字号订阅

%模拟退火算法程序

T_max=input('please input the start temprature');

T_min=input('please input the end temprature');

iter_max=input('please input the most interp steps on the fit temp');

s_max=input('please input the most steady steps ont the fit temp');

T=T_max;

load d:\address.txt;

order1=randperm(size(address,1))';%生成初始解。

plot(address(order1,1),address(order1,2),'*r-')

totaldis1=distance(address,order1);

while T>=T_min

iter_num=1;

s_num=1;

plot(T,totaldis1)

hold on

while iter_num

order2=exhgpath(order1);

totaldis2=distance(address,order2);

R=rand;

DeltaDis=totaldis2-totaldis1;

if DeltaDis<0;

order1=order2;

totaldis1=totaldis2;

elseif (exp((totaldis1-totaldis2)/(T))>R)

order1=order2;

totaldis1=totaldis2;

else s_num=s_num+1;

end

iter_num=iter_num+1;

end

T=T*0.99;

end

order1

totaldis1

figure(2)

plot(address(order1,1),address(order1,2),'*r-')

function y=distance(address,order)

nmb=size(address,1);

y=0;

for i=1:nmb-1

y=y+sqrt((address(order(i+1),1)-address(order(i),1))^2+(address(order(i+1) ,2)-address(order(i),2))^2);

end

y=y+sqrt((address(order(i+1),1)-address(order(1),1))^2+(address(order(i+1),2) -address(order(1),2))^2);

function y=exhgpath(order)

while 1

b=size(order,1);

r=unidrnd(b,1,2);

if r(1)-r(2)~=0

break

end

end

b=order(r(2));

order(r(2))=order(r(1));

order(r(1))=b;

(模拟退火算法Matlab源程序)

学术讨论2008-04-01 18:05:56 阅读1322 评论6 字号:大中小订阅

(模拟退火算法Matlab源程序)For glory

%模拟退火算法程序

T_max=input('please input the start temprature');

T_min=input('please input the end temprature');

iter_max=input('please input the most interp steps on the fit temp');

s_max=input('please input the most steady steps ont the fit temp');

T=T_max;

load d:\address.txt;

order1=randperm(size(address,1))';%生成初始解。

plot(address(order1,1),address(order1,2),'*r-')

totaldis1=distance(address,order1);

while T>=T_min

iter_num=1;

s_num=1;

plot(T,totaldis1)

hold on

while iter_num

order2=exhgpath(order1);

totaldis2=distance(address,order2);

R=rand;

DeltaDis=totaldis2-totaldis1;

if DeltaDis<0;

order1=order2;

totaldis1=totaldis2;

elseif (exp((totaldis1-totaldis2)/(T))>R)

order1=order2;

totaldis1=totaldis2;

else s_num=s_num+1;

end

iter_num=iter_num+1;

end

T=T*0.99;

end

order1

totaldis1

figure(2)

plot(address(order1,1),address(order1,2),'*r-')

function y=distance(address,order)

nmb=size(address,1);

y=0;

for i=1:nmb-1

y=y+sqrt((address(order(i+1),1)-address(order(i),1))^2+(address(order(i+1),2)-address(order(i),2))^2); end

y=y+sqrt((address(order(i+1),1)-address(order(1),1))^2+(address(order(i+1),2)-address(order(1),2))^2);

function y=exhgpath(order)

while 1

b=size(order,1);

r=unidrnd(b,1,2);

if r(1)-r(2)~=0

break

end

end

b=order(r(2));

order(r(2))=order(r(1));

order(r(1))=b;

y=order;

模拟退火算法(MATLAB实现)

实验用例: 用模拟退火算法解决如下10个城市的TSP 问题,该问题最优解为691.2 opt f 。 表1 10个城市的坐标 城市 X 坐标 Y 坐标 城市 X 坐标 Y 坐标 3 0.4000 0.4439 8 0.8732 0.6536 编程实现 用MATLAB 实现模拟退火算法时,共编制了5个m 文件,分别如下 1、swap.m function [ newpath , position ] = swap( oldpath , number ) % 对 oldpath 进 行 互 换 操 作 % number 为 产 生 的 新 路 径 的 个 数 % position 为 对 应 newpath 互 换 的 位 置 m = length( oldpath ) ; % 城 市 的 个 数 newpath = zeros( number , m ) ; position = sort( randi( m , number , 2 ) , 2 ); % 随 机 产 生 交 换 的 位 置 for i = 1 : number newpath( i , : ) = oldpath ; % 交 换 路 径 中 选 中 的 城 市 newpath( i , position( i , 1 ) ) = oldpath( position( i , 2 ) ) ; newpath( i , position( i , 2 ) ) = oldpath( position( i , 1 ) ) ; end 2、pathfare.m function [ objval ] = pathfare( fare , path ) % 计 算 路 径 path 的 代 价 objval % path 为 1 到 n 的 排 列 ,代 表 城 市 的 访 问 顺 序 ; % fare 为 代 价 矩 阵 , 且 为 方 阵 。 [ m , n ] = size( path ) ; objval = zeros( 1 , m ) ; for i = 1 : m for j = 2 : n objval( i ) = objval( i ) + fare( path( i , j - 1 ) , path( i , j ) ) ; end objval( i ) = objval( i ) + fare( path( i , n ) , path( i , 1 ) ) ; end

模拟退火算法Matlab源程序

MCM战备历程3(模拟退火算法Matlab源程序)For glory 2007-02-03 11:20:04| 分类:数学建模 | 标签:学习|字号订阅 %模拟退火算法程序 T_max=input('please input the start temprature'); T_min=input('please input the end temprature'); iter_max=input('please input the most interp steps on the fit temp'); s_max=input('please input the most steady steps ont the fit temp'); T=T_max; load d:\address.txt; order1=randperm(size(address,1))';%生成初始解。 plot(address(order1,1),address(order1,2),'*r-') totaldis1=distance(address,order1); while T>=T_min iter_num=1; s_num=1; plot(T,totaldis1) hold on while iter_numR) order1=order2; totaldis1=totaldis2; else s_num=s_num+1;

模拟退火算法原理及matlab源代码

模拟退火算法模拟退火算法是一种通用的随机搜索算法,是局部搜索算法的扩展。它的思想是再1953 年由metropolis 提出来的,到1983 年由kirkpatrick 等人成功地应用在组合优化问题中。 模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减为最小。根据Metropolis 准则,粒子在温度T 时趋于平衡的概率为e- △ E/(kT),其中E为温度T时的内能,AE为其改变量,k 为Boltzmann 常数。用固体退火模拟组合优化问题,将内能E模拟为目标函数值f ,温度T演化成控制参数t,即得到解组合优化问题的模拟退火算法:由初始解i和控制参数初值t开始,对当前解重复“产生新解-计算目标函数差T接受或舍弃”的迭代,并逐步衰减t值,算法终止时的当前解即为所得近似最优解,这是基于蒙特卡罗迭代求解法的一种启发式随机搜索过程。退火过程由冷却进度表(Cooli ng Schedule)控制,包括控制参数的初值t 及其衰减因子△ t、每个t值时的迭代次数L和停止条件S。 模拟退火算法新解的产生和接受可分为如下四个步骤:第一步是由一个产生函数从当前解产生一个位于解空间的新解;为便于后续的计算和接受,减少算法耗时,通常选择由当前新解经过简单地变换即可产生新解的方法,如对构成新解的全部或部分元素进行置换、互换等,注意到产生新解的变换方法决定了当前新解的邻域结构,因而对冷却进度表的选取有一定的影响。 第二步是计算与新解所对应的目标函数差。因为目标函数差仅由变换部分产生,所以目标函数差的计算最好按增量计算。事实表明,对大多数应用而言,这是计算目标函数差的最快方法。 第三步是判断新解是否被接受,判断的依据是一个接受准则,最常用的接受准则是Metropo1is准则:若厶t‘ <0 则接受S'作为新的当前解S,否则以概率exp(- △ t‘ /T) 接受S'作为新的当前解S。 第四步是当新解被确定接受时,用新解代替当前解,这只需将当前解中对应于产生新解时的变换部分予以实现,同时修正目标函数值即可。此时,当前解实现了一次迭代。 可在此基础上开始下一轮试验。而当新解被判定为舍弃时,

模拟退火算法(C++版)

/* * 使用模拟退火算法(SA)求解TSP问题(以中国TSP问题为例) * 参考自《Matlab 智能算法30个案例分析》 * 模拟退火的原理这里略去,可以参考上书或者相关论文 * update: 16/12/11 * author:lyrichu * email:919987476@https://www.360docs.net/doc/092625112.html, */ #include #include #include #include #include #define T0 50000.0 // 初始温度 #define T_end (1e-8) #define q 0.98 // 退火系数 #define L 1000 // 每个温度时的迭代次数,即链长 #define N 27 // 城市数量 int city_list[N]; // 用于存放一个解 double city_pos[N][2] = {{41,94},{37,84},{53,67},{25,62},{7,64},{2,99},{68,58},{71,44},{54,62}, {83,69},{64,60},{18,54},{22,60},{83,46},{91,38},{25,38},{24,42},{58,69},{71,71}, {74,78},{87,76}, {18,40},{13,40},{82,7},{62,32},{58,35},{45,21}}; // 中国27个城市坐标 //41 94;37 84;53 67;25 62;7 64;2 99;68 58;71 44;54 62;83 69;64 60; 18 54;22 60; //83 46;91 38;25 38;24 42;58 69;71 71;74 78;87 76;18 40;13 40;82 7; 62 32;58 35;45 21

模拟退火算法算法的简介及程序

模拟退火算法 模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减为最小。根据Metropolis准则,粒子在温度T时趋于平衡的概率为e-ΔE/(kT),其中E为温度T时的内能,ΔE为其改变量,k为Boltzmann常数。用固体退火模拟组合优化问题,将内能E模拟为目标函数值f,温度T演化成控制参数t,即得到解组合优化问题的模拟退火算法:由初始解i和控制参数初值t开始,对当前解重复“产生新解→计算目标函数差→接受或舍弃”的迭代,并逐步衰减t值,算法终止时的当前解即为所得近似最优解,这是基于蒙特卡罗迭代求解法的一种启发式随机搜索过程。退火过程由冷却进度表(Cooling Schedule)控制,包括控制参数的初值t及其衰减因子Δt、每个t值时的迭代次数L和停止条件S。 模拟退火算法的模型 模拟退火算法可以分解为解空间、目标函数和初始解三部分。 模拟退火的基本思想: (1)初始化:初始温度T(充分大),初始解状态S(是算法迭代的起 点),每个T值的迭代次数L (2) 对k=1,……,L做第(3)至第6步: (3) 产生新解S′ (4) 计算增量Δt′=C(S′)-C(S),其中C(S)为评价函数 (5) 若Δt′<0则接受S′作为新的当前解,否则以概率exp(-Δt′/T)

接受S′作为新的当前解. (6) 如果满足终止条件则输出当前解作为最优解,结束程序。终止条件通常取为连续若干个新解都没有被接受时终止算法。 (7) T逐渐减少,且T->0,然后转第2步。 算法对应动态演示图: 模拟退火算法新解的产生和接受可分为如下四个步骤: 第一步是由一个产生函数从当前解产生一个位于解空间的新解;为便于后续的计算和接受,减少算法耗时,通常选择由当前新解经过简单地变换即可产生新解的方法,如对构成新解的全部或部分元素进行置换、互换等,注意到产生新解的变换方法决定了当前新解的邻域结构,因而对冷却进度表的选取有一定的影响。 第二步是计算与新解所对应的目标函数差。因为目标函数差仅由变换部分产生,所以目标函数差的计算最好按增量计算。事实表明,对大多数应用而言,这是计算目标函数差的最快方法。 第三步是判断新解是否被接受,判断的依据是一个接受准则,最常用的接受准则是Metropo1is准则: 若Δt′<0则接受S′作为新的当前解S,否则以概率exp(-Δt′/T)接受S′作为新的当前解S。 第四步是当新解被确定接受时,用新解代替当前解,这只需将当前解中对应于产生新解时的变换部分予以实现,同时修正目标函数值即可。此时,当前解实现了一次迭代。可在此基础上开始下一轮试验。而当新解被判定为舍弃时,则

模拟退火算法及其Matlab实现

模拟退火算法及其Matlab 实现 模拟退火算法(Simulated Annealing algorithm ,简称SA )是柯克帕垂克(S. Kirkpatrick )于1982年受热力学中的固体退火过程与组合优化问题求解之间的某种“相似性”所启发而提出的,用于求解大规模组合优化问题的一种具有全局搜索 功能的随机性近似算法。与求解线性规划的单纯形法、Karmarkar 投影尺度法,求 解非线性规划的最速下降法、Newton 法、共轭梯度法,求解整数规划的分支定界法、割平面法等经典的优化算法相比,模拟退火算法在很大程度上不受制于优化问 题的具体形式和结构,具有很强的适应性和鲁棒性,因而也具有广泛的应用价值。 模拟退火算法源于对固体退火过程的模拟;采用Metropolis 接受准则;并用 一组称为冷却进度表的参数来控制算法进程,使得算法在多项式时间里给出一个近 似最优解。固体退火过程的物理现象和统计性质是模拟退火算法的物理背 景;Metropolis 接受准则使算法能够跳离局部最优的“陷阱”,是模拟退火算法能 够获得整体最优解的关键;而冷却进度表的合理选择是算法应用的关键。 1 物理退火过程 物理中的固体退火是先将固体加热至熔化,再徐徐冷却,使之凝固成规整晶体 的热力学过程。在加热固体时,固体粒子的热运动不断增加,随着温度的升高,粒子 与其平衡位置的偏离越来越大,当温度升至溶解温度后,固体的规则性被彻底破坏, 固体溶解为液体,粒子排列从较有序的结晶态转变为无序的液态,这个过程称为溶解。溶解过程的目的是消除系统中原先可能存在的非均匀状态,使随后进行的冷却 过程以某一平衡态为始点。溶解过程与系统的熵增过程相联系,系统能量也随温度 的升高而增大。 冷却时,液体粒子的热运动渐渐减弱,随着温度的徐徐降低,粒子运动渐趋有 序。当温度降至结晶温度后,粒子运动变为围绕晶体格点的微小振动,液体凝固成固体的晶态,这个过程称为退火。退火过程之所以必须“徐徐”进行,是为了使系统在每一温度下都达到平衡态,最终达到固体的基态(图1-1)。退火过程中系统的熵值

模拟退火算法和禁忌搜索算法的matlab源程序

%%% 模拟退火算法源程序 % 此题以中国31省会城市的最短旅行路径为例: % clear;clc; function [MinD,BestPath]=MainAneal(pn) % CityPosition存储的为每个城市的二维坐标x和y; CityPosition=[1304 2312;3639 1315;4177 2244;3712 1399;3488 1535;3326 1556;3238 1229;... 4196 1044;4312 790;4386 570;3007 1970;2562 1756;2788 1491;2381 1676;... 1332 695;3715 1678;3918 2179;4061 2370;3780 2212;3676 2578;4029 2838;... 4263 2931;3429 1908;3507 2376;3394 2643;3439 3201;2935 3240;3140 3550;... 2545 2357;2778 2826;2370 2975]; figure(1); plot(CityPosition(:,1),CityPosition(:,2),'o') m=size(CityPosition,1);%城市的数目 % D = sqrt((CityPosition(:,ones(1,m)) - CityPosition(:,ones(1,m))').^2 + ... (CityPosition(:,2*ones(1,m)) - CityPosition(:,2*ones(1,m))').^2); path=zeros(pn,m); for i=1:pn path(i,:)=randperm(m); end iter_max=100;%i m_max=5;% Len1=zeros(1,pn);Len2=zeros(1,pn);path2=zeros(pn,m); t=zeros(1,pn); T=1e5; tau=1e-5; N=1; while T>=tau iter_num=1; m_num=1; while m_num

模拟退火算法求解TSP问题Matlab源码

function [f,T]=TSPSA(d,t0,tf) %TSP问题(货郎担问题,旅行商问题)的模拟退火算法通用malab源程序% f目标最优值,T最优路线,d距离矩阵,t0初始温度,tf结束温度 [m,n]=size(d); L=100*n; t=t0; pi0=1:n; min_f=0; for k=1:n-1 min_f=min_f+d(pi0(k),pi0(k+1)); end min_f=min_f+d(pi0(n),pi0(1)); p_min=pi0; while t>tf for k=1:L; kk=rand; [d_f,pi_1]=exchange_2(pi0,d); r_r=rand; if d_f<0 pi0=pi_1; elseif exp(d_f/t)>r_r pi0=pi_1; else pi0=pi0; end end f_temp=0; for k=1:n-1 f_temp=f_temp+d(pi0(k),pi0(k+1)); end f_temp=f_temp+d(pi0(n),pi0(1)); if min_f>f_temp min_f=f_temp; p_min=pi0; end t=0.87*t; end f=min_f; T=p_min; %aiwa要调用的子程序,用于产生新解 function [d_f,pi_r]=exchange_2(pi0,d) [m,n]=size(d); clear m; u=rand;

u=u*(n-2); u=round(u); if u<2 u=2; end if u>n-2 u=n-2; end v=rand; v=v*(n-u+1); v=round(v); if v<1 v=1; end v=u+v; if v>n v=n; end pi_1(u)=pi0(v); pi_1(v)=pi0(u); if u>1 for k=1:u-1 pi_1(k)=pi0(k); end end if v>(u+1) for k=1:v-u-1 pi_1(u+k)=pi0(v-k); end end if v

M模拟退火最短距离问题 Matlab代码 亲测完美运行

模拟退火算法 基于模拟退火算法的TSP问题求解具体步骤如下: 1)随机产生一个初始解path(作为当前最优路径),计算目标函数值pathfare(fare,path)=e0,并设置初始温度t0,内循环终止方差istd,外循环终止方差ostd降温系数lam,温度更新函数tk=lam*tk-1,并令k=1,输入各城市坐标coord,计算城市间的距离fare。 2)根据控制参数更新函数来控制温度的下降过程,给定最大循环步数iLK,设置循环计数器的初始值in=1。 3)对当前的最优路径作随机变动,产生一个新路径newpath,计算新路径的目标函数值pathfare(fare,newpath)=e1和目标函数值的增量e1-e04)根据Metropolis准则,如果增量(e1-e0)<0,则接受新产生的路径newpath作为当前最优路径;如果(e1-e0)>=0,则以公式(1)来决定新路径newpath是否代替path。rand()随机产生一个在[0,1]之间的随机数。exp[-(e1-e0)/t]>rand() 4)如果目标函数值小于istd,则直接跳出内循环。 5)如果in

1. distance.m function [fare]=distance(coord) %coord为各城市的坐标 %fare为城市间的距离矩阵 [~,m]=size(coord);%m为城市的个数 fare=zeros(m); for i=1:m%外层为行 for j=1:m%内层为列 fare(i,j)=(sum((coord(:,i)-coord(:,j)).^2))^0.5; fare(j,i)=fare(i,j);%距离矩阵对称 end end 2. myplot.m function []=myplot(path,coord,pathfar) %做出路径的图形 %path为要做图的路径,coord为各个城市的坐标 %pathfar为路径path对应的费用 len=length(path); clf; hold on;

基于matlab的模拟退火法

基于matlab的模拟退火法 编写一个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; end if nargin < 6 q = 1; end if 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) %如果fx1

遗传模拟退火算法matlab通用源程序

% maxpop给定群体规模% pop群体 % newpop种群 %t0初始温度 function [codmin,finmin]=fc0(cc,v0,t0) N=length(cc(1,:)); %定群体规模 if N>50 maxpop=2*N-20; end if N<=40 maxpop=2*N; end %产生初始群体 pop=zeros(maxpop,N); pop(:,1)=v0; finmin=inf; codmin=0; for i=1:maxpop Ra=randperm(N); Ra(find(Ra==v0))=Ra(1);

pop(i,:)=Ra; end t=t0; while t>0 %用模拟退火产生新的群体pop=fc1(maxpop,pop,N,cc,v0,t); %转轮赌选择种群 f=zeros(1,maxpop); for i=1:maxpop for j=1:N-1 x=pop(i,j); y=pop(i,j+1); fo1=cc(pop(i,j),pop(i,j+1)); f(i)=f(i)+fo1; end f(i)=f(i)+cc(pop(i,1),pop(i,N)); end fmin=min(f); for i=1:maxpop if fmin==inf&f(i)==inf

end if fmin~=inf|f(i)~=inf dd=fmin-f(i); end ftk(i)=exp(dd/t); end [fin1,cod]=sort(-ftk); fin=abs(fin1); %f(cod(1)) if f(cod(1))=RR); % cod newpop(i,:)=pop(cod(cod2(end)),:); end %单亲繁殖

智能计算-模拟退火算法(matlab实现)

模拟退火算法 摘要:阐述了模拟退火算法的基本原理及实现过程,运用MATLAB语言实现了该算法。并将其运用到解决旅行商问题的优化之中。数值仿真的结果表明了该方法能够对函数进行全局寻优,有效克服了基于导数的优化算法容易陷入局部最优的问题。该方法既可以增加对MATLAB 语言的了解又可以加深对模拟退火过程的认识,并达到以此来设计智能系统的目的。 关键词:模拟退火算法,全局寻优,搜索策略

simulatedannealing algorithm Abstract:This paper describes the basic principles and processes simulatedannealing algorithm, using MATLAB language implementation of the algorithm. And use it to solve the traveling salesman problem among optimization. Simulation results show that the method can be a function of global optimization, effectively overcome the derivative-based optimization algorithm is easy to fall into local optimum. This method not only can increase the MATLAB language can deepen understanding and awareness of the simulated annealing process, and in order to achieve the purpose of the design of intelligent systems. Keywords:simulatedannealing algorithm,Global optimization,strategy

MATLAB-模拟退火法

MATLAB-模拟退火法一、 图1 题目一 MATLAB程序: D=10; %变量维数 Xs=20; %上限 Xx=-20; %下限 L=200; %马可夫链长度 K=0.998; %衰减参数

S=0.01; %步长因子 T=100; %初始温度 YZ=1e-8; %容差 P=0; %Metropolis过程中总接收点 %%%%%%%%%%%%%随机选点初值设定%%%%%%%%%%%%%%%%%% PreX=rand(D,1)*(Xs-Xx)+Xx; PreBestX=PreX; PreX=rand(D,1)*(Xs-Xx)+Xx; BestX=PreX; %%%%%%%%%%每迭代一次退火一次(降温),直到满足迭代条件为止deta=abs(func1(BestX)-func1(PreBestX)); while (deta>YZ)&&(T>0.001) T=K*T; %%%%%%%在当前温度T下迭代次数 for i=1:L %%%%%%%在此点附近随机选下一点 NextX=PreX+S*(rand(D,1)*(Xs-Xx)+Xx); %%%%%%%%边界条件处理 for ii=1:D if NextX(ii)>Xs | NextX(ii)func1(NextX)) %%%%%保留上一个最优解 PreBestX=BestX; %%%%%%%此为新的最优解 BestX=NextX; end %%%%%%%Mrteopolis过程 if (func1(PreX)-func1(NextX)>0) %%%%%%%接受新解 PreX=NextX; P=P+1; else changer=-1*(func1(NextX)-func1(PreX))/T; p1=exp(changer); %%%%%%%%%接受较差的解 if p1>rand

遗传模拟退火算法matlab通用源程序

% maxpop 给定群体规模 % pop 群体 % newpop 种群 %t0 初始温度 function [codmin,finmin]=fc0(cc,v0,t0) N=length(cc(1,:)); %定群体规模 if N>50 maxpop=2*N-20; end if N<=40 maxpop=2*N; end %产生初始群体 pop=zeros(maxpop,N); pop(:,1)=v0; finmin=inf; codmin=0; for i=1:maxpop Ra=randperm(N); Ra(find(Ra==v0))=Ra(1); Ra(1)=v0; pop(i,:)=Ra; end t=t0; while t>0 %用模拟退火产生新的群体 pop=fc1(maxpop,pop,N,cc,v0,t); %转轮赌选择种群 f=zeros(1,maxpop); for i=1:maxpop for j=1:N-1 x=pop(i,j); y=pop(i,j+1); fo1=cc(pop(i,j),pop(i,j+1)); f(i)=f(i)+fo1; end f(i)=f(i)+cc(pop(i,1),pop(i,N)); end fmin=min(f); for i=1:maxpop if fmin==inf&f(i)==inf dd=inf; end

if fmin~=inf|f(i)~=inf dd=fmin-f(i); end ftk(i)=exp(dd/t); end [fin1,cod]=sort(-ftk); fin=abs(fin1); %f(cod(1)) if f(cod(1))=RR); % cod newpop(i,:)=pop(cod(cod2(end)),:); end %单亲繁殖 if N>32 jmax=round(N/9); end if N<=32 jmax=2; end if mod(jmax,2) jmax=jmax-1; end for i=1:maxpop for j=1:2:jmax nn=randperm(N); x=nn(j); y=nn(j+1); if newpop(i,x)==v0|newpop(i,y)==v0 continue; end box1=newpop(i,x); newpop(i,x)=newpop(i,y); newpop(i,y)=box1; end end %变异 Pc Pc=0.02; for i=1:maxpop

相关文档
最新文档