1. 模拟退火算法简介
模拟退火(Simulated Annealing, SA)是一种基于随机搜索的全局优化算法,最初来自于物理学中的退火过程。它通过模拟金属冷却时原子排列逐渐趋于最低能量状态的过程来寻找问题的最优解。模拟退火算法常用于解决非线性、组合优化问题,特别适合于大规模、复杂的搜索空间。
模拟退火算法的核心思想是:在搜索过程中,允许一定的“劣解”被接受,从而避免陷入局部最优解。
2. 算法原理
模拟退火算法的基本过程可以概括为:
- 初始状态:随机生成一个解作为当前解。
- 温度控制:设置初始温度,在每次迭代中降低温度。
- 邻域搜索:在当前解的邻域中生成一个新解。
- 判断是否接受新解:
- 若新解优于当前解,则接受新解。
- 若新解劣于当前解,则以一定概率接受新解(概率与温度有关)。
- 终止条件:当温度降低到某个阈值或达到迭代次数时,算法停止,输出最优解。
2.1 退火过程
模拟退火的关键在于温度的变化。随着温度的降低,算法逐渐从全局搜索转为局部搜索,最终收敛到一个较优的解。
- 温度的初始值:通常根据问题的规模和复杂度设定。
- 降温策略:常见的降温策略有线性降温、指数降温等。常用的公式是:
T = T0 * α^k
,其中T0
是初始温度,α
是衰减率(0 < α < 1),k
是迭代次数。
2.2 接受新解的概率
新解的接受概率由Metropolis准则决定:
P = exp(-(f(new_solution) - f(current_solution)) / T)
其中,f(new_solution)
表示新解的目标函数值,f(current_solution)
表示当前解的目标函数值,T
是当前温度。当温度较高时,较差的解也有较大概率被接受;随着温度降低,较差解的接受概率变得越来越低。
3. 模拟退火算法的伪代码
SimulatedAnnealing():
初始化当前解 current_solution
初始化温度 T
while T > 终止温度:
生成新解 new_solution
计算目标函数差值 ΔE = f(new_solution) - f(current_solution)
if ΔE < 0:
接受 new_solution 作为当前解
else:
以 P = exp(-ΔE / T) 的概率接受 new_solution
更新温度 T = 降温函数(T)
输出最优解
4. 模拟退火的Python实现
为了更好地理解模拟退火的实现,我们通过一个简单的例子来进行说明。假设我们要优化一个函数的最小值,例如,f(x) = x^2
。
4.1 简单的一维函数优化
import math
import random
# 目标函数 f(x) = x^2
def objective_function(x):
return x ** 2
# 生成新的候选解
def generate_new_solution(current_solution):
# 生成一个附近的解,步长为 [-1, 1] 范围
return current_solution + random.uniform(-1, 1)
# 模拟退火算法
def simulated_annealing(initial_solution, initial_temp, cooling_rate, final_temp):
current_solution = initial_solution
current_temp = initial_temp
best_solution = current_solution
while current_temp > final_temp:
# 生成新解
new_solution = generate_new_solution(current_solution)
# 计算目标函数差值
current_energy = objective_function(current_solution)
new_energy = objective_function(new_solution)
delta_energy = new_energy - current_energy
# 判断是否接受新解
if delta_energy < 0 or random.uniform(0, 1) < math.exp(-delta_energy / current_temp):
current_solution = new_solution
# 更新最优解
if objective_function(current_solution) < objective_function(best_solution):
best_solution = current_solution
# 降低温度
current_temp *= cooling_rate
return best_solution
# 参数设置
initial_solution = random.uniform(-10, 10)
initial_temp = 1000
cooling_rate = 0.99
final_temp = 1
# 运行模拟退火算法
best_solution = simulated_annealing(initial_solution, initial_temp, cooling_rate, final_temp)
print(f"最优解: {best_solution}")
4.2 结果分析
在这个简单的示例中,模拟退火算法从一个随机点开始,通过逐渐降低温度,最终找到f(x) = x^2
函数的最小值。这个算法的强大之处在于,即使一开始随机生成的解距离最优解较远,算法也有机会找到全局最优解。
5. 复杂问题示例:TSP问题
旅行商问题(Travelling Salesman Problem, TSP)是一个经典的NP难题。我们可以使用模拟退火算法来寻找近似的最优解。
5.1 问题描述
假设有一组城市,旅行商需要访问每个城市一次并返回起点,要求总路程最短。
5.2 Python实现
import random
import math
# 生成城市坐标
def generate_cities(num_cities):
return [(random.uniform(0, 100), random.uniform(0, 100)) for _ in range(num_cities)]
# 计算两城市间的距离
def distance(city1, city2):
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
# 计算整个路线的总距离
def total_distance(route, cities):
return sum(distance(cities[route[i]], cities[route[i + 1]]) for i in range(len(route) - 1)) + distance(cities[route[-1]], cities[route[0]])
# 生成新解(交换两城市位置)
def generate_new_solution(route):
new_route = route[:]
i, j = random.sample(range(len(route)), 2)
new_route[i], new_route[j] = new_route[j], new_route[i]
return new_route
# 模拟退火算法
def simulated_annealing_tsp(cities, initial_temp, cooling_rate, final_temp):
num_cities = len(cities)
current_route = list(range(num_cities))
random.shuffle(current_route)
current_temp = initial_temp
best_route = current_route[:]
while current_temp > final_temp:
new_route = generate_new_solution(current_route)
current_distance = total_distance(current_route, cities)
new_distance = total_distance(new_route, cities)
delta_distance = new_distance - current_distance
# 判断是否接受新解
if delta_distance < 0 or random.uniform(0, 1) < math.exp(-delta_distance / current_temp):
current_route = new_route
# 更新最优解
if total_distance(current_route, cities) < total_distance(best_route, cities):
best_route = current_route
# 降温
current_temp *= cooling_rate
return best_route
# 参数设置
num_cities = 10
cities = generate_cities(num_cities)
initial_temp = 1000
cooling_rate = 0.995
final_temp = 1
# 运行模拟退火算法解决TSP问题
best_route = simulated_annealing_tsp(cities, initial_temp, cooling_rate, final_temp)
print(f"最优路线: {best_route}")
5.3 代码解释
- 随机生成了一组城市坐标。
- 使用模拟退火算法优化旅行商的行程,找出使得总距离最短的路线。
- 每次迭代通过交换两城市的位置生成新解,并根据温度控制接受或拒绝新解。
6. 总结
模拟退火算法是一种有效的全局优化算法,尤其适用于解决组合优化问题。通过引入随机性和接受“劣解”的机制,它能够在复杂的搜索空间中避免陷入局部最优解。本文介绍了模拟退火的基本原理,并通过一维函数优化和TSP问题展示了它的实际应用。