Scott Hurring » Code » Python » Game theory arena

Download Code
download
game_v0.1.tgz
v0.1 alpha · Nov 19, 2004
(browse code)

Description

I was thinking of The Selfish Gene and game theory the other day, and i'm also trying to learn Python better. So i decided to marry the two and try to come up with some rudimentary framework for "Bots" to compete against one another in "Games" (like Prisoner's Dilemma).

This is my first batch of source code, which i'm sure could be improved upon greatly. Below is a brief breakdown of the different modules, functions and what they're supposed to do.


Design of the Program

Games.py

The only type of game i've implemented is the "Two Person" Prisoner's-Dilemma-type game. Two bots are chosen at random out of the pool of available bots and prompted for an answer. Then, both answers are looked up in the payoff matrix (Field) and each bot is told what the other bot chose. Then a score is computed and the game is over.

Bots.py

A bot is simply an agent in the game. When a two-person game simulation is run, the Arena selects 2 random bots out of the pool of available bots, and asks each of them "Given this field, what's your choice?".

answer() - (called by Game) - Bot returns the item in the field that it would like.
inform() - (called by Game) - Game informs bot what the other player chose.

Fields.py

A field is simply a class that defines field, which is the payoff matrix for the bots.

Field is simply a three dimensional array: field = ( (w,x), (y,z) ). w,x,y,z are each a pairs of numbers (A,B). A is player1's score is, and B is player2's score.

Arena.py

The arena is responsible for initializing all the bots you want to compete against one another (via Arena.add_bot()), initializes the chosen game class.

It then iterates for a certain number of rounds, selecting two bots at random each round, asking them for their choices and computing their scores.

The Arena is also responsible for pruning and spawning new bots through the prune and birth functions. If a bot falls below the prune_thresh score, it will be removed from the pool of active bots. If it rises above birth_thres, it will then have the number of children specified by birth_children and the original parent will be killed.