模拟退火算法Matlab实现

瞎BB

代码

目标函数

function value=targetfun(x)
value=200*exp(-0.05*x).*sin(x);

选择两个个体交配

%select two individuals
function seln=selection(population,cumsump)
seln=zeros(1,2);
for i=1:2
    r=rand;
    prand=cumsump-r;
    j=1;
    %stop when cumsump>r
    while prand(j)<0
        j=j+1;
    end
    seln(i)=j;
end

二进制转十进制

function x=transform2to10(population)
BitLength=size(population,2);
x=population(BitLength);
for i=1:BitLength-1
    x=x+population(BitLength-i)*power(2,i);
end

适应度函数

function [fitvalue,cumsump]=fitness(population)
global BitLength
global boundsbegin
global boundsend
popsize=size(population,1);
cumsump=zeros(1,popsize);
fitvalue=zeros(1,popsize);
for i=1:popsize
    x=transform2to10(population(i,:));
    %tansform to range of variable
    xx=boundsbegin+x.*(boundsend-boundsbegin)./(power((boundsend),BitLength)-1);
    fitvalue(i)=targetfun(xx);
end
%ensure fitvalue>0
fitvalue=fitvalue'+230;
fsum=sum(fitvalue);
Pperpopulation=fitvalue/fsum;
cumsump(1)=Pperpopulation(1);
for i=2:popsize
    cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
cumsump=cumsump';

是否交叉或变异

function pcc=IfCroIfMut(cro_or_mut)
test(1:100)=0;
l=ceil(rand()*cro_or_mut);
test(1:l)=1;
n=ceil(rand*100);
pcc=test(n);

交叉

function scro=crossover(population,seln,pc)
%seln:two individuals
BitLength=size(population,2);
pcc=IfCroIfMut(pc);
if pcc==1
    %generate a random position
    chb=round(rand*(BitLength-2))+1;
    %crossover
    scro(1,:)=[population(seln(1),1:chb) population(seln(2),chb+1:BitLength)];
    scro(2,:)=[population(seln(2),1:chb) population(seln(1),chb+1:BitLength)];
else
    scro(1,:)=population(seln(1),:);
    scro(2,:)=population(seln(2),:);
end

变异

function snnew=mutation(snew,pmutation)
BitLength=size(snew,2);
snnew=snew;
pmm=IfCroIfMut(pmutation);
if pmm==1
    %generate a random position
    chb=round(rand*(BitLength-1))+1;
    %0to1 or 1to0
    snnew(chb)=abs(snew(chb)-1);
end

生成随机初等行变换的单位矩阵

function f=generate_random_eye(a)
n=size(a);
c1=ceil(rand*n);
c2=ceil(rand*n);
for i=1:20
    while c1==c2
        c1=ceil(rand*n);
        c2=ceil(rand*n);
    end
    temp=a(c1,:);
    a(c1,:)=a(c2,:);
    a(c2,:)=temp;
end
f=a;

主函数

%max goal function value
clc,clear
global BitLength
global boundsbegin
global boundsend
cro_mut_rate=0.01;
%range of variable
bounds=[-2 2];
%begin of variable
boundsbegin=bounds(:,1);
%end of variable
boundsend=bounds(:,2);
precision=0.0001;
%calc the BitLength
BitLength=ceil(log2((boundsend-boundsbegin)'./precision));

%init
popsize=50;
Generationmax=100;
pcro=0.9;
pmut=0.09;
ymax=zeros(1,Generationmax+1);
ymean=zeros(1,Generationmax+1);
xmax=zeros(1,Generationmax+1);
%generate random population
population=round(rand(popsize,BitLength));
%calc fitness return fitvalue and sum of probability
[fitvalue,cumsump]=fitness(population);

%main code
Generation=1;
tic
while Generation<Generationmax+1
    for j=1:2:popsize
        %select twoindividual
        seln=selection(population,cumsump);
        %crossover
        scro=crossover(population,seln,pcro);
        scnew(j,:)=scro(1,:);
        scnew(j+1,:)=scro(2,:);
        %mutation
        smnew(j,:)=mutation(scnew(j,:),pmut);
        smnew(j+1,:)=mutation(scnew(j+1,:),pmut);
    end
    population=smnew;
    [fitvalue,cumsump]=fitness(population);
    [fmax,nmax]=max(fitvalue);
    fmean=mean(fitvalue);
    ymax(Generation)=fmax;
    ymean(Generation)=fmean;
    x=transform2to10(population(nmax,:));
    xx=boundsbegin+x.*(boundsend-boundsbegin)./(power((boundsend),BitLength)-1);
    xmax(Generation)=xx;
    Generation=Generation+1;
end
toc
%Generation=Generation-1;
Bestpopulation=xx;
BestValue=targetfun(xx);

%figure
figure(1);
hand1=plot(1:Generation,ymax);
set(hand1,'linestyle','-','linewidth',1.8,'marker','*','markersize',6);
hold on;
hand2=plot(1:Generation,ymean);
set(hand2,'color','r','linestyle','-','linewidth',1.8,'marker','h','markersize',6);
%hold on;
xlabel('Generation');ylabel('max/mean');xlim([1 Generationmax]);
legend('fitness_max','fitness_mean');
box off;hold off;
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值