org.jscience.computing.game
Class PrisonersDilemmaAgent

java.lang.Object
  extended by org.jscience.computing.game.PrisonersDilemmaAgent

public class PrisonersDilemmaAgent
extends java.lang.Object

A simple implementation of an agent that obeys the rules of the prisoner's dilemma. This implementation contains five commonly-used strategies: all cooperate, all defect, tit-for-tat, pavlov, or random. For a more detailed look at the IPD and different strategies, please see the Generation5 essay.


Field Summary
protected  int agentPoints
          Number of points agent has acquired
static int ALL_C
          Cooperate for all
static int ALL_D
          Defect for all
static int COOPERATE
          Cooperate with partner
static int DEFECT
          Defect against partner
static int DREW
          Drew the last round (returned by run)
protected  int lastRoundResult
          What was the result of the last round?
static int LOST
          Lost the last round (returned by run)
protected  int myLastMove
          The move this agent last played
protected  int partnerLastMove
          What your opponent last played
static int PAVLOV
          Pavlov strategy
static int RANDOM
          Random strategy
static int STRATEGIES
          Number of strategies - for selecting random strategy
static int TFT
          Tit-for-tat strategy
static int WON
          Won the last round (returned by run)
 
Constructor Summary
PrisonersDilemmaAgent()
          Default constructor (uses TFT strategy).
PrisonersDilemmaAgent(int strategy)
          Create a new instance of PrisonersDilemmaAgent with a given strategy.
 
Method Summary
 void addPoints(int p)
          Increment the points for the agent.
 int getMove()
          Retrieve the agent's next move according to the current strategy.
 int getPoints()
          Retrieve the number of points that the agent has acquired.
 int getStrategy()
          Retrieve the strategy the PD agent.
static void main(java.lang.String[] args1)
          Simple test using an iterated prisoner's dilemma situtation.
 void reset()
          Reset the agent's points, note that strategy remains untouched
 int run(PrisonersDilemmaAgent partner)
          Make a run against your 'partner' with prisoner's dilemma rule.
 void setStrategy(int strategy)
          Set the strategy type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALL_C

public static final int ALL_C
Cooperate for all

See Also:
Constant Field Values

ALL_D

public static final int ALL_D
Defect for all

See Also:
Constant Field Values

TFT

public static final int TFT
Tit-for-tat strategy

See Also:
Constant Field Values

RANDOM

public static final int RANDOM
Random strategy

See Also:
Constant Field Values

PAVLOV

public static final int PAVLOV
Pavlov strategy

See Also:
Constant Field Values

STRATEGIES

public static int STRATEGIES
Number of strategies - for selecting random strategy


COOPERATE

public static final int COOPERATE
Cooperate with partner

See Also:
Constant Field Values

DEFECT

public static final int DEFECT
Defect against partner

See Also:
Constant Field Values

WON

public static final int WON
Won the last round (returned by run)

See Also:
Constant Field Values

DREW

public static final int DREW
Drew the last round (returned by run)

See Also:
Constant Field Values

LOST

public static final int LOST
Lost the last round (returned by run)

See Also:
Constant Field Values

agentPoints

protected int agentPoints
Number of points agent has acquired


partnerLastMove

protected int partnerLastMove
What your opponent last played


myLastMove

protected int myLastMove
The move this agent last played


lastRoundResult

protected int lastRoundResult
What was the result of the last round?

Constructor Detail

PrisonersDilemmaAgent

public PrisonersDilemmaAgent()
Default constructor (uses TFT strategy).


PrisonersDilemmaAgent

public PrisonersDilemmaAgent(int strategy)
Create a new instance of PrisonersDilemmaAgent with a given strategy.

Parameters:
strategy - the strategy to use.
See Also:
ALL_C, ALL_D, TFT, RANDOM, PAVLOV
Method Detail

getStrategy

public int getStrategy()
Retrieve the strategy the PD agent.

Returns:
the strategy currently being used.

setStrategy

public void setStrategy(int strategy)
Set the strategy type.

Parameters:
strategy - the strategy type.

reset

public void reset()
Reset the agent's points, note that strategy remains untouched


run

public int run(PrisonersDilemmaAgent partner)
Make a run against your 'partner' with prisoner's dilemma rule. A run consists of one iteration using the given strategies of this agent and the partner agent. Points are allocated to either this agent or the partner automatically, according to the result of the run.

Parameters:
partner - your partner for this run.
Returns:
WON, LOST or DRAW depending on the result.

getMove

public int getMove()
Retrieve the agent's next move according to the current strategy.

Returns:
either DEFECT or COOPERATE according to strategy.

addPoints

public void addPoints(int p)
Increment the points for the agent.

Parameters:
p - the number to increment the agent's point tally by.

getPoints

public int getPoints()
Retrieve the number of points that the agent has acquired.

Returns:
the number of points acquired.

main

public static void main(java.lang.String[] args1)
Simple test using an iterated prisoner's dilemma situtation. Results are printed as a simple table with percentage scores.

Parameters:
args1 - not required.