| 6 | 1/1 | 返回列表 |
| 查看: 839 | 回復(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 ] |
|
木蟲 (著名寫手)
|
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ā)表 | |
|---|---|---|---|---|
|
[碩博家園] 求調(diào)劑 330分 085600材料與化工 +3 | gqhhh 2026-03-22 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 2026年華南師范大學(xué)歡迎化學(xué),化工,生物,生醫(yī)工等專業(yè)優(yōu)秀學(xué)子加入! +3 | llss0711 2026-03-28 | 5/250 |
|
|
[考研] 322求調(diào)劑 +7 | 宋明欣 2026-03-27 | 7/350 |
|
|
[考研] 22408 359分調(diào)劑 +4 | Qshers 2026-03-27 | 5/250 |
|
|
[考研] 332求調(diào)劑 +4 | @MZB382400 2026-03-28 | 4/200 |
|
|
[考研] 求化學(xué)調(diào)劑 +4 | wulanna 2026-03-28 | 4/200 |
|
|
[考研] 調(diào)劑 +3 | 好好讀書。 2026-03-28 | 3/150 |
|
|
[考研] 291求調(diào)劑 +15 | hhhhxn.. 2026-03-23 | 21/1050 |
|
|
[考研]
|
孅華 2026-03-22 | 17/850 |
|
|
[考研] 070300化學(xué)求調(diào)劑 +4 | 起個(gè)名咋這么難 2026-03-27 | 4/200 |
|
|
[考研]
|
18419759900 2026-03-25 | 8/400 |
|
|
[考研] 085601 材料工程 313分 求調(diào)劑 +5 | Ong3 2026-03-27 | 5/250 |
|
|
[考研] 321求調(diào)劑 +6 | Ymlll 2026-03-24 | 6/300 |
|
|
[考研] 086000生物與醫(yī)藥292求調(diào)劑 +6 | 小小陳小小 2026-03-22 | 9/450 |
|
|
[考研] 一志愿天津大學(xué)339材料與化工求調(diào)劑 +3 | 江往賣魚 2026-03-26 | 3/150 |
|
|
[考研] 334分 一志愿武理-080500 材料求調(diào)劑 +4 | 李李不服輸 2026-03-25 | 4/200 |
|
|
[考研] 26考研-291分-廈門大學(xué)(085601)-柔性電子學(xué)院材料工程專業(yè)求調(diào)劑 +3 | min3 2026-03-24 | 4/200 |
|
|
[考研]
|
13659058978 2026-03-24 | 4/200 |
|
|
[考研] 341求調(diào)劑(一志愿湖南大學(xué)070300) +5 | 番茄頭--- 2026-03-22 | 6/300 |
|
|
[考研] 328求調(diào)劑 +4 | LHHL66 2026-03-23 | 4/200 |
|