模拟退火 八皇后_模拟退火和八皇后问题

本文介绍了使用模拟退火算法解决经典的八皇后问题。通过翻译自的数据科学文章,详细探讨了如何应用模拟退火来避免皇后之间的冲突,实现棋盘上的安全布局。
摘要由CSDN通过智能技术生成

模拟退火 八皇后

The Simulated Annealing (SA) algorithm is one of many random optimization algorithms. Unlike algorithms like the Hill Climbing algorithm where the intent is to only improve the optimization, SA allows for more exploration. The idea is that with this exploration it’s more likely to reach a global optima rather than a local optima (for more on local optima, global optima and the Hill Climbing Optimization algorithm follow this link).

模拟退火(SA)算法是许多随机优化算法之一。 与Hill Climbing算法之类的算法仅旨在改善优化的算法不同,SA允许进行更多探索。 这个想法是,通过这种探索,它更有可能达到全局最优而非局部最优(有关局部最优,全局最优和爬山优化算法的更多信息,请点击此链接 )。

The term “annealing” is a term used in mettalurgy where the objective is to lower and increase the temperature of a metal. The process allows for the molecules to regroupe and get closer every time so that the metal becomes harder. The analogy is applied on the SA algorithm by getting closer to a solution, going farther from it by doing exploration and getting closer again to an even better solution.

术语“退火”是在冶金术中使用的术语,其中目的是降低和升高金属的温度。 该过程允许分子每次重新聚集并靠近,从而使金属变得更硬。 通过接近解决方案,通过探索进一步远离它,再次接近甚至更好的解决方案,将类比应用于SA算法。

模拟退火算法 (The Simulated Annealing Algorithm)

The algorithm can be decomposed in 4 simple steps:

该算法可以分解为4个简单的步骤:

  1. Start at a random point x.

    从一个随机点x开始。
  2. Choose a new point xⱼ on a neighborhood N(x).

    在邻域N(x)上选择一个新点xⱼ。
  3. Decide whether or not to move to the new point xⱼ. The decision will be made based on the probability function P(x,xⱼ,T) (explained ahead).

    决定是否移动到新点xⱼ。 该决策将基于概率函数P(x,xⱼ,T)(前面说明)。
  4. Reduce T

    降低T

Like we mentioned before P(x,xⱼ,T) is the function that will guide us on whether we move to the new point or not:

就像我们之前提到的P(x,xⱼ,T)是函数,它将指导我们是否移至新点:

Image for post

Let’s explore what the function is telling us. F(x) is the objective function (the function for which we want to find the optimal point x). If the new point xⱼ improves the result of the objective function, we will move to the new point with probability 1.

让我们探索一下函数告诉我们什么。 F(x)是目标函数(我们要为其找到最佳点x的函数)。 如果新点xⱼ改善了目标函数的结果,我们将以概率1移到新点。

When the new point doesn’t improve the objective function, we will move to the new point depending on the difference of F(xⱼ)-F(x) and the variable T.

当新点不能改善目标函数时,我们将根据F(xⱼ)-F(x)与变量T的差移至新点。

When T is high the possibility of moving to the new point will also be high and when T is low the possibility of moving to the new point is also low. That’s why initially we’ll start with a high T to do more exploration, and gradually lower the value of T to reach the optimal point.

当T高时,移到新点的可能性也很高;当T低时,移到新点的可能性也很小。 这就是为什么我们最初将从高T开始进行更多探索,然后逐渐降低T的值以达到最佳点的原因。

Now that we know how the algorithm works it’s time to perform an example using python in order to take our understanding further.

现在我们知道了算法的工作原理,是时候使用python来执行示例了,以便进一步理解我们。

八皇后问题 (The Eight Queen Problem)

The eight queen problem consists in positioning 8 queens in an 8*8 table board without any of them being on the line of attack between each other. Queens can move horizontally, diagonally and vertically as seen in Image 1.1.

八个皇后的问题在于将8个皇后放置在8 * 8的桌面中,而没有一个在彼此之间的攻击线上。 如图1.1所示,皇后可以水平,对角和垂直移动。

Image for post
Brainmetrix Brainmetrix

Finding the solution to the problem is not easy since a great number of combinations exist. We can see some solutions in image 1.2 were only 6 and 7 queens were positioned in the board.

因为存在大量的组合,所以找到问题的解决方案并不容易。 我们可以在图1.2中看到一些解决方案,只有6个皇后和7个皇后被放置在板上。

Image for post
Brainmetrix Brainmetrix

Now that we understand the problem let’s go to python code and solve it.

既然我们已经理解了问题,那么我们就去研究python代码并解决它。

使用Python的8个皇后区 (The 8 Queens using Python)

In python there exists a library called “mlrose” that is very helpful for implementing random optimization algorithms so the first few lines of code will be used to import this library as well as the numpy library that helps us handle arrays.

