#56
启发式算法学习SPO
Champ2024.11.21 00:00created at 2024.11.21 00:00updated at 2024.11.23 00:00
0 次阅读

启发式算法学习
启发式算法:遍地撒网,重点捞鱼
启发式算法是一类通过经验和直觉来寻找问题的“足够好”解的算法,尤其适用于无法通过精确方法高效求解的问题。它们广泛应用于优化、搜索以及机器学习等领域 组合优化问题:如旅行商问题 (TSP)、背包问题。<br> 路径规划:如机器人导航、地图寻路。<br> 机器学习:用于超参数优化或特征选择。<br> 工程设计:如电路设计、图像处理。
粒子群算法SPO(基于群体智能)
一群粒子位置随机,探索方向随机,具备自身的运动惯性,具备共同目标(最优解)<br> 时刻更新个体最优解和群体最优解,信息共享,各粒子在个体最优解和已知群体最优解的共同影响下,朝特定方向移动,最终大部分粒子趋于群体最优解(不一定是全局最优解)<br> eg:鸟群觅食,王者荣耀<br>
import numpy as np
# 定义优化目标函数(这里以Rosenbrock函数为例)
def objective_function(x):
return sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
# 粒子群优化(PSO)算法
class Particle:
def __init__(self, dim, bounds):#初始化 d
self.position = np.random.uniform(bounds[0], bounds[1], dim)
self.velocity = np.random.uniform(-1, 1, dim)
self.best_position = np.copy(self.position)
self.best_score = float('inf')
self.score = float('inf')
def update_velocity(self, global_best_position, w, c1, c2):#更新速度
r1 = np.random.rand(self.position.shape[0])#随机生成r1,r2
r2 = np.random.rand(self.position.shape[0])
cognitive = c1 * r1 * (self.best_position - self.position)
social = c2 * r2 * (global_best_position - self.position)
self.velocity = w * self.velocity + cognitive + social
def update_position(self, bounds):
self.position += self.velocity
self.position = np.clip(self.position, bounds[0], bounds[1])
def pso(objective_function, dim, bounds, num_particles, max_iter, w=0.5, c1=1.5, c2=1.5):
# 初始化粒子群
particles = [Particle(dim, bounds) for _ in range(num_particles)]
global_best_position = None
global_best_score = float('inf')
# 主循环
for iteration in range(max_iter):
for particle in particles:
particle.score = objective_function(particle.position)
# 更新个体最优
if particle.score < particle.best_score:
particle.best_score = particle.score
particle.best_position = np.copy(particle.position)
# 更新全局最优
if particle.score < global_best_score:
global_best_score = particle.score
global_best_position = np.copy(particle.position)
# 更新粒子速度和位置
for particle in particles:
particle.update_velocity(global_best_position, w, c1, c2)
particle.update_position(bounds)
# 输出当前最优解
print(f"Iteration {iteration+1}/{max_iter}, Best Score: {global_best_score}")
return global_best_position, global_best_score
# 参数设置
dim = 2 # 问题的维度
bounds = [-5, 5] # 搜索范围
num_particles = 30 # 粒子数量
max_iter = 100 # 最大迭代次数
# 运行PSO算法
best_position, best_score = pso(objective_function, dim, bounds, num_particles, max_iter)
print(f"Global Best Position: {best_position}")
print(f"Global Best Score: {best_score}")
避免局部最优:动态修改惯性权重,避免在局部跳不出来;模拟退火,接受较差解;
模拟退火算法(基于局部搜索)
模拟退火算法(Simulated Annealing, SA)是一种基于随机搜索的全局优化算法,受到物理退火过程的启发。其核心思想是通过模拟金属退火过程中的加热和缓慢冷却,逐步接近全局最优解。 常用于:<br> 1.组合优化问题:如旅行商问题(TSP)、装箱问题等。<br> 2.机器学习:神经网络的超参数优化等。<br> 3.工程设计:如电路设计、机械优化等。<br>
import math
import random
# 距离矩阵
distance_matrix = [
[0, 10, 15, 20, 25, 30],
[10, 0, 35, 25, 17, 28],
[15, 35, 0, 30, 40, 50],
[20, 25, 30, 0, 22, 35],
[25, 17, 40, 22, 0, 16],
[30, 28, 50, 35, 16, 0]
]
# 计算路径的总距离
def calculate_total_distance(path, matrix):
total_distance = 0
for i in range(len(path) - 1):
total_distance += matrix[path[i]][path[i + 1]]
total_distance += matrix[path[-1]][path[0]] # 回到起点
return total_distance
# 模拟退火算法
def simulated_annealing(matrix, initial_temp, cooling_rate, max_iter):
num_cities = len(matrix)
# 初始化路径
current_path = list(range(num_cities))
random.shuffle(current_path)
current_distance = calculate_total_distance(current_path, matrix)
best_path = current_path[:]
best_distance = current_distance
temperature = initial_temp
for iteration in range(max_iter):
# 生成邻居解(交换路径中的两个城市)
new_path = current_path[:]
i, j = random.sample(range(num_cities), 2)
new_path[i], new_path[j] = new_path[j], new_path[i]
new_distance = calculate_total_distance(new_path, matrix)
# 判断是否接受新解
if new_distance < current_distance or \
random.random() < math.exp((current_distance - new_distance) / temperature):#math.exp->返回e的()次幂
current_path = new_path
current_distance = new_distance
# 更新最优解
if current_distance < best_distance:
best_path = current_path
best_distance = current_distance
# 降温
temperature *= cooling_rate
# 输出当前迭代信息(可选)
if iteration % 100 == 0 or iteration == max_iter - 1:
print(f"Iteration {iteration}: Best Distance = {best_distance}")
return best_path, best_distance
# 参数设置
initial_temperature = 1000 # 初始温度
cooling_rate = 0.995 # 降温速率
max_iterations = 10000 # 最大迭代次数
# 运行算法
best_path, best_distance = simulated_annealing(distance_matrix, initial_temperature, cooling_rate, max_iterations)
# 输出结果
print("\nOptimal Path:", best_path)
print("Optimal Distance:", best_distance)
