拼图登陆拼图二维码验证_八皇后拼图

拼图登陆拼图二维码验证

In my artificial intelligence (AI) class last semester I learned about different categories of mathematical problems that can be solved effectively using methods applied in modern AIs. One category contained constraint satisfaction problems (CSPs). As the name loosely implies, these problems give you variables that can have different states and a set of constraints, that have to be satisfied by the states of the variables. The difficult part of these problems is to find a state combination that solves the CSP without considering too many possibilities, which otherwise could quickly make the calculation too large (and computationally unfeasible).

在上学期的人工智能(AI)课上,我了解了不同类别的数学问题,这些问题可以使用现代AI中应用的方法有效地解决。 一类包含约束满足问题 (CSP)。 顾名思义,这些问题为您提供了可以具有不同状态和一组约束的变量,这些变量必须由变量的状态来满足。 这些问题中最困难的部分是找到一种无需考虑太多可能性即可解决CSP的状态组合,否则可能很快使计算量过大(并且在计算上不可行)。

八皇后拼图 (Eight Queens Puzzle)

One of the best examples for a CSP is the eight queens puzzle. It is a mathematical problem formulated using the rules of chess. Your goal is to place eight queens on a standard eight by eight chess board without any two queens threatening each other, meaning that a queen could take another queen in the next turn. Here are the directions a queen can attack on a chess board:

CSP最好的例子之一是八个皇后难题。 这是使用国际象棋规则制定的数学问题。 您的目标是将八个皇后放在标准的八乘八棋盘上,而没有两个皇后互相威胁,这意味着一个皇后可以在下一回合中再接另一个皇后。 女王可以在棋盘上攻击的方向如下:

  • the queen can move horizontally on the chessboard
  • the queen can move vertically on the chessboard
  • the queen can move diagonally in any direction on the chessboard
  • 女王可以在棋盘上水平移动
  • 女王可以在棋盘上垂直移动
  • 女王可以在棋盘上向任何方向对角移动

After trying the eight queens puzzle with my brother out on a standard chess board (by the way: he solved it in under 10 minutes), we quickly noticed that one of the harder aspects when trying to solve the eight queens puzzle is simply to recognize if the current placement is even a valid one, where no two queens threaten each other. You often forget to check the long diagonal directions of each queen.

在与我的兄弟在标准棋盘上尝试八个皇后难题之后(顺便说一句:他在10分钟内解决了问题),我们很快注意到,尝试解决八个皇后难题的难点之一就是简单地认识到如果当前位置是有效位置,则没有两个女王互相威胁。 您经常忘记检查每个女王的长对角线方向。

I decided to create a web version of the eight queens puzzle only using JavaScript making it playable on most browsers (mobile and desktop), that incorporates a view, that shows you where you can’t place another queen on the board according to your current queen placement.

我决定仅使用JavaScript来创建八个皇后难题的网络版本,使其在大多数浏览器(移动和台式机)上均可播放,并结合了一个视图,该视图显示了您无法根据当前情况在板上放置另一个皇后皇后位置。

So that I don’t have to create everything from scratch, I used EaselJS, which is a JavaScript library for working with the canvas element. This is a very lightweight solution to create simple web games and the documentation and user support was good enough, so that I didn’t get stuck too often during development. But like most times when developing something new, you underestimate how much time it would take to finish the project. I estimated only needing around 150 lines of code and one afternoon to get it done, but it ended up being a few days and around 300 lines of code.

为了不必从头开始创建所有内容,我使用了EaselJS ,这是一个用于处理canvas元素JavaScript库。 这是一个非常轻量级的解决方案,用于创建简单的网络游戏,并且文档和用户支持足够好,因此在开发过程中我不会经常被卡住。 但是像大多数时候开发新事物一样,您低估了完成项目所花费的时间。 我估计只需要大约150行代码和一个下午就可以完成它,但是最终却花了几天时间和300行代码。

8 Queens Puzzle网页游戏 (8 Queens Puzzle Web Game)

So this is my final game. You can go ahead and try to place eight queens onto the chessboard. Queens are placed by clicking (or tapping when you’re on a touchscreen) the tile you want to place your next queen on. You can press the UNDO button to remove the last placed queen. A red tile means, that this tile is being threatened by an already placed queen, so you can’t place your next queen there anymore.

这是我的最后一场比赛。 您可以继续尝试将八个皇后放在棋盘上。 通过单击(或在触摸屏上点击时)要放置下一个皇后的图块来放置皇后。 您可以按UNDO按钮删除最后放置的女王。 红色磁贴表示该磁贴受到已经放置的皇后的威胁,因此您不能再将下一个皇后放置在此处。

You need a Frames Capable browser to view this content.

您需要具有Frames Capable的浏览器才能查看此内容。

I really noticed from playing it with the red tiles showing you invalid positions, it is easier to come up with a solution just by guessing. If you had the same experience, here is another version of the game, only the red tiles aren’t displayed anymore and you only get a warning message when you try to place a queen at a threatened position. This is considerably harder and gives you an experience closer to actually trying the puzzle out on a real chessboard:

我真的注意到,通过使用显示您无效位置的红色方块来进行播放,仅凭猜测就更容易找到解决方案。 如果您具有相同的体验,则这是游戏的另一个版本,仅不再显示红色方块,并且当您尝试将女王放置在威胁位置时,您只会收到警告消息。 这要困难得多,并为您提供接近在真正的棋盘上实际尝试难题的体验:

You need a Frames Capable browser to view this content.

您需要具有Frames Capable的浏览器才能查看此内容。

So now that we know, that we humans can solve the eight queens puzzle, let us see how CSPs can be solved using Python.

现在我们知道人类可以解决八个皇后难题,让我们看看如何使用Python解决CSP。

用Python解决CSP (Solving CSPs in Python)

