Courses/CS 461/Winter 2006/Darren Iwaki/week 6

From CSWiki

Jump to: navigation, search

Image:DIPD.jpg Purpose: Tried to make homework 5 version into a repast model. Image:DIPD2.jpg Tried to allow the user to decide which strategies that wanted to play using the user interface. Had problems getting the graph and display to always update.. leaving a screen of all one player that never changed despite changing the agent types Image:DIPD3.jpg

ModelGUI.java


import uchicago.src.sim.analysis.OpenSequenceGraph; import uchicago.src.sim.analysis.Sequence; import uchicago.src.sim.engine.Controller; import uchicago.src.sim.engine.SimInit; import uchicago.src.sim.gui.DisplayConstants; import uchicago.src.sim.gui.DisplaySurface; import uchicago.src.sim.gui.Object2DDisplay;

public final class ModelGUI extends Model {

   // GUI variables
   private OpenSequenceGraph graph; // The dynamic graph chart (provided by Repast)
//   private DisplaySurface dsurf;    // The display (grid) (provided by Repast)
   /**
    * The constructor of the GUI
    */
   public ModelGUI() {
       super();
   Controller.ALPHA_ORDER = false;
   }
   /**
    * The sequence of the number of ALLC's
    */
   final class SeqALLC implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[ALLC];
       }
   }
   /**
    * The sequence of the number of TFT's
    */
   final class SeqTFT implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[TFT];
       }
   }
   /**
    * The sequence of the number of Pavlov's
    */
   final class SeqPAVLOV implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[PAVLOV];
       }
   }
   
   
   /**
    * The sequence of the number of Pavlov's
    */
   final class SeqSPITEFUL implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[SPITEFUL];
       }
   }
   
   /**
    * The sequence of the number of ATFT's
    */
   final class SeqATFT implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[ATFT];
       }
   }
   /**
    * The sequence of the number of ALLD's
    */
   final class SeqALLD implements Sequence {
       // This is the method to be defined for Sequence
       public final double getSValue() {
           // Returns the appropriate value
           return (double) num[ALLD];
       }
   }
   
   
   /**
    * Initializing the GUI model
    */
   public final void setup() {
       // Initializing the original model first
       super.setup();
       // Specify the parameters to be displayed for setting by the user
       // Note that we removed numPlayers as that's not an independent
       // parameter anymore, but it rather depends on worldSize.
       params = new String[]{"worldSize","CC","CD","DC","DD", "isALLCActive","isTFTActive",
                               "isPAVLOVActive","isSPITEFULActive","isATFTActive","isALLDActive",
                               "pAdapt","pNOISE","historySteps"};
       // Setting the dimensions of each cell on the display (grid)
       DisplayConstants.CELL_WIDTH = 10;
       DisplayConstants.CELL_HEIGHT = 10;
       // Initializing the GUI-part
       // If there is already a graphics object from a previous run, we need
       // to delete it to clean up the screen.
       if (graph != null)
           graph.dispose();
       // If the display exists, let's dispose it!
   //    if (dsurf != null)
     //      dsurf.dispose();
       // Now it's time to create the display surface.
       // (For details see RePast's appropriate "How to" document.)
//       dsurf = new DisplaySurface(this, "GridIPD Display");
       // Registering it as part of our GUI
  //     registerDisplaySurface("Main", dsurf);
   }
   /**
    * The method to build the Model's internals
    * Here we build the model instrumentation after the model itself has
    * been built.
    */
   public final void buildModel() {
       // Build the original model first
       super.buildModel();
       // Build the GUI part
       buildDisplay();
   }
   /**
    * Build the GUI part
    */
   private void buildDisplay() {
       // Create a sequence chart graph and set it up
       // (For details see RePast's appropriate "How to" document.)
       graph = new OpenSequenceGraph("Frequencies", this);
       graph.setXRange(0.0, 100.0);
       graph.setYRange(0.0, (double) numPlayers);
       graph.setAxisTitles("Generations", "Number");
       // Add the sequences we have defined
       if (isALLCActive)
           graph.addSequence("ALLC", new SeqALLC());
       if (isTFTActive)
           graph.addSequence("TFT", new SeqTFT());
       if (isPAVLOVActive)    
           graph.addSequence("PAVLOV", new SeqPAVLOV());
       if (isSPITEFULActive)
           graph.addSequence("SPITEFUL", new SeqTFT());
       if (isATFTActive)
           graph.addSequence("ATFT", new SeqATFT());
       if (isALLDActive)
           graph.addSequence("ALLD", new SeqALLD());
       // Display the graph right away
       graph.display();
       // Do the first update to it
       graph.step();
       // Now create the 2D-display itself
       final Object2DDisplay display = new Object2DDisplay(world);
       // Let's link our collection of the agents to it.
       // (The display will show players from this list.)
       display.setObjectList(agentList);
       // Adding the grid display to the display we have created before
 //      dsurf.addDisplayableProbeable(display, "Display");
       // Letting RePast know that we have this display up
   //    addSimEventListener(dsurf);
       // Displaying the display right away
     //  dsurf.display();
   }
   /////////////////////////////////////////////////////////////////////////
   //  Iterated method  ////////////////////////////////////////////////////
   /////////////////////////////////////////////////////////////////////////
   /**
    * The main activity of the time step.
    * In each time step, first execute the model step and then update the
    * graph
    */
   public final void step() {
       // Do the original model's step() method first
       super.step();
       // Do the GUI-part of it by updating the graph and the display
       graph.step();
 //      dsurf.updateDisplay();
   }
   /////////////////////////////////////////////////////////////////////////
   //  RePast Parameter Panel Methods  /////////////////////////////////////
   /////////////////////////////////////////////////////////////////////////
   // In the following we provide get/set methods for all the parameters
   // we listed in params (see the setup() method)
   // Note that we removed numPlayers from here, too, as that's not
   // listed in params anymore.
   public int getCC()  {   return prefs.getCC(); }
   public int getCD()  {   return prefs.getCD(); }
   public int getDC()  {   return prefs.getDC(); }
   public int getDD()  {   return prefs.getDD(); }
   
   public void setCC ( int x ) { prefs.setCC( x ); }
   public void setCD ( int x ) { prefs.setCD( x ); }
   public void setDC ( int x ) { prefs.setDC( x ); }
   public void setDD ( int x ) { prefs.setDD( x ); }
   
   
   public final int getWorldSize() {
       return worldSize;
   }
   public final void setWorldSize(final int n) {
       worldSize = n;
   }
   

