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.
For example:
Running a game with player1 and player2.
If player1 chooses "0" and player_2 chooses "1", then the
payoff pair for that particular game is
field[choice1][choice2], which in this
case is field[0][1].
To figure out the score that each player earns
for this particular game, simply look at the elements
of the pair at location field[0][1].
player1's score is field[0][1][0], player2's score is
field[0][1][1].
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.