Now I am certainly no expert on how so-called “CSP solvers” are written, but I know that logical methods like resolution are used to simplify the problem to a degree. From there on clever heuristics are used, that make it more probable for you to find a solving solution faster. You have to define the variables and the constraints, but the actual calculation is going to be a black box for us in this case.

现在,对于编写所谓的“ CSP求解器”,我当然不是专家,但是我知道使用诸如解析度之类的逻辑方法可以在一定程度上简化问题。 从那里开始,就使用了巧妙的启发式方法,这使您更有可能更快地找到解决方案。 您必须定义变量和约束,但是在这种情况下,实际计算对我们而言将是一个黑匣子。

I found a module called python-constraint, that offers us a CSP resolver written in Python. The module’s web page also shows some additional examples for puzzles that can be solved as a CSP. It’s really straightforward to use, although it seems to only work with Python 2 (not 3) so be aware of that.

我找到了一个名为python-constraint的模块,该模块为我们提供了用Python编写的CSP解析器。 该模块的网页还显示了一些其他难题示例,可以将其作为CSP解决。 它实际上非常简单易用,尽管它似乎仅适用于Python 2(不适用于3),所以请注意这一点。

In a CSP there are variables that can hold certain values (sometimes called states) and constraints that have to be held by the variables. The set of values that a variable can be assigned with is also called its domain. A constraint is a logical expression that has to hold. Now lets see how we can describe the eight queens puzzle as a CSP.

在CSP中,有些变量可以保存某些值(有时称为状态),而这些变量必须包含约束。 可以分配给变量的一组值也称为其 。 约束是必须保留的逻辑表达式。 现在让我们看看如何将八个皇后难题描述为CSP。

First we are going the define our variables and their respective domains. The most intuitive approach is to have one variable describe the position of one queen on the chessboard. So we are going to need a total of eight variables. In our solution (that I found here on stackoverflow) we are going to name each variable (/queen) as a number and this will describe the column in which this queen is placed, so the number is in the range from 1 to 8. We can do this, since a column uniquely describes one queen, since two queens in the same column would threaten each other. The actual value of each variable will then describe the row in which the queen is placed. This means that each variable has a domain of the natural numbers between 1 and 8. This is how it looks written in Python code using python-constraint:

首先,我们要定义变量及其各自的域。 最直观的方法是用一个变量来描述一个皇后在棋盘上的位置。 因此,我们总共需要八个变量。 在我们的解决方案(即,我发现这里 #1)我们将命名每个变量(/王后),为号码和将描述在此王后被放置在列中,所以数量范围为从1到8。我们可以这样做,因为一列唯一地描述了一个女王,因为同一列中的两个女王会互相威胁。 然后,每个变量的实际值将描述放置皇后的行。 这意味着每个变量的自然数都在1到8之间。这就是使用python-constraint用Python代码编写的样子:

from constraint import *

problem = Problem()
cols = range(1,9) # these are the variables
rows = range(1,9) # these are the domains
problem.addVariables(cols, rows) # adding multiple variables at once

The next step is to add our constraints. In the eight queens puzzle the overall constraint is that no two queens should threaten each other. But since we have to write our constraints as logical expressions, let’s divide the different moves of the queen as separate constraints. So we are going to loop through our list of queens and add constraints to each pair of different queens. Let’s first look at the code and then see what was done here:

下一步是添加我们的约束。 在八个皇后难题中,总的限制是任何两个皇后都不应该互相威胁。 但是由于必须将约束写为逻辑表达式,因此我们将女王的不同动作划分为单独的约束。 因此,我们将遍历皇后列表,并为每对不同的皇后添加约束。 让我们先看一下代码,然后看这里做了什么:

We are adding constraints between each pair of two queens saying, that are not allowed to meet up diagonally, described by this clever comparison of the absolute difference between the row and column of each queen. The second part of the logical expression makes sure, that no two queens are in the same row. And this then outputs us the following solution:

我们在每两个皇后之间增加了约束,即不能斜对接,这是通过对每个皇后的行和列之间的绝对差的巧妙比较来描述的。 逻辑表达式的第二部分确保没有两个皇后在同一行。 然后向我们输出以下解决方案:

{1: 8, 2: 4, 3: 1, 4: 3, 5: 6, 6: 2, 7: 7, 8: 5}

The first number describing the column and the second one the row of a queen (although this also can be swapped as long as it is done consistently). And if you try this out on the game on top, you will notice that this is a valid solution to the eight queen puzzle.

第一个数字描述的是列的第一个数字,第二个数字描述的是女王/王后的行(尽管也可以交换,只要操作一致即可)。 并且,如果您在最上面的游戏中尝试此操作,您会注意到这是八皇后难题的有效解决方案。

Here is a gist to the JavaScript code for the queen game and the full script for the python CSP solution.

这是女王游戏JavaScript代码和python CSP解决方案的完整脚本的要点。

If you have any questions regarding me or this post, then just write a comment in the section below. And if you enjoy reading my content I suggest you to sign up to my Rather Read Newsletter, where I will notify you about every new post as well as about recent books, articles, artists that I discovered in the past few weeks.

如果您对我或这篇文章有任何疑问,请在下面的部分中写评论。 如果您喜欢阅读我的内容,我建议您注册我的Rather Read时事通讯,在这里我会通知您有关我最近几周发现的每条新帖子以及最近的书籍,文章和艺术家的信息。

Thank you for reading, have a nice week!

谢谢您的阅读,祝您度过愉快的一周!

订阅我的邮件列表 (Subscribe to my mailing list)

* indicates required
*表示必填

View previous campaigns.

查看以前的广告系列。

翻译自: https://www.pybloggers.com/2016/03/eight-queens-puzzle/

拼图登陆拼图二维码验证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值