模拟退火算法Matlab实现

模拟退火算法Matlab实现

  • 瞎BB
  • 代码
    • 目标函数
    • 选择两个个体交配
    • 二进制转十进制
    • 适应度函数
    • 是否交叉或变异
    • 交叉
    • 变异
    • 生成随机初等行变换的单位矩阵
    • 主函数

瞎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

你可能感兴趣的:(优化算法)