作业:今天以自由探索的思路为主,尝试检索资料、视频、文档,用尽可能简短但是清晰的语言看是否能说清楚这三种算法每种算法的实现逻辑,帮助更深入的理解。
1. 遗传算法 (Genetic Algorithm, GA)
def genetic_algorithm():
population = initialize_population()
while not convergence:
fitness = evaluate(population)
new_population = []
for _ in range(pop_size//2):
parent1, parent2 = select_parents(population, fitness)
child1, child2 = crossover(parent1, parent2)
child1 = mutate(child1)
child2 = mutate(child2)
new_population.extend([child1, child2])
population = replace(population, new_population)
return best_individual
核心逻辑:
-
仿生机制:染色体编码、自然选择、基因重组
-
关键操作:适应度评估 → 选择压力 → 交叉创造多样性 → 变异防早熟
-
适用场景:组合优化、参数搜索(如特征选择)
2. 粒子群算法 (Particle Swarm Optimization, PSO)
def pso():
swarm = initialize_swarm()
global_best = find_global_best(swarm)
for each particle in swarm:
particle.personal_best = particle.position
while not convergence:
for each particle in swarm:
velocity = inertia*velocity
+ c1*rand()*(personal_best - position)
+ c2*rand()*(global_best - position)
position += velocity
if fitness(position) > fitness(personal_best):
personal_best = position
if fitness(position) > fitness(global_best):
global_best = position
return global_best
核心逻辑:
-
群体智能:粒子通过个体经验和群体共享信息协同搜索
-
关键公式:速度更新三要素(惯性保持、自我记忆、社会学习)
-
适用场景:连续空间优化(如神经网络超参调优)
3. 模拟退火算法 (Simulated Annealing, SA)
def simulated_annealing():
current_solution = initialize()
current_energy = evaluate(current_solution)
best_solution = current_solution
T = initial_temperature
while T > final_temperature:
for _ in range(num_iter_per_T):
new_solution = perturb(current_solution)
new_energy = evaluate(new_solution)
delta = new_energy - current_energy
if delta < 0 or random() < exp(-delta/T):
current_solution = new_solution
current_energy = new_energy
if current_energy < best_solution.energy:
best_solution = current_solution
T = cool(T)
return best_solution
核心逻辑:
-
物理隐喻:模仿金属退火过程的热力学平衡
-
核心策略:温度控制下的概率性劣解接受(避免局部最优)
-
适用场景:单解迭代优化(如VLSI电路布局)
关键对比维度
算法 | 搜索方式 | 核心操作 | 参数敏感性 | 并行性 |
---|---|---|---|---|
GA | 种群进化 | 交叉/变异/选择 | 高(交叉率等) | 强(种群) |
PSO | 群体协作 | 速度-位置更新 | 中(c1,c2) | 中 |
SA | 单解扰动 | 温度衰减/邻域生成 | 高(降温策略) | 弱 |
理解要点:
-
GA:通过基因重组探索解空间,强调多样性
-
PSO:粒子间信息共享加速收敛,强调群体协作
-
SA:通过概率跳脱局部最优,强调逐步精细化搜索