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

📄 hillclimbing.m

📁 遗传算法与爬山法用于光纤与波导芯片对准仿真分析
💻 M
📖 第 1 页 / 共 2 页
字号:
%Hill-climbing method
%对于光纤对准问题
%The hill-climbing algorithm can only address one demension at a time.
%fun:name of the function f(x)
%x0:nx1 vector of initial point
%option=[iterations,inistep,checkpoints,goal]'
%beita:0<beita<1
%gama: gama>1
%eps:precision control
%x:returned maximum
% [x,counts,y]=hillclimbing([3 2 3 4 20]',[3 1 4 0.99]);
function [x,counts,y]=hillclimbing(x0,option)

if nargin<=1 || isempty(option)
    iterations=3;
    inistep=1;
    checkpoints=4;
    goal=0.999;
else
    iterationsb=option(1);                       %迭代次数
    inistep=option(2);                           %初始步长
    checkpointsb=option(3);                      %检验点数
    goal=option(4);                              %搜索目标,耦合效率达到0.999
end
direction=0;                                     %origin direction
counts=0;
countsx=0;                                      
countsy=0;                                      
countsdegx=0;                                      
countsdegy=0;                                      
countsz=0;        

fhandle = str2func('funn');                      %函数名字符串转换为函数句柄
n=length(x0);
stepsize=inistep;                               
iterations=iterationsb;
checkpoints=checkpointsb;
                                                %Start X axis search
dx(1,1)=x0(1);                                  %记录自变量的变化  
r0=x0;
y(1,1)=feval(fhandle,r0);                       %初始点函数值,爬山法通过单自由度轮换寻优完成多自由度搜索,首先
                                                %沿x1搜索,x2不变。x包含更多方向时怎办?如何体现?
                                                %五自由度搜索,x=[x1 x2 x3 x4 x5]
                                                %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);                 %每次迭代后的函数值
while y(counts+1,1)<goal
if y(counts+1,1)<y(counts,1)                    %power decrease?初步探测
    direction=1;                                %change direction flag
    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

while y(counts+1,1)<goal && iterations>0         %爬山
    iterations=iterations-1;
    if direction==0                             %正方向爬山
        while y(counts+1,1)>y(counts,1)
            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);  
            if y(counts+1,1)>goal
                break;
            end
        end
    else                                        %反方向爬山
        while y(counts+1,1)>y(counts,1)
            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);
            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
                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); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            else
                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); 
                if y(counts+1,1)>y(counts,1)            %another peak may exist
            
                end  
            end
            if y(counts+1,1)>goal
                break;
            end
        end
     end
    
    stepsize=stepsize/2;                          %ruduce step size by half and turn back toward peak
    if direction==0                                 %继续逼近顶点
        direction=1;
        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); 
    else
        direction=0;
        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
end

r=[dx(countsx+1,1) x0(2) x0(3) x0(4) x0(5)];         % Start Y axis search
x=r;
stepsize=inistep;      
iterations=iterationsb;
checkpoints=checkpointsb;
direction=0;                                        %origin direction
dy(1,1)=x0(2);                                      %记录自变量的变化

r=[x(1) dy(countsy+1,1) x0(3) x0(4) x0(5)];
y(counts+1,1)=feval(fhandle,r);                     %初始点函数值
                                                    %go checkpoints
countsy=countsy+1;                                                
counts=counts+1;                                    %迭代次数计数 
dy(countsy+1,1)=dy(countsy,1)+stepsize;             %每次迭代后的自变量
r=[x(1) dy(countsy+1,1) x0(3) x0(4) x0(5)];
y(counts+1,1)=feval(fhandle,r);;%每次迭代后的函数值
if y(counts+1,1)<y(counts,1)                        %power decrease?
    direction=1;                                    %change direction flag
    countsy=countsy+1; 
    counts=counts+1; 
    dy(countsy+1,1)=dy(countsy,1)-stepsize; 
    r=[x(1) dy(countsy+1,1) x0(3) x0(4) 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)
            countsy=countsy+1; 
            counts=counts+1; 
            dy(countsy+1,1)=dy(countsy,1)+stepsize; 
            r=[x(1) dy(countsy+1,1) x0(3) x0(4) 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)
            countsy=countsy+1; 
            counts=counts+1; 
            dy(countsy+1,1)=dy(countsy,1)-stepsize; 
            r=[x(1) dy(countsy+1,1) x0(3) x0(4) 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
                countsy=countsy+1; 
                counts=counts+1; 
                dy(countsy+1,1)=dy(countsy,1)+stepsize; 
                r=[x(1) dy(countsy+1,1) x0(3) 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
                countsy=countsy+1; 
                counts=counts+1; 
                dy(countsy+1,1)=dy(countsy,1)-stepsize; 
                r=[x(1) dy(countsy+1,1) x0(3) 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;
        countsy=countsy+1; 
        counts=counts+1; 
        dy(countsy+1,1)=dy(countsy,1)-stepsize; 
        r=[x(1) dy(countsy+1,1) x0(3) x0(4) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    else
        direction=0;
        countsy=countsy+1; 
        counts=counts+1; 
        dy(countsy+1,1)=dy(countsy,1)+stepsize; 
        r=[x(1) dy(countsy+1,1) x0(3) x0(4) x0(5)];
        y(counts+1,1)=feval(fhandle,r); 
    end
end
x=[x(1) dy(countsy+1,1) x0(3) x0(4) x0(5)];

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

r=[x(1) x(2) degx(countsdegx+1,1) x0(4) x0(5)];
y(counts+1,1)=feval(fhandle,r);                     %初始点函数值
                                                    %go checkpoints
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)                        %power decrease?
    direction=1;                                    %change direction flag
    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

while y(counts+1,1)<goal && iterations>0
    iterations=iterations-1;
    if direction==0   
        while y(counts+1,1)>y(counts,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); 
            if y(counts+1,1)>goal
                break;
            end
        end
    else
        while y(counts+1,1)>y(counts,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); 
            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

⌨️ 快捷键说明

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