FoolGo

基于 MCTS 的 Go A.I.,不含神经网络。「A Go A.I. based on MCTS WITHOUT Neural Networks」

  • Owner: chncwang/FoolGo
  • Platform: Linux
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

FoolGo 是一个基于 MCTS(蒙特卡洛树搜索)的围棋人工智能。

它显示了目前 9x9 棋盘上的初级水平,因为它不使用神经网络,而只使用简单的UCT(UCB for Tree)算法和蒙特卡洛模拟。

底层的数据结构和算法是相当有效的。具体来说,它在我的 MacBook Air 2014 上每秒运行4万个蒙特卡洛游戏。

围棋游戏实现的关键思想如下:

  • 在游戏过程中,我们使用不相交集合实现了一串连接的片段,从而有效地更新片段串。
  • 我们使用位操作来有效地更新片串的自由点数。例如,当我们将几个片段字符串合并成一个字符串时,我们使用 or。
  • 我们没有为树搜索实现树结构,而是使用哈希表来保持游戏状态。特别是,我们使用 Zobrist 哈希递增地计算游戏状态的哈希值。

对于 MCTS,我们在 CPU 上使用多线程搜索。具体来说,我们只是禁止一个线程访问正在被对等线程访问的节点。

由于其精心设计的 OOP 设计,这个项目具有高度的可读性和可扩展性。一些重要的类列举如下:

  • FullBoard:一个游戏板类,提供有效的方法,如 PlayMove 来更新游戏状态。
  • Player:一个接口,提供可继承的方法,如 NextMove。例如,RandomPlayer,一个 Player 子类,在调用它的 NextMove 时将返回下一步棋的随机合法位置,而 UCTPlayer 则执行 UCT 算法来寻找下一步棋。
  • Game:一个包含两个 Player 对象的接口。例如,MonteCarloGame 包含两个 RandomPlayer 对象。

Make

  • 需要 boost
  • mkdir build && cd build
  • cmake ...
  • make

联系我们

电子邮件:chncwang@gmail.com

QQ群:823648893

Main metrics

Overview
Name With Ownerchncwang/FoolGo
Primary LanguageC++
Program languageC++ (Language Count: 3)
PlatformLinux
License:MIT License
所有者活动
Created At2012-12-03 19:25:56
Pushed At2021-06-29 13:38:02
Last Commit At2021-06-29 21:38:02
Release Count3
Last Release Namev0.2.1-alpha (Posted on )
First Release Namev0.1.0-alpha (Posted on )
用户参与
Stargazers Count379
Watchers Count51
Fork Count194
Commits Count97
Has Issues Enabled
Issues Count4
Issue Open Count0
Pull Requests Count2
Pull Requests Open Count0
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

A Go A.I. based on MCTS(Monte Carlo Tree Search).

it only shows beginner level on 9x9 board by far, for the temporary lack of deep learning.

The underlying data structure and algorithms are quite efficient, and I can run 40 thousands monte carlo game on my MacBook Air(multithread & 9x9 board)

Key implementation of the board structure:

  • Piece strings are implemented with disjoint sets.
  • Liberty intersections of piece strings are modified fast by bit operations.
  • Boards' hash algorithm is the zobrist hash and game states' information is all stored in a hash table.

This projectr is highly readable and scalable, for its well-designed OOP design (I learned it from the book《现代计算围棋基础》 authored by Professor Liu).
Here lists several important classes:

  • FullBoard is a fully functional board class which supplies efficient methods such as PlayMove
  • Player interface supplies scalable methods such as NextMove. For example, RandomPlayer which is an implementation of Player, chooses a random position per time in NextMove's implementation.
  • Game interface is the combination of two Player classes. For example MonteCarloGame accepts two RandomPlayer instances in its build method.

C++ coding style

  • This project mainly follows google C++ coding style guideline, approving most items.
  • Considering both performance and supporting boards of variant sizes, it uses template classes, which treat board size as template parameters. Thus it allocates boards on stack area.
  • std::unique_ptr is prefered, instead of raw pointers.
  • Unit tests are prefered, using gtest.

基于蒙特卡罗树搜索的围棋A.I.

目前只有在9路棋盘上表现出入门级棋力。

底层的数据结构和算法相当高效,在我的MacBook Air上1秒钟能跑4万局蒙特卡罗对局(多线程)。

关键方法:

  • 并查集实现的棋串(简单的链表实现,若改为树结构,加上路径压缩,会更高效些,但对整体的表现应该影响不大)。
  • 位或运算实现棋串『气』的更新。
  • 棋盘的哈希算法用Zobrist哈希,通过哈希表存放每个状态的胜率等信息

在面向对象设计上,这个项目高度可扩展(参考了刘知青教授的《现代计算机围棋基础》),介绍主要的几个类:

  • FullBoard类,功能完整的棋盘类,提供高效的落子方法等。
  • Player抽象类,提供可扩展的棋步选择方法,子类如RandomPlayer,就是Player类的随机落子的实现。
  • Game类,Player类的组合,比如子类MonteCarloGame就组合了两个RandomPlayer。

C++编码指南

  • 本项目遵循google的guideline中的多数条目。
  • 出于性能的考虑,本项目大量使用模板类,在编译期确定棋盘的大小,以便尽可能多地使用静态数组。
  • C + class + 模板,不使用模板元编程。
  • 用智能指针管理内存
  • 用gtest做单元测试

有关N3LDG

  • 本项目的深度学习部分采用经我魔改过(主要是针对每一类节点,实现了cuda部分,以期更好地压榨GPU性能)的N3LDG框架。
  • N3LDG是一个用于自然语言处理的深度学习框架,支持动态计算图,C++实现。

TODO

  • 正实现policy network,俟国庆再续。
  • 被value network要用的计算资源吓了一跳,如果谁有用有监督学习训练value network的思路,请不吝赐教!

Make

  • boost is required
  • mkdir build && cd build
  • cmake ..
  • make

问题交流