// Settings for ALLC -----------------------------------------------------------

   public boolean getisALLCActive ()
   {
       return isALLCActive; 
   }
   public void setisALLCActive (boolean x)
   {
       isALLCActive = x;
   }
   public final double getPALLC() {
       return pALLC;
   }
   public final void setPALLC(final double p) {
       pALLC = p;
   }

// ********************************************************************************

// Settings for TFT --------------------------------------------------------------

   public boolean getisTFTActive ()    {   return isTFTActive; }
   public void setisTFTActive( boolean x)  {   isTFTActive = x;    setup(); }
   public final double getPTFT() { return pTFT;  }
   public final void setPTFT(final double p) {  if ( p == 0 ) setisTFTActive( false);   pTFT = p;   }

// ******************************************************************************** // Settings for PAVLOV -----------------------------------------------------------

   public boolean getisPAVLOVActive ()    {   return isPAVLOVActive; }
   public void setisPAVLOVActive( boolean x)  {   isPAVLOVActive = x;    setup(); }   
   public final double getPPAVLOV() { return pPAVLOV;}
   public final void setPPAVLOV(final double p) {  if ( p == 0 ) setisPAVLOVActive( false ); pPAVLOV = p; }

// *********************************************************************************** // Settings for SPITEFUL -----------------------------------------------------------

   public boolean getisSPITEFULActive ()    {   return isSPITEFULActive; }
   public void setisSPITEFULActive( boolean x)  {   isSPITEFULActive = x;    setup(); }   
   public final double getPSPITEFUL()  {   return pSPITEFUL;   }
   public final void setPSPITEFUL( final double p ) {  if ( p == 0 ) setisSPITEFULActive( false ); pSPITEFUL = p; }

// *********************************************************************************** // Settings for ATFT -----------------------------------------------------------

   public boolean getisATFTActive ()    {   return isATFTActive; }
   public void setisATFTActive( boolean x)  {   isATFTActive = x;    setup(); }  
   public final double getPATFT() {    return pATFT;   }
   public final void setPATFT(final double p) {    if ( p == 0) setisATFTActive( false ); pATFT = p;   }

