• 基于萤火虫算法的线性规划求解matlab程序


    基于萤火虫算法的线性规划求解matlab程序
    在这里插入图片描述
    1 萤火虫算法概述

    萤火虫,别名火金姑,大多数的萤火虫都会发光,因为它们体内有专门的发亮细胞,发出光亮是它们的基本能力。但是个体间存在差异,如性别、年龄、品种等,导致发出的冷光亮度是不同的。发光不仅仅是为了照明,还是为了交流、警戒、求偶等。以求偶为例,当一个雌性萤火虫发出光亮信号被异性萤火虫察觉到以后,也会通过发光来回应并向求偶者不断靠近、交流,然后交配、繁衍后代。英国剑桥大学的Yang X.S.正是受此启发,在建模萤火虫种群行为的基础上提出了FA算法。在一定范围内萤火虫可感应同伴的闪光频次、强度并判定同伴位置,FA算法正是基于此类生物行为而来。FA算法舍弃了一些生物学特点,在搜索域内仅仅依靠发光能力来寻觅周围较亮个体,并移向较亮的个体以更新位置,通过适应度函数完成算法的评价。算法的提出时间不长,对算法的改进和优化仍然处于初级阶段,但基本的FA算法足以被用来求解几乎所有领域科学和工程,如调度问题、旅行商问题、语义组成等领域。

    在这里插入图片描述
    FA算法的提出是建立在萤火虫种群中个体的种种社会行为的基础之上的。在具体的求解中,只考虑发光特性,萤火虫更新的关键因素是荧光亮度和吸引度。个体之间的相互吸引即为搜索操作,位置更新即为优化过程,从而获得全局最优解。将FA算法用数学语言来表述,为方便表述,只考虑关键参数,即吸引度和亮度,忽略其他次要因素,亮度代表目标函数,算法求解时需遵循以下原则:
    (1)所有个体之间都可相互吸引,吸引度只由亮度、个体间距离决定。亮度强的萤火虫会吸引亮度弱的萤火虫,亮度弱的个体会移动到亮度强的个体周围。亮度大小代表了个体所处位置的好坏,最佳解即为亮度最大的个体。自然界中,光的传播介质会吸收光,影响光的传播,降低个体可视度。因此,吸引度与距离有关,距离越近,吸引度越大,反之越小。最亮个体自由移动。
    (2)发光强弱与目标函数值有关,即适应度函数值。

    在这里插入图片描述
    FA算法的中心思想就是,所有个体都飞向比自己更亮的个体四周,飞行的最后结果就是所有的个体都围在了亮度最强的个体的四周,从而达到算法求解的目的。步骤如下:
    在这里插入图片描述
    FA算法属于群体智能算法,拥有此类算法的所有优势。在每一次迭代时都只需要经过位置、亮度更新等操作,没有繁琐的步骤、参数少且基本不影响算法求解,因而实现较为简单。局部搜索能力强,可以精准定位局部最优解。但是和其它算法一样,FA算法也存在一些缺点,如个体在峰值附近容易发生振荡现象、过于依赖优秀个体导致收敛速度较慢。

    在这里插入图片描述
    2 线性规划算例
    2.1算例
    在这里插入图片描述
    2.2算例答案
    在这里插入图片描述
    3 萤火虫算法求解结果

    1)迭代曲线

    在这里插入图片描述
    2)求解答案
    在这里插入图片描述

    4 matlab程序
    1)主函数

    %% 基于萤火虫算法的线性规划求解
    
    
    clc;
    clear;
    close all;
    
    %% Problem Definition
    CostFunction = @(x) Rosenbrock(x);        % Cost Function
    nVar = 3;                 % 变量个数
    VarSize = [1 nVar];       % Decision Variables Matrix Size
    %变量上下限
    VarMin = 0;             % Decision Variables Lower Bound
    VarMax = 15;             % Decision Variables Upper Bound
    
    %% Firefly Algorithm Parameters
    
    MaxIt = 300;         % Maximum Number of Iterations
    nPop = 25;            % Number of Fireflies (Swarm Size)
    gamma = 1;            % Light Absorption Coefficient
    beta0 = 2;            % Attraction Coefficient Base Value
    alpha = 0.2;          % Mutation Coefficient
    alpha_damp = 0.98;    % Mutation Coefficient Damping Ratio
    delta = 0.05*(VarMax-VarMin);     % Uniform Mutation Range
    m = 2;
    if isscalar(VarMin) && isscalar(VarMax)
        dmax = (VarMax-VarMin)*sqrt(nVar);
    else
        dmax = norm(VarMax-VarMin);
    end
    
    %% Initialization
    % Empty Firefly Structure
    firefly.Position = [];
    firefly.Cost = [];
    % Initialize Population Array
    pop = repmat(firefly, nPop, 1);
    % Initialize Best Solution Ever Found
    BestSol.Cost = inf;
    % Create Initial Fireflies
    for i = 1:nPop
       pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
       pop(i).Cost = CostFunction(pop(i).Position);
       
       if pop(i).Cost <= BestSol.Cost
           BestSol = pop(i);
       end
    end
    % Array to Hold Best Cost Values
    BestCost = zeros(MaxIt, 1);
    
    %% Firefly Algorithm Main Loop
    for it = 1:MaxIt    
        newpop = repmat(firefly, nPop, 1);
        for i = 1:nPop
            newpop(i).Cost = inf;
            for j = 1:nPop
                if pop(j).Cost < pop(i).Cost
                    rij = norm(pop(i).Position-pop(j).Position)/dmax;
                    beta = beta0*exp(-gamma*rij^m);
                    e = delta*unifrnd(-1, +1, VarSize);
                    %e = delta*randn(VarSize);
                    
                    newsol.Position = pop(i).Position ...
                                    + beta*rand(VarSize).*(pop(j).Position-pop(i).Position) ...
                                    + alpha*e;
                    
                    newsol.Position = max(newsol.Position, VarMin);
                    newsol.Position = min(newsol.Position, VarMax);
                    
                    newsol.Cost = CostFunction(newsol.Position);
                    
                    if newsol.Cost <= newpop(i).Cost
                        newpop(i) = newsol;
                        if newpop(i).Cost <= BestSol.Cost
                            BestSol = newpop(i);
                        end
                    end
                    
                end
            end
        end
        
        % Merge
        pop = [pop
             newpop];  %#ok    
        % Sort
        [~, SortOrder] = sort([pop.Cost]);
        pop = pop(SortOrder);    
        % Truncate
        pop = pop(1:nPop);    
        % Store Best Cost Ever Found
        BestCost(it) = BestSol.Cost;    
        % Show Iteration Information
        disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);    
        % Damp Mutation Coefficient
        alpha = alpha*alpha_damp;
        
    end
    
    %% Results
    figure;
    plot(BestCost, 'LineWidth', 2);
    % semilogy(BestCost, 'LineWidth', 2);
    xlabel('Iteration');
    ylabel('Best Cost');
    
    disp('输出结果')
    disp('最优变量')
    newsol.Position
    disp('最优结果')
    BestSol.Cost
    
    。。。。。略
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    4 完整matlab程序链接
    https://mp.weixin.qq.com/s?__biz=Mzg4MTc1MjE2Mg==&mid=2247484487&idx=1&sn=4b7d31c2fbb1f39472335d38ba93b616&chksm=cf60654af817ec5c1e14ac7cbb44b079b4c137b180a31636e92c38b5d2291ca8d91cbd1d3ff5&token=790532862&lang=zh_CN#rd

    在这里插入图片描述

  • 相关阅读:
    Tesseract .Net SDK C# OCR 2022.1
    Linux 系统监控与性能调优
    DL Homework 4
    性能优化之懒加载 - 基于观察者模式和单例模式的实现
    四旋翼飞行器控制和路径规划附Matlab代码
    【Android】Bugly使用
    利用Excel制作库房管理系统
    ALEXNET论文及其复现代码
    MetaObjectHandler的使用
    CKKS同态加密方案初步学习
  • 原文地址:https://blog.csdn.net/weixin_47365903/article/details/126382066