萤火虫算法

目录

1、简介:

2、算法的假设

3、算法实现


 

 

                                                                                                未足临书卷,时能点客衣。

                                                                                                随风隔幔小,带雨傍林微。

                                                                                                                              ----《萤火》 杜甫

1、简介:

参考论文《Firefly Algorithms for Multimodal Optimization

萤火虫算法是一种生物启发算法。是观察自然界中萤火虫相互吸引的现象,抽象得到的一种算法。在文章中所用的目标函数上,表现优于遗传算法和粒子群算法。

2、算法的假设

1、萤火虫不区分性别,一只萤火虫会被其他萤火虫吸引;  
2、假设吸引力与它们的亮度成正比,所以萤火虫会朝着更亮的萤火虫的方向移动,同时它们之间的距离减少。那如果没有比它更亮的萤火虫,则随机移动;  
3、 萤火虫的亮度影响或决定目标函数的值。对于求max问题,可以简单地假设亮度与目标函数的值成比例。也就是说亮度可以用遗传算法适应度函数算法类似的方式定义。
 

3、算法实现

 
 

# -*- coding: utf-8 -*-
# @Use     : 萤火虫算法,用于搜索最优解
# @Time    : 2022/5/26 23:01
# @FileName: firefly_algorithm.py
# @Software: PyCharm

import numpy as np
import random
from math import exp


class FireflyAlgorithmConfig:
    """
    萤火虫算法配置
    """

    def __init__(self):
        self.objective_dim = 10  # 目标参数的维度
        self.objective_dtype = np.int  # 目标的类型
        self.population_size = 25  # 种群中萤火虫数目
        self.gamma = 1  # 光吸收参数,一般取[0.01, 100]
        self.beta_0 = 1  # attractiveness 吸引度
        self.alpha = 0.2  # [0, 1]
        self.max_generation = 100  # 最大世代
        self.betas = self.__init_beta(self.beta_0, self.population_size)

    @staticmethod
    def __init_beta(beta_0, population_size):
        """
        初始化beta
        """
        betas = []
        for i in range(population_size):
            beta = []
            for j in range(population_size):
                beta.append(beta_0)
            betas.append(beta)
        return betas


class FireflyIndividual:
    """
    萤火虫
    """

    def __init__(self, x, intensity):
        self.x = np.array(x)
        self.intensity = intensity


def objective_fun(x: np.array):
    """
    目标函数
    """
    intensity = 0

    return intensity


def init_population(population_size, objective_dim, objective_dtype):
    """
    生成初始萤火虫种群
    """
    fireflies = []
    for i in range(population_size):
        init_intensity = 0
        init_x = np.zeros(objective_dim, dtype=objective_dtype)
        firefly = FireflyIndividual(x=init_x, intensity=init_intensity)
        fireflies.append(firefly)
    return fireflies


def iter_fireflies(config: FireflyAlgorithmConfig, fireflies):
    """
    迭代萤火虫种群
    """
    beta_0 = config.beta_0
    gamma = config.gamma
    alpha = config.alpha
    betas = config.betas

    t = 0
    while t < config.max_generation:
        for i in range(len(fireflies)):
            for j in range(0, i):
                if fireflies[j].intensity > fireflies[i].intensity:
                    # Move firefly i towards j in d-dimension
                    xi = fireflies[i].intensity
                    xj = fireflies[j].intensity
                    rand = random.random((0, 1))
                    rij = np.sqrt(np.sum(np.square(xi - xj)))
                    fireflies[i].x = xi + beta_0 * exp(-gamma * np.square(rij)) * (xj - xi) + alpha * (rand - 0.5)
                    # Attractiveness varies with distance r via exp[−γr]
                    betas[i][j] *= exp(-gamma * np.square(rij))
                    # Evaluate new solutions and update light intensity
                    fireflies[i].intensity = objective_fun(fireflies[i].x)
        # Rank the fireflies and find the current best
        t += 1
    sorted_firefies = sorted(fireflies, key=lambda firefly: -firefly.intensity)
    postprocess()
    return objective_fun(sorted_firefies[0])


def postprocess():
    """
    萤火虫算法后处理
    """


def visualization():
    """
    萤火虫算法可视化
    """


if __name__ == '__main_':
    g_config = FireflyAlgorithmConfig()
    g_fireflies = init_population(g_config.population_size,
                                  g_config.objective_dim,
                                  g_config.objective_dtype)
    result = iter_fireflies(g_config, g_fireflies)

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KPer_Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值