当前位置: 首页 > article >正文

寄生虫仿生算法:基于寄生虫特征的算法设计

寄生虫仿生算法:基于寄生虫特征的算法设计
基于寄生虫行为特征的仿生算法设计

import random
import numpy as np


class EnhancedPBOA:
    def __init__(self, host_env, max_generations, population_size=50):
        self.host_env = host_env
        self.max_generations = max_generations
        self.population_size = population_size
        self.population = self.initialize_population()
        self.adaptive_mutation_rate = 0.2
        self.fitness_history = []

    def initialize_population(self):
        return [self.create_individual() for _ in range(self.population_size)]

    def create_individual(self):
        return [random.uniform(self.host_env['min'], self.host_env['max'])
                for _ in range(self.host_env['dimension'])]

    def mutate(self, parent, rate):
        child = parent.copy()
        if random.random() < rate:
            idx = random.randint(0, len(child) - 1)
            delta = random.gauss(0, 0.1)
            child[idx] = np.clip(child[idx] + delta,
                                 self.host_env['min'],
                                 self.host_env['max'])
        return child

    def hyper_reproduction(self, feasible_solutions, mutation_rate):
        offspring = []
        for parent in feasible_solutions:
            for _ in range(3):  # 每个父代生成3个子代(模拟高繁殖率)
                child = self.mutate(parent, mutation_rate)
                offspring.append(child)
        return offspring

    def update_mutation_rate(self, gen):
        return max(0.05, 0.2 - (gen / self.max_generations) * 0.15)

    def stagnation_detected(self):
        if len(self.fitness_history) < 5:
            return False
        recent = self.fitness_history[-5:]
        return np.std(recent) < 1e-4

    def switch_host_domain(self):
        elite_size = int(0.1 * self.population_size)
        elites = sorted(self.population, key=lambda x: self.objective_function(x), reverse=True)[:elite_size]
        self.host_env['min'] *= 0.9
        self.host_env['max'] *= 1.1
        self.population = elites + [self.create_individual() for _ in range(self.population_size - elite_size)]

    def calculate_energy_efficiency(self, individual):
        return self.objective_function(individual) / self.resource_usage(individual)

    def select_by_efficiency(self, offspring, efficiencies):
        threshold = np.mean(efficiencies) - 0.5 * np.std(efficiencies)
        selected = [ind for ind, eff in zip(offspring, efficiencies) if eff >= threshold]
        return selected[:self.population_size]

    def objective_function(self, individual):
        return -np.sum([x ** 2 - 10 * np.cos(2 * np.pi * x) for x in individual])

    def resource_usage(self, individual):
        return len(individual)

    def evolve(self):
        for gen in range(self.max_generations):
            self.adaptive_mutation_rate = self.update_mutation_rate(gen)

            # 宿主适应与筛选
            feasible = [ind for ind in self.population
                        if all(self.host_env['min'] <= x <= self.host_env['max'] for x in ind)]

            # 繁殖扩散,子代生成
            offspring = self.hyper_reproduction(feasible, self.adaptive_mutation_rate)

            # 资源掠夺能效评估与选择
            efficiencies = [self.calculate_energy_efficiency(ind) for ind in offspring]
            self.population = self.select_by_efficiency(offspring, efficiencies)

            # 记录适应度历史
            current_best = max([self.objective_function(ind) for ind in self.population])
            self.fitness_history.append(current_best)

            # 宿主切换
            if self.stagnation_detected():
                self.switch_host_domain()


http://www.kler.cn/a/587099.html

相关文章:

  • 【论文笔记】FLARE:feed-forward+posegeometry estimate+GS
  • RK3588 编译 openssl
  • 备赛蓝桥杯-Python-Day1-基础语法回顾
  • RunningHub:瞄准图形音视频,做AIGC应用共创平台,它有何特点?
  • 面试vue2开发时怎么加载编译速度(webpack)
  • 整数分段c++
  • 【虚幻C++笔记】打印输出的方式
  • 游戏引擎学习第159天
  • 【电路笔记】-多谐振荡器
  • 基于gitea+act_runner 搭建CI/CD自动化部署
  • PostgreSQL 权限管理详解
  • nvm踩坑记录--nvm 切换node版本 node -v却不是切换的版本
  • FastDVDnet:不需要显示学习运动的实时视频降噪
  • 个人常用的chrome好用插件
  • C51点灯学习
  • Docker命令笔记
  • Kafka相关的面试题
  • C++类与对象——拷贝构造与运算符重载
  • Blender-MCP服务源码5-BlenderSocket插件安装
  • INSERT ... ON DUPLICATE KEY UPDATE