a*算法matlab代码_粒子群算法Matlab图形用户界面(GUI)代码分享

GUI, 四月结

粒子群优化算法(Particle Swarm optimization,PSO)又翻译为粒子群算法、微粒群算法、或微粒群优化算法。是通过模拟鸟群觅食行为而发展起来的一种基于群体协作的随机搜索算法。通常认为它是群集智能 (Swarm intelligence, SI) 的一种。它可以被纳入多主体优化系统(Multiagent Optimization System, MAOS)。粒子群优化算法是由Eberhart博士和kennedy博士发明。

粒子群算法(particle swarm optimization,PSO)是计算智能领域中的一种生物启发式方法,属于群体智能优化算法的一种,常见的群体智能优化算法主要有如下几类:

  (1)蚁群算法(Ant Colony Optimization,简称ACO)[1992年提出];


  (2)粒子群优化算法(Particle Swarm Optimization,简称PSO)[1995年提出](简单易于实现,也是目前应用最为广泛的群体智能优化算法);


  (3)菌群优化算法(Bacterial Foraging Optimization,简称BFO)[2002年提出];


  (4)蛙跳算法(Shuffled Frog Leading Algorithm,简称SFLA)[2003年提出];


  (5)人工蜂群算法(Artificial Bee Colony Algorithm,简称ABC)[2005年提出];


  除了上述几种常见的群体智能算法以外,还有一些并不是广泛应用的群体智能算法,比如萤火虫算法、布谷鸟算法、蝙蝠算法以及磷虾群算法等等。

而其中的粒子群优化算法(PSO)源于对鸟类捕食行为的研究,鸟类捕食时,找到食物最简单有限的策略就是搜寻当前距离食物最近的鸟的周围

举个通俗的例子:

一群鸟在寻找食物,在这个区域内有一个食物,所有的鸟都不知道食物在哪里,但食物发出了香味,鸟通过香味的浓烈能判断出自己的当前位置距离食物有多远,同时鸟群之间是可以交流并告知离食物最近的鸟的位置的。试想一下这个时候会发生什么?

鸟甲:哈哈哈,原来我离食物最近!

鸟乙、丙、丁…:我得赶紧往鸟甲那里过去看看!

同时各自鸟在位置不停变化的时候,离食物的距离也不断的变化,所以一定有过离食物最近的位置,这是他们的一个参考。

鸟某某:我刚才的位置好像离食物很近了,我得往那里再靠近点!

通过这样一个过程,不断的变化,最终鸟群会向食物方向聚集,达到目标。在这个变化的过程中,影响鸟的运动状态变化的有两个因素:

  • 离食物最近的鸟的位置

  • 自己之前达到过的离食物最近的位置

而鸟的每次的位置变化,除了考虑以上的两个因素还有一个因素就是惯性。而对位置的变化,通过变化量(速度)来表示。

3ff111625e1778a446d358f8db14f350.png

MATLAB GUI 为图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面,是MATLAB用户可视化交互式的工具,运用GUI生成的操作界面用户可以不用浏览繁冗的代码而进行操作。

GUI不仅深受用户的喜爱也是工程人员运用MATLAB进行可视化操作的捷径,工程人员只需要拖动相应的工具,编写回调函数即可。由于第一次的、发文在这里不做过多学习介绍在后续的推文里我会把MATLAB GUI各个功能通过例子的方式介绍给大家,MATLAB GUI还有一个优点就是有利于编译成独立的.exe文件,用户不需要安装庞大内存的MATLAB软件就可以运行程序。

以上文字根据网络资料整理

下面分享一段完整的粒子群算法Matlab图形用户界面(GUI)代码

同样来源于网络

主程序代码

function [] = psoToolbox

%%psoToolbox V1.0 帮助:

%   psoToolbox 是用粒子群算法解决优化问题的交互式界面

%   创建一个适应度函数的M文件.

%   输入:

%     Function        : 适应度函数的函数句柄。

%     Nvars           : 变量个数,即粒子的维数。

%     LB              : 变量下界

%     UB              : 变量上界

%   参数:

%     C1              : 个体权重

%     C2              : 全局权重

%     W               : 惯性因子

%     群体数量     : 粒子数目

%     最大迭代次数  : 计算循环的次数

%%   重要说明:本函数只能用来求[LB,UB]区间内的最小值;如果要求最大值,只需做相应转变就行!

%% 图像及背景

% set(0,'defaultAxesFontName', '');

set(0,'units','pixel');

screensize = get(0,'screensize');

height = 600;

width = 800;

basecolor = [rand 1 rand]./2;

imdata = creatback(height,width,basecolor);

fig_position = [(screensize(3)/2)-(width/2) (screensize(4)/2)-(height/2) width height];

