• 【PID优化】基于蝗虫算法PID控制器优化设计含Matlab源码


    1 内容介绍

    该文针对广泛应用的PID控制器,在MATLAB仿真软件环境下,开发出一个过程控制系统的仿真软件包,能够实现模型辨识和PID参数调节,为过程控制系统仿真研究提供了方便. 该软件界面友好,操作简便,具有实际应用价值.​

    2 仿真代码

    %_________________________________________________________________________%

    %  Grasshopper Optimization Algorithm (GOA) source codes demo V1.0        %

    %                                                                         %

    %  Developed in MATLAB R2014a                                             %

    % The Grasshopper Optimization Algorithm

    function [TargetFitness,TargetPosition,Convergence_curve,Trajectories,fitness_history, position_history]=GOA(N, Max_iter, lb,ub, dim, fobj)

    disp('GOA is now estimating the global optimum for your problem....')

    flag=0;

    if size(ub,1)==1

        ub=ones(dim,1)*ub;

        lb=ones(dim,1)*lb;

    end

    if (rem(dim,2)~=0) % this algorithm should be run with a even number of variables. This line is to handle odd number of variables

        dim = dim+1;

        ub = [ub; 100];

        lb = [lb; -100];

        flag=1;

    end

    %Initialize the population of grasshoppers

    GrassHopperPositions=initialization(N,dim,ub,lb);

    GrassHopperFitness = zeros(1,N);

    fitness_history=zeros(N,Max_iter);

    position_history=zeros(N,Max_iter,dim);

    Convergence_curve=zeros(1,Max_iter);

    Trajectories=zeros(N,Max_iter);

    cMax=1;

    cMin=0.00004;

    %Calculate the fitness of initial grasshoppers

    for i=1:size(GrassHopperPositions,1)

        if flag == 1

            GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,1:end-1));

        else

            GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,:));

        end

        fitness_history(i,1)=GrassHopperFitness(1,i);

        position_history(i,1,:)=GrassHopperPositions(i,:);

        Trajectories(:,1)=GrassHopperPositions(:,1);

    end

    [sorted_fitness,sorted_indexes]=sort(GrassHopperFitness);

    % Find the best grasshopper (target) in the first population 

    for newindex=1:N

        Sorted_grasshopper(newindex,:)=GrassHopperPositions(sorted_indexes(newindex),:);

    end

    TargetPosition=Sorted_grasshopper(1,:);

    TargetFitness=sorted_fitness(1);

    % Main loop

    l=2; % Start from the second iteration since the first iteration was dedicated to calculating the fitness of antlions

    while l

        

        c=cMax-l*((cMax-cMin)/Max_iter); % Eq. (2.8) in the paper

        

        for i=1:size(GrassHopperPositions,1)

            temp= GrassHopperPositions';

            for k=1:2:dim

                S_i=zeros(2,1);

                for j=1:N

                    if i~=j

                        Dist=distance(temp(k:k+1,j), temp(k:k+1,i)); % Calculate the distance between two grasshoppers

                        

                        r_ij_vec=(temp(k:k+1,j)-temp(k:k+1,i))/(Dist+eps); % xj-xi/dij in Eq. (2.7)

                        xj_xi=2+rem(Dist,2); % |xjd - xid| in Eq. (2.7) 

                        

                        s_ij=((ub(k:k+1) - lb(k:k+1))*c/2)*S_func(xj_xi).*r_ij_vec; % The first part inside the big bracket in Eq. (2.7)

                        S_i=S_i+s_ij;

                    end

                end

                S_i_total(k:k+1, :) = S_i;

                

            end

            

            X_new = c * S_i_total'+ (TargetPosition); % Eq. (2.7) in the paper      

            GrassHopperPositions_temp(i,:)=X_new'; 

        end

        % GrassHopperPositions

        GrassHopperPositions=GrassHopperPositions_temp;

        

        for i=1:size(GrassHopperPositions,1)

            % Relocate grasshoppers that go outside the search space 

            Tp=GrassHopperPositions(i,:)>ub';Tm=GrassHopperPositions(i,:)

            

            % Calculating the objective values for all grasshoppers

            if flag == 1

                GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,1:end-1));

            else

                GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,:));

            end

            fitness_history(i,l)=GrassHopperFitness(1,i);

            position_history(i,l,:)=GrassHopperPositions(i,:);

            

            Trajectories(:,l)=GrassHopperPositions(:,1);

            

            % Update the target

            if GrassHopperFitness(1,i)

                TargetPosition=GrassHopperPositions(i,:);

                TargetFitness=GrassHopperFitness(1,i);

            end

        end

            

        Convergence_curve(l)=TargetFitness;

        disp(['In iteration #', num2str(l), ' , target''s objective = ', num2str(TargetFitness)])

        

        l = l + 1;

    end

    if (flag==1)

        TargetPosition = TargetPosition(1:dim-1);

    end

    3 运行结果

    4 参考文献

    [1]周蓓晨, 张宏立. 基于SOA-MC算法的二自由度PID控制器优化设计[J]. 计算机仿真, 2018, 35(4):5.

    [1]彭珍瑞, 栾睿, 王娴. 基于人工鱼群算法的伺服系统PID控制器参数优化[J]. 兰州交通大学学报, 2012, 31(4):4.

    博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

    部分理论引用网络文献,若有侵权联系博主删除。

  • 相关阅读:
    ElementUI两个小坑
    加速智能驾驶项目落地?你还缺一套真值测评系统
    2023华为杯研究生数学建模竞赛CDEF题思路+模型代码
    将AI带入企业,红帽选择了开源
    基于物联网的教室人数检测系统-设计说明书
    数据结构与算法训练:第二十七、二十八弹
    【刷题记录】排列子序列
    2022年最新安徽建筑安全员考试题库及答案
    高斯分布与高斯过程
    激光slam学习记录
  • 原文地址:https://blog.csdn.net/qq_59747472/article/details/126238719