function [Zp,Xp,Yp,LC1,LC2]=GACFA(M,N,Pm) %-------------------------------------------------------------------------- % GACFA.m % Genetic Algorithm for Capacity and Flow Assignment % 链路容量和流量优化分配的遗传算法 % GreenSim团队原创作品,转载请注明 %-------------------------------------------------------------------------- % 函数功能 % 使用遗传算法求解通信网链路容量和流量联合优化分配问题 %-------------------------------------------------------------------------- % 参考文献 % 叶大振,吴新余.基于遗传算法的计算机通信网优化设计[J]. % 南京邮电学院学报.1996,16(2):9-15 %-------------------------------------------------------------------------- % 输入参数列表 % M 遗传进化迭代次数 % N 种群规模(取偶数) % Pm 变异概率 %-------------------------------------------------------------------------- % 输出参数列表 % Zp 目标函数最优值 % Xp 路由选择决策变量最优值 % Yp 线路型号决策变量最优值 % LC1 收敛曲线1,各代最优个体适应值的记录 % LC2 收敛曲线2,各代群体平均适应值的记录 %-------------------------------------------------------------------------- %第一步:载入数据和输出变量初始化 load DATA_CFA; Xp=zeros(14,1); Yp=zeros(8,3); LC1=zeros(1,M); LC2=LC1; %第二步:随机产生初始种群 farm_X=zeros(14,N); farm_Y=zeros(8,3*N); for i=1:N for j=1:2:13 RAND=rand; if RAND>0.5 farm_X(j,i)=1; else farm_X(j+1,i)=1; end end end for i=1:N for j=1:8 RAND=rand; if RAND<1/3 farm_Y(j,3*i-2)=1; elseif RAND>2/3 farm_Y(j,3*i)=1; else farm_Y(j,3*i-1)=1; end end end counter=0;%设置迭代计数器 while counter<M%停止条件为达到最大迭代次数 %第三步:交叉 newfarm_X=zeros(14,N); newfarm_Y=zeros(8,3*N); Ser=randperm(N); %对X做交叉 for i=1:2:(N-1) A_X=farm_X(:,Ser(i)); B_X=farm_X(:,Ser(i+1)); cp=2*unidrnd(6); a_X=[A_X(1:cp);B_X((cp+1):end)]; b_X=[B_X(1:cp);A_X((cp+1):end)]; newfarm_X(:,i)=a_X; newfarm_X(:,i+1)=b_X; end %对Y做交叉 for i=1:2:(N-1) A_Y=farm_Y(:,(3*Ser(i)-2):(3*Ser(i))); B_Y=farm_Y(:,(3*Ser(i+1)-2):(3*Ser(i+1))); cp=unidrnd(7); a_Y=[A_Y(1:cp,:);B_Y((cp+1):end,:)]; b_Y=[B_Y(1:cp,:);A_Y((cp+1):end,:)]; newfarm_Y(:,(3*i-2):(3*i))=a_Y; newfarm_Y(:,(3*i+1):(3*i+3))=b_Y; end %新旧种群合并 FARM_X=[farm_X,newfarm_X]; FARM_Y=[farm_Y,newfarm_Y]; %第四步:选择复制 Ser=randperm(2*N); FITNESS=zeros(1,2*N); fitness=zeros(1,N); for i=1:(2*N) X=FARM_X(:,i); Y=FARM_Y(:,(3*i-2):(3*i)); FITNESS(i)=COST(X,Y,x1_x14,F_x1_x14,A,Q,C,S,b); end for i=1:N f1=FITNESS(Ser(2*i-1)); f2=FITNESS(Ser(2*i)); if f1<f2 farm_X(:,i)=FARM_X(:,Ser(2*i-1)); farm_Y(:,(3*i-2):(3*i))=FARM_Y(:,(3*Ser(2*i-1)-2):(3*Ser(2*i-1))); fitness(i)=f1; else farm_X(:,i)=FARM_X(:,Ser(2*i)); farm_Y(:,(3*i-2):(3*i))=FARM_Y(:,(3*Ser(2*i)-2):(3*Ser(2*i))); fitness(i)=f2; end end %记录最佳个体和收敛曲线 minfitness=min(fitness); meanfitness=mean(fitness); LC1(counter+1)=minfitness; LC2(counter+1)=meanfitness; pos=find(fitness==minfitness); Xp=farm_X(:,pos(1)); Yp=farm_Y(:,(3*pos(1)-2):(3*pos(1))); Zp=minfitness; %第五步:变异 for i=1:N if Pm>rand GT_X=farm_X(:,i); GT_Y=farm_Y(:,(3*i-2):(3*i)); pos1=2*unidrnd(7); if GT_X(pos1)==1 GT_X(pos1-1)=1; GT_X(pos1)=0; farm_X(:,i)=GT_X; elseif GT_X(pos1)==0 GT_X(pos1-1)=0; GT_X(pos1)=1; farm_X(:,i)=GT_X; else end pos2=unidrnd(8); GT_Y(pos2,:)=zeros(1,3); GT_Y(pos2,unidrnd(3))=1; end end counter=counter+1 end Xp=Xp'; Yp=Yp'; %plot(LC1) %hold on plot(LC2) |
GMT+8, 2024-11-24 18:44 , Processed in 0.033554 second(s), 15 queries , Gzip On.
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.