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

📄 hillclimbing.m

📁 遗传算法与爬山法用于光纤与波导芯片对准仿真分析
💻 M
📖 第 1 页 / 共 2 页
字号:
            if direction==0
                countsdegx=countsdegx+1; 
                counts=counts+1; 
                degx(countsdegx+1,1)=degx(countsdegx,1)+stepsize; 
                r=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            else
                countsdegx=countsdegx+1; 
                counts=counts+1; 
                degx(countsdegx+1,1)=degx(countsdegx,1)-stepsize; 
                r=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            end
            if y(counts+1,1)>goal
                break;
            end
        end
     end
    if y(counts+1,1)>goal
        break;
    end
    
    stepsize=stepsize/2;                          %ruduce step size by half and turn back toward peak
    if direction==0
        direction=1;
        countsdegx=countsdegx+1; 
        counts=counts+1; 
        degx(countsdegx+1,1)=degx(countsdegx,1)-stepsize; 
        r=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    else
        direction=0;
        countsdegx=countsdegx+1; 
        counts=counts+1; 
        degx(countsdegx+1,1)=degx(countsdegx,1)+stepsize; 
        r=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    end
end
x=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];

r=x;                                                % Start DegY axis search
stepsize=inistep;      
iterations=iterationsb;
checkpoints=checkpointsb;
direction=0;                                        %origin direction
degy(1,1)=x0(4);                                    %记录自变量的变化

r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
y(counts+1,1)=feval(fhandle,r);                     %初始点函数值
                                                    %go checkpoints
countsdegy=countsdegy+1;                                                
counts=counts+1;                                    %迭代次数计数 
degy(countsdegy+1,1)=degy(countsdegy,1)+stepsize;   %每次迭代后的自变量
r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
y(counts+1,1)=feval(fhandle,r);                     %每次迭代后的函数值
if y(counts+1,1)<y(counts,1)                        %power decrease?
    direction=1;                                    %change direction flag
    countsdegy=countsdegy+1;  
    counts=counts+1; 
    degy(countsdegy+1,1)=degy(countsdegy,1)-stepsize;
    r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
    y(counts+1,1)=feval(fhandle,r); 
end

while y(counts+1,1)<goal && iterations>0
    iterations=iterations-1;
    if direction==0   
        while y(counts+1,1)>y(counts,1)
            countsdegy=countsdegy+1;  
            counts=counts+1; 
            degy(countsdegy+1,1)=degy(countsdegy,1)+stepsize;
            r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
            y(counts+1,1)=feval(fhandle,r); 
            if y(counts+1,1)>goal
                break;
            end
        end
    else
        while y(counts+1,1)>y(counts,1)
            countsdegy=countsdegy+1;  
            counts=counts+1; 
            degy(countsdegy+1,1)=degy(countsdegy,1)-stepsize;
            r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
            y(counts+1,1)=feval(fhandle,r); 
            if y(counts+1,1)>goal
                break;
            end
        end
    end
    if y(counts+1,1)>goal
        break;
    end
    
    checkpoints=checkpointsb;
    if iterations>3                                     %检验只在步长较大时才有意义,步长较小时主要任务是逼近山峰,没必要再进行检验。
        for i=1:checkpoints                             %check for the purpose of prventing trap into local peak
            if direction==0
                countsdegy=countsdegy+1;  
                counts=counts+1; 
                degy(countsdegy+1,1)=degy(countsdegy,1)+stepsize;
                r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            else
                countsdegy=countsdegy+1;  
                counts=counts+1; 
                degy(countsdegy+1,1)=degy(countsdegy,1)-stepsize;
                r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            end
            if y(counts+1,1)>goal
                break;
            end
        end
     end
    if y(counts+1,1)>goal
        break;
    end
    
    stepsize=stepsize/2;                          %ruduce step size by half and turn back toward peak
    if direction==0
        direction=1;
        countsdegy=countsdegy+1;  
        counts=counts+1; 
        degy(countsdegy+1,1)=degy(countsdegy,1)-stepsize;
        r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    else
        direction=0;
        countsdegy=countsdegy+1;  
        counts=counts+1; 
        degy(countsdegy+1,1)=degy(countsdegy,1)+stepsize;
        r=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    end
end
x=[x(1) x(2) x(3) degy(countsdegy+1,1) x0(5)];

r=x;                                                % Start Z axis search
stepsize=2*inistep;                                 %Z向搜索初始步长可以比其他方向大一些。
iterations=iterationsb;
checkpoints=checkpointsb;
direction=0;                                        %origin direction
z(1,1)=x0(5);                                       %记录自变量的变化

r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
y(counts+1,1)=feval(fhandle,r);                     %初始点函数值
                                                    %go checkpoints
countsz=countsz+1;                                                
counts=counts+1;                                    %迭代次数计数 
z(countsz+1,1)=z(countsz,1)+stepsize;               %每次迭代后的自变量
r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
y(counts+1,1)=feval(fhandle,r);                     %每次迭代后的函数值
if y(counts+1,1)<y(counts,1)                        %power decrease?
    direction=1;                                    %change direction flag
    countsz=countsz+1;  
    counts=counts+1; 
    z(countsz+1,1)=z(countsz,1)-stepsize;
    r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
    y(counts+1,1)=feval(fhandle,r); 