handle.fig = figure('Name','粒子群算法',...

    'units','pixel',...

    'position',fig_position,...

    'numbertitle','off',...

    'menubar','none',...

    'resize','off');

handle.back = axes('parent',handle.fig,...

    'units','pixel',...

    'position',[0 0 width+2 height+2],...

    'xtick',[],...

    'ytick',[]);

imshow(imdata);

handle.titletext = text(20,20,'粒子群算法工具箱 V1.0',...

    'fontsize',20,...

    'fontname','',...

    'color','w');

%% 面板1

handle.box1 = line([30 20 20 300 300 90],[80 80 300 300 80 80],...

    'linewidth',2,...

    'color','w');

handle.box1name = text(30,75,'输入',...

    'fontsize',16,...

    'fontname','',...

    'color','w');

handle.text11 = text(35,110,'函数 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit11 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[132 478 150 25],...

    'backgroundcolor',basecolor*(height-475)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text12 = text(35,160,'变量个数: ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit12 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[132 428 150 25],...

    'backgroundcolor',basecolor*(height-428)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text13 = text(35,210,'区间下限 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit13 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[162 378 120 25],...

    'backgroundcolor',basecolor*(height-378)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text14 = text(35,260,'区间上限 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit14 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[162 328 120 25],...

    'backgroundcolor',basecolor*(height-328)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

%% 面板2

handle.box2 = line([30 20 20 300 300 135],[335 335 555 555 335 335],...

    'linewidth',2,...

    'color','w');

handle.box2name = text(30,330,'参数',...

    'fontsize',16,...

    'fontname','',...

    'color','w');

handle.text21 = text(35,365,'C1 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit21 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[72 223 70 25],...

    'backgroundcolor',basecolor*(height-223)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text22 = text(150,365,'C2 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit22 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[186 223 90 25],...

    'backgroundcolor',basecolor*(height-223)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text23 = text(35,415,'W  : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit23 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[72 173 90 25],...

    'backgroundcolor',basecolor*(height-173)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text24 = text(35,465,'群体数量 : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit24 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[186 123 90 25],...

    'backgroundcolor',basecolor*(height-123)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

handle.text25 = text(35,515,'最大迭代次数   : ',...

    'fontsize',14,...

    'fontname','',...

    'color','w');

handle.edit25 = uicontrol('style','edit',...

    'parent',handle.fig,...

    'position',[186 73 90 25],...

    'backgroundcolor',basecolor*(height-73)/height,...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'horizontalalignment','left');

%% 收敛过程坐标轴 

handle.axes1 = axes('parent',handle.fig,...

    'units','pixel',...

    'position',[380 300 380 220],...

    'gridlinestyle','--',...

    'fontsize',11,...

    'fontname','',...

    'color',basecolor*(height-380)/height,...

    'xcolor','w',...

    'ycolor','w');

xlabel('迭代');

ylabel('F(x)');

h=title('F(x)收敛过程');

set(h,'color','w');

grid on

handle.edit3 = uicontrol('style','edit',...

    'units','pixel',...

    'position',[380 40 380 200],...

    'horizontalalignment','left',...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'backgroundcolor',basecolor*(height-200)/height,...

    'max',10,...

    'string','PSO :',...

    'Enable','on');

%% 按钮

F = getframe(handle.back,[70 10 160 30]);

handle.push = uicontrol('style','pushbutton',...

    'position',[70 10 160 30],...

    'cdata',F.cdata,...

    'string',' Run PSO',...

    'fontsize',14,...

    'fontname','',...

    'foregroundcolor','w',...

    'callback',@push1_callback);

%% 背景图片

    function cdata = creatback(height,width,basecolor)

        cdata = zeros(height,width,3);

        [height , ~, page] = size(cdata);

        for i = 1:height

            color = basecolor.*(i/height);

            for j = 1:page

                cdata(i,:,j) = color(j);

            end

        end

    end

%% 默认值

set(handle.edit21,'string','1.2');%C1

set(handle.edit22,'string','0.012');%C2

set(handle.edit23,'string','0.0004');%W

set(handle.edit24,'string','50');%N

set(handle.edit25,'string','10000');%迭代次数

set(handle.edit11,'string','@fitness');%适应度函数

set(handle.edit12,'string','3');

set(handle.edit13,'string','[0 0 0]');

set(handle.edit14,'string','[10 10 10]');

    function [] = push1_callback(varargin)

        axes(handle.axes1);cla

        set(handle.edit3,'string','PSO');

        C1=str2double(get(handle.edit21,'string'));%C1

        C2=str2double(get(handle.edit22,'string'));%C2

        W=str2double(get(handle.edit23,'string'));%W

        N=str2double(get(handle.edit24,'string'));%N

        MaxIterations=str2double(get(handle.edit25,'string'));%最大迭代

        ObjectiveFunction = get(handle.edit11,'string');%函数对象,即适应度函数

        Nvars = str2double(get(handle.edit12,'string'));% nvars

        LB = str2num(get(handle.edit13,'string'));% LB

        UB = str2num(get(handle.edit14,'string'));%UB

        if isempty(ObjectiveFunction)

            errordlg('PSO:输入函数不能为空!');

        else

            ObjectiveFunction=str2func(ObjectiveFunction);

        end

        if ~isequal(Nvars,size(LB,2),size(UB,2))

            errordlg('PSO:区间上下长度必须一致!');

        end 

        str{1} = get(handle.edit3,'string');

        str{2} = sprintf('\nPSO Begin...');

        set(handle.edit3,'string',str);

        %% PSO算法

        CurrentPosition = zeros(N,Nvars); % 初始化位置

        for i = 1:Nvars

            CurrentPosition(:,i) = random('unif',LB(i),UB(i),N,1);

        end

        Velocity = W.*rand(N,Nvars) ; %初始化速度

        % 计算初始化位置

        CurrentFitness = zeros(N,1); % 适应度值

        for i = 1:N

            CurrentFitness(i) = ObjectiveFunction(CurrentPosition(i,:));

        end

        % 更新局部极值

        LocalBestPosition = CurrentPosition; % 局部极值

        LocalBestFitness = CurrentFitness;

        %  更新全局极值

        [GlobalBestFitness,index] = min(LocalBestFitness);

        GlobalBestPosition = repmat(LocalBestPosition(index,:),N,1); % 全局极值

        % 更新速度及位置

        R1 = rand(N,Nvars); % 随机数1

        R2 = rand(N,Nvars); % 随机数2

        Velocity = W.*Velocity + C1.*(R1.*(LocalBestPosition-CurrentPosition))...

            + C2.*(R2.*(GlobalBestPosition-CurrentPosition));

        CurrentPosition = CurrentPosition + Velocity ;

        % 跟新区间LB和UB

        for i = 1:Nvars

            indexes = CurrentPosition(:,i) < LB(i).*ones(N,1);

            CurrentPosition(indexes,i) = LB(i);

            indexes = CurrentPosition(:,i) > UB(i).*ones(N,1);

            CurrentPosition(indexes,i) = UB(i);

        end

        %迭代求极值

        Iter = 0;

        while (Iter < MaxIterations)

            Iter = Iter+1;

            %计算当前位置

            for i = 1:N

                CurrentFitness(i) = ObjectiveFunction(CurrentPosition(i,:));

            end

            % 更新局部极值

            indexes = find(CurrentFitness < LocalBestFitness);

            LocalBestFitness(indexes) = CurrentFitness(indexes);

            LocalBestPosition(indexes,:) = CurrentPosition(indexes,:);

            % 更新全局极值

            [GlobalBestFitnessNew,index] = min(LocalBestFitness);

            if GlobalBestFitnessNew < GlobalBestFitness

                GlobalBestFitness = GlobalBestFitnessNew;

                GlobalBestPosition = repmat(LocalBestPosition(index,:),N,1);

            end

            % 更新速度及位置

            R1 = randn(N,Nvars); %随机数1

            R2 = randn(N,Nvars); %随机数2

            Velocity = W.*Velocity + C1.*(R1.*(LocalBestPosition-CurrentPosition))...

                + C2.*(R2.*(GlobalBestPosition-CurrentPosition));

            CurrentPosition = CurrentPosition + Velocity ;

            % 跟新区间LB和UB

            for i = 1:Nvars

                indexes = CurrentPosition(:,i) < LB(i).*ones(N,1);

                CurrentPosition(indexes,i) = LB(i);

                indexes = CurrentPosition(:,i) > UB(i).*ones(N,1);

                CurrentPosition(indexes,i) = UB(i);

            end

            if (Iter==1)||(rem(Iter,100)==0)

                plot(Iter,GlobalBestFitness,'r.','Linewidth',3);hold on

                set(handle.axes1,'xcolor','w','ycolor','w',...

                    'fontsize',11,...

                    'fontname','',...

                    'color',basecolor*(height-380)/height);

                xlabel('迭代');

                ylabel('F(x)');

                h = title(['最优值为 : ' num2str(GlobalBestFitness)]);

                set(h,'color','w');

                grid on

                drawnow

            end

        end

        str{3}= sprintf('\nPSO Done!');

        set(handle.edit3,'string',str);

        str{4} = sprintf('\nF(x) = %d',GlobalBestFitness);

        str{5} = sprintf('\nx = %d',GlobalBestPosition(1,:));

        set(handle.edit3,'string',str);

        clear('str');

    end

end

子程序代码
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值