国际象棋简单ai

by Lauri Hartikka

通过劳里·哈蒂卡(Lauri Hartikka)

建立简单国际象棋AI的分步指南 (A step-by-step guide to building a simple chess AI)

Let’s explore some basic concepts that will help us create a simple chess AI:

让我们探索一些基本概念,这些概念将帮助我们创建一个简单的国际象棋AI:

  • move-generation移动代
  • board evaluation董事会评估
  • minimax极小值
  • and alpha beta pruning.和alpha beta修剪。

At each step, we’ll improve our algorithm with one of these time-tested chess-programming techniques. I’ll demonstrate how each affects the algorithm’s playing style.

在每一步中,我们都会使用这些经过时间考验的国际象棋编程技术中的一种来改进算法。 我将演示每种方法如何影响算法的播放风格。

You can view the final AI algorithm here on GitHub.

您可以在GitHub上查看最终的AI算法。

第1步:移动世代和展示板 (Step 1: Move generation and board visualization)

We’ll use the chess.js library for move generation, and chessboard.js for visualizing the board. The move generation library basically implements all the rules of chess. Based on this, we can calculate all legal moves for a given board state.

我们将使用Chess.js库生成棋盘 ,并使用Chessboard.js可视化棋盘。 移动生成库基本上实现了所有国际象棋规则。 基于此,我们可以计算给定董事会状态下的所有合法举动。

Using these libraries will help us focus only on the most interesting task: creating the algorithm that finds the best move.

使用这些库将帮助我们仅专注于最有趣的任务:创建找到最佳动作的算法。

We’ll start by creating a function that just returns a random move from all of the possible moves:

我们将从创建一个函数开始,该函数仅从所有可能的移动中返回随机移动:

Although this algorithm isn’t a very solid chess player, it’s a good starting point, as we can actually play against it:

尽管此算法不是非常可靠的国际象棋棋手,但它是一个很好的起点,因为我们可以与之抗衡:

步骤2:位置评估 (Step 2 : Position evaluation)

Now let’s try to understand which side is stronger in a certain position. The simplest way to achieve this is to count the relative strength of the pieces on the board using the following table:

现在,让我们尝试了解在特定位置上哪一方更强。 实现此目的的最简单方法是使用下表来计算木板上零件的相对强度:

With the evaluation function, we’re able to create an algorithm that chooses the move that gives the highest evaluation:

使用评估功能,我们能够创建一种算法,该算法选择具有最高评估能力的举动:

The only tangible improvement is that our algorithm will now capture a piece if it can.

唯一切实的改进是,我们的算法现在可以捕获一块。

步骤3:使用Minimax搜索树 (Step 3: Search tree using Minimax)

Next we’re going to create a search tree from which the algorithm can chose the best move. This is done by using the Minimax algorithm.

接下来,我们将创建一个搜索树,算法可以从中选择最佳移动。 这是通过使用Minimax算法完成的。

In this algorithm, the recursive tree of all possible moves is explored to a given depth, and the position is evaluated at the ending “leaves” of the tree.

在该算法中,将所有可能移动的递归树探索到给定深度,并在树的末尾“叶子”处评估位置。

After that, we return either the smallest or the largest value of the child to the parent node, depending on whether it’s a white or black to move. (That is, we try to either minimize or maximize the outcome at each level.)

之后,根据要移动的是白色还是黑色,我们将子节点的最小值或最大值返回到父节点。 (也就是说,我们尝试在每个级别上最小化或最大化结果。)

With minimax in place, our algorithm is starting to understand some basic tactics of chess:

有了minimax,我们的算法就开始了解国际象棋的一些基本策略:

The effectiveness of the minimax algorithm is heavily based on the search depth we can achieve. This is something we’ll improve in the following step.

minimax算法的有效性很大程度上取决于我们可以实现的搜索深度。 这是我们将在下一步中改进的内容。

步骤4:Alpha-beta修剪 (Step 4: Alpha-beta pruning)

Alpha-beta pruning is an optimization method to the minimax algorithm that allows us to disregard some branches in the search tree. This helps us evaluate the minimax search tree much deeper, while using the same resources.

