爬山搜索算法 matlab_【优化】全局搜索算法案例

本文通过MATLAB实现爬山搜索、模拟退火、粒子群和遗传算法,详细介绍了每个算法的实现步骤、关键代码及结果展示,帮助读者深入理解这些全局优化算法的工作原理。
摘要由CSDN通过智能技术生成

本文使用Matlab实现全局搜索算法案例,包括Melder_Mead单纯形法、模拟退火算法、粒子群算法和遗传算法,从而进一步理解相应的算法。

案例1:Melder_Mead单纯形法

1.题目要求:

48858b468ee9dfba0bba468269ea0460.png

2.MATLAB实现:

2.1 初始点选择

function [ output_args ] = nm_simplex( input_args )
%Nelder-Mead simplex method
%Based on the program by the Spring 2007 ECE580 student, Hengzhou Ding
disp ('We minimize a function using the Nelder-Mead method.')
disp ('There are two initial conditions.')
disp ('You can enter your own starting point.')
disp ('---------------------------------------------')

% disp(’Select one of the starting points’)
% disp (’[0.55;0.7] or [-0.9;-0.5]’)
% x0=input(’’)
disp (' ')
clear
close all;

disp('Select one of the starting points, or enter your own point')
disp('[0.55;0.7] or [-0.9;-0.5]')
disp('(Copy one of the above points and paste it at the prompt)')
x0=input(' ');

hold on
axis square

2.2 函数轮廓绘制

%Plot the contours of the objective function
%画目标函数的轮廓
[X1,X2]=meshgrid(-1:0.01:1);
Y=(X2-X1).^4+12.*X1.*X2-X1+X2-3;
[C,h] = contour(X1,X2,Y,20);
clabel(C,h);

2.3 初始参数和迭代点

% Initialize all parameters
%初始化参数
%{
lambda用于生成n+1个点
rho用于生成pr
chi用于生成pe
gamma用于生成pc
sigma用于生成重点
e1、e2为空间的标准基
%}
lambda=0.1;
rho=1;
chi=2;
gamma=1/2;
sigma=1/2;
e1=[1 0]';
e2=[0 1]';
%x0=[0.55 0.7]’;
%x0=[-0.9 -0.5]’;

% Plot initial point and initialize the simplex
%画初始的迭代点
%x(:,3)=x0表示矩阵x的第三列为向量x0
plot(x0(1),x0(2),'--*');
x(:,3)=x0;
x(:,1)=x0+lambda*e1;
x(:,2)=x0+lambda*e2;

2.4 迭代过程

%{
递归迭代的过程
%}
while 1
    % Check the size of simplex for stopping criterion
    % 检查单纯形是否满足停止条件
    simpsize=norm(x(:,1)-x(:,2))+norm(x(:,2)-x(:,3))+norm(x(:,3)-x(:,1));
    if(simpsize<1e-6)
        break;
    end
    %上一次的迭代点
    lastpt=x(:,3);
    % Sort the simplex
    % 对单纯形中的点进行排序——排序后x(:,3)中的点对应函数值最大
    x=sort_points(x,3);
    % Reflection
    % 反射操作
    centro=1/2*(x(:,1)+x(:,2));
    xr=centro+rho*(centro-x(:,3));
    % Accept condition
    % 接受条件判断
    % 条件1:反射点pr对应函数值位于fnl和fs之间
    if(obj_fun(xr)>=obj_fun(x(:,1)) && obj_fun(xr)<obj_fun(x(:,2)))
        x(:,3)=xr;
        % Expand condition
    %条件2:反射点pr对应的函数值小于fs
    elseif(obj_fun(xr)<obj_fun(x(:,1)))
        xe=centro+rho*chi*(centro-x(:,3));
            if(obj_fun(xe)<obj_fun(xr))
                x(:,3)=xe;
            else
                x(:,3)=xr;
            end

    % Outside contraction or shrink
    %条件3:反射点pr对应的函数值大于 fnl,但小于fl。——外收缩
    elseif(obj_fun(xr)>=obj_fun(x(:,2)) && obj_fun(xr)<obj_fun(x(:,3)))
        xc=centro+gamma*rho*(centro-x(:,3));
            if(obj_fun(xc)<obj_fun(x(:,3)))
                x(:,3)=xc;
            else
                x=shrink(x,sigma);
            end
        % Inside contraction or shrink
    % 条件4:反射点pr对应的函数值大于 fl——内收缩
    else
        xcc=centro-gamma*(centro-x(:,3));
        if(obj_fun(xcc)<obj_fun(x(:,3)))
            x(:,3)=xcc;
        else
            x=shrink(x,sigma);
        end
    end
    % Plot the new point and connect
    % 打印反射出来的新的迭代点,并与上一个点连线
    plot([lastpt(1),x(1,3)],[lastpt(2),x(2,3)],'--*');
end
% Output the final simplex (minimizer)
% 输出最终的单纯形
x(:,1)
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值