模拟退火算法 python_如何在python中实现模拟退火算法

本文介绍了如何在Python中实现模拟退火算法,详细解释了算法原理,并提供了相关代码示例,帮助读者理解并应用到实际问题解决中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟退火算法 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 = 90
final_temp = .1
alpha = 0.01
current_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_state
solution = 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 math


def simulated_annealing(initial_state):
    """Peforms simulated annealing to find a solution"""
    initial_temp = 90
    final_temp = .1
    alpha = 0.01
    
    current_temp = initial_temp


    # Start by initializing the current state with the initial state
    current_state = initial_state
    solution = current_state


    while current_temp > final_temp:
        neighbor = random.choice(get_neighbors())


        # Check if neighbor is best so far
        cost_diff = get_cost(self.current_state) = get_cost(neighbor)


        # if the new solution is better, accept it
        if 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 temperature
        current_temp -= alpha


    return solution


def get_cost(state):
    """Calculates cost of the argument state for your solution."""
    raise NotImplementedError
    
def 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值