// *********************************************************************************** // Settings for ALLD -----------------------------------------------------------

   public boolean getisALLDActive ()    {   return isALLDActive; }
   public void setisALLDActive( boolean x)  {   isALLDActive = x;   setup(); }  
   public final double getPALLD() {    return pALLD;   }
   public final void setPALLD(final double p) {    if ( p == 0) setisALLDActive( false );  pALLD = p;  }

// **************************************************************************************

// Settings for Noise ------------------------------------------------------------------

   public final double getPNOISE() {   return pNOISE;  }
   public final void setPNOISE(final double p) {   pNOISE = p; }

// *************************************************************************************

// Settings for history steps ----------------------------------------------------------

   public final double gethistorySteps(){ return historySteps; }
   public final void sethistorySteps( final int p )    {   historySteps = p;}

// *************************************************************************************

// Settings for Adapating --------------------------------------------------------------

   public final double getPAdapt() {   return pAdapt;  }
   public final void setPAdapt(final double p) {   pAdapt = p; }

// **************************************************************************************


   /**
    * Creating and starting your model.
    *
    * @param args the parameters to the program
    */
   public static void main(final String[] args) {
       final SimInit init = new SimInit();
       // We MUST create a ModelGUI object instead of an instance of Model
       // as in the parent class, in order to get the GUI functionality.
       final Model m = new ModelGUI();
       init.loadModel(m, null, false);
   }

}

Model.java


import uchicago.src.sim.engine.Controller; import uchicago.src.sim.engine.SimInit; import uchicago.src.sim.engine.SimpleModel; import uchicago.src.sim.space.Object2DTorus; import uchicago.src.sim.util.SimUtilities;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

public class Model extends SimpleModel {

   // [] position ( < 6)
   int ALLC;
   int TFT;
   int PAVLOV;
   int SPITEFUL;
   int ATFT;
   int ALLD;
   
   int NUMTYPES;               //possible number active assumed to be 6 if all true 
   final int POSSIBLENUMTYPES = 6;
   
   // Booleans to set what methods are actively in place, ALLC and ALLD set to active to escape 'no one playing errors'
   boolean isALLCActive      = true;
   boolean isTFTActive       = false;
   boolean isPAVLOVActive    = false;
   boolean isSPITEFULActive  = false;
   boolean isATFTActive      = false;
   boolean isALLDActive      = false;
   
   
   // Model variables
   Object2DTorus world;                  // The 2D grid (torus) representing
                                         // the world
   int worldSize;                        // The size of the world's 'sides'
   int numPlayers;                       // The number of agents, every single agent in the  (>100)
   
   double pALLC, pTFT, pPAVLOV, pSPITEFUL, pATFT, pALLD;     // Probability of types
   double pNOISE;
   int[] num;                            // Number of agents of the different
                                         // types active 
   
   int historySteps = 1;                 // number of steps an agent can remember
   
   private int maxIter = 111111;                  // Number of rounds
   double pAdapt = 1;                        // Probability of adaptation
                                         // Note that this feature is an
                                         // extension of the original Cohen et al.
                                         // model. pAdapt=1.0 yields the original
                                         // behavior.
   PayOff prefs = new PayOff(3,0,5,1);   // Payoff Model, can be modified with CC:, CD:, DC:, DD:
   
   public Model() 
   {
       super();
       Controller.CONSOLE_ERR = false;
       Controller.CONSOLE_OUT = false;
   }
   /**
    * Initializing the model.
    */
   public void setup() 
   {
       // The setup() method of the parent MUST be called first!
       super.setup();
       // Setting the name of our model
       name = "GridIPD Model";
       worldSize = 10;                     // We set this one instead of numPlayers
       numPlayers = worldSize * worldSize;
       int numCountActive = 0;             //numCountActive [] array index < 6  should be equal to NUMTYPES - 1      
       // setting INTs ALLC, TFT, etc. to hold the [] index in num[]
       if (isALLCActive)       { ALLC   = numCountActive; numCountActive ++;}
       if (isTFTActive)        { TFT    = numCountActive; numCountActive ++;}
       if (isPAVLOVActive)     { PAVLOV = numCountActive; numCountActive ++;}
       if (isSPITEFULActive)   { SPITEFUL = numCountActive; numCountActive ++;}
       if (isATFTActive)       { ATFT = numCountActive; numCountActive ++;}
       if (isALLDActive)       { ALLD = numCountActive; numCountActive ++;}
       NUMTYPES = numCountActive;
       
       distributeShare();
         
       maxIter = 4;           //setting default values of the environment that can be changed
       pAdapt = 1;           
       isSpatial = true;
       isVonNeumann = true;

   }
   
