| 6 | 1/1 | 返回列表 |
| 查看: 840 | 回復(fù): 5 | |||
[求助]
矩陣的問(wèn)題
|
|
我做遺傳算法,看的別人的程序運(yùn)行是正確的,可是自己同樣的就出現(xiàn)了錯(cuò)誤。 就是這段程序,就是select被賦值了,但是在下一行時(shí)select(y1(2*p-1),q)卻顯示的是空矩陣,請(qǐng)問(wèn)是什么原因呢? [ Last edited by xiegangmai on 2011-7-5 at 09:38 ] |
|
木蟲(chóng) (著名寫(xiě)手)
|
function [out]=my_function(input) clear all; clc; popsize=10; codel=4; G=30; tic s=sprintf('程序正在運(yùn)行中,請(qǐng)稍等......'); %要優(yōu)化的是y(k)=b1*y(k-1)+2*y(k-2)+a1*u(k-1)+a2*u(k-2)的b1,b2,a1,a2這四個(gè)參數(shù),已知 disp(s); popu=2*rand(popsize,codel)-1 %初始化種群 evo_pop=zeros(popsize-1,codel); %要進(jìn)化的種群 select=zeros(popsize-1,codel); %選擇的種群 cross=zeros(popsize-1,codel); %交叉的種群 mutation=zeros(popsize-1,codel); %變異的種群 Trace=zeros(G,codel); %最優(yōu)軌跡 for kg=1:G time(kg)=kg; %進(jìn)化到第幾代 for i=1:popsize %開(kāi)始循環(huán) xi=popu(i, ;x1=xi(1); x2=xi(2); x3=xi(3); x4=xi(4); c=0; b1=[-0.1;0;0.2;0.5;1;1.8;2.1;4.2;6.6;11]; a1=[0.1;0.2;0.1;0.2;0.1;0.2;0.1;0.2;0;0.4] ; for k=3:10 f=[b1(k)-x1*b1(k-1)-x2*b1(k-2)-x3*a1(k-1)-x4*a1(k-2)]^2; 把它定 為目標(biāo)函數(shù) c=c+f; Msum(i,k)=c; Mfit(i,k)=1/c; %適應(yīng)度函數(shù) end fitness=Mfit(:,k) Bestf(i)=max(fitness) end [Oderf,index]=sort(Bestf,'descend') %排列 Bestfitness=Oderf(1) bfi(kg)=Bestfitness best_gene=popu(index(1), ![]() [max_fitness,index1]=max(fitness) [min_fitness,index2]=min(fitness) vari=mean(popu(index1, );popu(index2, =popu(index1,![]() fitness(index2, =fitness(index1,![]() index(index1)=0 index=nonzeros(index) evo_popu=popu(index, ![]() evo_fitness=fitness(index, ![]() evo_popsize=popsize-1 fitness_sum=sum(evo_fitness) fitscore=evo_fitness/fitness_sum fitnesscum=cumsum(fitscore) r=rand(1,evo_popsize) selected=sum(fitnesscum*ones(1,popsize-1) ![]() 這就是上面的程序,你能否幫我看看,十分感謝 |
|
function [out]=my_function(input) clear all; clc; popsize=10; codel=4; G=30; tic s=sprintf('程序正在運(yùn)行中,請(qǐng)稍等......'); %要優(yōu)化的是y(k)=b1*y(k-1)+2*y(k-2)+a1*u(k-1)+a2*u(k-2)的b1,b2,a1,a2這四個(gè)參數(shù),已知 disp(s); popu=2*rand(popsize,codel)-1 %初始化種群 evo_pop=zeros(popsize-1,codel); %要進(jìn)化的種群 select=zeros(popsize-1,codel); %選擇的種群 cross=zeros(popsize-1,codel); %交叉的種群 mutation=zeros(popsize-1,codel); %變異的種群 Trace=zeros(G,codel); %最優(yōu)軌跡 for kg=1:G time(kg)=kg; %進(jìn)化到第幾代 for i=1:popsize %開(kāi)始循環(huán) xi=popu(i, ;x1=xi(1); x2=xi(2); x3=xi(3); x4=xi(4); c=0; b1=[-0.1;0;0.2;0.5;1;1.8;2.1;4.2;6.6;11]; a1=[0.1;0.2;0.1;0.2;0.1;0.2;0.1;0.2;0;0.4] ; for k=3:10 f=[b1(k)-x1*b1(k-1)-x2*b1(k-2)-x3*a1(k-1)-x4*a1(k-2)]^2; 把它定 為目標(biāo)函數(shù) c=c+f; Msum(i,k)=c; Mfit(i,k)=1/c; %適應(yīng)度函數(shù) end fitness=Mfit(:,k) Bestf(i)=max(fitness) end [Oderf,index]=sort(Bestf,'descend') %排列 Bestfitness=Oderf(1) bfi(kg)=Bestfitness best_gene=popu(index(1), ![]() [max_fitness,index1]=max(fitness) [min_fitness,index2]=min(fitness) vari=mean(popu(index1, );popu(index2, =popu(index1,![]() fitness(index2, =fitness(index1,![]() index(index1)=0 index=nonzeros(index) evo_popu=popu(index, ![]() evo_fitness=fitness(index, ![]() evo_popsize=popsize-1 fitness_sum=sum(evo_fitness) fitscore=evo_fitness/fitness_sum fitnesscum=cumsum(fitscore) r=rand(1,evo_popsize) selected=sum(fitnesscum*ones(1,popsize-1) ![]() 這就是上面的程序,你能否幫我看看,十分感謝 |
| 6 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 070305高分子化學(xué)與物理 304分求調(diào)劑 +7 | c297914 2026-03-28 | 7/350 |
|
|---|---|---|---|---|
|
[考研]
|
nnnnnnn5 2026-03-25 | 7/350 |
|
|
[考研] 295求調(diào)劑 +4 | wei-5 2026-03-26 | 4/200 |
|
|
[考研] 一志愿中南大學(xué)化學(xué)0703總分337求調(diào)劑 +5 | niko- 2026-03-27 | 5/250 |
|
|
[考研] 0703化學(xué) +10 | 妮妮ninicgb 2026-03-27 | 10/500 |
|
|
[考研] 085701環(huán)境工程,267求調(diào)劑 +16 | minht 2026-03-26 | 16/800 |
|
|
[考研] 一志愿南昌大學(xué)324求調(diào)劑 +7 | hanamiko 2026-03-27 | 7/350 |
|
|
[考研] 一志愿南師大0703化學(xué) 275求調(diào)劑 +4 | Ripcord上岸 2026-03-27 | 4/200 |
|
|
[考研] 安徽大學(xué)專碩生物與醫(yī)藥專業(yè)(086000)324分,英語(yǔ)已過(guò)四六級(jí),六級(jí)521,求調(diào)劑 +4 | 美味可樂(lè)雞翅 2026-03-26 | 4/200 |
|
|
[考研] 315調(diào)劑 +4 | 0860求調(diào)劑 2026-03-26 | 5/250 |
|
|
[考研] 材料求調(diào)劑 +5 | .m.. 2026-03-25 | 5/250 |
|
|
[考研] 324求調(diào)劑 +5 | hanamiko 2026-03-26 | 5/250 |
|
|
[考研]
材料學(xué)碩,求調(diào)劑
6+5
|
糖葫蘆888ll 2026-03-22 | 10/500 |
|
|
[考研] 294分080500材料科學(xué)與工程求調(diào)劑 +4 | 柳溪邊 2026-03-26 | 4/200 |
|
|
[考研] 329求調(diào)劑 +5 | 1() 2026-03-22 | 5/250 |
|
|
[考研] 打過(guò)很多競(jìng)賽,085406控制工程300分,求調(diào)劑 +3 | askeladz 2026-03-26 | 3/150 |
|
|
[考研] 調(diào)劑 +4 | 13853210211 2026-03-24 | 4/200 |
|
|
[考研] 化工專碩求調(diào)劑 +3 | question挽風(fēng) 2026-03-24 | 3/150 |
|
|
[考博] 26申博自薦 +3 | whh869393 2026-03-24 | 3/150 |
|
|
[考研] 275求調(diào)劑 +6 | shansx 2026-03-22 | 8/400 |
|