寄生虫仿生算法:基于寄生虫特征的算法设计
基于寄生虫行为特征的仿生算法设计
import random
import numpy as npclass EnhancedPBOA:def __init__(self, host_env, max_generations, population_size=50):self.host_env = host_envself.max_generations = max_generationsself.population_size = population_sizeself.population = self.initialize_population()self.adaptive_mutation_rate = 0.2self.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 childdef 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 offspringdef 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 Falserecent = self.fitness_history[-5:]return np.std(recent) < 1e-4def 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.9self.host_env['max'] *= 1.1self.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.populationif 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()