模拟退火算法 Simulated Annealing

2023-12-03 00:30

本文主要是介绍模拟退火算法 Simulated Annealing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

模拟退火算法 Simulated Annealing

1. 介绍

模拟退火算法(Simulated Annealing, SA)是一种启发式的优化算法。它适用于在大型离散或连续复杂问题中寻找全局最优解,例如组合优化,约束优化,图问题等。模拟退火是一种随机(概率性)搜索算法,基于物理中固体晶体退火过程的模拟。退火过程中,晶体内部由高能状态向低能状态演化,最终在足够低的温度时稳定在能量最低的状态。

模拟退火算法的主要思想是在搜索过程中接受比当前解差的解,以跳出局部最优。具体来说,模拟退火算法过程如下:

  1. 初始化设置:设置初始解、初始温度、温度衰减系数,最低温度等参数。

  2. 在当前温度下,随机选取一个邻近解并计算其能量变化量。

  3. 如果能量降低(对于最小化问题),则接受该解作为当前解;如果能量增加,则以一定的概率(通常依赖于温度和能量变化量)接受该解。

  4. 更新温度(通常为当前温度乘以衰减系数)。

  5. 重复步骤2-4,直到温度降至最低温度并稳定。

  6. 输出最佳解。

模拟退火算法的关键在于选择合适的初始温度、衰减系数和结束条件,以及针对问题的邻近解生成策略和概率接受函数。

2. 经典应用

接下来,我们将以三个经典问题为例,展示如何使用模拟退火算法求解。

2.1 旅行商问题(TSP)

旅行商问题即求解在给定城市、距离的情况下,找到一条道路,使得从起点出发,经过所有城市后回到起点,且总距离最短。

import random
import math
import numpy as np# 计算路径长度
def calculate_distance(path, distance_matrix):distance = 0for i in range(len(path)-1):distance += distance_matrix[path[i]][path[i+1]]distance += distance_matrix[path[-1]][path[0]]return distance# 邻近解生成策略:交换两个城市的位置
def generate_neighbor(path):new_path = path.copy()i, j = random.sample(range(len(path)), 2)new_path[i], new_path[j] = new_path[j], new_path[i]return new_path# 模拟退火算法求解TSP问题
def simulated_annealing_tsp(distance_matrix, initial_temperature, min_temperature, cooling_factor, max_iteration):n = len(distance_matrix)current_path = list(range(n))random.shuffle(current_path)current_distance = calculate_distance(current_path, distance_matrix)temperature = initial_temperaturebest_path = current_path[:]best_distance = current_distancefor it in range(max_iteration):if temperature < min_temperature:breaknew_path = generate_neighbor(current_path)new_distance = calculate_distance(new_path, distance_matrix)delta_distance = new_distance - current_distanceif delta_distance < 0:current_path = new_pathcurrent_distance = new_distanceif new_distance < best_distance:best_path = new_path[:]best_distance = new_distanceelse:prob = math.exp(-delta_distance / temperature)if random.random() < prob:current_path = new_pathcurrent_distance = new_distancetemperature *= cooling_factorreturn best_path, best_distance# 用随机距离矩阵测试
n = 10
distance_matrix = np.random.randint(1, 100, size=(n, n))
initial_temperature = 1000
min_temperature = 1e-6
cooling_factor = 0.99
max_iteration = 10000best_path, best_distance = simulated_annealing_tsp(distance_matrix, initial_temperature, min_temperature, cooling_factor, max_iteration)
print('Best path:', best_path)
print('Best distance:', best_distance)
2.2 函数寻优

给定一个连续空间的实数函数f(x),求解其在给定区间上的最小值。

