Day 12 训练
Day 12 训练
- 1.遗传算法
- 2.粒子群优化(Particle Swarm Optimization, PSO)
- 3.模拟退火(Simulated Annealing, SA)
超参数调整专题2
1.三种启发式算法的示例代码:遗传算法、粒子群算法、退火算法
2.学习优化算法的思路(避免浪费无效时间)
作业:今天以自由探索的思路为主,尝试检索资料、视频、文档,用尽可能简短但是清晰的语言看是否能说清楚这三种算法每种算法的实现逻辑,帮助更深入的理解。
1.遗传算法
遗传算法(Genetic Algorithm, GA)是一种模拟生物自然选择和遗传机制的优化算法。它通过维护一组潜在解(称为群体或种群),利用选择、交叉和变异等操作逐代进化,最终找到问题的最优或近似最优解。遗传算法适用于搜索空间大、复杂或非线性的问题,尤其在传统优化方法难以应用时表现良好。
基本步骤包括:
初始化:随机生成一组个体(解)。
评估:用适应度函数评估每个个体的优劣。
选择:根据适应度选择优良的个体作为父代。
交叉:对父代进行组合,生成新的后代。
变异:对部分后代随机变异,增加多样性。
替换:用新一代替换旧一代,重复上述步骤直到满足条件。
在scikit-learn中,没有直接的遗传算法实现,但可以结合sklearn的模型与deap或其他遗传算法库进行优化。以下示例演示使用deap库对一个简单模型(比如线性回归的正则化参数)进行遗传算法优化。
import numpy as np
from sklearn.datasets import make_regression
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
from deap import base, creator, tools, algorithms# 生成一些回归数据
X, y = make_regression(n_samples=200, n_features=1, noise=10, random_state=42)# 定义适应度函数:最小化负的交叉验证得分(即最大化得分)
def eval_ridge(alpha):alpha = alpha[0]model = Ridge(alpha=alpha)scores = cross_val_score(model, X, y, cv=5, scoring='neg_mean_squared_error')return (scores.mean(),)# 设置遗传算法参数
creator.create("FitnessMax", base.Fitness, weights=(1.0,)) # 目标最大化
creator.create("Individual", list, fitness=creator.FitnessMax)toolbox = base.Toolbox()
# 定义个体编码:正则化参数alpha在[0.0, 10.0]
toolbox.register("attr_alpha", np.random.uniform, 0.0, 10.0)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_alpha, n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)# 遗传操作
toolbox.register("evaluate", eval_ridge)
toolbox.register("mate", tools.cxUniform, indpb=0.5)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=2, indpb=1)
toolbox.register("select", tools.selBest)# 运行遗传算法
population = toolbox.population(n=20)
NGEN = 30
for gen in range(NGEN):offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.2)fits = list(map(toolbox.evaluate, offspring))for ind, fit in zip(offspring, fits):ind.fitness.values = fitpopulation = toolbox.select(offspring, k=len(population))# 找到最优解
best_ind = tools.selBest(population, 1)[0]
print(f"最优正则化参数alpha: {best_ind[0]:.4f}")
2.粒子群优化(Particle Swarm Optimization, PSO)
是一种模拟鸟群、鱼群等群体在寻找食物或导航时的协作行为的优化算法。它通过一群“粒子”在搜索空间中不断移动,并根据自身和同伴的经验调整位置,以找到问题的最优解。
基本思想:
每个粒子代表一个潜在解,具有位置和速度。
粒子会受到自己历史最优位置和全体粒子全局最优位置的影响,调整自己的速度和位置。
经过多轮迭代,粒子逐渐集中在最优区域。
简单步骤:
初始化:在搜索空间随机生成一群粒子。
评估:计算每个粒子的位置对应的目标函数值。
更新:每个粒子根据自己和邻居的最佳经验调整速度和位置。
重复:直到达到最大迭代次数或满足停止条件。
以下是使用Python实现的一个简单的粒子群优化示例,优化一个简单的数值函数(例如:最小化函数f(x) = x^2):
import numpy as np# 定义目标函数:我们要找到使f(x)最小的x
def fitness(x):return x**2# 粒子参数
num_particles = 30
max_iterations = 100
dimension = 1 # 一维问题# 初始化粒子位置和速度
positions = np.random.uniform(-10, 10, (num_particles, dimension))
velocities = np.random.uniform(-1, 1, (num_particles, dimension))# 初始化个体最优(记录每个粒子的最佳位置和对应的适应度)
personal_best_positions = positions.copy()
personal_best_scores = fitness(positions.reshape(-1))# 初始化全局最优
best_idx = np.argmin(personal_best_scores)
global_best_position = personal_best_positions[best_idx].copy()
global_best_score = personal_best_scores[best_idx]# PSO参数
w = 0.5 # 惯性权重
c1 = 1.0 # 个体学习因子
c2 = 2.0 # 社会学习因子for iteration in range(max_iterations):for i in range(num_particles):# 更新速度r1, r2 = np.random.rand(), np.random.rand()velocities[i] = (w * velocities[i] +c1 * r1 * (personal_best_positions[i] - positions[i]) +c2 * r2 * (global_best_position - positions[i]))# 更新位置positions[i] += velocities[i]# 计算适应度score = fitness(positions[i])# 更新个体最优if score < personal_best_scores[i]:personal_best_scores[i] = scorepersonal_best_positions[i] = positions[i].copy()# 更新全局最优current_best_idx = np.argmin(personal_best_scores)if personal_best_scores[current_best_idx] < global_best_score:global_best_score = personal_best_scores[current_best_idx]global_best_position = personal_best_positions[current_best_idx].copy()# 输出每轮的最优值(可选)# print(f"Iteration {iteration+1}: Best Score = {global_best_score}, Best Position = {global_best_position}")print(f"最优解:x = {global_best_position[0]}, f(x) = {global_best_score}")这是一个简单的一维优化示例,目标函数是f(x) = x^2,通过粒子群算法成功找到最小值(在0附近)。你可以扩展到多维问题或不同的目标函数,调整参数以获得更好的性能。
3.模拟退火(Simulated Annealing, SA)
是一种模拟物理中金属退火过程的随机优化算法。它通过模拟金属从高温慢慢冷却的方式,让系统逐步达到最低能量状态,从而找到问题的最优解。
基本思想:
从一个随机解开始,计算其目标函数值(能量)。
在每次迭代中,随机产生一个新解。
如果新解比旧解更优(能量更低),就接受它。
如果新解较差,则以一定概率接受它,这个概率随“温度”降低而逐渐变小,帮助算法跳出局部最优。
随着“温度”逐步降低,系统逐渐“冷却”到最优状态。
步骤:
初始化:选一个起始解和初始“温度”。
产生新解:在当前位置附近随机扰动。
判断:根据目标函数值和概率决定是否接受新解。
降温:逐步降低温度。
重复:直至达到终止条件(如最低温度或最大迭代数)。
下面是一个简单的模拟退火示例,优化函数f(x) = x^2,寻找最小值(理想在0附近):
import numpy as np# 目标函数:最小化的函数
def func(x):return x**2# 模拟退火参数
initial_temp = 100 # 初始温度
final_temp = 1e-3 # 终止温度
alpha = 0.9 # 每次降温比例
max_iter = 1000 # 最大迭代次数# 初始解
current_x = np.random.uniform(-10, 10)
current_score = func(current_x)
best_x = current_x
best_score = current_score
temp = initial_tempfor i in range(max_iter):# 产生新解:在当前点附近扰动new_x = current_x + np.random.uniform(-1, 1)new_score = func(new_x)# 以一定概率接受较差解delta = new_score - current_scoreif delta < 0 or np.random.rand() < np.exp(-delta / temp):current_x = new_xcurrent_score = new_score# 更新最优解if current_score < best_score:best_x = current_xbest_score = current_score# 降温temp *= alphaif temp < final_temp:breakprint(f"最优解:x = {best_x:.4f}, f(x) = {best_score:.4f}")
这段代码模拟了一个金属系统逐步冷却寻找最低能量状态的过程,通过随机扰动和概率接受较差解,帮助算法跳出局部最优,并最终获得接近全局最优的解。你可以根据具体问题调整参数和目标函数,使模拟退火适应不同的优化场景。
@浙大疏锦行