当前位置: 首页 > news >正文

Python模拟退火算法

目录

      • 模拟退火算法简介
      • 模拟退火算法的步骤
      • 模拟退火算法的Python实现
        • 场景:函数优化问题
      • 代码解释
      • 总结

模拟退火算法简介

模拟退火算法(Simulated Annealing, SA)是一种基于物理退火过程的随机搜索算法,用于寻找全局最优解。其灵感来源于物理中的退火过程,退火是指将固体材料加热到高温再逐渐冷却,使其内部结构趋于有序状态,从而达到能量最低的稳定状态。模拟退火算法通过引入“温度”参数,逐渐降低“温度”,从而减少接受较差解的概率,最终收敛到全局最优解。

模拟退火算法的步骤

  1. 初始化

    • 设置初始解和初始温度。
    • 定义目标函数。
  2. 生成新解

    • 在当前解的邻域中随机生成一个新解。
  3. 接受新解

    • 计算新解的目标函数值。如果新解比当前解好,则接受新解。
    • 如果新解比当前解差,则根据一定的概率接受该解,接受概率与当前温度和目标函数差值相关。
  4. 降温

    • 按照一定的策略降低温度(例如线性降温或指数降温)。
  5. 迭代

    • 重复步骤2至步骤4,直到温度降至某个阈值或达到最大迭代次数。
  6. 输出结果

    • 返回最终的最优解。

模拟退火算法的Python实现

我们将通过Python实现模拟退火算法,并通过解决一个简单的函数优化问题来演示该算法的应用。

场景:函数优化问题

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as pltdef objective_function(x):return x**2 + 4 * np.sin(5 * x) + 0.1 * x**4def simulated_annealing(objective_function, bounds, n_iterations, initial_temp, cooling_rate):best = bounds[0] + (bounds[1] - bounds[0]) * np.random.rand()best_eval = objective_function(best)curr, curr_eval = best, best_evaltemp = initial_tempfor i in range(n_iterations):candidate = curr + np.random.uniform(-1, 1)candidate = np.clip(candidate, bounds[0], bounds[1])candidate_eval = objective_function(candidate)if candidate_eval < best_eval:best, best_eval = candidate, candidate_evaldiff = candidate_eval - curr_evalt = np.exp(-diff / temp)if diff < 0 or np.random.rand() < t:curr, curr_eval = candidate, candidate_evaltemp = temp * cooling_ratereturn best, best_eval# 参数设置
bounds = [-10, 10]  # 搜索空间
n_iterations = 1000  # 迭代次数
initial_temp = 10.0  # 初始温度
cooling_rate = 0.99  # 降温速率# 运行模拟退火算法
best_solution, best_value = simulated_annealing(objective_function, bounds, n_iterations, initial_temp, cooling_rate)
print(f"最优解: x = {best_solution}, 最小值: f(x) = {best_value}")# 绘制结果
x = np.linspace(bounds[0], bounds[1], 1000)
y = objective_function(x)
plt.plot(x, y, label='Objective Function')
plt.plot(best_solution, best_value, 'ro', label='Best Solution')
plt.title('Simulated Annealing Optimization')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()

代码解释

  1. 目标函数

    • objective_function定义了我们要最小化的函数。
  2. 模拟退火主循环

    • 初始解best随机生成,并在其邻域内生成候选解candidate
    • 通过计算目标函数值candidate_eval来决定是否接受新解。
    • 如果新解更优,则直接接受。如果不如当前解,则根据概率t来决定是否接受,这种机制允许算法跳出局部最优,寻找全局最优。
  3. 温度下降

    • 温度temp逐渐下降,降低接受较差解的概率,最终收敛到最优解。
  4. 结果输出与绘图

    • 打印最优解best_solution及其对应的最小值best_value
    • 使用Matplotlib绘制函数曲线及最优解位置。

总结

模拟退火算法通过模拟物理退火过程来求解复杂的优化问题。该算法的优势在于能够有效地跳出局部最优解,寻找全局最优解。尽管在理论上无法保证找到绝对最优解,但在实际应用中,模拟退火算法通常能够找到非常接近最优的解,并且实现相对简单、参数调整灵活。

http://www.lryc.cn/news/423130.html

相关文章:

  • C语言典型例题36
  • 实现高亮的全文分页检索
  • 【buildroot与yocto区别】
  • 原创音乐小程序的设计
  • 使用 MongoDB 构建 AI:Flagler Health 的 AI 旅程如何彻底改变患者护理
  • 在 Linux 系统中下载 Python 并配置环境
  • 优化if-else的几种方式
  • 关于k8s集群Pod启动过程
  • Linux Vim教程(十五):使用Vimscript进行脚本编写
  • 解决element-ui回车键绑定按钮功能后却刷新浏览器的问题
  • MySQL基础练习题37-查找结果的质量和占比
  • 酒店行业如何利用XML进行营销短信
  • 【模型】TFLiteModel
  • 【Kubernetes】Service 概念与实战
  • RTSP|RTMP流如何指定坐标位置和分辨率获取RGB数据实时渲染和算法分析
  • 基于ssm+vue+uniapp的英语学习交流平台小程序
  • 如何判断一个TimerTask是否已经完成
  • Android常用面试题
  • JSON与Jsoncpp库:数据交换的灵活选择
  • salesforce rich text 字段支持html中内嵌JavaScript吗
  • Ubuntu24.04、22.04或20.04安装Golang方法教程
  • 学习记录第二十二天
  • 红酒与艺术展览:品味艺术与风味的双重盛宴
  • 1Panel配置
  • 场外期权是如何定价的?场外期权定价的基本原理是什么?
  • vue中数据持久化
  • 小白零基础学数学建模系列-Day3-线性回归模型的构建与评估
  • Flutter中的 extended_nested_scroll_view 库:介绍与使用指南
  • Elasticsearch 综合搜索案例解析
  • Web存储革命:揭秘JavaScript的会话存储(sessionStorage)