import random
import math# 定义函数
def f(x):return x * x - 4 * x + 4def generate_neighbor(x, step):return x + random.uniform(-step, step)def simulated_annealing_function_optimization(f, initial_temperature, min_temperature, cooling_factor, max_iteration, search_interval, neighbor_step):current_x = random.uniform(search_interval[0], search_interval[1])current_y = f(current_x)temperature = initial_temperaturebest_x = current_xbest_y = current_yfor it in range(max_iteration):if temperature < min_temperature:breaknew_x = generate_neighbor(current_x, neighbor_step)if new_x < search_interval[0] or new_x > search_interval[1]:continuenew_y = f(new_x)delta_y = new_y - current_yif delta_y < 0:current_x = new_xcurrent_y = new_yif new_y < best_y:best_x = new_xbest_y = new_yelse:prob = math.exp(-delta_y / temperature)if random.random() < prob:current_x = new_xcurrent_y = new_ytemperature *= cooling_factorreturn best_x, best_yinitial_temperature = 1000
min_temperature = 1e-6
cooling_factor = 0.99
max_iteration = 10000
search_interval = [0, 5]
neighbor_step = 0.1best_x, best_y = simulated_annealing_function_optimization(f, initial_temperature, min_temperature, cooling_factor, max_iteration, search_interval, neighbor_step)
print('Best x:', best_x)
print('Best y:', best_y)
2.3 定点覆盖问题

给定一个二维平面,有若干个点和一定数量的覆盖盒。要求通过移动覆盖盒的位置使得尽可能多的点被覆盖。覆盖盒的形状为边长为1的正方形。

import random
import mathdef point_covered(points, box):# 判断点是否在覆盖盒内return sum([1 for p in points if box[0] <= p[0] <= box[0]+1 and box[1] <= p[1] <= box[1]+1])def generate_neighbor(box, step):# 随机生成一个相邻覆盖盒return (box[0] + random.uniform(-step, step), box[1] + random.uniform(-step, step))def simulated_annealing_point_cover(points, n_boxes, initial_temperature, min_temperature, cooling_factor, max_iteration, neighbor_step):boxes = [(random.uniform(0, 5), random.uniform(0, 5)) for _ in range(n_boxes)]temperature = initial_temperature# 当前解:覆盖的点数量和覆盖盒集合current_cover = sum([point_covered(points, box) for box in boxes])current_boxes = boxes# 迭代过程for it in range(max_iteration):if temperature < min_temperature:break# 随机选择一个覆盖盒生成相邻解idx = random.randrange(len(boxes))new_box = generate_neighbor(boxes[idx], neighbor_step)new_boxes = boxes[:]new_boxes[idx] = new_boxnew_cover = sum([point_covered(points, box) for box in new_boxes])delta_cover = new_cover - current_coverif delta_cover > 0:current_boxes = new_boxescurrent_cover = new_coverelse:prob = math.exp(delta_cover / temperature)if random.random() < prob:current_boxes = new_boxescurrent_cover = new_covertemperature *= cooling_factorreturn current_boxes# 生成点集
points = [(random.uniform(0, 5), random.uniform(0, 5)) for _ in range(50)]
n_boxes = 5initial_temperature = 1000
min_temperature = 1e-6
cooling_factor = 0.99
max_iteration = 10000
neighbor_step = 0.1result = simulated_annealing_point_cover(points, n_boxes, initial_temperature, min_temperature, cooling_factor, max_iteration, neighbor_step)
print(result)

以上三个案例展示了如何使用模拟退火算法求解旅行商问题、函数寻优以及定点问题。请注意,模拟退火算法在每个问题中的效果取决于选择的初始参数和邻近解生成策略。

如果你想更深入地了解人工智能的其他方面,比如机器学习、深度学习、自然语言处理等等,也可以点击这个链接,我按照如下图所示的学习路线为大家整理了100多G的学习资源,基本涵盖了人工智能学习的所有内容,包括了目前人工智能领域最新顶会论文合集和丰富详细的项目实战资料,可以帮助你入门和进阶。

链接: 人工智能交流群(大量资料)

在这里插入图片描述

这篇关于模拟退火算法 Simulated Annealing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/447422

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第