遗传算法产生初始种群源代码producechromosome
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
function[chromosome,zeroposition]=producechromosome(chromosomesize,zhongqunscale)
for chromosomeline=1:zhongqunscale
data=[1 2 3 4 5 6 7 8 ];
changdu=(size(data,2));
fori=1:changdu
chromo(i)=0;
end
rl=randint(l,1,[1,changdu]);
chromo(1)=data(rl);
data(r1)=0;
kl=l;
for i=l:changdu
if data~=0
datal(kl)=data(i);
kl=kl+1;
end
end
changdu=kl-1; %size(datal,2);
rl=randint(l,l,[1,changdu]);
chromo(2)=datal(rl);
datal(r1)=O;
k2=1;
for i=l:changdu
if datal(i)~=0
data2(k2)=datal(i);
k2=k2+1;
end
end
changdu=k2-1; %size(data2,2);
rl=randint(l,l,[1,changdu]);
chromo(3)=data2(rl);
data2(r1)=0;
k3=1;
for i=1:changdu
if data2(i)~=O
data3(k3)=data2(i);
k3=k3+l;
end
end
changdu=k3-1; %size(data3,2);
rl=randint(1,l,[1,changdu]);
chromo(4)=data3(r1);
data3(rl)=0;
k4=1;
for i=1:changdu
if data3(i)~=O
data4(k4)=data3(i);
k4=k4+l;
end
end
changdu=k4-1; %size(data4,2);
rl=randint(1,l,[1,changdu]);
chromo(5)=data4(r1);
data4(rl)=0;
k5=1;
for i=1:changdu
if data4(i)~=O
data5(k4)=data5(i);
k5=k5+l;
end
end
changdu=k5-1; %size(data5,2);
rl=randint(1,l,[1,changdu]);
chromo(6)=data5(r1);
data5(rl)=0;
k6=1;
for i=1:changdu
if data5(i)~=O
data6(k5)=data6(i);
k6=k6+l;
end
end
changdu=k6-1; %size(data6,2);
rl=randint(1,l,[1,changdu]);
chromo(7)=data6(r1);
data6(rl)=0;
k7=1;
for i=1:changdu
if data6(i)~=O
data7(k6)=data7(i);
k7=k7+l;
end
end
changdu=k7-1; %size(data7,2);
rl=randint(1,l,[1,changdu]);
chromo(8)=data7(r1);
changdu=size(chromo,2);
sumchromo=0;
for i=1:changdu
sumchromo=sumchromo+chromo(i);
end
sumchromo;
%%%%产生染色体,即将4个O,插入到"chomochusi
%%%%随机产生两个[3,10]间的不同数据即可
%%%%根据前面的措施
datal=[3 4 5 6 7 8 9 10];
twosuijidata=[0,0];
for i=1:2
for j=l:20
i1=randint(1,1,[1,size(data1,2)]);
if datal(i1)~=0
twosuijidata(i)=datal(i1);
data1(i1)=0 ; %令该位置为O,同时让与该位置相邻位为O
if(i1+l)<8 ||(i1+1)==8
datal(i1+1)=0;
end
if i1一1>0
datal(i1一1)=0;
end
break;
else
bbbb=0;
end
end
end
twosuijidata;
%%对该两个随机数按照大小顺序排列
little=twosuijidata(l);
if twosuijidata(2)
twosuijidata(2)=twosuijidata(1);
end
twosuijidata(1)=little;
twosuijidata;
%%保存随机数的大小
fori=l:2
zeroposition(chromosomeline,i)=twosuijidata(i); %%%%%保存随机数的大小,
fori=l:chromosomesize
chromochushi(i)=0;
end
%%从第2个数位置开始到第一个随机数减去1,用初始染色体的开始数据代替
n=1;
for i=2:twosuijidata(l)-l
chromochushi(i)=chromo(n);
n=n+1;
end
%%染色体从第一个随机数加1开始到第二个随机数据一1
m=n;
fori=twosuijidata(1)+1:twosuijidata(2)-1
chiomochushi(i)=chromo(m);
m=m+1;
end
%染色体从twosuijidata(2)twosuijidata(1)开始
mn=m;
for i=twosuijidata(2)+l:11
chromochushi(i)=chromo(mn);
mn=mn+1;
end
%%产生一条染色体
onechromosomesum=0;
for i=l:chromosomesize
onechromosomesize=onechromosomesum+chromochushi(i);
end
for chromosomerow=1:chromosomesize
chromosome(chromosomeline,chromosomerow)=chromochushi(chromosomerow);
end
end