Alpha-beta修剪是对minimax算法的一种优化方法,它使我们可以忽略搜索树中的某些分支。 这有助于我们在使用相同资源的情况下更深入地评估minimax搜索树。

The alpha-beta pruning is based on the situation where we can stop evaluating a part of the search tree if we find a move that leads to a worse situation than a previously discovered move.

alpha-beta修剪基于这样的情况:如果我们发现比以前发现的移动导致更糟的情况的移动,我们可以停止评估搜索树的一部分。

The alpha-beta pruning does not influence the outcome of the minimax algorithm — it only makes it faster.

alpha-beta修剪不会影响minimax算法的结果-只会使其更快。

The alpha-beta algorithm also is more efficient if we happen to visit first those paths that lead to good moves.

α-β算法也更有效的,如果我们碰巧访问这些路径是导致好棋。

With alpha-beta, we get a significant boost to the minimax algorithm, as is shown in the following example:

有了alpha-beta,我们极大地提高了minimax算法,如以下示例所示:

Follow this link to try the alpha-beta improved version of the chess AI.

单击此链接尝试国际象棋AI的alpha-beta改进版本。

步骤5:改进的评估功能 (Step 5: Improved evaluation function)

The initial evaluation function is quite naive as we only count the material that is found on the board. To improve this, we add to the evaluation a factor that takes in account the position of the pieces. For example, a knight on the center of the board is better (because it has more options and is thus more active) than a knight on the edge of the board.

初始评估功能非常幼稚,因为我们只计算板上的材料。 为了改善这一点,我们在评估中增加了考虑部件位置的因素。 例如,板子中央的骑士比板子边缘的骑士更好(因为它有更多选择,因此更活跃)。

We’ll use a slightly adjusted version of piece-square tables that are originally described in the chess-programming-wiki.

我们将使用经过稍作调整的棋子方桌版本,该版本最初在chess-programming-wiki中进行了描述。

With the following improvement, we start to get an algorithm that plays some “decent” chess, at least from the viewpoint of a casual player:

经过以下改进,至少从休闲玩家的角度出发,我们开始获得一种可以下“体面”象棋的算法:

结论 (Conclusions)

The strength of even a simple chess-playing algorithm is that it doesn’t make stupid mistakes. This said, it still lacks strategic understanding.

即使是简单的下棋算法,其优点也在于它不会犯愚蠢的错误。 也就是说,它仍然缺乏战略上的了解。

With the methods I introduced here, we’ve been able to program a chess-playing-algorithm that can play basic chess. The “AI-part” (move-generation excluded) of the final algorithm is just 200 lines of code, meaning the basic concepts are quite simple to implement. You can check out the final version is on GitHub.

通过这里介绍的方法,我们已经能够编写能够下棋的国际象棋算法。 最终算法的“ AI部分”(不包括移动代)只有200行代码,这意味着基本概念非常容易实现。 您可以在GitHub上查看最终版本。

Some further improvements we could make to the algorithm would be for instance:

我们可以对该算法进行一些进一步的改进,例如:

  • move-ordering

    移动顺序

  • faster move generation

    更快的动作生成

  • and end-game specific evaluation.

    以及比赛结束后的评估。

If you want to learn more, check out the chess programming wiki. It’s a helpful resource for exploring beyond these basic concepts I introduced here.

如果您想了解更多信息,请查阅国际象棋编程维基 。 这是探索我在这里介绍的这些基本概念之外的有用资源。

Thanks for reading!

谢谢阅读!

翻译自: https://www.freecodecamp.org/news/simple-chess-ai-step-by-step-1d55a9266977/

国际象棋简单ai

