模拟退火算法 python

The Simulated Annealing algorithm is commonly used when we’re stuck trying to optimize solutions that generate local minimum or local maximum solutions, for example, the Hill-Climbing algorithm. So we use the Simulated Annealing algorithm to have a better solution to find the global maximum or global minimum.

当我们试图优化生成局部最小值或局部最大值解的解决方案时,通常会使用“模拟退火”算法,例如,Hill-Climbing算法。 因此,我们使用模拟退火算法来找到全局最大值或全局最小值的更好解决方案。

为什么要模拟退火? (Why Simulated Annealing?)

It’s called Simulated Annealing because it’s modeling after a real physical process of annealing something like a metal. When you heat a particular metal, there’s a lot of energy there, and you can move things around quite systematically. But over time, as the system cools down, it eventually settles into a final position.

之所以称其为“模拟退火”,是因为它是在对诸如金属之类的东西进行退火的真实物理过程之后进行建模的。 当您加热一种特定的金属时,那里有很多能量,您可以相当系统地移动东西。 但是随着时间的流逝,随着系统冷却,它最终会稳定在最终位置。

We’re going to simulate that process of some high-temperature systems, where things can move around quite frequently but, over time, decreasing that temperature until we eventually settle at an ultimate solution.

我们将模拟某些高温系统的过程,其中事物可能会频繁移动,但随着时间的流逝,温度会降低,直到最终确定最终解决方案。

它是如何工作的? (How does it work?)

In this Python code, we will have an algorithm to find the global minimum, but you can easily modify this to find the global maximum.

在此Python代码中,我们将提供一种算法来查找全局最小值,但您可以轻松地对其进行修改以查找全局最大值。

First, we have to determine how we will reduce the temperature on each iteration. In this example, we will start with a temperature of 90 degrees, and we will decrease the current temperature by 0.01 linearly until we reach the final temperature of 0.1 degrees.

首先,我们必须确定如何降低每次迭代的温度。 在此示例中,我们将从90度的温度开始,然后将当前温度线性降低0.01,直到达到0.1度的最终温度。

initial_temp = 90final_temp = .1alpha = 0.01current_temp = initial_temp

Then we will set the initial state and set it as the solution. You can set it up as a particular state or generate it randomly.

然后,我们将设置初始状态并将其设置为解决方案。 您可以将其设置为特定状态或随机生成。

current_state = initial_statesolution = current_state

Now, we will repeat this process until the current temperature is less than the final temperature.

现在,我们将重复此过程,直到当前温度低于最终温度为止。

while current_temp > final_temp

For each iteration, we will get a random neighbor of the current state (the following state that we can go from the current state).

对于每次迭代,我们将获得当前状态的随机邻居(可以从当前状态获得以下状态)。

neighbor = random.choice(self.get_neighbors())

Then we calculate the differences between the neighbor and the current state.

然后,我们计算邻居与当前状态之间的差异。

cost_diff = self.get_cost(self.current_state) = self.get_cost(neighbor)

If the new solution is better, we will accept it.

如果新的解决方案更好,我们将接受它。

if cost_diff > 0:  solution = neighbor

If the new solution is not better, we will still accept it if the temperature is high. With this approach, we will use the worst solution in order to avoid getting stuck in local minimum. But we will get a neighbor that is not that bit worse than the current state. We can determine that with the following probability equation:

如果新的解决方案不是更好,如果温度很高,我们仍然会接受。 通过这种方法,我们将使用最差的解决方案,以避免陷入局部最小值。 但是我们会得到一个比当前状态还差一点的邻居。 我们可以通过以下概率方程来确定:

if random.uniform(0, 1) < math.exp(cost_diff / current_temp):  solution = neighbor

The next step is to decrement the current temperature according to the alpha value.

下一步是根据alpha值降低当前温度。

current_temp -= alpha

So at the very end, we just return to whatever the current state happens to be.

因此,最后,我们将返回到当前状态。

This is the big picture for Simulated Annealing algorithm, which is the process of taking the problem and continuing with generating random neighbors. We’ll always move to a neighbor if it’s better than our current state. But even if the neighbor is worse than our current state, we’ll sometimes move there depending the temperature and how bad it is.

