matlab小程序(3)

2019-01-27 18:22

i=i+1;

end2 W [8 z, r) i: a0 i) K7 ? r t=1:eranum;

plot(t,Trace(:,1)');$ O1 r/ r I) Y) v4 }; w

title('函数优化的遗传算法');xlabel('进化世代数(eranum)');ylabel('每一代最优适应度(maxfitness)');

[MaxFval,I]=max(Trace(:,1));( A: H2 G$ W0 h8 A- c9 y X=Trace(I,(2:length(bits)+1)); hold on; plot(I,MaxFval,'*');

text(I+5,MaxFval,['FMAX=' num2str(MaxFval)]);

str1=sprintf('进化到 %d 代 ,自变量为 %s 时,得本次求解的最优值 %f\\n对应染色体是:%s',I,num2str(X),MaxFval,num2str(BestPop(I,disp(str1);

%figure(2);plot(t,p);%绘制变异值增大过程6 B+ x6 y/ {' J9 Z T2=clock;/ e4 q% y2 |# D/ Z! R

elapsed_time=T2-T1;* u) `7 o\ if elapsed_time(6)<0- B, C\

elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1; end2 O9 {\

if elapsed_time(5)<03 U9 d5 K6 A\

elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;/ U1 R* _6 A/ A0 x end %像这种程序当然不考虑运行上小时啦+ U4 K5 w8 ]5 [# @6 g) a; T

str2=sprintf('程序运行耗时 %d 小时 %d 分钟 %.4f ',elapsed_time(4),elapsed_time(5),elapsed_time(6)); disp(str2);6 X, c# E& ?* l * A; s5 i* O9 s/ k

%初始化种群+ Y\

%采用二进制Gray编码,其目的是为了克服二进制编码的Hamming悬崖缺点 function [initpop]=InitPopGray(popsize,bits) len=sum(bits);

initpop=zeros(popsize,len);%The whole zero encoding individual1 i, w% q: X8 K. I for i=2:popsize-1/ ^2 V4 Q. c& a! O

pop=round(rand(1,len));

pop=mod(([0 pop]+[pop 0]),2);6 A' O: Z# L5 B8 ?6 a6 K' @

%i=1时,b(1)=a(1);i>1时,b(i)=mod(a(i-1)+a(i),2)' Z8 @9 n) e L1 q, A8 a) o, k %其中原二进制串:a(1)a(2)...a(n),Gray串:b(1)b(2)...b(n)+ V- M6 o9 W/ ^0 S initpop(i,

=pop(1:end-1);

));( B$ ]\

end- }% b5 ^% y) H! T initpop(popsize,

=ones(1,len);%The whole one encoding individual/ B' h4 Y' z) G; |

%解码8 \\, @- r1 U0 B: B; e4 M q

$ \\5 z# j$ O8 E3 X& r9 z

function [fval] = b2f(bval,bounds,bits)3 T$ C2 `3 o3 M& U4 |% k+ \\: `# d+ I % fval - 表征各变量的十进制数

% bval - 表征各变量的二进制编码串 % bounds - 各变量的取值范围

% bits - 各变量的二进制编码长度

scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables numV=size(bounds,1);) K1 y! m; M/ k9 C4 |9 [6 n2 W) j P cs=[0 cumsum(bits)];

for i=1:numV9 E# b0 y t+ s% i$ ~& d) k% w a=bval((cs(i)+1):cs(i+1));

fval(i)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1);( {6 R9 u7 Y\ end9 p, ^- N\ %选择操作* R, k' ~5 B7 N9 i& U

%采用基于轮盘赌法的非线性排名选择5 g\

%各个体成员按适应值从大到小分配选择概率:- G0 W1 |/ Y3 n T\ %P(i)=(q/1-(1-q)^n)*(1-q)^i, 其中 P(0)>

function [selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits) global m n c) J2 T1 u0 D: ]

selectpop=zeros(m,n);& Z2 _) U\ fit=zeros(m,1);3 V _\ for i=1:m0 U* t\ fit(i)=feval(FUN(1,end

selectprob=fit/sum(fit);%计算各个体相对适应度(0,1)6 W/ ` P, _( o+ Q q=max(selectprob);%选择最优的概率 x=zeros(m,2);

x(:,1)=[m:-1:1]';

[y x(:,2)]=sort(selectprob);' z' C! l( W* m

r=q/(1-(1-q)^m);%标准分布基值+ M3 e, E8 ^& H: `

newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成选择概率& _\ newfit=cumsum(newfit);%计算各选择概率之和+ R/ K, J9 G5 j; Y rNums=sort(rand(m,1));9 m8 @) {6 ]; A2 j! ?/ c\ fitIn=1;newIn=1;

while newIn<=m

if rNums(newIn)

=pop(fitIn,

;

,(b2f(pop(i,

,bounds,bits)));%以函数值为适应值做排名依据

(1)>...>

(n), sum(P(i))=1- m# B9 q1 Q* o t. o$ Q

end6 O) C s9 Y2 K/ M# u& g

end4 E: {2 p6 b9 V\ %交叉操作

function [NewPop]=CrossOver(OldPop,pCross,opts)8 P2 ?8 J; X/ {5 W0 ] %OldPop为父代种群,pcross为交叉概率 global m n NewPop: V6 ]2 ]% D8 K- w r=rand(1,m);! C2 G: A8 k: F7 D! v y1=find(r=pCross);. S1 p7 C1 e0 b2 S len=length(y1);6 o) o1 @6 M8 q1 c4 w4 s$ q

if len>2&mod(len,2)==1%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数- }' v: |) Z- M& D\

y2(length(y2)+1)=y1(len); y1(len)=[];

end S6 i$ w6 `! d8 [. y, ?3 u

if length(y1)>=2\ for i=0:2:length(y1)-2% M' c, L; E! l7 J/ t if opts==0! O5 a. R8 m\

[NewPop(y1(i+1),

);/ ~; n6 H8 O. n

,NewPop(y1(i+2),

]=EqualCrossOver(OldPop(y1(i+1),

,OldPop(y1(i+2),

else. t3 p. L9 p2 R7 f! D$ o' {5 k

[NewPop(y1(i+1),

);

,NewPop(y1(i+2),

]=MultiPointCross(OldPop(y1(i+1),

,OldPop(y1(i+2),

end! r) H2 u+ j, B0 e! e1 s0 | end ' K# y$ i: ]! ~. R7 K end9 [. ?, ~7 T6 Z) Y NewPop(y2,

=OldPop(y2,

;: b+ t& m) `+ C1 ~0 X% B) O. M5 Q5 s: G

%采用均匀交叉

function [children1,children2]=EqualCrossOver(parent1,parent2)! {, C\ # d$ G8 G6 K, q% L+ @

global n children1 children2

hidecode=round(rand(1,n));%随机生成掩码( b# a0 S9 ]$ F\ crossposition=find(hidecode==1);. |. K s3 Q/ o# I. V6 V

holdposition=find(hidecode==0);

children1(crossposition)=parent1(crossposition);%掩码为1,父1为子1提供基因; Z8 j8 u/ Y7 R( a$ T) l

children1(holdposition)=parent2(holdposition);%掩码为0,父2为子1提供基因 children2(crossposition)=parent2(crossposition);%掩码为1,父2为子2提供基因. x9 x7 x$ @& o6 u4 a7 O5 {

children2(holdposition)=parent1(holdposition);%掩码为0,父1为子2提供基因 - o* T3 A9 G6 n

%采用多点交叉,交叉点数由变量数决定+ x+ N; g) W! `* P7 N5 _4 p- D$ k2 x

function [Children1,Children2]=MultiPointCross(Parent1,Parent2)8 K+ {$ b: M9 l8 X' W # C. X( z\

global n Children1 Children2 VarNum$ W8 ^5 b/ Q2 V! [' ?& k Children1=Parent1;\ Children2=Parent2;

Points=sort(unidrnd(n,1,2*VarNum));0 H+ u0 O% m) L\ for i=1:VarNum& l; \\\ Children1(Points(2*i-1) Children2(Points(2*i-1)end

7 ? B; R0 N\

oints(2*i))=Parent2(Points(2*i-1)oints(2*i))=Parent1(Points(2*i-1)

oints(2*i));7 e# s3 P5 s! ?; B! b oints(2*i));& B3 L6 Q) L: a) S8 ~

%变异操作- B4 L, s6 l: u& g- [; w5 _\

function [NewPop]=Mutation(OldPop,pMutation,VarNum)0 I0 f5 P( v# s4 s& X$ A

global m n NewPop+ l% J) {1 s% t% D. I r=rand(1,m);

position=find(r<=pMutation); len=length(position); if len>=1' L7 y% r# A% [& d for i=1:len) G* v9 E% {+ f# C

k=unidrnd(n,1,VarNum); %设置变异点数,一般设置1点! q4 A+ T( C! H. { for j=1:length(k)3 ]\ if OldPop(position(i),k(j))==1

OldPop(position(i),k(j))=0;5 G. F% g) Q m- K9 `+ K- d else

OldPop(position(i),k(j))=1;9 O# n- T1 a$ n/ w$ E; G0 o- H end$ N$ l1 @% |9 g3 t* Y- _ end

end) k; ]2 y\ end

NewPop=OldPop;

%倒位操作

( E% C; M. \\6 l! M3 e% j3 B

function [NewPop]=Inversion(OldPop,pInversion)$ n/ C6 d9 f2 w5 l1 ^8 o , D+ x( ?' M& x0 y. d+ V' e global m n NewPop

NewPop=OldPop;

r=rand(1,m);1 Q l( z3 }$ v: L- _ B& j( S

PopIn=find(r<=pInversion);/ b( f\ len=length(PopIn);: U5 a' w) j; A' D if len>=1

for i=1:len

d=sort(unidrnd(n,1,2));! g) n o0 j% ]7 x& [: s

if d(1)~=1&d(2)~=n! s6 A) r7 o$ k

NewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1);

NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1));# `3 f/ v1 |. @\ NewPop(PopIn(i),d(2)+1:n)=OldPop(PopIn(i),d(2)+1:n); end- K) t4 t! ]7 Z% ~. G4 K% X& ~ S end

end L- w! r: B) {1 c' e

七径向基神经网络训练程序

clear all;

clc; ~- U% q& D0 Y! v( O& w3 i8 E, ?% [

%newrb建立一个径向基函数神经网络0 C% {, q0 r A/ ]' u\ p=0:0.1:1; %输入矢量: ~8 U; b: G. ]3 D( u t=[0 -1 0 1 1 0 -1 0 0 1 1 ];%目标矢量 goal=0.01; %误差& `0 S, h7 A: b8 E sp=1; %扩展常数

mn=100;%神经元的最多个数

df=1; %训练过程的显示频率% b, u( s+ P: W

[net,tr]=newrb(p,t,goal,sp,mn,df); %创建一个径向基函数网络. V# U- c0 i8 ?, f8 ]/ x % [net,tr]=train(net,p); %调用traingdm算法训练网络) @. ` F& d' w) f! W4 r3 |- z %对网络进行仿真,并绘制样本数据和网络输出图形; `% g4 @' }4 n' O' ~* j A=sim(net,p);& G, @' l0 M/ }9 e$ H E=t-A;1 L/ A; g8 N. f5 { sse=sse(E);

figure;

plot(p,t,'r-+',p,A,'b-*');* z; E ~; j' O5 c2 y! S legend('输入数据曲线','训练输出曲线'); echo off

说明:newrb函数本来在创建新的网络的时候就进行了训练!5 M7 y) V( R: X$ S. Q5 r B\ 每次训练都增加一个神经元,都能最大程度得降低误差,如果未达到精度要求,

那么继续增加神经元,程序终止条件是满足精度要求或者达到最大神经元的数目.关键的一个常数是spread(即散布常数的设置,扩展常数的设置).不能对创建的net调用train函数进行训练!

' f+ @! l- l: p) x* S9 h

训练结果显示:

NEWRB, neurons = 0, SSE = 5.0973 NEWRB, neurons = 2, SSE = 4.87139 NEWRB, neurons = 3, SSE = 3.61176

NEWRB, neurons = 4, SSE = 3.4875' I& _* x1 d& Z! D& C NEWRB, neurons = 5, SSE = 0.534217; L8 @! \\+ w, ~8 a8 j9 X NEWRB, neurons = 6, SSE = 0.51785

NEWRB, neurons = 7, SSE = 0.4342593 j\ NEWRB, neurons = 8, SSE = 0.341518

NEWRB, neurons = 9, SSE = 0.3415194 s! \\' _% N' @5 Q/ s' z# i3 o NEWRB, neurons = 10, SSE = 0.00257832 * B' x' `: G7 w+ T- J# m: k/ \\

八删除当前路径下所有的带后缀.asv的文件5 q3 \\( n' T, r6 `/ Q2 k( C% ` 说明:该程序具有很好的移植性,用户可以根据自己地 要求修改程序,删除不同后缀类型的文件! function delete_asv(bpath) 7 a1 z k j7 D( {

%If bpath is not specified,it lists all the asv files in the current %directory and will delete all the file with asv8 D9 X) p1 @8 M( }' f % Example:

% delete_asv('*.asv') will delete the file with name *.asv; % delete_asv will delete all the file with .asv. 6 C6 c3 K' J, F. N9 E5 O

if nargin< 1

%list all the asv file in the current directory! I. F- Q# [+ U$ J8 ], V) L files=dir('*.asv');

else

% find the exact file in the path of bpath

[pathstr,name] = fileparts(bpath);7 M/ y! M# U% F6 M* ^4 M if exist(bpath,'dir'). \\6 E* H; F, I3 Y1 `4 K$ A name = [name '\\*'];/ B c+ c% d% I [ end$ z8 n. }1 o, J3 N& n

ext = '.asv';% Q& [& f; ?7 l\

files=dir(fullfile(pathstr,[name ext])); end

' e/ U\ if ~isempty(files) for i=1:size(files,1)

title=files(i).name;' h* M\ delete(title);( @\ end% C2 O6 u1 R* R8 H1 ` end1 z- `, e9 `& w7 J, W& C

# n0 W, ?: h7 u# z

同样也可以在Matlab的窗口设置中取消保存.asv文件!


matlab小程序(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2010一年级汉语拼音笔试卷参考

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: