越界 发表于 2013-7-8 09:16
有偿服务哦
function chushis)
K=4; %最多4辆车
inn=100;%迭代次数上限
citynum=8;%需求点数量
KM=citynum+K+1; %配送途径种类
%产生初始种群
m=zeros(1,inn);
m=m';
s=zeros(inn,citynum+K+1);
for i=1:1:inn
s(i,:)=randperm(KM); %随机排列构成个体
end
s=[m s];
for i=1:inn
for j=1:KM-1
if s(i,j)>citynum
s(i,j)=0;
end
end
end
xlswrite('data.xls',s)
%计算适应度
function [f,p]=shyd(s);
K=4; %最多4辆车
M=10000;
inn=100;%迭代次数上限
citynum=8;%需求点数量
KM=citynum+K+1; %配送途径种类
q=[2 1.5 4.5 3 1.5 4 2.5 3]; %需求点的需求量
t=[1 2 1 3 2 2.5 3 0.8]; %各需求点卸货时间
a1=[1 4 1 4 3 2 5 1]; %送货车辆到达时间上限
b=[4 6 2 7 5.5 5 8 4];%送货车辆时间下线
c=[0,40,60,75,90,200,100,160,80
40,0,65,40,100,50,75,110,100
60,65,0,75,100,100,75,75,75
75,40,75,0,100,50,90,90,150
90,100,100,100,0,100,75,75,100
200,50,100,50,100,0,70,90,75
100,75,75,90,75,70,0,70,100
160,110,75,90,75,90,70,0,100
80,100,75,150,100,75,100,100,0];
y=zeros(citynum+1,citynum+1);
for i=1:inn-1
a=s(i,:);
for j=1:KM-1
m=a(j);
n=a(j+1);
m=m+1;
n=n+1;
end
y(m,n)=1; y=y';
for i=1:citynum
for j=1:citynum
mubiaob=c(i,j)*y(i,:);
end
end
xuq1=0;
for i=1:citynum
for j=1:citynum
xuq1=xuq1+s(i)*y(i,:)-q(i);
end
xuqiu=max((xuq1),0)*M;
end
end
shij1=0;
shij2=0;
for i=1:citynum
for j=1:citynum
for l=1:citynum
shij1=shij1+t(i)-a1(i);
shij2=shij2+b(i)-t(i);
end
shij3=max((shij1),0);
shij4=max((shij2),0);
shijian=M*shij3+M*shij4;
end
end
f=mubiaob+xuqiu+shijian;
f=1./f;
%计算选择概率
fsum=0;
for i=1:14
fsum=fsum+f(i);
end
for i=1:14
ps(i)=f(i)/fsum;
end
%计算累积概率
p(1)=ps(1);
for i=2:14
p(i)=p(i-1)+ps(i);
end
p=p';
%“选择”操作
%从种群中选择两个个体
function xuze=sell(s,p)
inn=size(p,1);
for i=1:inn
r=rand(inn,1); %产生一个随机数
prand=p-r;
j=1;
while prand(j)<0
j=j+1;
end
xuze(i)=j;
%选中个体的序号
end
sel1=xuze(1);
sel2=xuze(2);
%“交叉”操作
function snew=cross(s,B,pc)
A=s(4,:);
B=s(3,:);
c=find(A==0);
d=find(B==0);
a=sym(A);
b=sym(B);
k=size(a,1);
for i=1:size(a,2)
for j=1:k
e(i,c(k))=a(i+k-1);
end
end
for i=1:size(a,2)
for j=1:k
e(i,d(k))=b(i+k-1);
end
end
[f,p]=shyd(s);
f=f';
a=a';
b=b';
c0=round(rand*(k-1))+1;
c1=round(rand*(k-1))+1;
a=[f(:,c0),a]; b=[e(:,c1),b];
for i=1:size(a,2);
j=1:size(e,2)
if a(i)==e(j)
a(i)==[];
end
end
for i=1:size(b,2);
j=1:size(f,2)
if b(i)==f(j)
b(i)==[];
end
end
a=double(a);
b=double(b);
g=zeros(size(A));
for i=1:size(a)
for j=1:size(c)
g(i+j)=a(i);
end
end
h=zeros(size(A));
for i=1:size(b)
for j=1:size(d)
h(i+j)=b(i);
end
end
g=g';
h=h';
snew=[g h];
%变异
function snew=bianyi(snew,pm)
bn=size(snew,2);
snnew=snew;
c2=round(rand*(bn-2))+1;
%在[1,bn-1]范围内随机产生一个变异位
c3=round(rand*(bn-2))+1;
chb1=min(c2,c2);
chb2=max(c3,c2);
x=snew(chb1+1:chb2);
snnew(chb1+1:chb2)=fliplr(x);
pmm=pro(pm);
%根据变异概率决定是否进行变异操作,1则是,0则否
if pmm==1
c2=round(rand*(bn-2))+1;
%在[1,bn-1]范围内随机产生一个变异位
c3=round(rand*(bn-2))+1;
chb1=min(c2,c3);
chb2=max(c2,c3);
x=snew(chb1+1:chb2);
snnew(chb1+1:chb2)=fliplr(x);
end
function pcc=pro(pc);
test(1:100)=0;
l=round(100*pc);
test(1:l)=1;
n=round(rand*99)+1;
pcc=test(n);
%主程序
function ga
s=xlsread('data.xls');
[f,p]=shyd(s);
pm=0.8; %变异概率
pc=0.2; % 交叉比率
gn=1;
inn=100;%迭代次数上限
gnmax=1000; %最大代数
while gn
for j=1:2:inn
xuze=sell(s,p);
%选择操作
scross=cross(s,xuze,pc);
%交叉操作
scnew(j,:)=scross(1,:);
scnew(j+1,:)=scross(2,:);
smnew(j,:)=bianyi(scnew(j,:),pm);
%变异操作
smnew(j+1,:)=bianyi(scnew(j+1,:),pm);
end
s=smnew;
%产生了新的种群
[f,p]=shyd(s);
%计算新种群的适应度
%记录当前代最好和平均的适应度
[fmax,nmax]=max(f);
ymean(gn)=1/mean(f);
ymax(gn)=1/fmax;
%记录当前代的最佳个体
x=s(nmax,:);
gn=gn+1;
end
gn=gn-1;
figure(2);
plot(ymax,'r');
hold on;
plot(ymean,'b');
grid;
title('搜索过程');
legend('最优解','平均解');