资源描述
硕士生考查课程考试试卷
考试科目: MATLAB教程
考生姓名: 胡晓聪 考生学号: 2130132003
学 院: 管理学院 专 业: 企业管理
考生成绩:
任课老师 (签名)
考试日期:20 年 月 日 午 时至 时
《MATLAB教程》试题:
A、利用MATLAB设计遗传算法程序,寻找下图11个端点的最短路径,其中没有连接的端点表示没有路径。要求设计遗传算法对该问题求解。
B、设计遗传算法求解f(x)极小值,具体表达式如下:
要求必须使用m函数方式设计程序。
选择题目: B
一、问题分析(10分)
这是一个求极小值的问题,所以首先应该将其转换为遗传算法优化下的求最大值的问题,即可以使F=。转换以后即可利用遗传算法对其进行求解。
二、实验原理与数学模型(20分)
原理即是利用遗传算法,模拟自然界的进化过程,将三个自变量通过二进制编码形成种群,然后选择、交配、突变,通过一定代数的遗传最后得到真实问题的一个近似解。
数学模型其实是一个不断选择下逼近要求函数最低点的过程。
三、实验过程记录(含基本步骤、程序代码及异常情况记录等)(60分)
(1)基本步骤:
1. 编码:对三个变量进行二进制编码,选择精度为0.0001,选定初始种群的个数
2. 完成一次的遗传,即对初始种群进行选择、交叉、突变得到新一代的种群
3. 重复上述过程一定次数,得到相应的近似解
4. 调试程序,根据得出的结果进行相应初始值以及算法的修改。
(2)程序:
主函数Bmain.m
%整个过程:设定初始参数--初始化,用rand产生初始种群--
%在for循环里完成一轮的selection、crossover、mutation得到新种群并记录最大适应度值,平均值
%在while循环里完成整体的gennerationmax次数的迭代,画出ymax,ymean的图像
%涉及到的子函数:fitnessfun targetfun selection crossover mutation ifcroifmutation
%transform
%变量含义:bitlength-用二进制表达个体所需要的bit位数 popsize-种群大小 population-种群的二进制矩阵
%precision-精度 pcrossover-交配的概率 pmutation-突变的概率 bounds-区间范围
%generationmax-最大循环代数
clc;
clear all;
close all;
global bitlength
global boundsbegin
global boundsend
%%
%参数带入
popsize=150;
generationmax=50;
precision=0.0001;
pmutation=0.09;
pcrossover=0.9;
bounds=[-5.12 5.12]; %因为三个变量的区间一样,这里为了简化只列出了一个 如果三个区间不一样 这里可以用矩阵列出
boundsbegin=bounds(:,1);
boundsend=bounds(:,2);
%%
%编码位数计算
bitlength=ceil(log2((boundsend-boundsbegin)./precision));%位数计算公式,用ceil进一
%种群初始化
population=round(rand(popsize,bitlength*3));%用rand产生0-1之间的随机数,并用round四舍五入
[fitvalue,accumup]=fitnessfun(population);%计算适应度
ymax=zeros(generationmax,1);
ymean=zeros(generationmax,1);
xmax=zeros(generationmax,3);
scross=zeros(popsize,3*bitlength);
scnew=zeros(popsize,3*bitlength);
smnew=zeros(popsize,3*bitlength);
generation=1;
%%
%while 循环下进行每一代的遗传过程 并记录相信的信息
while generation<generationmax+1 %大循环下完成迭代
for i=1:2:popsize %for循环下完成一代
seln=selection(accumup); %完成选择--2个个体
scross1=crossover(population(:,1:bitlength),seln,pcrossover); %完成所选择的两个个体的crossover
scross2=crossover(population(:,bitlength+1:2*bitlength),seln,pcrossover);
scross3=crossover(population(:,2*bitlength+1:3*bitlength),seln,pcrossover);
scross=[scross1 scross2 scross3];
scnew(i,:)=scross(1,:);
scnew(i+1,:)=scross(2,:);
smnew(i,1:bitlength)=mutation(population(:,1:bitlength),scnew(i,1:bitlength),pmutation); %突变
smnew(i,bitlength+1:2*bitlength)=mutation(population(:,bitlength+1:2*bitlength),scnew(i,bitlength+1:2*bitlength),pmutation);
smnew(i,2*bitlength+1:3*bitlength)=mutation(population(:,2*bitlength+1:3*bitlength),scnew(i,2*bitlength+1:3*bitlength),pmutation);
smnew(i+1,1:bitlength)=mutation(population(:,1:bitlength),scnew(i+1,1:bitlength),pmutation); %突变
smnew(i+1,bitlength+1:2*bitlength)=mutation(population(:,bitlength+1:2*bitlength),scnew(i+1,bitlength+1:2*bitlength),pmutation);
smnew(i+1,2*bitlength+1:3*bitlength)=mutation(population(:,2*bitlength+1:3*bitlength),scnew(i+1,2*bitlength+1:3*bitlength),pmutation);
end
population=smnew;
[fitvalue,accumup]=fitnessfun(population); %求新一代的适应值
[fmax,nmax]=max(fitvalue); %max函数求每一代最大适应值
fmean=mean(fitvalue); %mean函数求每一代适应值平均值
ymax(generation)=fmax;
ymean(generation)=fmean;
x1=transform(population(:,1:bitlength),nmax);
x2=transform(population(:,bitlength+1:2*bitlength),nmax);
x3=transform(population(:,2*bitlength+1:3*bitlength),nmax);
xmax(generation,1:3)=[x1 x2 x3];
generation=generation+1;
end
%%
%画出相应的每代遗传的图
generation=generation-1;
bestpopulation=[x1 x2 x3];
besttargetfunvalue=targetfun(x1,x2,x3);
plot(1:generation,ymax,'r*-');
hold on
plot(1:generation,ymean,'bh-');
xlabel('进化代数');
ylabel('最大/平均适应值');
legend('最大适应度','平均适应度');
axis([0 60 150 220]);
各类子函数:
function [fitvalue,accumup]=fitnessfun(population)
popsize=size(population,1);
fitvalue=zeros(popsize,1); %定义矩阵大小
accumup=zeros(popsize,1);
bitlength=size(population,2);
bitlength=round(bitlength./3);
for i=1:popsize %先转换为区间上的值,然后计算适应值
x1=transform(population(:,1:bitlength),i);
x2=transform(population(:,bitlength+1:2*bitlength),i);
x3=transform(population(:,2*bitlength+1:3*bitlength),i);
fitvalue(i,1)=targetfun(x1,x2,x3);
end
fitvalue=fitvalue'+200;
singlefit=0;
sumf=sum(fitvalue); %利用sum函数计算总值
for j=1:popsize
singlefit=singlefit+fitvalue(j); %得出累积概率
accumup(j)=singlefit./sumf;
end
accumup=accumup';
function seln=selection(accumup)
seln=zeros(1,2);
for i=1:2
r=rand;
prand=accumup-r;
j=1;
while(prand(j)<0)
j=j+1;
end
seln(i)=j;
End
function scross=crossover(population,seln,pcrossover)
bitlength=size(population,2);
cp=ifcroifmut(pcrossover);
crossn=round(rand*(bitlength-1))+1;
if cp==1
scross(1,:)=[population(seln(1),1:crossn) population(seln(2),crossn+1:bitlength)];
scross(2,:)=[population(seln(2),1:crossn) population(seln(1),crossn+1:bitlength)];
else
scross(1,:)=population(seln(1),:);
scross(2,:)=population(seln(2),:);
End
function snnew=mutation(population,smnew,pmutation)
bitlength=size(population,2);
cp=ifcroifmut(pmutation);
mutn=round(rand*(bitlength-1))+1;
snnew=smnew;
if cp==1
snnew(mutn)=1-smnew(mutn);
else
snnew(mutn)=smnew(mutn);
End
function fitvalue=targetfun(x1,x2,x3)
fitvalue=-x1.^2-x2.^2-x3.^2;
function x=transform(population,a)
global boundsbegin
global boundsend
bitlength=size(population,2);
binx=0;
for j=1:bitlength
if population(a,j)==1
binx=binx+2.^(bitlength-j);
else
end
end
x=boundsbegin+binx*(boundsend-boundsbegin)./(2.^bitlength-1);
function cp=ifcroifmut(p)
test(1:100)=0;
length=100*p;
test(1:length)=1;
cp=test(round(rand*99)+1);
x1
x2
x3
四、实验结果与总结(10分)
该算法的执行过程中,种群大小的选择以及遗传代数的选择对实验结果的精度会有一定的影响,而不同的精度下的计算量的大小也是不一样的。精度高的情况下,结果能够达到0.05而低的情况下只能达到小数点后一位,因此该算法的稳定性尚有待提高。
以下是其中一个结果的举例:
展开阅读全文