   public void distributeShare()
   {
       int count = 0;
       double share = 0;   
       if (NUMTYPES != 0)
           share = (100/(NUMTYPES));
       if (isALLCActive) 
           pALLC = share;
       else 
           pALLC = 0;
       if (isTFTActive)     
           pTFT = share;
       else
           pTFT = 0;
       if (isPAVLOVActive)
           pPAVLOV = share;
       else
           pPAVLOV = 0;
       if (isSPITEFULActive)   
           pSPITEFUL = share;
       else 
           pSPITEFUL = 0;
       if (isATFTActive)
           pATFT = share;
       else
           pATFT = 0;
       if (isALLDActive)    
           pALLD = share;
       else
           pALLD = 0;
   }

/////////////////////////////////////////////////////////////////////////

   // The method to build the Model's internals //////////////////////////
   public void buildModel() {
       // Creating the world
       world = new Object2DTorus(worldSize, worldSize);
       // Calculating the number of players (one player per grid cell)
       numPlayers = worldSize * worldSize;
       // Determining the number of agents in each types
       num = new int[ NUMTYPES];   // Create the array holding the number of players per type
       
       if (isALLCActive)       { num[ALLC]       = (int) (numPlayers * pALLC)/100; } // Calculate these numbers
       if (isTFTActive)        { num[TFT]        = (int) (numPlayers * pTFT)/100;  }
       if (isPAVLOVActive)     { num[PAVLOV]     = (int) (numPlayers * pPAVLOV)/100; }
       if (isSPITEFULActive)   { num[SPITEFUL]   = (int) (numPlayers * pSPITEFUL)/100; }
       if (isATFTActive)       { num[ATFT]       = (int) (numPlayers * pATFT)/100; }
       if (isALLDActive)       { num[ALLD]       = (int) (numPlayers * pALLD)/100; }
       
       // Makes sure that an mismatched number of agents aren't created
       int tempcount = 0;    
       for ( int i = 0; i < NUMTYPES; i ++)        //count the agents created
       {
           tempcount += num [ i ];
       }
       if ( tempcount != numPlayers)               // if they're not equal add difference to some category
       {
           int diff = numPlayers - tempcount;
           if (isALLCActive) { num[ALLC] += diff; }
           else if (isTFTActive) { num[TFT] += diff; }
           else if (isPAVLOVActive) { num[PAVLOV] += diff; }
           else if (isSPITEFULActive) { num[SPITEFUL] += diff; }
           else if (isATFTActive) { num[ATFT] += diff; }
           else if (isALLDActive) { num[ALLD] += diff; }
       }
       
       // Creating the agents
       int playerID = 0;   // The ID of the agent under creation
       // We double-loop through each strategy and number of players
       // per strategy.
       // First, loop through all the different types
       // *Created vairables store if that type of agent has been created or not yet
       boolean ALLCCreated     = false;
       boolean TFTCreated      = false;
       boolean PAVLOVCreated   = false;
       boolean SPITEFULCreated = false; 
       boolean ATFTCreated     = false;
       boolean ALLDCreated     = false;
       
       
       for (int playerType = 0; playerType < NUMTYPES; playerType++) 
       {
               // Creating the appropriate number of agents
           if (isALLCActive && !ALLCCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
                    
               // Increment the identity counter
               playerID++;
               // Create a player
    
               Player aPlayer = new Player(playerID, playerType , this, prefs, 0);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               ALLCCreated = true;
           }
           else if (isTFTActive && !TFTCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
               // Increment the identity counter
               playerID++;
               // Create a player
               Player aPlayer = new Player(playerID, playerType , this, prefs, 1);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               TFTCreated = true;
           }
           else if (isPAVLOVActive && !PAVLOVCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
               // Increment the identity counter
               playerID++;
               // Create a player
               Player aPlayer = new Player(playerID, playerType , this, prefs, 2);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               PAVLOVCreated = true;
           }
           else if (isSPITEFULActive && !SPITEFULCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
               // Increment the identity counter
               playerID++;
               // Create a player
               Player aPlayer = new Player(playerID, playerType , this, prefs, 3);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               SPITEFULCreated = true;
           }
           else if (isATFTActive && !ATFTCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
               // Increment the identity counter
               playerID++;
               // Create a player
               Player aPlayer = new Player(playerID, playerType , this, prefs, 4);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               ATFTCreated = true;
           }
           else if (isALLDActive && !ALLDCreated)
           {
               for (int i = 0; i < num[playerType]; i++) 
               {
               // Increment the identity counter
               playerID++;
               // Create a player
              //  System.out.println ("5");
               Player aPlayer = new Player(playerID, playerType , this, prefs, 5);
               // Add the newly created agent to the list of agents
               agentList.add(aPlayer);
               }
               ALLDCreated = true;
           }
           
       }
       // Use Repast's shuffle method to randomize the order of the agents
       SimUtilities.shuffle(agentList);


       // Now it's time to put the players into the grid.
       // Since the list has been shuffled, we can scan through the array from
       // the upper left with a double-loop.
       for (int x = 0; x < worldSize; x++)
           for (int y = 0; y < worldSize; y++) {
               final Player aPlayer = (Player) agentList.get(x * worldSize + y);
               world.putObjectAt(x, y, aPlayer); // This call places the agent
               aPlayer.placeTo(x, y); // The player has to record its own position
           }
       // Show the initial statistics
       reportResults();
   }
   /////////////////////////////////////////////////////////////////////////
   //  Iterated methods  ///////////////////////////////////////////////////
   /////////////////////////////////////////////////////////////////////////
   /**
    * The main activity of the time step.
    */
   public void step() {
       // We carry out four sub-activities:
       resetPlayers();     // Reset the agents' statistics
       interactions();     // Let them interact with each other
       adaptation();       // Let them adapt
       reportResults();    // Calculate and report some statistics
   }
   /**
    * Sub-activity: Resetting the agents.
    */
   private void resetPlayers() {
       
       // Loop through the entire agent list
       for (int i = 0; i < numPlayers; i++) {
           final Player aPlayer = (Player) agentList.get(i); // Pick an agent
           aPlayer.reset();                            // Let it reset it's stats
       }
   }
   /**
    * Sub-activity: Interactions.
    * The interaction method lets each player play against as many as
    * numNeighbors others
    */
   private void interactions() {
       // Looping through all the agents
        for (int i = 0; i < numPlayers; i++) {
           // Selecting the next player
           final Player aPlayer = (Player) agentList.get(i); // Get one player
           // Retrieve its neighbors (according to the neighborhood type)
          List neighbors;
                   neighbors = world.getVonNeumannNeighbors(aPlayer.x, aPlayer.y, false);
           final Iterator it = neighbors.iterator();
           while (it.hasNext()) {
               // Pick the next neighbor...
               final Player otherPlayer = (Player) it.next();
               // ... and play a game against it.
               game(aPlayer, otherPlayer);
           }
       }
   }
   /**
    * A two-person game.
    *
    * @param rowPlayer the first player
    * @param colPlayer the second player
    */
   private void game(final Player rowPlayer, final Player colPlayer) {
       // First we need to tell the two players about each other's existence and
       // add the other player to their neighborhood memory (i.e. otherList).
       // The latter will be used in the adaptation method.
       rowPlayer.other = colPlayer;
       rowPlayer.otherList.add(colPlayer);
       colPlayer.other = rowPlayer;
       colPlayer.otherList.add(rowPlayer);
       // Here the iterated game unfolds in maxIter rounds as in SimpleIPD
       for (int i = 1; i <= maxIter; i++) {    // Note that 'time' starts at 1.
           rowPlayer.play(i);
           colPlayer.play(i);
           rowPlayer.remember();
           colPlayer.remember();
           rowPlayer.addPayoff();
           colPlayer.addPayoff();
       }
   }
   /**
    * Sub-activity: Let the agents adapt themselves to the best strategy
    * in their neighborhood (what they have recorded).
    */
   private void adaptation() {
       // NOTE DOUBLE BUFFERING!
       // Let all agents calculate their adapted type first
       for (int i = 0; i < numPlayers; i++) 
       {
           final Player aPlayer = (Player) agentList.get(i);
           aPlayer.adapt();
       }
       // Second, once they have all calculated their new strategy,
       // let them update to the new type
/*      for (int i = 0; i < numPlayers; i++) {
           final Player aPlayer = (Player) agentList.get(i);
           aPlayer.updateType();
       }*/
   }
   /**
    * Sub-activity: Calculating and outputting the statistics.
    */
   private void reportResults() {
       // Initializing the temporary variables (the array of counters)
       for (int i = 0; i < NUMTYPES; i++)
           num[i] = 0;
       // Counting the number of players per strategy type by incrementing
       // the appropriate counter
       for (int i = 0; i < numPlayers; i++) {
           Player aPlayer = (Player) agentList.get(i);
           num[aPlayer.getType()]++;                                                //*****************************************
       }
       // Printing the results to the screen
       System.out.print(getTickCount() + ": ");
       for (int playerType = 0; playerType < NUMTYPES; playerType++)
           System.out.print(num[playerType] + " ");
       System.out.println();
   }
   /**
    * Creating and starting your model.
    *
    * @param args the parameters to the program
    */
   public static void main(final String[] args) {
       final SimInit init = new SimInit();
       final Model m = new Model();
       init.loadModel(m, null, false);
   }

}