这是模拟退火算法的概况,这是解决问题并继续生成随机邻居的过程。 如果比我们现在的状态好,我们将始终搬到邻居那里。 但是,即使邻居比我们当前的状态更糟,我们有时也会根据温度及其状况而移到那里。

import random
import mathdef simulated_annealing(initial_state):"""Peforms simulated annealing to find a solution"""initial_temp = 90final_temp = .1alpha = 0.01current_temp = initial_temp# Start by initializing the current state with the initial statecurrent_state = initial_statesolution = current_statewhile current_temp > final_temp:neighbor = random.choice(get_neighbors())# Check if neighbor is best so farcost_diff = get_cost(self.current_state) = get_cost(neighbor)# if the new solution is better, accept itif cost_diff > 0:solution = neighbor# if the new solution is not better, accept it with a probability of e^(-cost/temp)else:if random.uniform(0, 1) < math.exp(cost_diff / current_temp):solution = neighbor# decrement the temperaturecurrent_temp -= alphareturn solutiondef get_cost(state):"""Calculates cost of the argument state for your solution."""raise NotImplementedErrordef get_neighbors(state):"""Returns neighbors of the argument state for your solution."""raise NotImplementedError

结论 (Conclusion)

And as a result, the goal of this whole process is that as we begin to try and find our way to the global maximum or the global minimum, we can dislodge ourselves if we ever get stuck at a local maximum or a local minimum in order to eventually make our way to exploring the best solution. And then as the temperature decreases, eventually we settle there without moving around too much from what we’ve found to be the globally best thing that we can do thus far.

结果,整个过程的目标是,当我们开始尝试找到通往全局最大值或全局最小值的方法时,如果我们陷入了局部最大值或局部最小值的顺序中,我们就可以摆脱自我最终使我们能够探索最佳解决方案。 然后,随着温度降低,我们最终定居在那儿,而与迄今所能找到的全球最佳做法相比,并没有太大的变动。

翻译自: https://medium.com/@cesarwilliam/how-to-implement-simulated-annealing-algorithm-in-python-ab196c2f56a0

模拟退火算法 python


http://www.taodudu.cc/news/show-6890047.html

相关文章:

  • 仿nuxt.js,自动构建路由,释放你的双手?!
  • 4 AI基础:超越经典搜索算法
  • 『杭电1101』The Parallel Challenge Ballgame
  • “3Sum”类问题总结
  • vue路由文件自动化处理
  • 先锋机器人启动失败
  • [精简]托福核心词汇41
  • 外包三年给整废了,备战两月终拿到4家大厂offer,阿里P6+这回稳了
  • 三年外包终上岸,这些公司能不去就不去,这回阿里P6+稳了啊
  • 中控技术:5T技术融合实现工业3.0到工业4.0的转变
  • 数组和字符串心得体会
  • 一个简单的银行、账户演示程序
  • JAVA|银行账户Account类
  • 力扣-银行账户概要 II
  • 银行账户管理项目(纯java)
  • 项目名称:Bank Account Management System 银行账户管理系统 简称BAM
  • SCAU 银行账户类的定义与使用
  • 2022年WordPress主题最优秀热门主题排名TOP30
  • slideLoad
  • Gradle实现的两种简单的多渠道打包方法
  • 【Raspberry Pi】树莓派垃圾分类识别项目
  • SLAM——开源项目学习
  • Android Studio 注释模板
  • Android Studio科普篇——1.几个个性化设置
  • 博客要考虑的最佳WordPress主题
  • 使用sLDA
  • eclipse导入工程的中文注释为菱形问号的乱码(已解决)
  • PostgreSQL JSONB类型及其操作
  • 电力行业智能电网建设背景
  • 【电力电子技术速通】七、PWM控制技术