在python中,存在一个名为“ mlrose”的库,该库对于实现随机优化算法非常有帮助,因此前几行代码将用于导入该库以及帮助我​​们处理数组的numpy库。

### Importing the necessary libraries
import mlrose
import numpy as np

The next step is defining the objective function. The objective function will count the number of queens that are positioned in a place where they cannot be attacked. Given that queens move vertically, it’s reasonable to say that no queen should be placed in the same vertical and thus we can represent the position of each queen in a simple array of 8 positions. For example, in a chess board an array A=[0,2,1,3,7,4,5,6] would look like Image 2.1.

下一步是定义目标函数。 目标函数将计算位于无法被攻击的地方的皇后数量。 鉴于皇后垂直移动,可以合理地说,不应将皇后放置在同一垂直方向上,因此我们可以用8个位置的简单数组表示每个皇后的位置。 例如,在国际象棋棋盘中,数组A = [0,2,1,3,7,4,5,6]看起来像图像2.1。

Image for post
DataGenetics DataGenetics

Now it is time to code the objective function:

现在是时候编写目标函数了:

###Defining the objective function
def queens_max(position):
# We start the count
no_attack_on_j = 0
queen_not_attacking=0
# Compare for each pair of queens
for i in range(len(position) - 1):
no_attack_on_j=0
for j in range(i + 1, len(position)):
### Check if there is any diagonal or horizontal attack.
###Iterative process for each column
if (position[j] != position[i]) and (position[j] != position[i] + (j - i)) and (position[j] != position[i] - (j - i)):
"""If there isn't any attack on the evaluated column.
The count is increased by one.
This counter is only used as a reference ."""
no_attack_on_j += 1
"""If there is no attack on all the columns.
The general counter is increased by one.
This counter indicates the number of queens that are correctly
positioned."""
if(no_attack_on_j==len(position)-1-i):
queen_not_attacking+=1
"""The return number is the number of queens not attacking each
other. If this number is 7 we add 1 cause it means the last
queen is also free of attack."""
if(queen_not_attacking==7):
queen_not_attacking+=1
return queen_not_attacking

The objective function previously defined is assigned as an argument to the method “CustomFitness” in mlrose. That’s how any objective function can work with this library.

先前定义的目标函数在mlrose中被指定为方法“ CustomFitness”的自变量。 这就是任何目标函数可以与此库一起工作的方式。

# Assign the objective function to "CustomFitness" method.
objective= mlrose.CustomFitness(queens_max)

Now, we finished the tricky part. The only thing remaining is to tell mlrose the type of problem is has to solve. The problem we are solving has discrete numbers (whole numbers) that’s why we’ll use the method “DiscreteOpt”. This method will hold the decription of the problem and it has 4 arguments:

现在,我们完成了棘手的部分。 剩下的唯一一件事就是告诉mlrose问题类型必须解决。 我们要解决的问题是离散数(整数),这就是为什么我们将使用“ DiscreteOpt”方法的原因。 此方法将保留问题的描述,并且具有4个参数:

  1. length- The size of the array we are working with (for the eight queen problem it’s 8).

    length-我们正在处理的数组的大小(对于八个皇后问题是8)。
  2. fitness_fn- The objective function we previously defined with the name “objective”.

    fitness_fn-我们先前定义的目标函数名称为“ objective”。
  3. maximize-It must be “True” if we want to maximize the objective function and “False” otherwise.

    最大化-如果我们要最大化目标函数,则必须为“ True”,否则为“ False”。
  4. max_val- This parameter indicates the maximum value the function can take. It starts in 0 and ends in max_val-1. In our case the queen can be positioned from 0 to 7 so max_val=8.

    max_val-此参数指示函数可以采用的最大值。 它以0开始,以max_val-1结尾。 在我们的情况下,女王可以从0定位到7,因此max_val = 8。

The line of code:

代码行:

#Description of the problem
problem = mlrose.DiscreteOpt(length = 8, fitness_fn = objective, maximize = True, max_val = 8)

Finally, it’s time to tell mlrose how to solve the problem. We know we are going to use Simulated Annealing(SA) and it’s important to specify 5 parameters.

最后,是时候告诉mlrose如何解决问题了。 我们知道我们将使用模拟退火(SA),并且指定5个参数很重要。

  1. problem-This parameter contains the information of the problem. We defined it earlier with the name “problem”.

    问题-此参数包含问题的信息。 我们之前用“问题”来定义它。
  2. schedule-This parameter tells T how to decrease over each iteration. There are many ways to decrease T and it’s possible to check them on mlrose. This time T will be decreased exponentially using ExpDecay().

    schedule-这个参数告诉T如何在每次迭代中减少。 降低T的方法有很多,可以在mlrose上进行检查。 使用ExpDecay()将以指数形式减少此时间T。

  3. max_attempts- It’s important to define the number of attempts the algorithm will try to search for a better solution. If the number of attempts reaches the maximum it should stop.

    max_attempts-重要的是定义算法尝试搜索更好解决方案的次数。 如果尝试次数达到最大值,则应停止。
  4. max_iter-It indicates the maximum number of new points the algorithm can find or the maximum number of iteration it does.

    max_iter-表示算法可以找到的新点数的最大值或它可以执行的最大迭代数。
  5. init_state-The parameter “init_state” is the inital position of the array.

    init_state-参数“ init_state”是数组的初始位置。

