%% 【Input】:
% ryk2—日盈亏矩阵,xt_num—组合系统数,P—PRM左区间,M—MD左区间% P_index—PRM区间类型,1:[5,6) 2:(5,6]% M_index—MD区间类型,1:[3,4) 2:(3,4]% PM_index—PRM、MD逼近方向,1表示在满足PRM的情况下逼近MD的闭区间端,2表示在满足MD的情况下逼近PRM的闭区间端%% 【Output】:% SYD—种群适应度,MD或PRM% ZQ_org—优化种群,即组合的系统编号% QQ—适应度是否满足区间要求function [SYD,ZQ_org]=SS_GA_gaijin4(ryk,xt_num,M,PM_index)xt_num_all=size(ryk,2);%序列总长度【改】%% step1:GA Process
%====================【初始种群】=================num_org=50;%【改】ZQ_org=zeros(num_org,xt_num);for mRows=1:num_org temp1=randperm(xt_num_all); ZQ_org(mRows,:)=temp1(1:xt_num);end%====================【遗传进化】=================daishu=10;%【改】for mRows=1:daishu% disp(['【种群进化代数】:',num2str(mRows)]); %****************【交叉】**************** ZQ_cross=[]; cross_m=nchoosek(1:size(ZQ_org,1),2); for K=1:size(cross_m,1) temp1=ZQ_org(cross_m(K,1),:); temp2=ZQ_org(cross_m(K,2),:); [temp3,temp4]=fun_cross(temp1,temp2,xt_num_all); ZQ_cross=[ZQ_cross;temp3;temp4]; end ZQ_cross=sort(ZQ_cross,2); ZQ_cross=unique(ZQ_cross,'rows'); %****************【变异】**************** ZQ_variation=[]; num_variation=10;%变异倍数【改】 for K=1:size(ZQ_org,1) A_temp1=ZQ_org(K,:); B_temp1=setdiff([1:xt_num_all],A_temp1); for KK=1:num_variation a_temp2=randperm(length(A_temp1)); b_temp2=randperm(length(B_temp1)); a_temp3=union(B_temp1(b_temp2(1)),setdiff(A_temp1,A_temp1(a_temp2(1)))); ZQ_variation=[ZQ_variation;a_temp3]; end end ZQ_variation=sort(ZQ_variation,2); ZQ_variation=unique(ZQ_variation,'rows'); %****************【新物种生成】********** num_org1=10*num_org;%【改】 ZQ_new=zeros(num_org1,xt_num); for K=1:num_org1 temp1=randperm(xt_num_all); ZQ_new(K,:)=temp1(1:xt_num); end %****************【进化淘汰】************ ZQ_inherit=[ZQ_cross;ZQ_variation;ZQ_new;ZQ_org]; ZQ_inherit=sort(ZQ_inherit,2); ZQ_inherit=unique(ZQ_inherit,'rows'); SYD_inherit=zeros(size(ZQ_inherit,1),1); for flag_k=1:size(ZQ_inherit,1) m_temp1=ZQ_inherit(flag_k,:); SYD_inherit(flag_k,1)=fun_syd(m_temp1,ryk,M,PM_index);%【改】 end [m_temp3,m_temp2]=sort(SYD_inherit,'descend');%【改】 ZQ_org=ZQ_inherit(m_temp2(1:num_org),:);% disp(['【进化序列】:',num2str(ZQ_org(1,:))]);disp(['【种群进化代数】:',num2str(mRows),'—【进化序列】:',num2str(ZQ_org(1,:)),'—',num2str(m_temp3(1,:))]);end%% step3:具体问题输出
for flag_k=1:size(ZQ_org,1) m_temp1=ZQ_org(flag_k,:); SYD(flag_k,1)=fun_syd(m_temp1,ryk,M,PM_index);end%% 运行完毕对话框
% tt=toc;% msgbox(['【运行时间】:',num2str(tt),'秒'],'温馨提示!');% disp(['【运行时间】:',num2str(tt),'秒!']);%% ******************************************************************************************************
%% 【子函数】交叉算子 function [new_XL1,new_XL2]=fun_cross(A1,B1,C) c_temp1=[1:C]; new_XL1=A1; new_XL2=B1; A2=setdiff(c_temp1,A1); B2=setdiff(c_temp1,B1); c_temp2=intersect(A1,B2); c_temp3=intersect(B1,A2); if ~isempty(c_temp2) if ~isempty(c_temp3) c_temp4=randperm(length(c_temp2)); X_temp=c_temp2(c_temp4(1)); c_temp5=randperm(length(c_temp3)); Y_temp=c_temp3(c_temp5(1)); new_XL1=union(Y_temp,setdiff(A1,X_temp)); new_XL2=union(X_temp,setdiff(B1,Y_temp)); end end end%% 【子函数】适应度函数
function sydm = fun_syd( XL,ryk,M,PM_index ) h_temp1=ryk(:,XL); h_temp2=sum(h_temp1,2); [ljyk,qqgd,huitiao,huitiao_didian,huitiao_didian_time,buyinliqi,lianxuyinli,lianxukuisuan] = fun_MD(h_temp2,200000*length(XL)); PP=sum(h_temp2); MM=abs(min(huitiao))*100;%【改】考虑2种方式:当前MD或这一段时间最小MD if PM_index==1 %表示在满足MD区间[M,M+1]的情况下PRM尽量大 if (MM<M)|(MM>M+1) sydm=-inf; else sydm=PP; end elseif PM_index==2 %表示在满足MD区间[M,M+1]的情况下PRM尽量小 if (MM<M)|(MM>M+1) sydm=-inf; else sydm=-PP; end else msgbox('PM_index设置错误,取值范围:1或2'); return; end endend