⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crossover.m

📁 matlab环境下的遗传算法交叉程序
💻 M
字号:
function Cpop=crossover(pc,spop,nxc,u,pass,obj,limit)
for r=4:pass(1)
    temp(r-3,:)=spop(r,:);
end
sizec=pass(1)-3;
for r=1:10
    px=rand(1);
    index=floor(rand(1)*pass(1));
    if index>=1 & index<=10
        if px<pc(1)
            cross=rand(1);
            tempc(1,:)=cross*temp(r,:)+(1-cross)*temp(index,:);
            tempc(2,:)=cross*temp(index,:)+(1-cross)*temp(r,:);
            tempc(3,:)=temp(r,:);
            
            for rr=1:3             
                for rc=1:2
                    if tempc(rr,rc)<limit(rc,1)|tempc(rr,rc)>limit(rc,2)
                        tempc(rr,:)=0;
                    end
                end
            end
            
            moutt=zeros(3,pass(4));
            
            for rt=1:3
                
                if tempc(rt,1)==0
                    Qt(rt)=100;
                    continue;
                end
                
               
                
                moutt(rt,:)=simu(nxc,tempc(rt,1),tempc(rt,2),u,pass(2),pass(3));
                qt(rt,:)=(moutt(rt,:)-obj).^2;
                Qt(rt)=sum(qt(rt,:))*100;
            end
            [Qtmi,Ic]=min(Qt);
            if Qtmi~=100
                temp(r,:)=tempc(Ic,:);
            end
        end
    end
    if index>10 & index<=sizec
        if px<pc(1) & px<pc(2)
            cross=rand(1);
            tempc(1,:)=cross*temp(r,:)+(1-cross)*temp(index,:);
            tempc(2,:)=cross*temp(index,:)+(1-cross)*temp(r,:);
            tempc(3,:)=temp(r,:);
            
            for rr=1:3             
                for rc=1:2
                    if tempc(rr,rc)<limit(rc,1)|tempc(rr,rc)>limit(rc,2)
                        tempc(rr,:)=0;
                    end
                end
            end
            
            moutt=zeros(3,pass(4));
            
            for rt=1:3
                
                if tempc(rt,1)==0
                    Qt(rt)=100;
                    continue;
                end
               
                
                moutt(rt,:)=simu(nxc,tempc(rt,1),tempc(rt,2),u,pass(2),pass(3));
                qt(rt,:)=(moutt(rt,:)-obj).^2;
                Qt(rt)=sum(abs(qt(rt,:)))*100;
            end
            [Qtmi,Ic]=min(Qt); 
            if Qtmi~=100
                temp(r,:)=tempc(Ic,:);
            end
        end
    end
end
for r=11:sizec
    px=rand(1);
    index=floor(rand(1)*pass(1));
    if index>=1 & index<=10
        if px<pc(1) & px<pc(2)
            cross=rand(1);
            tempc(1,:)=cross*temp(r,:)+(1-cross)*temp(index,:);
            tempc(2,:)=cross*temp(index,:)+(1-cross)*temp(r,:);
            tempc(3,:)=temp(r,:);
            
            for rr=1:3             
                for rc=1:2
                    if tempc(rr,rc)<limit(rc,1)|tempc(rr,rc)>limit(rc,2)
                        tempc(rr,:)=0;
                    end
                end
            end
            
            moutt=zeros(3,pass(4));
            for rt=1:3
                
                if tempc(rt,1)==0
                    Qt(rt)=100;
                    continue;
                end
                
                
                moutt(rt,:)=simu(nxc,tempc(rt,1),tempc(rt,2),u,pass(2),pass(3));
                qt(rt,:)=(moutt(rt,:)-obj).^2;
                Qt(rt)=sum(abs(qt(rt,:)))*100;
            end
            [Qtmi,Ic]=min(Qt);
            if Qtmi~=100
                temp(r,:)=tempc(Ic,:);
            end
        end
    end
    if index>10 & index<=sizec
        if px<pc(2)
            cross=rand(1);
            tempc(1,:)=cross*temp(r,:)+(1-cross)*temp(index,:);
            tempc(2,:)=cross*temp(index,:)+(1-cross)*temp(r,:);
            tempc(3,:)=temp(r,:);
            
            for rr=1:3             
                for rc=1:2
                    if tempc(rr,rc)<limit(rc,1)|tempc(rr,rc)>limit(rc,2)
                        tempc(rr,:)=0;
                    end
                end
            end
            
            moutt=zeros(3,pass(4));
            for rt=1:3
                
                if tempc(rt,1)==0
                    Qt(rt)=100;
                    continue;
                end
                
                moutt(rt,:)=simu(nxc,tempc(rt,1),tempc(rt,2),u,pass(2),pass(3));
                qt(rt,:)=(moutt(rt,:)-obj).^2;
                Qt(rt)=sum(abs(qt(rt,:)))*100;
            end
            [Qtmi,Ic]=min(Qt);
            if Qtmi~=100
                temp(r,:)=tempc(Ic,:);
            end
        end
    end
end
for r=1:3
    Cpop(r,:)=spop(r,:);
end
for r=4:pass(1)
    Cpop(r,:)=temp(r-3,:);
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -