# 萤火虫算法

萤火虫算法（firefly algorithm, FA）是一种受自然启发的元启发式方法（用于为困难优化问题寻找优良解的通用搜索框架），它通过模拟一群闪烁的萤火虫来搜索目标函数的最优值，搜索空间中的每个位置都是一个候选解。它面向工程与应用数学中的连续、离散和多目标优化问题，与粒子群优化（PSO）同属群体智能方法家族，常被与之比较。

| 关键事实 | 详情 |
|---|---|
| 问题类型 | 无约束与有约束函数优化；存在连续、离散和多目标变体 <sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> |
| 核心更新式 | \( \mathbf{x}_{i}^{t+1} = \mathbf{x}_{i}^{t} + \beta_{0} e^{-\gamma r_{ij}^{2}} (\mathbf{x}_{j}^{t} - \mathbf{x}_{i}^{t}) + \alpha\, \boldsymbol{\epsilon}_{i}^{t} \) <sup>[2](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)</sup> |
| 典型参数 | \( \alpha_{0} = 0.01L \)，冷却因子 \( \delta = 0.95 \) 至 0.97，\( \beta_{0} = 1 \)，\( \gamma = 1/\sqrt{L} \)，种群规模 \( n = 15 \) 至 100（最佳 25 至 40）<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> |
| 复杂度 | 最坏情况下为 \( O(n^{2} t) \)，采用基于排序的名次评定可降至 \( O(n t \log n) \) <sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> |
| 主要弱点 | 早熟收敛、参数调优，以及一篇已发表的批评意见，指其极限情形不含算法上的新意 <sup>[3](https://thesai.org/Downloads/Volume14No3/Paper_50-Scouting_Firefly_Algorithm_and_its_Performance.pdf)</sup><sup> • </sup><sup>[4](https://iridia.ulb.ac.be/~ccamacho/publications/ANTS2020-paper.pdf)</sup> |

## 工作原理

萤火虫算法将萤火虫的闪烁行为理想化为三条规则：所有萤火虫不分性别，因此任何萤火虫都可以被任何其他萤火虫吸引；吸引力与光强成正比，而光强由目标函数值 \( f(\mathbf{x}_{i}) \) 决定；光强随距离衰减。在物理上，强度随距光源的距离 \( r \) 按 \( I/r^{2} \) 衰减，空气吸收会使其进一步减弱；算法将这两种效应合并为吸引力随距离的单一衰减。<sup>[5](https://www.matjazperc.com/publications/ApplMathComput_252_155.pdf)</sup> 在伪代码中，\( \mathbf{x}_{i} \) 处的光强 \( I_{i} \) 由 \( f(\mathbf{x}_{i}) \) 设定，萤火虫 \( i \) 在全部 \( d \) 个维度上向任何更亮的萤火虫 \( j \) 移动，吸引力通过 \( \exp[-\gamma r^{2}] \) 随距离 \( r \) 变化。<sup>[6](https://ar5iv.labs.arxiv.org/html/1003.1409)</sup>

由此得到的位置更新式为

\[ \mathbf{x}_{i}^{t+1} = \mathbf{x}_{i}^{t} + \beta_{0} e^{-\gamma r_{ij}^{2}} (\mathbf{x}_{j}^{t} - \mathbf{x}_{i}^{t}) + \alpha\, \boldsymbol{\epsilon}_{i}^{t}, \]

其中第二项为吸引力项，第三项为随机化项，随机向量 \( \boldsymbol{\epsilon}_{i} \) 每次迭代均从高斯分布中抽取。<sup>[6](https://ar5iv.labs.arxiv.org/html/1003.1409)</sup> 参数 \( \alpha \) 是控制随机游走步长的缩放因子，\( \gamma \) 是依赖尺度的参数，控制萤火虫的可见度从而控制搜索模式，\( \beta_{0} \) 是两只萤火虫之间距离为零时的吸引力。<sup>[2](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)</sup> 在大多数实现中，随机化强度按 \( \alpha = \alpha_{0} \theta^{t} \) 退火。<sup>[7](https://arxiv.org/pdf/2407.02537)</sup>

## 实施方法

每次迭代中，每只萤火虫都与其他所有萤火虫比较；若 \( j \) 比 \( i \) 更亮，则萤火虫 \( i \) 用上述更新式向 \( j \) 移动，随后对种群重新评估。由于存在两个关于种群 \( n \) 的内层循环和一个关于迭代次数 \( t \) 的外层循环，最坏情况复杂度为 \( O(n^{2} t) \)，采用基于排序的名次评定可降至 \( O(n t \log n) \)。<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup>

已发表的参数指导相当一致。设 \( L \) 为问题的平均尺度，初始取 \( \alpha_{0} = 0.01L \)，冷却因子 \( \delta = 0.95 \) 至 0.97，大多数应用取 \( \beta_{0} = 1 \)，并设 \( \gamma = 1/\sqrt{L} \)；种群规模 \( n = 15 \) 至 100 可行，最佳范围为 \( n = 25 \) 至 40。<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> 理论上 \( \gamma \in [0, \infty) \)，但实践中 \( \gamma = O(1) \)，对大多数问题可在 0.001 至 1000 范围内使用，尺度未知时 \( \gamma = 1 \) 是合理的起点；\( \alpha \) 必须逐渐减小，否则过多的随机性会拖慢收敛。<sup>[2](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)</sup> 对于多目标变体，参数研究发现 \( \alpha_{0} = 0.1 \) 至 0.5、\( \beta_{0} = 0.7 \) 至 1.0、\( \gamma = 1 \) 对大多数问题有效，推荐设置为 \( \alpha = 0.01L \) 和 \( \gamma = 0.5/L^{2} \)。<sup>[8](https://ar5iv.labs.arxiv.org/html/1303.6336)</sup>

## 起源

萤火虫算法由杨新社（Xin-She Yang）在《Firefly Algorithms for Multimodal Optimization》一文中提出，该文于 2009 年发表在《Lecture Notes in Computer Science》（doi:10.1007/978-3-642-04944-6_14）。<sup>[9](https://doi.org/10.48550/arxiv.1003.1466)</sup> 从形式上看，该方法与早期的群体算法和进化算法密切相关：当 \( \gamma = 0 \)、\( \alpha = 0 \) 且 \( \beta_{0} \) 固定时，FA 变为无交叉的[差分进化](https://www.edgepedia.org/zh/differential-evolution)变体；将 \( \mathbf{x}_{j} \) 替换为最优解 \( g^{*} \) 后，它等价于加速 PSO 的一个特例；而当 \( \beta_{0} = 0 \) 时，它变为简单的随机游走，由 \( \alpha \) 控制步长。<sup>[2](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)</sup>

## 变体

由于标准 FA 是为连续域设计的，离散与组合问题需要修改；针对旅行商问题、图着色和 NP 难调度问题已开发出离散 FA 版本。<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> 综述文献描述的其他变体家族包括：自适应调整自身参数的自适应 FA、采用新移动机制的改进或增强 FA、利用混沌来调节参数并提升性能的混沌 FA，以及与其他算法结合的混合 FA。<sup>[2](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)</sup><sup> • </sup><sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup>

对于多目标问题，杨将标准 FA 扩展为多目标萤火虫算法（MOFA），直接生成帕累托最优前沿，已在多目标测试函数上验证，并应用于双目标梁设计和盘式制动器设计。<sup>[10](https://doi.org/10.48550/arxiv.1303.6336)</sup>

## 应用

已报告的应用领域涵盖：工程设计，FA 能高效求解高度非线性、多模态的设计问题；数字图像压缩，一种基于萤火虫的算法被证明计算时间最少；特征选择，FA 表现出一致且更优的性能；天线设计，一些研究中 FA 可胜过人工蜂群和 PSO；以及分类与聚类。<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup>

## 局限与替代方案

**早熟收敛**是记录最多的失效模式。在一项针对全局最优为 \( \mathbf{x} = (0,0) \) 处 \( f(\mathbf{x}) = 0 \) 的问题的比较中，标准 FA 在早期迭代中过早收敛到接近 0.20 的适应度值，而一种侦察 FA（Scouting FA）变体避免了早熟收敛，在刚过 50 次迭代时就逼近了全局最优。<sup>[3](https://thesai.org/Downloads/Volume14No3/Paper_50-Scouting_Firefly_Algorithm_and_its_Performance.pdf)</sup>

基准证据不一。杨自己的基准报告了大幅节省：为在一个函数上达到 \( 10^{-5} \) 的精度，GA 需要 \( 25412 \pm 1237 \) 次评估，PSO 需要 \( 17040 \pm 1123 \) 次，而 FA 以 \( 5657 \pm 730 \) 次达到，节省了约 78% 和 67% 的计算成本；在杨的 16 维森林函数上，FA 以 \( 5152 \pm 2493 \) 次评估取得 100% 成功率，而 GA 为 \( 37079 \pm 8920 \) 次（88% 成功率），PSO 为 \( 19725 \pm 3204 \) 次（98%）。<sup>[1](https://arxiv.org/pdf/1308.3898v1.pdf)</sup> 另一方面，一项独立研究在 10、30、50 和 100 维下考察 CEC2014 中由 30 个基准函数组成的套件，发现人工蜂群在高维下对多数问题表现最佳，[布谷鸟搜索](https://www.edgepedia.org/zh/cuckoo-search)在小维度下最佳，FPA 次之，随后是 BA 和 FA，FA 在五种受测算法中排名最后。<sup>[11](https://link.springer.com/article/10.1007/s10462-017-9547-5)</sup> 注意该研究未纳入 DE 或 GWO，因此没有与这两种算法对比的有据可查的 CEC 结果。在计算效率方面，一项比较发现 APSO 是最快的算法，FA 紧随其后，其成本与 SPSO 相近；自适应 FA 版本因新采用的机制都比标准 FA 慢。<sup>[12](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0112634)</sup>

一项结构性批评认为 FA 不含算法上的新意：当 \( \gamma \to \infty \) 时，吸引力项趋于零，新解只能由随机向量产生，FA 退化为纯随机搜索；而相反的极限情形对应光强不衰减，于是“在域内任何地方都能看到萤火虫”。<sup>[4](https://iridia.ulb.ac.be/~ccamacho/publications/ANTS2020-paper.pdf)</sup>

2023 年末以来的工作集中于变体和混合方法，而非对这些批评的定论性解决：2025 年《Scientific Reports》的一篇论文提出了一种基于性别差异、具有多重学习能力的 FA，称 FA 在非线性和多模态问题上优于 GA 和 PSO，并编录了近期的混合化工作，包括 FA 与 PSO 结合、FA 与[量子遗传算法](https://www.edgepedia.org/zh/quantum-genetic-algorithm)结合，以及 FA 与深度学习策略结合。<sup>[13](https://www.nature.com/articles/s41598-025-09523-9)</sup>

## References

1. [Firefly Algorithm: Recent Advances and Applications (Yang and He, 2013)](https://arxiv.org/pdf/1308.3898v1.pdf)
2. [Why the Firefly Algorithm Works?](https://link.springer.com/chapter/10.1007/978-3-319-67669-2_11)
3. [Scouting Firefly Algorithm and its Performance on Global Optimization Problems (IJACSA, 2023)](https://thesai.org/Downloads/Volume14No3/Paper_50-Scouting_Firefly_Algorithm_and_its_Performance.pdf)
4. [Grey Wolf, Firefly and Bat Algorithms: Three Widespread Algorithms that Do Not Contain Any Novelty (ANTS 2020)](https://iridia.ulb.ac.be/~ccamacho/publications/ANTS2020-paper.pdf)
5. [A review of chaos-based firefly algorithms: Perspectives and research challenges (Applied Mathematics and Computation 252, 155; author-site copy)](https://www.matjazperc.com/publications/ApplMathComput_252_155.pdf)
6. [Firefly Algorithm, Stochastic Test Functions and Design Optimisation (Yang, 2010)](https://ar5iv.labs.arxiv.org/html/1003.1409)
7. [Parameter Tuning of the Firefly Algorithm by Standard Monte Carlo and Quasi-Monte Carlo Methods (2024)](https://arxiv.org/pdf/2407.02537)
8. [Multiobjective Firefly Algorithm for Continuous Optimization (Yang, 2013)](https://ar5iv.labs.arxiv.org/html/1303.6336)
9. [Yang, Xin-She (2010). Firefly Algorithms for Multimodal Optimization. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1003.1466)
10. [Yang, Xin-She (2013). Multiobjective Firefly Algorithm for Continuous Optimization. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1303.6336)
11. [Empirical analysis of five nature-inspired algorithms on real parameter optimization problems (Artificial Intelligence Review)](https://link.springer.com/article/10.1007/s10462-017-9547-5)
12. [Adaptive Firefly Algorithm: Parameter Analysis and its Application (PLOS One)](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0112634)
13. [Firefly algorithm with multiple learning ability based on gender difference | Scientific Reports](https://www.nature.com/articles/s41598-025-09523-9)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Optimization and dynamic programming › Swarm intelligence optimizers

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/firefly-algorithm

English original: https://www.edgepedia.org/firefly-algorithm