import uchicago.src.sim.gui.DisplayConstants; import uchicago.src.sim.gui.Drawable; import uchicago.src.sim.gui.SimGraphics; import uchicago.src.sim.util.SimUtilities;

import java.awt.*; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List;


public final class Player {

   // Constants for actions
   private static final int C = 0;          // Cooperate
   private static final int D = 1;          // Defect
   // Conversion array from action to String
   final String[] actionToString = {"C", "D"};         //@switch C->D, D-> C


   int x, y;                                // The player's location on the grid
   private final Model model;               // Reference (handle) to the model
   private final int playerID;              // The player's identification
   Player other;                            // Handle to the opponent
   int type;                                // The agent's strategy
   int newType;                     // The agent's calculated strategy
   
   // during adaptation
   PayOff prefs;   // (this one is PD)
   private int action;                      // The current action
   private int memory = C;                      // The opponent's last action
   private int myMemory = C;
   private int memorySize = 1;
   private int cumulPayoff = 0;                 // The agent's cumulated payoff
   private int numPlays = 0;                    // Number of games played
                                            // (for statistics)
   final List otherList;                    // List of opponents
   
   private Strategy strategy;              //strategy for the player
   

   /**
    * Creating the agent.
    *
    * @param i the id of the player
    * @param t the strategy type
    * @param m the model
    */
   public Player(final int i, final int t, final Model m, PayOff pref, int strat) 
   {
       playerID = i;
       type = t;
       strategy = new Strategy( t, memorySize, strat );
       model = m;
       otherList = new ArrayList();
       
       prefs = new PayOff(pref.getCC(), pref.getCD(), pref.getDC(), pref.getDD());
   }
   // Setting the agent's position on the grid
   public final void placeTo(final int a, final int b) {
       x = a;
       y = b;
   }
   /**
    * Initializing the agent's variables (all counters have to be reset).
    */
   public final void reset() 
   {
       numPlays = 0;
       cumulPayoff = 0;
       otherList.clear();
   }
   /**
    * Storing the opponent's last action.
    */
   public final void remember() {
       memory = other.action;
       myMemory = action;
   }
   public final void play(final int time) {
       // Keeping track of the number of games played
       numPlays++;
       action = strategy.getCurrResponse ( time, memory, myMemory);
      //     System.out.println ( action);
   }


