概述
研赛数学建模期间编写的程序拿出来分享下:
(纤细题目见2007年研究生数学建模竞赛试题B)
编码
function pop = bianma(popsize)
pop=4*(round(10*rand(popsize,5))/10)-2;
建立适应度函数
function f = fitness(pop,dian,ztjd)
popsize=size(pop,1);dian=dian';
f=zeros(1,popsize);
for i=1:popsize
ag1=pop(i,1)+ztjd(1);ag2=pop(i,2)+ztjd(2);ag3=pop(i,3)+ztjd(3);ag4=pop(i,4)+ztjd(4);ag5=pop(i,5)+ztjd(5);
ag1=ag1*pi/180;ag2=ag2*pi/180;ag3=ag3*pi/180;ag4=ag4*pi/180;ag5=ag5*pi/180;
T1=[cos(ag1),-sin(ag1),0,0;sin(ag1),cos(ag1),0,0;0,0,1,140;0,0,0,1];
T2=[1,0,0,0;0,cos(ag2),-sin(ag2),-255*sin(ag2);0,sin(ag2),cos(ag2),255*cos(ag2);0,0,0,1];
T3=[1,0,0,0;0,cos(ag3),-sin(ag3),0;0,sin(ag3),cos(ag3),0;0,0,0,1];
T4=[cos(ag4),-sin(ag4),0,0;sin(ag4),cos(ag4),0,0;0,0,1,255;0,0,0,1];
T5=[1,0,0,0;0,cos(ag5),-sin(ag5),0;0,sin(ag5),cos(ag5),0;0,0,0,1];
A=[0;0;0;1];
AE=[0;0;65;1];
TC=T1*T2*A;
TD=T1*T2*T3*T4*A;
TE=T1*T2*T3*T4*T5*AE;
x0=((180-TC(3))/(TD(3)-TC(3)))*(TD(1)-TC(1))+TC(1);
y0=((180-TC(3))/(TD(3)-TC(3)))*(TD(2)-TC(2))+TC(2);
r0=(x0-210)^2+y0^2;
r1=(TD(1)-210)^2+TD(2)^2;
R0=(88-5)^2;
R1=(160-72*TD(3)/180-5)^2;
if r0
> R0 || r1 > R1
f(i)=0;
elseif
(TE(1)-dian(1))^2+(TE(2)-dian(2))^2+(TE(3)-dian(3))^2 == 0
f(i)=1000000;
else
f(i)=1/((TE(1)-dian(1))^2+(TE(2)-dian(2))^2+(TE(3)-dian(3))^2);
end
end
选择
function [pop1,best,max_f] = selection(pop,f)
popsize = length(f);
[max_f,index1] = max(f);
[min_fitness,index2] = min(f);
best = pop(index1,:);
index = 1:popsize; index(index1) = 0; index(index2) = 0;
index = nonzeros(index);
pop1 = pop(index,:);
evo_fitness = f(index,:);
evo_popsize = popsize-2;
ps = evo_fitness/sum(evo_fitness);
pscum= cumsum(ps);
r = rand(1,evo_popsize);
selected = sum(pscum * ones(1,evo_popsize) <
ones(evo_popsize,1) * r)+1;
pop1 = pop1(selected,:);
max_f=sqrt(1/max_f);
交叉
function pop2 = crossover(pop1,pc)
[nouse,mating] = sort(rand(size(pop1,1),1));
mat_gen = pop1(mating,:);
pairs = size(mat_gen,1)/2;
bits = size(mat_gen,2);
cpairs = rand(pairs,1) < pc;
cpoints = randint(pairs,1,[1,bits]);
cpoints = cpairs .* cpoints;
for i = 1:pairs
pop2([2*i-1
2*i],:) = [mat_gen([2*i-1 2*i],1:cpoints(i)) mat_gen([2*i
2*i-1],cpoints(i)+1:bits)];
end
变异
function pop3=mutation(pop2,pm)
[px,py]=size(pop2);
pop3=ones(size(pop2));
for i=1:px
if
rand
mpoint=round(rand*py);
if mpoint == 0
mpoint=1;
end
pop3(i,:)=pop2(i,:);
pop3(i,mpoint)=4*(round(10*rand)/10)-2;
else
pop3(i,:)=pop2(i,:);
end
end
%主程序
%clear;clc;popsize=30;dai=100;pc=0.80;pm=0.1;
function [jg jl]=ycsf(popsize,dai,pc,pm);
%#ok
%上圆台中心及要求焊接的四个位置
x=[210 320,120,190,255];y=[0 -104,106,-125,88];
z=zeros(1,5);
for i=2:5
z(i)=(180/72)*(160-sqrt((x(i)-210)^2+y(i)^2));
end
z(1)=179.96;
aaa=[x;y;z];
[b index]=sort(aaa(3,:));
for i=1:5
aa(:,i)=aaa(:,index(i));
%#ok
end
a(:,1:5)=aa(:,5:-1:1);
clear aa aaa b i index x y z;
%离散化焊点到焊点线段成点
ins=1;jns=2;%上圆台中心到第一个焊点
zl=a(3,ins):-0.2:a(3,jns);
bb=length(zl);
for i=1:bb
xl(i)=((zl(i)-a(3,ins))/(a(3,jns)-a(3,ins)))*(a(1,jns)-a(1,ins))+a(1,ins);
%#ok
yl(i)=((zl(i)-a(3,ins))/(a(3,jns)-a(3,ins)))*(a(2,jns)-a(2,ins))+a(2,ins);
%#ok
end
xl(i+1)=a(1,jns);yl(i+1)=a(2,jns);zl(i+1)=a(3,jns);
xl(i+2)=a(1,jns);yl(i+2)=a(2,jns);zl(i+2)=a(3,jns);
p=[xl;yl;zl];
clear bb i ins jns xl yl zl;
%主程序
mm=length(p);
jg = zeros(dai,5,mm);
jl = zeros(dai,1,mm);
ztjd=zeros(mm,5);
ztjd(1,:) = [-90 -14 -130.4 -147.7 0];
for i=1:mm-1
dian=p(:,i+1);
pop =
bianma(popsize);%初始化
T=0;
while T
< dai
f = fitness(pop,dian,ztjd(i,:));%适应度函数
[pop1,best,max_f] = selection(pop,f);%选择
pop2 = crossover(pop1,pc);%交叉
pop3=mutation(pop2,pm);%变异
pop=[pop3;best;best];
T=T+1;
jg(T,:,i)=best;
jl(T,1,i)=max_f;
end
ins =
jg(dai,:,i);
ztjd(i+1,:)=ins; end
最后
以上就是懵懂溪流为你收集整理的matlab 遗传算法rep添加路径,采用matlab编写的遗传算法的机器人路径控制问题的全部内容,希望文章能够帮你解决matlab 遗传算法rep添加路径,采用matlab编写的遗传算法的机器人路径控制问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复