| 5 | 1/1 | 返回列表 |
| 查看: 849 | 回復(fù): 5 | ||
| 當(dāng)前只顯示滿足指定條件的回帖,點(diǎn)擊這里查看本話題的所有回帖 | ||
[求助]
矩陣的問題
|
||
|
我做遺傳算法,看的別人的程序運(yùn)行是正確的,可是自己同樣的就出現(xiàn)了錯(cuò)誤。 就是這段程序,就是select被賦值了,但是在下一行時(shí)select(y1(2*p-1),q)卻顯示的是空矩陣,請(qǐng)問是什么原因呢? [ 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 %開始循環(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 %開始循環(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) ![]() 這就是上面的程序,你能否幫我看看,十分感謝 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 一志愿北京科技大學(xué)材料學(xué)碩328分求調(diào)劑 +3 | 1段時(shí)間 2026-03-31 | 4/200 |
|
|---|---|---|---|---|
|
[考研] 一志愿a區(qū)211,085601-307分求調(diào)劑 +10 | 黨嘉豪 2026-03-31 | 23/1150 |
|
|
[考研] 340求調(diào)劑 +4 | 希望如此i 2026-03-31 | 4/200 |
|
|
[考研] 311求調(diào)劑 +9 | 藍(lán)月亮亮 2026-03-30 | 9/450 |
|
|
[考研] 347求調(diào)劑 +11 | 山頂見α 2026-03-25 | 11/550 |
|
|
[考研] 354求調(diào)劑 +3 | lxb598 2026-03-31 | 4/200 |
|
|
[考研] 一志愿南昌大學(xué)324求調(diào)劑 +6 | hanamiko 2026-03-30 | 6/300 |
|
|
[考研] 08工科,295,接受跨專業(yè)調(diào)劑 +6 | lmnlzy 2026-03-30 | 6/300 |
|
|
[考研] 293分求調(diào)劑,外語為俄語 +5 | 加一一九 2026-03-31 | 5/250 |
|
|
[考研] 22408 359分調(diào)劑 +4 | Qshers 2026-03-27 | 8/400 |
|
|
[考研] 370求調(diào)劑 +3 | 080700調(diào)劑 2026-03-30 | 3/150 |
|
|
[考研] 一志愿南昌大學(xué)324求調(diào)劑 +9 | hanamiko 2026-03-27 | 9/450 |
|
|
[考研] 0703化學(xué)321分求調(diào)劑 +10 | 三dd. 2026-03-30 | 11/550 |
|
|
[考研] 322求調(diào)劑 +10 | 宋明欣 2026-03-27 | 10/500 |
|
|
[考研] 327求調(diào)劑 +6 | 汲亦昊 2026-03-29 | 6/300 |
|
|
[考研] 279求調(diào)劑 +4 | 蝶舞輕繞 2026-03-29 | 4/200 |
|
|
[考研] 085602 化工專碩 338分 求調(diào)劑 +12 | 路癡小琪 2026-03-27 | 12/600 |
|
|
[有機(jī)交流]
高溫高壓反應(yīng)求助
10+4
|
chibby 2026-03-25 | 4/200 |
|
|
[考研] 化學(xué)調(diào)劑一志愿上海交通大學(xué)336分-本科上海211 +4 | 小魚愛有機(jī) 2026-03-25 | 4/200 |
|
|
[考研] 各位老師您好:本人初試372分 +5 | jj涌77 2026-03-25 | 6/300 |
|