   public final void addPayoff() 
   {
       if ((action == 0) && (other.action == 0))   { cumulPayoff = cumulPayoff + prefs.getCC(); System.out.println(cumulPayoff);}
       else if ((action == 1) && (other.action == 0))   { cumulPayoff = cumulPayoff + prefs.getDC();}
       else if ((action == 0) && (other.action == 1))   { cumulPayoff = cumulPayoff + prefs.getCD();}
       else { cumulPayoff = cumulPayoff + prefs.getDD();}
   }
   // In this pair of methods the player updates the strategy to that of the
   // most successful player encountered (based on the otherList).
   // (They are the same as those in GraphIPD, except the randomization
   // of the neighbor-list in adapt().)
   /**
    * Calculating the agent's new strategy.
    */
   public final void adapt() {
       // We use double-buffering by storing the result in newType which will
       // later be used to update type (but we can't to do that until we've gone
       // through all players):
       newType = type;
    
       // We use SimpleModel's uniform random number generator to draw a random
       // number and with probability pAdapt we let the player execute the body
       // of the method
       if (model.getNextDoubleFromTo(0.0, 1.0) < model.pAdapt) {
           // We make sure we are not biased by the order in which we check the
           // neighbors

// SimUtilities.shuffle(otherList);

           // This is a simple search loop going through the otherList to find
           // the best playoff among the opponents. The first candidate for the
           // best payoff player is the agent itself.
           double bestPayoff = getAveragePayoff();
           final Iterator i = otherList.iterator();
           while (i.hasNext()) 
           {
               final Player aPlayer = (Player) i.next();
               final double payoff = aPlayer.getAveragePayoff();
               if (payoff > bestPayoff) {  
                   bestPayoff = payoff;
                   // Set the new type to the best known (up to now)
                   newType = aPlayer.type;
               }
           }
       }
      
     }
   /**
    * Complete double-buffering by updating the strategy type to the
    * recently calculated value.
    */
   public final void updateType() {
        System.out.println( "I "+type+" am changing to "+newType);
       type = newType;
   }
   /**
    * Helper function returning the agent's average cumulative payoff.
    *
    * @return the agent's average cumulative payoff
    */
   private double getAveragePayoff() {
     //  if (numPlays == 0)          // Check that avoids division by zero
       //    return -1.0;              // Extreme value as an 'error message'
     //  else
           return (double) cumulPayoff;// / ((double) numPlays);
   }
   /////////////////////////////////////////////////////////////////////////
   //  Functions of the GUI interface  /////////////////////////////////////
   /////////////////////////////////////////////////////////////////////////
   /**
    * Providing get methods for x in order to make them displayable
    *
    * @return the x coordinate of the player
    */
   public final int getX() {
       return x;
   }
   /**
    * Providing get methods for y in order to make them displayable
    *
    * @return the y coordinate of the player
    */
   public final int getY() {
       return y;
   }
   /**
    * The following accessor method make it possible to retrieve the type
    * in the probe window.
    *
    * @return the type of the player
    */
   public final int getType() {
       return type;
   }
   /**
    * The following accessor method make it possible to change the type
    * in the probe window.
    *
    * @param v the new type of the player
    */
   public final void setType(final int v) {
       type = v;
   }
   // Code to draw the agent on the grid display
   // Constant matrix to convert strategies into color-codes

// private static final Color[] COLOR = {Color.red, Color.blue, Color.green, Color.white, Color.magenta, Color.yellow};