The last chunk of code looks like this:

最后一段代码如下所示:

# Define decay schedule
T = mlrose.ExpDecay()
# Define initial state
initial_position = np.array([4, 6, 1, 5, 2, 0, 3, 7])
# Solve problem using simulated annealing
best_position, best_objective = mlrose.simulated_annealing(problem=problem, schedule = T,
max_attempts = 500, max_iters = 5000,
init_state = initial_position)
print('The best position found is: ', best_position)
print('The number of queens that are not attacking each other is: ', best_objective)Output:The best position found is: [4 6 1 5 2 0 3 7]
The number of queens that are not attacking each other is: 8.0

We solved the 8 queen problem succesfully as seen on Image 2.2

如图2.2所示,我们成功解决了8个皇后问题

Image for post
DataGenetics DataGenetics

最终说明和代码 (Final Remarks and Code)

I hope you enjoyed the article and play with the eight queen problem. I leave you the code on google colab and github. It’s possible to run this code and with a few tweaks even solve the 9,10…n queen problem.

希望您喜欢这篇文章,并解决“八皇后”问题。 我把代码留在Google colab和github上。 可以运行此代码,并进行一些调整甚至可以解决9,10…n女王问题。

Bibliography:

参考书目:

Machine Learning, Randomized Optimization and SEarch¶. (n.d.). Retrieved August 13, 2020, from https://mlrose.readthedocs.io/en/stable/

机器学习,随机优化和SEarch¶。 (nd)。 于2020年8月13日从https://mlrose.readthedocs.io/en/stable/检索

Brain Training. (n.d.). Retrieved August 13, 2020, from http://www.brainmetrix.com/8-queens/

脑训练。 (nd)。 于2020年8月13日从http://www.brainmetrix.com/8-queens/检索

Eight Queens. (n.d.). Retrieved September 10, 2020, from http://www.datagenetics.com/blog/august42012/

八个皇后。 (nd)。 于2020年9月10日从http://www.datagenetics.com/blog/august42012/检索

Originally published at https://www.estudiodedatos.com on August 25, 2020.

最初于 2020年8月25日 https://www.estudiodedatos.com 发布

翻译自: https://towardsdatascience.com/simulated-annealing-and-the-eight-queen-problem-10f737edbb7e

模拟退火 八皇后

引用提供了一个使用Python解决八皇后问题的代码示例。这个问题是将8个皇后放置在8x8的棋盘上,使得它们互相不攻击。八皇后问题是一个经典的组合优化问题。 引用提到了八皇后问题的算法工作原理,并提到使用Python来执行示例代码可以进一步理解问题。 根据引用和引用的信息,可以使用Python实现八皇后问题模拟退火算法。模拟退火算法是一种启发式的优化算法,通过模拟退火过程来搜索问题的解空间。 以下是一个使用Python实现的八皇后问题模拟退火算法的代码示例: ```python import random import math def simulated_annealing(): board_size = 8 temperature = 1000 cooling_rate = 0.99 def initial_state(): return [random.randint(0, board_size-1) for _ in range(board_size)] def attack_count(state): count = 0 for i in range(board_size): for j in range(i+1, board_size): if state[i == state[j or abs(state[i]-state[j]) == abs(i-j): count += 1 return count def next_state(state): next_state = state.copy() row = random.randint(0, board_size-1) col = random.randint(0, board_size-1) next_state[row = col return next_state def acceptance_probability(attack_diff, temperature): if attack_diff < 0: return 1.0 return math.exp(-attack_diff / temperature) current_state = initial_state() current_attack_count = attack_count(current_state) while current_attack_count > 0: next_state = next_state(current_state) next_attack_count = attack_count(next_state) attack_diff = next_attack_count - current_attack_count if acceptance_probability(attack_diff, temperature) > random.random(): current_state = next_state current_attack_count = next_attack_count temperature *= cooling_rate return current_state solution = simulated_annealing() print(solution) ``` 上面的代码示例中,`simulated_annealing`函数实现了八皇后问题模拟退火算法。初始状态由`initial_state`函数生成,攻击计数由`attack_count`函数计算,下一个状态由`next_state`函数生成,接受概率由`acceptance_probability`函数计算。 最终,在退火过程中找到了一个解,它将8个皇后放置在棋盘上,使得它们互相不攻击。该解被打印出来。 这个例子是使用Python来模拟八皇后问题的退火算法。你可以根据自己的需求和偏好进行修改和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [模拟退火 八皇后_模拟退火八皇后问题](https://blog.csdn.net/weixin_26735933/article/details/108925219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值