模拟退火算法 python_如何在python中实现模拟退火算法相关推荐

  1. closecmd python_如何在python中禁止控制台/ cmd错误消息

    如何在 python中为chromedriver和pyinstaller exe压缩错误消息? 我注意到当我在pyinstaller中使用chromedriver并运行它时,我得到一个错误消息列表.我 ...

  2. float在python_如何在python中读取.float文件? - python

    Improve this question 我正在处理大脑MRI数据,它是.float数据. 您知道如何在python中使用它吗? 与 with open('[43x25520].float') as ...

  3. opencv检测图片失焦 python_如何在Python中使用OpenCV执行模糊检测

    如何在Python中使用OpenCV执行模糊检测 目标检测 最后更新 2020-10-12 14:23 阅读 154 最后更新 2020-10-12 14:23 阅读 154 目标检测 ##FlyAI ...

  4. 怎么证明会python_如何在python中验证SSL证书?

    我需要验证证书是否由我的自定义CA签名.使用OpenSSL命令行实用程序很容易做到:# Custom CA file: ca-cert.pem # Cert signed by above CA: b ...

  5. 三次样条python_如何在python中进行三次样条插值?

    如果安装了scipy version&gt:=0.18.0,则可以使用scipy.interpolate中的cubic spline函数进行三次样条曲线插值. 您可以通过在python中运行以 ...

  6. python 100以内3的倍数_关于算法:如何在Python中找到低于1000的3或5的所有倍数的总和?...

    不确定我是否应该把这个贴在math.stackexchange上,但是它包含了更多的编程,所以我把它贴在这里. 这个问题似乎很简单,但我已经在这里坐了至少一个小时,现在还没弄明白.我尝试过不同的解决方 ...

  7. 价钱转换python_如何在python中转换货币?

    我正在做一个虚拟助手项目.我想让它告诉我其他货币的美元汇率. 我用beauthoulsoup编写了以下代码,它从给定的网站获取数据,对其进行解析并在命令行中打印结果供我阅读.但这只是美元对巴基斯坦卢比 ...

  8. python实现ks算法_如何在Python中实现KSTest

    kstest的cdf参数可以是一个可调用的,它实现了要根据其测试数据的分布的累积分布函数.要使用它,您必须实现双峰分布的CDF.你希望这个分布是两个正态分布的混合.您可以通过计算组成混合的两个正态分布 ...

  9. 返回1到n的所有组合python_如何在Python中生成0-1矩阵的所有可能组合?

    如何生成大小为K的0-1矩阵的所有可能组合? 例如,如果我取K = 2且N = 2,我得到以下组合. combination 1 [0, 0; 0, 0]; combination 2 [1, 0; ...

最新文章

  1. SpringBoot 多个src文件夹,入口类的位置
  2. 1.2.2 OSI参考模型 下
  3. java使用三种循环打印99表_编程题:利用for循环打印 9*9 表
  4. LeetCode 836. 矩形重叠
  5. 园友们大家好,我是“一只酷酷的恺”
  6. java连接hive代码_Hive:用Java代码通过JDBC连接Hiveserver
  7. 上海交大发布全球首款专用光量子计算软件
  8. boa与cgic库写cgi简介
  9. 利用matlab求图像均值和方差的几种方法
  10. java使用jsp建立项目+视频
  11. 没有wan接口_“伪千兆”路由器,快回家看看你家中招了没有?
  12. PIC16F887 实战编程 单片机编程 基础实验教程
  13. 医学PASS样本量计算软件
  14. solidity教程【0.5.7】
  15. 51单片机制作计算机1602显示,51单片机对LCD1602液晶显示器的控制
  16. ubuntu系统启动项的修改
  17. python里创建数据库表Column常用参数总结
  18. golang对比python
  19. MySQL八股文连环45问,你能坚持第几问?
  20. [深度][PyTorch] DDP系列第三篇:实战与技巧

热门文章

  1. 用ps画一个邪恶写轮眼!
  2. 计算机和音乐结合的作品,人们将计算机生成的音乐与JS巴赫的作品混为一谈
  3. javascript写字技巧_网站建设对JavaScript书写如何更加规范化
  4. Windows10 安装 Vue3
  5. mac电脑安装vuedevtools的步骤
  6. mysql union minus_MySQL实现差集(Minus)和交集(Intersect)
  7. 【JS面试题】minus(m)(n) 高阶函数实现减法运算
  8. Unity3D开发 巫师狩魔猎人感官功能(简陋版)
  9. win7无密码访问共享
  10. 学透Java自增(++)自减(--)运算符,看这一篇就够了!