   /**
    * Drawing the agent's 'dot' on the grid with the appropriate color
    *
    * @param g the graphic context
    
   public final void draw(final SimGraphics g) {
       // Enable anti-aliasing to the underlying Java graphic context
       final Graphics2D g2 = g.getGraphics();
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
       g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
               RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       // Draw the background of each cell with the average payoff as greyscale value
       final double averagePayoff = this.getAveragePayoff();
       
       final double maxPayoff = 5;
       final int lum = 255 - (int)(255.0 * averagePayoff / maxPayoff);
       g.setDrawingParameters(DisplayConstants.CELL_WIDTH,
                DisplayConstants.CELL_HEIGHT,
                DisplayConstants.CELL_DEPTH);
      g.drawRect(new Color(lum, lum, lum));
     
       // Draw a centered circle with the stragegy color and the payoff value as text
       final int xTrans = g.getCellWidthScale();
       final int yTrans = g.getCellHeightScale();
       g.setDrawingCoordinates(getX() * xTrans + (DisplayConstants.CELL_WIDTH * 3 / 16),
               getY() * yTrans + (DisplayConstants.CELL_HEIGHT * 3 / 16),
               0);
       g.setDrawingParameters(DisplayConstants.CELL_WIDTH * 3 / 4,
                DisplayConstants.CELL_HEIGHT * 3 / 4,
                DisplayConstants.CELL_DEPTH * 3 / 4);
        Color c = Color.white;
        if (COLOR[type] == Color.white)
            c = Color.black;
        DecimalFormat df = new DecimalFormat("#.00");
       g.drawStringInOval(COLOR[type], c, df.format(averagePayoff));
       g.drawHollowOval(c);
       // Put back anti-aliasing to its default value
       g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.
               VALUE_TEXT_ANTIALIAS_DEFAULT);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.
               VALUE_ANTIALIAS_DEFAULT);
   }*/

}

