python打卡day12@浙大疏锦行

作业:今天以自由探索的思路为主,尝试检索资料、视频、文档,用尽可能简短但是清晰的语言看是否能说清楚这三种算法每种算法的实现逻辑,帮助更深入的理解。

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:通过概率跳脱局部最优,强调逐步精细化搜索

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值