
目录
一、生物启发的优化范式
1.1 遗传算法的数学本质
轮盘赌选择概率公式:

其中表示个体ii的适应度,
为种群大小
# 遗传算法核心操作实现
import numpy as np
def roulette_selection(population, fitness, num_parents):
"""轮盘赌选择"""
probs = fitness / np.sum(fitness)
selected_indices = np.random.choice(len(population), num_parents, p=probs, replace=False)
return population[selected_indices]
def crossover(parents, offspring_size):
"""单点交叉"""
offspring = np.empty(offspring_size)
crossover_point = np.random.randint(1, offspring_size[1])
for k in range(offspring_size[0]):
parent1_idx = k % parents.shape[0]
parent2_idx = (k+1) % parents.shape[0]
offspring[k, :crossover_point] = parents[parent1_idx, :crossover_point]
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring, mutation_rate=0.01):
"""高斯噪声变异"""
mask = np.random.rand(*offspring.shape) < mutation_rate
noise = np.random.normal(0, 0.1, offspring.shape)
return offspring + mask * noise
1.2 进化策略的梯度近似
自然进化策略(NES)的梯度估计:

# 进化策略的PyTorch实现
import torch
class EvolutionStrategy:
def __init__(self, model, population_size=50, sigma=0.1, lr=0.01):
self.model = model
self.pop_size = population_size
self.sigma = sigma
self.lr = lr
def generate_perturbations(self):
return [torch.randn_like(p) for p in self.model.parameters()]
def update(self, rewards):
grad_estimate = []
for param in self.model.parameters():
epsilons = torch.stack([p for p, _ in self.perturbations])
r = torch.tensor(rewards, dtype=torch.float32)
update = (r @ epsilons) / (self.pop_size * self.sigma)
grad_estimate.append(update)
# 应用梯度更新

最低0.47元/天 解锁文章

5万+

被折叠的 条评论
为什么被折叠?



