Tag Archives: WAI

The architecture of w-ai.org – Protocol

I’ll describe how to run a match, and how to implement a judge (and a player) if you want to add a new game to WAI.

Running a match is very simple. Each judge (an executable complied from *_judge.cpp) and players (executables complied from users’ submissions) are isolated. To run a game, just run match.exe with three parameters:

./match.exe judge player1 player2

where judge, player1, player2 are three executable file.

When match.exe starts, three processes will be forked, i.e., one judge process and two player processes. (Actually it’s six because of the sandbox, but we will ignore this for now.) There processes interact with the master process through stdin and stdout.

Let’s take the othello for example. At first, You should refer the protocol spec and sample player code here. Initially, the judge should tell one player to move firstly, and tell the other player to move secondly. He should write two messages to stdout, ”>1: first” and “>2: second”. The prefix “>x: ” indicates sending the following message to player x. (The two players are numbered 1 and 2.) So the player 1’s first scanf(“%s”, buf) will receive string “first”, while the player 2 will receive “second”. Another command for the judge is “<x“, indicating that the judge is expecting player x‘s response, and the judge’s following read from his stdin will get the string which outputted by player x. You can refer the code of othello judge.

There is another common approach for this kind of ‘interactive’ program – providing a public library interface (for example, a header file), and requiring user’s program to call the library functions for communication. However, it make user’s local debugging more difficult, because the users have to link their codes with the given library to run. And another problem is how to give out the library. We can’t just put the binary file, because the ABI may be not compatible for different C/C++ compiler, let alone for different languages in future. I believe communicating through stdin/out is simple and robust, not only for the user, but also for the platform itself.

In next post, I’ll describe the magical match.exe in detail.

The architecture of w-ai.org – Introduction

I’ve introduced WAI (http://w-ai.org) in my previous post. You can find the source code at github. To help someone who may be interested in contribution to this project, I’ll write a series of posts about the architecture of the system.

There are four main components of WAI:

1) Sandbox and other stuff which tightly depends on specified OS

This component is similar with the judge service of traditional ACM-ICPC online judge systems. The main function is providing permission control, timing and the communication between user’s AI process and outside world.

Because it is full of system calls (ptrace() and pipe(), for example), this part is implemented in C++.

2) Web server (based on Node.js)

I have to admit that Node.js may not be the best choice for this project, for WAI is CPU-tensive, which usually can’t take advantage of Node’s asynchronous behavior. In fact, I wrote the web server in Python in the first place, but later I found I need a bi-way real-time communication between client-side and server-side (for the HvC feature), and the only simple method I can find is Socket.io, which is based on Node. Another reason of choosing Node is, I want to learn it, and it sounds cool :)

There is also another component in Node – the scheduling module, to decide which two AI should fight next.

3) Client side code (JavaScript / HTML5)

The UI is based on Twitter’s Bootstrap. Obviously, I’m not an expert at UI design, and the current UI is almost the best I can make. I’ll be very glad if you can help improving it.

And the most complicated code in this part is the logic of interaction with users for each specific game. For example, drawing the chess board in canvas (yes, HTML5), responding user’s mouse click, showing computer AI’s moving, and so forth. Some games are more complicated to show (for example, TankCraft) than others.

4) Judges for each game

Once a new game is added, the corresponding judge should be provided, which will receive the AI’s moving and determine who is the winner. There is a simple protocol based on std i/o, and the judges are designed to be interchangeable easily.

I’ll describe the details of each component in the future posts. Stay tuned~

广告一下我最近的成果: w-ai.org

最近略闲,做了一个AI对战平台,请猛击这里:http://w-ai.org。网站上有各种各样的策略型游戏,用户可以提交并不停改进自己的程序,与别人提交的程序进行比赛。

这个想法最早来自2010年百度之星初赛时,那年的初赛题目是一个以坦克大战为背景的策略型游戏,大家提交自己的程序互相PK,通过多轮淘汰赛,前若干名可以晋级。当时曾经有大牛组织了小规模的交流小组,定期把大家的最新程序互相跑一遍。虽然只是初赛,但有不少人都玩得很high。当时我就想到可以做这么个网站,现在终于有时间把这个想法实现一下,虽然肯定是非常小众的东西,但还是觉得挺有趣的。

目前这个平台支持中国象棋、黑白棋、五子棋(包含无禁手和有禁手两种规则)以及当年那个坦克大战,以后应该会陆续加入更多的游戏。用户提交的程序与系统的交互方式都是通过标准输入输出完成,具体到不同的游戏会有不同的格式,在网站的游戏介绍页面有详细说明,也有样例程序供参考。

另外,为了增加趣味性,部分游戏(主要是棋类)提供了Human vs Computer的功能,用户可以直接在网站上(用人脑)与别人提交的程序对战。

目前网站只实现了最基本的功能,只能算是alpha版,可以想见的是一定充满了各种各样的bug。欢迎各位多多试用,提出宝贵意见。

ps. 代码是放在github上的。网站涉及的技术方面的东西我以后有时间会另文详述。