end

while y(counts+1,1)<goal && iterations>0
    iterations=iterations-1;
    if direction==0   
        while y(counts+1,1)>y(counts,1)
            countsz=countsz+1;  
            counts=counts+1; 
            z(countsz+1,1)=z(countsz,1)+stepsize;
            r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
            y(counts+1,1)=feval(fhandle,r); 
            if y(counts+1,1)>goal
                break;
            end
        end
    else
        while y(counts+1,1)>y(counts,1)
            countsz=countsz+1;  
            counts=counts+1; 
            z(countsz+1,1)=z(countsz,1)-stepsize;
            r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
            y(counts+1,1)=feval(fhandle,r); 
            if y(counts+1,1)>goal
                break;
            end
        end
    end
    if y(counts+1,1)>goal
        break;
    end
    
    checkpoints=checkpointsb;
    if iterations>3                                     %检验只在步长较大时才有意义,步长较小时主要任务是逼近山峰,没必要再进行检验。
        for i=1:checkpoints                             %check for the purpose of prventing trap into local peak
            if direction==0
                countsz=countsz+1;  
                counts=counts+1; 
                z(countsz+1,1)=z(countsz,1)+stepsize;
                r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            else
                countsz=countsz+1;  
                counts=counts+1; 
                z(countsz+1,1)=z(countsz,1)-stepsize;
                r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
                y(counts+1,1)=feval(fhandle,r); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            end
            if y(counts+1,1)>goal
                break;
            end
        end
     end
    if y(counts+1,1)>goal
        break;
    end
    
    stepsize=stepsize/2;                          %ruduce step size by half and turn back toward peak
    if direction==0
        direction=1;
        countsz=countsz+1;  
        counts=counts+1; 
        z(countsz+1,1)=z(countsz,1)-stepsize;
        r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
        y(counts+1,1)=feval(fhandle,r); 
    else
        direction=0;
        countsz=countsz+1;  
        counts=counts+1; 
        z(countsz+1,1)=z(countsz,1)+stepsize;
        r=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
        y(counts+1,1)=feval(fhandle,r); 
    end
end
                                                %第一轮循环完毕,如果所有自由度爬山后还没有达到顶点,
                                                %应该再从X轴开始第二轮循环。
if y(counts+1,1)>goal
        break;    
    end        
x=[x(1) x(2) x(3) x(4) z(countsz+1,1)];
r=x;
x0=x;
stepsize=inistep;      
iterations=iterationsb;
checkpoints=checkpointsb;
direction=0;                                        %origin direction
dx(1,1)=x0(1);                                      %记录自变量的变化

y(counts+1,1)=feval(fhandle,r);                     %初始点函数值
                                                    %go checkpoints
countsx=countsx+1;                                                
counts=counts+1;                                    %迭代次数计数 
dx(countsx+1,1)=dx(countsx,1)+stepsize;             %每次迭代后的自变量
r=[dx(countsx+1,1) x0(2) x0(3) x0(4) x0(5)];
y(counts+1,1)=feval(fhandle,r);                    %每次迭代后的函数值
end

%figure(1);
%plot([0:1:counts]',y,'b');xlabel('搜索步数');ylabel('归一化耦合效率');grid on;hold on;
%plot([0:1:counts]',y,'s');xlabel('');ylabel('');grid on;hold off;
%figure(2);
%plot([0:1:countsx]',dx(:,1),'b');xlabel('steps');ylabel('x(um)');grid on;hold on;
%plot([0:1:countsx]',dx(:,1),'o');xlabel('steps');ylabel('x(um)');grid on;hold off;
%figure(3);
%plot([0:1:countsy]',dy(:,1),'b');xlabel('steps');ylabel('y(um)');grid on;hold on;
%plot([0:1:countsy]',dy(:,1),'o');xlabel('steps');ylabel('y(um)');grid on;hold off;
%n=length(degx);
%figure(4);
%plot([0:1:countsdegx]',degx(:,1),'b');xlabel('steps');ylabel('pitch(deg)');grid on;hold on;
%plot([0:1:countsdegx]',degx(:,1),'o');xlabel('steps');ylabel('pitch(deg)');grid on;hold off;
%figure(5);
%plot([0:1:countsdegy]',degy(:,1),'b');xlabel('steps');ylabel('yaw(deg)');grid on;hold on;
%plot([0:1:countsdegy]',degy(:,1),'o');xlabel('steps');ylabel('yaw(deg)');grid on;hold off;
%figure(6);
%plot([0:1:countsz]',z(:,1),'b');xlabel('steps');ylabel('z(um)');grid on;hold on;
%plot([0:1:countsz]',z(:,1),'o');xlabel('steps');ylabel('z(um)');grid on;hold off;

⌨️ 快捷键说明

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