Strategy.java


public class Strategy {

    int numberOfStrategies = 16;
    private static final int C = 0;     
    private static final int D = 1;      

/*

   static final int ALLC = 0;
   static final int TFT = 1;
   static final int PAVLOV = 2;
   static final int SPITEFUL = 3;
   static final int ATFT = 4;
   static final int ALLD = 5;
  • /
    static final int ALLC = 0;
    static final int UKN0 = 4;
    static final int TFT  = 5;
    static final int PAVL = 6;
    static final int SPIT = 7;
    static final int UKN1 = 8;
    static final int UKN2 = 9;
    static final int ATFT = 10;
    static final int UKN4 = 12;
    static final int UKN5 = 13;
    static final int UKN6 = 14;
    static final int ALLD = 15;    
    
    int type = -1;
    int memorySize = 1;
    int data [][];
    HistoryList myHistory;
    
    
   public Strategy( int stratNum, int memorySize, int response )
   {
       type = getConversion(response);
       this.memorySize = memorySize;
       myHistory = new HistoryList ( memorySize );
       data = new int [2][2];
       data[0][0] = type >> 3;
       data[0][1] = (type >> 2)%2;
       data[1][0] = (type >> 1)%2;
       data[1][1] = type%2;
   }
   
   public int getConversion( int x )
   {
       if ( x == 0 ) { return 0; }
       else if ( x == 1) { return 5; }
       else if ( x == 2) { return 6; }
       else if ( x == 3) { return 7; }
       else if ( x == 4) { return 10;}
       else {return 15;}
   }
   
   
   public int getCurrResponse (int time, int myMove, int opponentsMove )
   {
       //first store the data in the historylist
    //   myHistory.add( time, myMove, opponentsMove );

       if ( time == 1)
       {
           return data [C][C];     //assuming everyone cooperates the first time
       }
       else //if (memorySize == 1)
       {
           myHistory.add( time, myMove, opponentsMove);
           return data [myMove][opponentsMove];
       }       
             
   }
   


} HistoryList.java public class HistoryList {

   HistoryElement [] data;
   int size = 0;
   int time = 0;
   int timePlacement = 0;
   
   public HistoryList(int size )
   {
       data = new HistoryElement [ size ];
       this.size = size;
      
   }
   
   public void add(int time, int myMove, int oppMove )
   {
       if ( timePlacement == size )
       {
           for ( int i = 1; i < size; i++)
           {
               data [ i - 1 ] = data [ i ];
           }
           timePlacement = size - 1;
       }
       data[ timePlacement ] = new HistoryElement( time, myMove, oppMove );
       timePlacement ++;  
   }
   
   public HistoryElement get (int timePosition)   // 0 - (size-1)
   {
       return data [ timePosition ];
   }


} HistoryElement.java

public class HistoryElement {

   int time, myMove, oppMove;
   
   public HistoryElement(int time, int myMove, int oppMove)
   {
       this.time = time;
       this.myMove = myMove;
       this.oppMove = oppMove;
   }
   public int getMyMove()  {   return myMove; }
   
   public int getOppMove() {   return oppMove; }
   
   public int getTime()    {   return time; }

}

PayOff.java


public class PayOff {

   private int cc = 3;  
   private int cd = 0;
   private int dc = 5;
   private int dd = 1;
   
   public PayOff( int cc, int cd, int dc, int dd)
   {
       this.cc = cc;
       this.cd = cd;
       this.dc = dc;
       this.dd = dd;        
   }
   public void setValues ( int cc, int cd, int dc, int dd)
   {
       this.cc = cc;
       this.cd = cd;
       this.dc = dc;
       this.dd = dd;        
   }    
   public void setCC( int x ) { cc = x; }
   public void setCD( int x ) { cd = x; }
   public void setDC( int x ) { dc = x; }
   public void setDD( int x ) { dd = x; }
   
   public int getCC()  { return cc; }
   public int getCD()  { return cd; }
   public int getDC()  { return dc; }
   public int getDD()  { return dd; }
   

}