国际象棋简单ai_建立简单国际象棋AI的分步指南相关推荐

  1. 以每月5美元的价格建立自己的博客[分步指南]

    关于技术的博客很有趣. 拥有自己的博客可以帮助他人,提高自己的技能,甚至可以找到工作. 在热门技术网站上写博客很棒. 显然,您是在Hackernoon上阅读的,所以您知道我这样做. 很有趣. 但是,拥 ...

  2. 使用html 语言建立一个简单的网页,如何用记事本建立简单的网页(1).doc

    第九章 网页制作 实验一 用记事本建立简单的HTML文件 [实验目的] 学会用HTML语言建立一个简单的网页. [实验内容] 建立一个网页,布局自定,包括自我介绍.图片.自己的电子信箱地址等,要求在标 ...

  3. Spring和WebSocket整合并建立简单的Web聊天室

    Spring和WebSocket整合并建立简单的Web聊天室 官方主页 Spring WebSocket 一.概述 WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. Web ...

  4. 简单快速建立pytorch环境YOLOv5目标检测 模型跑起来(超简单)

    简单快速建立pytorch环境+实现YOLOv5目标检测 模型跑起来(超简单) 一.下载yolov5模型代码: yolo代码 提取码:2022 下载后解压 二.简单快速创建pytorch环境: 1.条 ...

  5. 【实验练习】请建立简单线性回归模型,实现依据身高预测以为女性的体重,并对模型进行评估和优化。

    题目: 一组women的实验数据,数据内容来自The World Almanac and Book of Facts 1975,该数据集给出了年龄在30-39岁的15名女性的身高和体重数据,主要属性如 ...

  6. 通过Gazebo建立简单室内环境模型并用launch文件打开

    本文叙述如何直接使用Gazebo创建简单室内环境模型,并能够使用launch文件打开 一.利用Gazebo建立好环境模型 1. 打开Gazabo的编辑界面 运行如下命令后按Ctrl+B,进入到编辑界面 ...

  7. Photon教程——建立简单的Photon服务器(二)

    建立简单的Photon服务器(二) 上一篇博文(Photon教程--建立简单的Photon服务器(一))的地址:https://blog.csdn.net/ultramansail/article/d ...

  8. 自己快速提升SCI论文质量的简单方法:分清研究类型,遵从指南修改

    有什么诀窍吗? 还真有.就是: 根据指南把论文检查一遍. 临床科研常见类型 临床科研有不同的研究类型,并建立有不同的研究规范/指南(guidelines),也就形成了论文的审核表(checklist) ...

  9. ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 异常和错误处理 上一章节中,我们学习了 ASP.NET Cor ...

最新文章

  1. C++知识点杂记1——typedef、static_cast、const_cast、遍历二维数组、聚合类
  2. 树结构练习——排序二叉树的中序遍历(二叉搜索树)
  3. python基础补充内容
  4. vb跨域访问ajax,解决AJAX的跨域访问-两种有效示例
  5. 九、股票收盘价与滑动平均线MA
  6. OpenCV Dbt人脸检测Dbt face detection的实例(附完整代码)
  7. 超炫酷的HTML5视频播放器 支持手机移动页面
  8. Github 15K! 亿级向量相似度检索库Faiss 原理+应用
  9. 微信开发第7章 通过accesstoken获取用户黑名单列表
  10. Matlab随笔之三维图形绘制
  11. cupsd进程_Linux进程基础
  12. cb rm –rf_php执行rmrf命令
  13. [汇总]Web前端优化
  14. php mescroll,mescroll快速入门
  15. 固态硬盘测试软件有哪些,SSD测试软件有哪些?SSD测试软件盘点
  16. np.eye()和np.identity()
  17. PHP 把ofd格式文件转PDF,打开OFD格式文件及将OFD格式文件转换成PDF文件
  18. 10种方式卸掉感情垃圾
  19. 软件开发生命周期中的设计阶段_软件过程模型|如何进行团队式的软件开发?...
  20. 持久续航蓝牙耳机推荐,即使音质再好电量不足又有什么用?

热门文章

  1. 如何批量下载美拍作者页短视频
  2. python 时间序列prophet 模型分析_时间序列预测模型Prophet[Facebook]
  3. Sonar Qube安装
  4. 2021年茶艺师(初级)考试报名及茶艺师(初级)找解析
  5. 《商务与经济统计》学习笔记(二)---辛普森悖论
  6. TIANBOT MINI机器人在gazebo中使用键盘控制运动
  7. 一个验证字符串是否纯英语的方法
  8. p5js动态交互式码绘作业二小结
  9. js小游戏 - 消灭星星
  10. 1.1 系统介绍【斯纳克PACS远程会诊系统】