模式识别与智能计算第9章

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

例9.1functiony=myfun(x)ifx(1)=60&x(1)=-60ifx(2)=60&x(2)=-60y=-(0.5+((sin(sqrt(x(1).^2+x(2).^2))).^2-0.5)…/(1+0.001*(x(1).^2-x(2).^2).^2));%要转化对-f(x)求极小elsey=0;endend例9.2functiony=myfun(x)%计算目标函数值的函数名ifx=2&x=-1y=-(x*sin(10*pi*x)+2.0);%把问题的最大化转化为最小化elsey=0;end例9.4functiony=myfun(x)y=696.744*x(1)^1.962+10586.71*x(1)^5.9898+63.927*x(2)^1.8815+9054.54*x(2)^5.9898...+375.658*x(3)^2.9972+57.428*x(3)^1.8731+5200.91*x(3)^5.9898+113.471*x(4)^1.8815...+223.825*x(4)^5+23.626*x(4)^4.8344+5431.427*x(4)^5.9898+3982;Lb=[0,0,0,0];Lu=[9,9,9,9];A=[-20.475000;-17.037-12.99800;-15.660-11.942-8.8220;-14.229-10.855-8.026-21.965];b=[1.7190;-6.532;-12.3930;-30.499];options=gaoptimset('TolFun',1e-12);%改变参数[x,fval]=ga(@myfun,4,A,b,[],[],Lb,Lu);x=0.47250.51000.47140.6585%其中一次的结果fval=5.0776e+003可以多运行几次,以求得最好的结果。如果要使运算结果重复,可使用以下方法:[x,fval,reason,output]=ga(@myfun,4,A,b,[],[],Lb,Lu);%改变输出结构rand('twister',output.randstate);randn('state',output.randnstate);%设置随机函数例9.5functiony=myf_4(x)%x为二进制的个体loadmydata;%读入数据a=guiyi(a);y1=guiyi(y1);%自编的归一化函数aa=find(x==1);%找每个个体中1的位,即选中的变量bb=a(:,aa(:));%找到对应的数据y=press(bb,y1);%求PRESS值,此值要小function[y1,b]=press(x,y)%求press的函数x1=[ones(length(y),1),x];%回归式中有常数项,所以数据阵的第一列为1[b,bint,r]=regress(y,x1,0.01);%多元线性回归y1=0;fori=1:length(y)hi(i)=x1(i,:)*inv(x1'*x1)*x1(i,:)';y1=y1+(r(i)/(1-hi(i)))^2;%求press值end例9.6首先编写目标函数并以文件名myfun:functiony=myfun(x)loadmydata;%导入数据d=squareform(pdist(p));%求样本的欧氏距离fori=1:15%设置映射二维数据结构y1(i,1)=x(:,i);endfori=16:30y1(i-15,2)=x(:,i);endd1=squareform(pdist(y1));%求映射二维数据的欧氏距离a=0;b=0;%求误差函数fori=1:14forj=i+1:15a=a+(d(i,j)-d1(i,j))^2/d(i,j);b=b+d(i,j);endendy=a/b;然后利用遗传算法的GUI就可计算,其中变量数设置为30(样品数×映射维数)。一次的计算结果如下:x=-3.6698-2.4521-1.3034-1.12891.52782.34943.0598-1.2933-0.38760.10880.47961.01886.03916.49946.73503.54073.49681.93200.5881-0.2300-1.2397-3.06163.56201.59180.4698-1.6132-2.54420.5995-0.29891.4899x1(:,1)=x(1:15);x1(:,2)=x(16:30);plot(x1(:,1),x1(:,2),'o')%二维数据点的空间分布holdongname%显示空间各点对应的样品序号例9.7loadmyda;a=guiyi(a);maxterm=300;num=50;numvar=15;%输入数据及设置参数pop=fix(1+3*rand(num,numvar));%产生种群pm=0.1;px=0.95;xtype=2;%变异概率、交叉概率和交叉方式fork=1:maxtermfori=1:numb1=find(pop(i,:)==1);b2=find(pop(i,:)==2);b3=find(pop(i,:)==3);%找类别a1=a(b1(:),:);a2=a(b2(:),:);a3=a(b3(:),:);%找类别对应的数据d1=dis1(a1,a2)+dis1(a1,a3)+dis1(a2,a3);%求类间距离d2=dis2(a1)+dis2(a2)+dis2(a3);%求类内距离d(i)=-(d1+d2);%适应度函数end[topfit1,topi1]=max(d);%求最大值及对应的个体序号bestchrome=pop(topi1,:);%最佳个体ifkmaxterm%退出条件y=[bestchrome,-topfit1,k];breakendoldpop=[pop,d'];%选择算子的要求pop=normgeomselect(oldpop);%选择算子pop=pop(:,1:15);[child,txnum]=gacrossover(pop,px,xtype);%交叉算子[child,tnum]=gamutation1(child,pm);%变异算子pop=child;pop(num,:)=bestchrome;%将上一代的最佳个体传入下一代fori=1:num%求进化后的个体适应度值b1=find(pop(i,:)==1);b2=find(pop(i,:)==2);b3=find(pop(i,:)==3);a1=a(b1(:),:);a2=a(b2(:),:);a3=a(b3(:),:);d1=dis1(a1,a2)+dis1(a1,a3)+dis1(a2,a3);d2=dis2(a1)+dis2(a2)+dis2(a3);d(i)=-(d1+d2);end[topfit2,topi2]=max(d);ifabs(topfit2-topfit1)1e10-6%退出条件bestchrome=pop(topi2,:);y=[bestchrome,-topfit2,k];endendfunctiond=dis2(x)%求类内距离的函数ifsize(x,1)~=1%要考虑到可能只有一个样品,否则会出错m1=mean(x);%类中心elsem1=x;endd=0;fori=1:size(x,1)forj=1:size(x,2)d=d+(x(i,j)-m1(j))^2;%各样品到类中心的距离endend例9.8function[best_fval,best_route,Tb]=MainAneal(d,Tn,alpha,Tf)%d为城市距离坐标,alpha为温度调节系数,Tn为最大退火次数,Tf为终止温度d=squareform(pdist(cityposition));%城市距离矩阵n=length(d);L=100*n;%Markov链的长度route=randperm(n);fval=value(route,d);%初始路径和初始目标值best_fval=fval;best_route=route;t=fval;ii=0;Tc=1;Tb=0;t2=0;%设置初始值whileTc=Tn%fori=1:L[fval_after,route_after]=exchange(route,d);%改变路径e=fval_after-fval;if(fval_afterbest_fval);best_fval=fval_after;best_route=route_after;Tb=Tc;end%记录最优ifbest_fval==0;break;endifabs(e)=1e-6;t2=t2+1;else;t2=0;end%记录目标函数连续无变化的次数iflength(t2)=6;break;end%连续6次目标函数无变化,退出ife0%接受新值route=route_after;fval=fval_after;elseifexp(-e/t)rand%接受新值route=route_after;fval=fval_after;elseroute=route;fval=fval;endendendt=alpha*t;Tc=Tc+1;ift==Tf;break;endif(Tc-Tb)Tn/2;break;end%连续Tn/2退火无改变,结束退火ii=ii+1;fval_sequence(ii)=fval;endplot(1:ii,fval_sequence);%目标函数值functionfval=value(route,d)%求路径的长度,即目标函数n=length(d);fval=0;fori=1:n-1;fval=fval+d(route(i),route(i+1));endfunction[fval_after,route_after]=exchange(route,d)%改变路径的函数n=length(d);location=unidrnd(n,1,2);%随机产生两个自然数,此两数间的路径改变loc1=min(location);loc2=max(location);middle_route=fliplr(route(loc1:loc2));%改变路径,在这里为反向颠倒route_after=[route(1:loc1-1)middle_routeroute(loc2+1:n)];%改变后的路径fval_after=value(route_after,d);例9.10functionm=cluster_center(x,pattern,centernum)%求聚类中心函数n=length(x);fori=1:nforj=1:centernumifsize(x(find(pattern==j),:),1)==1%只有1个样本时m(j,:)=x(find(pattern==j),:);elsem(j,:)=mean(x(find(pattern==j),:));endendendfunctiond=cluster_dis(x,pattern,m_center)%求距离函数n=size(x,1);[centern,c]=size(m_center);d=zeros(n,centern);forj=1:centernfori=1:nfork=1:c;d(i,j)=d(i,j)+(x(i,k)-m_center(j,k))^2;end%每个样本到各类中心的距离endenda1=zeros(1,centern);fori=1:nforj=1:centernifpattern(i)==j;a1(1,j)=a1(1,j)+d(i,pattern(i));else;continue;end%类内距离endenda2=0;fori=1:centernforj=i+1:centernfork=1:c;a2=a2+(m_center(i,k)-m

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功