Skip to content

roblaing/ggp_python_player

Repository files navigation

A Python script for Coursera's General Game Playing course

This is my attempt to write a Python player for Coursera's General Game Playing course offered by Stanford University's Professor Michael Genesereth.

It requires Prolog — yap, swipl or any other Prolog should work provided it can be called from the command line as a script.

After many hours of trying to write my own interpreter and looking at the various Prolog interfaces available for Python, I opted for the kludge of having my Python script generate a Prolog script which is then executed via subprocess.Popen(PROLOG, stdin = subprocess.PIPE, stdout = subprocess.PIPE), where the constant PROLOG is defined near the top of the file as:

PROLOG = ['swipl','-s', '/dev/stdin']

If you prefer yap, you can change that to:
PROLOG = ['yap','-L', '/dev/stdin']

Presumably other prologs would work the same with whatever flags they need to read a script (which is read from /dev/stdin for the above two prologs since they don't respect Unix's '-' convention).

At this stage, my script needs python2.7 or higher (for argparse) but doesn't work with python3 (yet) because of its dependency on BaseHTTPServer.

The default hostname is 127.0.0.1 and port is 9147 which can be changed by calling, say, python2.7 ggp_python_player.py -n 171.64.71.18 -p 9148.

There are two versions, a "no cache" version which I did to work around the problem that my hosting service quickly switches off the instance of the player because it uses too much memory, and a "with cache version" which creates a stronger player and also has the option of writing the game tree created as python dictionary out as a graphviz graphic.

Adding -g filename will generate a graphviz dot file which can the be used to generate a graphic of the game tree like the example below.

I'm only an intermediate Python and novice Prolog programmer, so suggestions from advanced programmers on how to improve this code will be gladly accepted.

A third version which stores the game tree as a postgres database is work in progress.

Game Description Language (GDL)

A key job of this script is to interpret Game Description Language (GDL) scripts sent to it via http by the general game playing server. A fine manual is available online.

Using a partially completed game of Tic Tac Toe used in Exercise 6.4.1 as an example, the diagram (produced by graphiz from the python dictionary structure I use to store the game in) illustrates how the Montecarlo method explores the game tree and comes up a "best move" from a given state.

Glossary

Game Tree
A directed graph whose nodes are positions in a game and whose edges are moves.
Percept
A percept is detected and acted upon by an actuator.
Ply
One ply corresponds to one level of the game tree.

About

A Python script for Coursera's General Game Playing course

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages