www.pudn.com > GA.rar > fitnessfun.m, change:2015-09-24,size:7116b

```%子程序：计算适应度函数, 函数名称存储为fitnessfun
function [Fitvalue,cumsump,VV,SS,AA,FF,WW]=fitnessfun(population);

popsize=size(population,1);   %有popsize个个体
VV=zeros(220,popsize);
SS=zeros(220,popsize);
AA=zeros(220,popsize);
FF=zeros(220,popsize);
WW=zeros(220,popsize);

for i=1:popsize
t1(i,1)=transform2to10(population(i,1:8));
t2(i,1)=transform2to10(population(i,9:16));
t3(i,1)=transform2to10(population(i,17:24));
t4(i,1)=transform2to10(population(i,25:32));
t5(i,1)=transform2to10(population(i,33:40));
end

%每条染色体对应的适应度函数值确定
for j=1:popsize
if (~((t1(j)>0)&&(t1(j)<t2(j))&&(t2(j)+1<t3(j))&&(t3(j)<t4(j))&&(t4(j)<t5(j))&&(t5(j)+5<220)))
Fitvalue(j,1)=10000000000;

end
if (t1(j)>0)&&(t1(j)<t2(j))&&(t2(j)+1<t3(j))&&(t3(j)<t4(j))&&(t4(j)<t5(j))&&(t5(j)+5<220)
%%
%条件初始化
v=0;
A=2.031;
B=0.0622;
C=0.001807;
g=10;
M=194295;   %单位Kg
Tzd=30;
i=0;
s=0;
wi=0;
E1=0;
E2=0;
E=0;
%%
for i=1:220
w0=A+B*v+C*v*v;
if(i>=0&&i<=t1(j))%牵引1
if s<=304;
wi=0;
end
if s>304&&s<=684
wi=1.8;
end
if s>684&&s<=1304
wi=-3.5;
end
if s>1304&&s<=1354
wi=0;
end
wc=0;
w1=wi+wc;
if v<=14.3
Fmax=203000;
else Fmax=(-0.002032*v*v*v+0.4928*v*v-42.13*v+1343)*1000;
end
%a0=solve('M*a0+(w0+w1)*g*M/1000-a0*Fmax=0','a0');
a=(w0+w1)*g*M/(1000*(Fmax-M));
if (a<0)
a=0;
end
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=Fmax*a;
E1=Fmax*(v+0.5*a)*a;
W(i,1)=E1;
E=E+E1;
end
if(i>t1(j)&&i<=t2(j))%惰行1
if s<=304;
wi=0;
end
if s>304&&s<=684
wi=1.8;
end
if s>684&&s<=1304
wi=-3.5;
end
if s>1304&&s<=1354
wi=0;
end
wc=0;
w1=wi+wc;
a=-(w0+w1)*g/1000;
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
end
if(i>t2(j)&&i<t3(j))%制动1
if((1354-S(i-1)-50)<0.5*V(i-1)*(t3(j)-i)&&0.5*V(i-1)*(t3(j)-i)<(1354-S(i-1)+50)&&(V(i-1)/(t3(j)-i))<=1)
a=-V(i-1)/(t3(j)-i);%如果进入到这个判断里，那么以这个加速度运动到t3必然停住了
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
end

if(~((1354-S(i-1)-50)<0.5*V(i-1)*(t3(j)-i)&&0.5*V(i-1)*(t3(j)-i)<(1354-S(i-1)+50)&&(V(i-1)/(t3(j)-i+0.000001))<=1))
Fitvalue(j,1)=10000000000;
break
end
end
if i==t3(j)
%能进入到这里面表示已经成功制动
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
end
if(i>t3(j)&&i<=t4(j))%牵引2
if s>=1354&&s<1594;
wi=0;
end
if s>=1594&&s<1994
wi=3;
end
if s>=1994&&s<2554
wi=-2;
end
if s>=2554&&s<=2634
wi=0;
end
wc=0;
w1=wi+wc;
if v<=14.3
Fmax=203000;
else Fmax=(-0.002032*v*v*v+0.4928*v*v-42.13*v+1343)*1000;
end
a=(w0+w1)*g*M/(1000*(Fmax-M));
if (a<0)
a=0;
end
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=Fmax*a;
E2=Fmax*(v+0.5*a)*a;
W(i,1)=E2;
E=E+E2;
end
if(i>t4(j)&&i<=t5(j))%惰行2
if s>=1354&&s<1594;
wi=0;
end
if s>=1594&&s<1994
wi=3;
end
if s>=1994&&s<2554
wi=-2;
end
if s>=2554&&s<=2634
wi=0;
end
wc=0;
w1=wi+wc;
if v<=14.3
Fmax=203000;
else Fmax=(-0.002032*v*v*v+0.4928*v*v-42.13*v+1343)*1000;
end
a=-(w0+w1)*g/1000;
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
end
if(i>t5(j)&&i<220)%制动2
if((2634-S(i-1)-50<0.5*V(i-1)*(220-i))&&(0.5*V(i-1)*(220-i)<2634-S(i-1)+50)&&(abs(V(i-1)/(220-i))<=1))
a=-V(i-1)/(220-i);%如果进入到这个判断里，那么以这个加速度运动到220秒必然停住了
v=v+a;
s=s+v;
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
else
Fitvalue(j,1)=10000000000;
break%这种工况序列不满足要求直接舍去 看下一条染色体
end
end
if i==220
V(i,1)=v;
S(i,1)=s;
J(i,1)=a;
F(i,1)=0;
W(i,1)=0;
end
[n,m]=size(V);
if n==220   %判断执行到此时的情况是跳出了循环还是循环执行完毕
Fitvalue(j,1)=E;
VV(:,j)=V;
SS(:,j)=S;
AA(:,j)=J;
FF(:,j)=F;
WW(:,j)=W;
end
end
end
end

%计算选择概率
fsum=sum(Fitvalue);
Pperpopulation=Fitvalue/fsum;
%计算累积概率
cumsump(1)=Pperpopulation(1);
for i=2:popsize
cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
cumsump=cumsump';```